commit 5dccb596b0a310168ab5380889f829eb2d194818
Author: Filipe Tomaz <eusurplus@gmail.com>
Date:   Sat Mar 19 18:38:58 2016 +0000

    Added option to select the Fanuc standard for tool tip direction (lathe)
    User can add this option in the INI file with the line:
    FANUC_TOOLTIP_ORIENTATION = 1
    in the EMCIO section

diff --git a/src/emc/iotask/ioControl.cc b/src/emc/iotask/ioControl.cc
index 4483c31..b3c0aca 100644
--- a/src/emc/iotask/ioControl.cc
+++ b/src/emc/iotask/ioControl.cc
@@ -79,7 +79,7 @@ static NML *emcErrorBuffer = 0;
 static char *ttcomments[CANON_POCKETS_MAX];
 static int fms[CANON_POCKETS_MAX];
 static int random_toolchanger = 0;
-
+static int fanuc_tooltip_orientation = 0;
 
 struct iocontrol_str {
     hal_bit_t *user_enable_out;	/* output, TRUE when EMC wants stop */
@@ -244,7 +244,8 @@ static int iniLoad(const char *filename)
     }
 
     inifile.Find(&random_toolchanger, "RANDOM_TOOLCHANGER", "EMCIO");
-
+    inifile.Find(&fanuc_tooltip_orientation, "FANUC_TOOLTIP_ORIENTATION", "EMCIO");
+    
     // close it
     inifile.Close();
 
@@ -764,7 +765,7 @@ int main(int argc, char *argv[])
     }
 
     if (0 != loadToolTable(tool_table_file, emcioStatus.tool.toolTable,
-		fms, ttcomments, random_toolchanger)) {
+		fms, ttcomments, random_toolchanger, fanuc_tooltip_orientation)) {
 	rcs_print_error("can't load tool table.\n");
     }
 
@@ -838,7 +839,7 @@ int main(int argc, char *argv[])
 	case EMC_TOOL_INIT_TYPE:
 	    rtapi_print_msg(RTAPI_MSG_DBG, "EMC_TOOL_INIT\n");
 	    loadToolTable(tool_table_file, emcioStatus.tool.toolTable,
-		    fms, ttcomments, random_toolchanger);
+		    fms, ttcomments, random_toolchanger, fanuc_tooltip_orientation);
 	    reload_tool_number(emcioStatus.tool.toolInSpindle);
 	    break;
 
@@ -925,7 +926,7 @@ int main(int argc, char *argv[])
 		if(!strlen(filename)) filename = tool_table_file;
 		rtapi_print_msg(RTAPI_MSG_DBG, "EMC_TOOL_LOAD_TOOL_TABLE\n");
 		if (0 != loadToolTable(filename, emcioStatus.tool.toolTable,
-				  fms, ttcomments, random_toolchanger))
+				  fms, ttcomments, random_toolchanger, fanuc_tooltip_orientation))
 		    emcioStatus.status = RCS_ERROR;
 		else
 		    reload_tool_number(emcioStatus.tool.toolInSpindle);
diff --git a/src/emc/iotask/ioControl_v2.cc b/src/emc/iotask/ioControl_v2.cc
index b4caa51..0b568b2 100644
--- a/src/emc/iotask/ioControl_v2.cc
+++ b/src/emc/iotask/ioControl_v2.cc
@@ -82,6 +82,7 @@ static int fms[CANON_POCKETS_MAX];
 static int random_toolchanger = 0;
 static int support_start_change = 0;
 static const char *progname;
+static int fanuc_tooltip_orientation = 0;
 
 typedef enum {
     V1 = 1,
@@ -339,6 +340,7 @@ static int iniLoad(const char *filename)
     rtapi_print_msg(RTAPI_MSG_DBG,"%s: [EMCIO] using v%d protocol\n",progname,proto);
 
     inifile.Find(&random_toolchanger, "RANDOM_TOOLCHANGER", "EMCIO");
+    inifile.Find(&fanuc_tooltip_orientation, "FANUC_TOOLTIP_ORIENTATION", "EMCIO");
 
     // close it
     inifile.Close();
@@ -882,7 +884,7 @@ int main(int argc, char *argv[])
     }
 
     if (0 != loadToolTable(tool_table_file, emcioStatus.tool.toolTable,
-			   fms, ttcomments, random_toolchanger)) {
+			   fms, ttcomments, random_toolchanger, fanuc_tooltip_orientation)) {
 	rcs_print_error("%s: can't load tool table.\n",progname);
     }
 
@@ -985,7 +987,7 @@ int main(int argc, char *argv[])
 	case EMC_TOOL_INIT_TYPE:
 	    rtapi_print_msg(RTAPI_MSG_DBG, "EMC_TOOL_INIT\n");
 	    loadToolTable(tool_table_file, emcioStatus.tool.toolTable,
-			  fms, ttcomments, random_toolchanger);
+			  fms, ttcomments, random_toolchanger, fanuc_tooltip_orientation);
 	    reload_tool_number(emcioStatus.tool.toolInSpindle);
 	    break;
 
@@ -1108,7 +1110,7 @@ int main(int argc, char *argv[])
 	    if (!strlen(filename)) filename = tool_table_file;
 	    rtapi_print_msg(RTAPI_MSG_DBG, "EMC_TOOL_LOAD_TOOL_TABLE\n");
 	    if (0 != loadToolTable(filename, emcioStatus.tool.toolTable,
-				   fms, ttcomments, random_toolchanger))
+				   fms, ttcomments, random_toolchanger, fanuc_tooltip_orientation))
 		emcioStatus.status = RCS_ERROR;
 	    else
 		reload_tool_number(emcioStatus.tool.toolInSpindle);
diff --git a/src/emc/rs274ngc/tool_parse.cc b/src/emc/rs274ngc/tool_parse.cc
index cad4b13..ea8456a 100644
--- a/src/emc/rs274ngc/tool_parse.cc
+++ b/src/emc/rs274ngc/tool_parse.cc
@@ -96,7 +96,8 @@ int loadToolTable(const char *filename,
 			 CANON_TOOL_TABLE toolTable[],
 			 int fms[],
 			 char *ttcomments[],
-			 int random_toolchanger)
+			 int random_toolchanger,
+			 int fanuc_tooltip_orientation)
 {
     int fakepocket = 0;
     int t;
@@ -253,6 +254,16 @@ int loadToolTable(const char *filename,
             toolTable[pocket].diameter = diameter;
             toolTable[pocket].frontangle = frontangle;
             toolTable[pocket].backangle = backangle;
+	    if (fanuc_tooltip_orientation) {
+		if (orientation == 2) toolTable[pocket].orientation = 3;
+			else if (orientation == 3) toolTable[pocket].orientation = 2;
+				else if (orientation == 6) toolTable[pocket].orientation = 8;
+                    else if (orientation == 8) toolTable[pocket].orientation = 6;
+                        else if (orientation == 1) toolTable[pocket].orientation = 4;
+                            else if (orientation == 4) toolTable[pocket].orientation = 1;
+                                else toolTable[pocket].orientation = orientation;
+		}
+         else
             toolTable[pocket].orientation = orientation;
 
             if (ttcomments && comment)
diff --git a/src/emc/rs274ngc/tool_parse.h b/src/emc/rs274ngc/tool_parse.h
index 684407b..0d8265d 100644
--- a/src/emc/rs274ngc/tool_parse.h
+++ b/src/emc/rs274ngc/tool_parse.h
@@ -25,7 +25,8 @@ int loadToolTable(const char *filename,
 	struct CANON_TOOL_TABLE toolTable[CANON_POCKETS_MAX],
 	int fms[CANON_POCKETS_MAX],
 	char *ttcomments[CANON_POCKETS_MAX],
-	int random_toolchanger
+	int random_toolchanger,
+    int fanuc_tooltip_direction
 	);
 
 #ifdef CPLUSPLUS
diff --git a/src/emc/sai/driver.cc b/src/emc/sai/driver.cc
index 3102ef6..fb15a91 100644
--- a/src/emc/sai/driver.cc
+++ b/src/emc/sai/driver.cc
@@ -334,7 +334,7 @@ int read_tool_file(  /* ARGUMENTS         */
       tool_file_name = buffer;
     }
 
-  return loadToolTable(tool_file_name, _tools, 0, 0, 0);
+  return loadToolTable(tool_file_name, _tools, 0, 0, 0, 0);
 }
 
 /************************************************************************/
