commit d2ae1e3a3ed4aaa51b222c1d97dbd47f136f3aeb
Author: Filipe Tomaz <eusurplus@gmail.com>
Date:   Sun Mar 20 22:25:49 2016 +0000

    Added option to select the standard tool tip direction (lathe)
    User can add this option in the INI file with the line:
    STANDARD_TOOLTIP_ORIENTATION = 1
    in the EMCIO section
    
    Signed-off-by: Filipe Tomaz <eusurplus@gmail.com>

diff --git a/docs/src/config/ini-config.txt b/docs/src/config/ini-config.txt
index 7ea6c54..7e762bd 100644
--- a/docs/src/config/ini-config.txt
+++ b/docs/src/config/ini-config.txt
@@ -1326,7 +1326,8 @@ image::images/encoder-scale.png[align="center"]
     it came from. For example, machines that exchange the tool in the
     active pocket with the tool in the spindle.
 
-* 'FANUC_TOOLTIP_ORIENTATION = 1' -
+* 'STANDARD_TOOLTIP_ORIENTATION = 1' -
     This is for lathe machines where the user wants to use the industry standard
-    tooltip direction/orientation.
+    tooltip direction/orientation. Not using this feature or setting it to zero
+    does not make any change on the default linuxcnc behaviour.
 
diff --git a/src/emc/iotask/ioControl.cc b/src/emc/iotask/ioControl.cc
index 5561c9c..f936d54 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;
+static int standard_tooltip_orientation = 0;
 
 struct iocontrol_str {
     hal_bit_t *user_enable_out;	/* output, TRUE when EMC wants stop */
@@ -244,7 +244,7 @@ static int iniLoad(const char *filename)
     }
 
     inifile.Find(&random_toolchanger, "RANDOM_TOOLCHANGER", "EMCIO");
-    inifile.Find(&fanuc_tooltip_orientation, "FANUC_TOOLTIP_ORIENTATION", "EMCIO");
+    inifile.Find(&standard_tooltip_orientation, "STANDARD_TOOLTIP_ORIENTATION", "EMCIO");
     
     // close it
     inifile.Close();
@@ -309,7 +309,7 @@ static int saveToolTable(const char *filename,
             if (toolTable[pocket].frontangle) fprintf(fp, " I%+f", toolTable[pocket].frontangle);
             if (toolTable[pocket].backangle) fprintf(fp, " J%+f", toolTable[pocket].backangle);
             if (toolTable[pocket].orientation){
-                if (fanuc_tooltip_orientation == 1) { // Save as Fanuc tool tip orientation
+                if (standard_tooltip_orientation == 1) { // Save as Fanuc tool tip orientation
                     if (toolTable[pocket].orientation == 2) fprintf(fp, " Q3");
                     else if (toolTable[pocket].orientation == 3) fprintf(fp, " Q2");
                     else if (toolTable[pocket].orientation == 6) fprintf(fp, " Q8");
@@ -776,7 +776,7 @@ int main(int argc, char *argv[])
     }
 
     if (0 != loadToolTable(tool_table_file, emcioStatus.tool.toolTable,
-		fms, ttcomments, random_toolchanger, fanuc_tooltip_orientation)) {
+		fms, ttcomments, random_toolchanger, standard_tooltip_orientation)) {
 	rcs_print_error("can't load tool table.\n");
     }
 
@@ -850,7 +850,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, fanuc_tooltip_orientation);
+		    fms, ttcomments, random_toolchanger, standard_tooltip_orientation);
 	    reload_tool_number(emcioStatus.tool.toolInSpindle);
 	    break;
 
@@ -937,7 +937,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, fanuc_tooltip_orientation))
+				  fms, ttcomments, random_toolchanger, standard_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 816e9cb..41b6db5 100644
--- a/src/emc/iotask/ioControl_v2.cc
+++ b/src/emc/iotask/ioControl_v2.cc
@@ -82,7 +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;
+static int standard_tooltip_orientation = 0;
 
 typedef enum {
     V1 = 1,
@@ -340,7 +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");
+    inifile.Find(&standard_tooltip_orientation, "STANDARD_TOOLTIP_ORIENTATION", "EMCIO");
 
     // close it
     inifile.Close();
@@ -363,7 +363,7 @@ static int iniLoad(const char *filename)
  *
  ********************************************************************/
 static int saveToolTable(const char *filename,
-			 CANON_TOOL_TABLE toolTable[], int fanuc_tooltip_orientation)
+			 CANON_TOOL_TABLE toolTable[], int standard_tooltip_orientation)
 {
     int pocket;
     FILE *fp;
@@ -405,7 +405,7 @@ static int saveToolTable(const char *filename,
 	    if (toolTable[pocket].frontangle) fprintf(fp, " I%+f", toolTable[pocket].frontangle);
 	    if (toolTable[pocket].backangle) fprintf(fp, " J%+f", toolTable[pocket].backangle);
         if (toolTable[pocket].orientation){
-            if (fanuc_tooltip_orientation == 1) { // Save as Fanuc tool tip orientation
+            if (standard_tooltip_orientation == 1) { // Save as Fanuc tool tip orientation
                 if (toolTable[pocket].orientation == 2) fprintf(fp, " Q3");
                 else if (toolTable[pocket].orientation == 3) fprintf(fp, " Q2");
                 else if (toolTable[pocket].orientation == 6) fprintf(fp, " Q8");
@@ -584,7 +584,7 @@ void load_tool(int pocket) {
 	ttcomments[0] = ttcomments[pocket];
 	ttcomments[pocket] = comment_temp;
 
-	if (0 != saveToolTable(tool_table_file, emcioStatus.tool.toolTable, fanuc_tooltip_orientation))
+	if (0 != saveToolTable(tool_table_file, emcioStatus.tool.toolTable, standard_tooltip_orientation))
 	    emcioStatus.status = RCS_ERROR;
     } else if (pocket == 0) {
 	// magic T0 = pocket 0 = no tool
@@ -895,7 +895,7 @@ int main(int argc, char *argv[])
     }
 
     if (0 != loadToolTable(tool_table_file, emcioStatus.tool.toolTable,
-			   fms, ttcomments, random_toolchanger, fanuc_tooltip_orientation)) {
+			   fms, ttcomments, random_toolchanger, standard_tooltip_orientation)) {
 	rcs_print_error("%s: can't load tool table.\n",progname);
     }
 
@@ -998,7 +998,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, fanuc_tooltip_orientation);
+			  fms, ttcomments, random_toolchanger, standard_tooltip_orientation);
 	    reload_tool_number(emcioStatus.tool.toolInSpindle);
 	    break;
 
@@ -1121,7 +1121,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, fanuc_tooltip_orientation))
+				   fms, ttcomments, random_toolchanger, standard_tooltip_orientation))
 		emcioStatus.status = RCS_ERROR;
 	    else
 		reload_tool_number(emcioStatus.tool.toolInSpindle);
@@ -1158,7 +1158,7 @@ int main(int argc, char *argv[])
 		emcioStatus.tool.toolTable[0] = emcioStatus.tool.toolTable[p];
 	    }
 	}
-	if (0 != saveToolTable(tool_table_file, emcioStatus.tool.toolTable, fanuc_tooltip_orientation))
+	if (0 != saveToolTable(tool_table_file, emcioStatus.tool.toolTable, standard_tooltip_orientation))
 	    emcioStatus.status = RCS_ERROR;
 	break;
 
diff --git a/src/emc/rs274ngc/interp_convert.cc b/src/emc/rs274ngc/interp_convert.cc
index 8324123..d3fd003 100644
--- a/src/emc/rs274ngc/interp_convert.cc
+++ b/src/emc/rs274ngc/interp_convert.cc
@@ -1915,7 +1915,17 @@ int Interp::convert_cutter_compensation_on(int side,     //!< side of path cutte
       radius = block->d_number_float / 2;
       if(block->l_number != -1) {
           CHKS((settings->plane != CANON_PLANE_XZ), _("G%d.1 with L word, but plane is not G18"), block->g_modes[7]/10);
-          orientation = block->l_number;
+          if (settings->standard_tooltip_orientation) {
+              if (block->l_number == 2) orientation = 3;
+              else if (block->l_number == 3) orientation = 2;
+              else if (block->l_number == 6) orientation = 8;
+              else if (block->l_number == 8) orientation = 6;
+              else if (block->l_number == 1) orientation = 4;
+              else if (block->l_number == 4) orientation = 1;
+              else orientation = block->l_number;
+          }
+          else
+            orientation = block->l_number;
       } else {
           orientation = 0;
       }
@@ -3690,7 +3700,17 @@ int Interp::convert_setup_tool(block_pointer block, setup_pointer settings) {
     if(block->q_number != -1.0) {
         CHKS((!is_near_int(&q, block->q_number)), _("Q number in G10 is not an integer"));
         CHKS((q > 9), _("Invalid tool orientation"));
-        settings->tool_table[pocket].orientation = q;
+        if (settings->standard_tooltip_orientation) {
+            if (q == 2) settings->tool_table[pocket].orientation = 3;
+            else if (q == 3) settings->tool_table[pocket].orientation = 2;
+            else if (q == 6) settings->tool_table[pocket].orientation = 8;
+            else if (q == 8) settings->tool_table[pocket].orientation = 6;
+            else if (q == 1) settings->tool_table[pocket].orientation = 4;
+            else if (q == 4) settings->tool_table[pocket].orientation = 1;
+            else settings->tool_table[pocket].orientation = q;
+        }
+        else
+            settings->tool_table[pocket].orientation = q;
     }
 
     SET_TOOL_TABLE_ENTRY(pocket,
diff --git a/src/emc/rs274ngc/interp_internal.hh b/src/emc/rs274ngc/interp_internal.hh
index f2eb3a9..4ef1ff0 100644
--- a/src/emc/rs274ngc/interp_internal.hh
+++ b/src/emc/rs274ngc/interp_internal.hh
@@ -700,6 +700,7 @@ typedef struct setup_struct
   double program_z;             // program y, used when cutter comp on
   RETRACT_MODE retract_mode;    // for cycles, old_z or r_plane
   int random_toolchanger;       // tool changer swaps pockets, and pocket 0 is the spindle instead of "no tool"
+  int standard_tooltip_orientation; // user setting to use the same tool tip orientation as fanuc, haas, and the rest of the world
   int selected_pocket;          // tool slot selected but not active
     int selected_tool;          // start switchover to pocket-agnostic interp
   int sequence_number;          // sequence number of line last read
diff --git a/src/emc/rs274ngc/interp_setup.cc b/src/emc/rs274ngc/interp_setup.cc
index 2120991..2c66b18 100644
--- a/src/emc/rs274ngc/interp_setup.cc
+++ b/src/emc/rs274ngc/interp_setup.cc
@@ -117,6 +117,7 @@ setup_struct::setup_struct() :
     program_z (0.0),
     retract_mode(R_PLANE),
     random_toolchanger(0),
+    standard_tooltip_orientation(0),
     selected_pocket(0),
     selected_tool(0),
     sequence_number(0),
diff --git a/src/emc/rs274ngc/interpmodule.cc b/src/emc/rs274ngc/interpmodule.cc
index e5e9683..bea6291 100644
--- a/src/emc/rs274ngc/interpmodule.cc
+++ b/src/emc/rs274ngc/interpmodule.cc
@@ -714,6 +714,12 @@ static inline int get_random_toolchanger (Interp &interp)  {
 static inline void set_random_toolchanger(Interp &interp, int value)  {
     interp._setup.random_toolchanger = value;
 }
+static inline int get_standard_tooltip_orientation (Interp &interp)  {
+    return interp._setup.standard_tooltip_orientation;
+}
+static inline void set_standard_tooltip_orientation(Interp &interp, int value)  {
+    interp._setup.standard_tooltip_orientation = value;
+}
 static inline int get_remap_level (Interp &interp)  {
     return interp._setup.remap_level;
 }
@@ -940,6 +946,7 @@ BOOST_PYTHON_MODULE(interpreter) {
 	.add_property("plane", &get_plane, &set_plane)
 	.add_property("pockets_max", &get_pockets_max, &set_pockets_max)
 	.add_property("random_toolchanger", &get_random_toolchanger, &set_random_toolchanger)
+    .add_property("standard_tooltip_orientation", &get_standard_tooltip_orientation, &set_standard_tooltip_orientation)
 	.add_property("remap_level", &get_remap_level, &set_remap_level)
 	.add_property("retract_mode", &get_retract_mode, &set_retract_mode)
 	.add_property("selected_pocket", &get_selected_pocket, &set_selected_pocket)
diff --git a/src/emc/rs274ngc/rs274ngc_pre.cc b/src/emc/rs274ngc/rs274ngc_pre.cc
index 53231d2..33e51f9 100644
--- a/src/emc/rs274ngc/rs274ngc_pre.cc
+++ b/src/emc/rs274ngc/rs274ngc_pre.cc
@@ -820,6 +820,7 @@ int Interp::init()
   _setup.b_axis_wrapped = 0;
   _setup.c_axis_wrapped = 0;
   _setup.random_toolchanger = 0;
+  _setup.standard_tooltip_orientation = 0;
   _setup.a_indexer = 0;
   _setup.b_indexer = 0;
   _setup.c_indexer = 0;
@@ -849,6 +850,7 @@ int Interp::init()
           inifile.Find(&_setup.b_axis_wrapped, "WRAPPED_ROTARY", "AXIS_4");
           inifile.Find(&_setup.c_axis_wrapped, "WRAPPED_ROTARY", "AXIS_5");
           inifile.Find(&_setup.random_toolchanger, "RANDOM_TOOLCHANGER", "EMCIO");
+          inifile.Find(&_setup.standard_tooltip_orientation, "STANDARD_TOOLTIP_ORIENTATION", "EMCIO");
           inifile.Find(&_setup.feature_set, "FEATURES", "RS274NGC");
 
           inifile.Find(&_setup.a_indexer, "LOCKING_INDEXER", "AXIS_3");
diff --git a/src/emc/rs274ngc/tool_parse.cc b/src/emc/rs274ngc/tool_parse.cc
index dc16fd7..4736fdc 100644
--- a/src/emc/rs274ngc/tool_parse.cc
+++ b/src/emc/rs274ngc/tool_parse.cc
@@ -97,7 +97,7 @@ int loadToolTable(const char *filename,
 			 int fms[],
 			 char *ttcomments[],
 			 int random_toolchanger,
-			 int fanuc_tooltip_orientation)
+			 int standard_tooltip_orientation)
 {
     int fakepocket = 0;
     int t;
@@ -254,7 +254,7 @@ int loadToolTable(const char *filename,
             toolTable[pocket].diameter = diameter;
             toolTable[pocket].frontangle = frontangle;
             toolTable[pocket].backangle = backangle;
-            if (fanuc_tooltip_orientation) {
+            if (standard_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;
diff --git a/src/emc/task/taskclass.cc b/src/emc/task/taskclass.cc
index 7b61183..2c934cf 100644
--- a/src/emc/task/taskclass.cc
+++ b/src/emc/task/taskclass.cc
@@ -448,7 +448,7 @@ struct _inittab builtin_modules[] = {
 
 
 
-Task::Task() : use_iocontrol(0), random_toolchanger(0) {
+Task::Task() : use_iocontrol(0), random_toolchanger(0), standard_tooltip_orientation(0) {
 
     IniFile inifile;
 
@@ -457,6 +457,7 @@ Task::Task() : use_iocontrol(0), random_toolchanger(0) {
     if (inifile.Open(ini_filename)) {
 	use_iocontrol = (inifile.Find("EMCIO", "EMCIO") != NULL);
 	inifile.Find(&random_toolchanger, "RANDOM_TOOLCHANGER", "EMCIO");
+    inifile.Find(&standard_tooltip_orientation, "STANDARD_TOOLTIP_ORIENTATION", "EMCIO");
 	const char *t;
 	if ((t = inifile.Find("TOOL_TABLE", "EMCIO")) != NULL)
 	    tooltable_filename = strdup(t);
diff --git a/src/emc/task/taskclass.hh b/src/emc/task/taskclass.hh
index d1c992f..29d91c3 100644
--- a/src/emc/task/taskclass.hh
+++ b/src/emc/task/taskclass.hh
@@ -54,6 +54,7 @@ public:
 
     int use_iocontrol;
     int random_toolchanger;
+    int standard_tooltip_orientation;
     const char *ini_filename;
     const char *tooltable_filename;
 private:
diff --git a/src/emc/task/taskmodule.cc b/src/emc/task/taskmodule.cc
index cb4001f..7dbf733 100644
--- a/src/emc/task/taskmodule.cc
+++ b/src/emc/task/taskmodule.cc
@@ -321,6 +321,7 @@ BOOST_PYTHON_MODULE(emctask) {
 
 	.def_readonly("use_iocontrol", &Task::use_iocontrol)
 	.def_readonly("random_toolchanger", &Task::random_toolchanger)
+    .def_readonly("standard_tooltip_orientation", &Task::standard_tooltip_orientation)
 	.def_readonly("tooltable_filename", &Task::tooltable_filename)
 	;
 
diff --git a/src/emc/usr_intf/axis/scripts/axis.py b/src/emc/usr_intf/axis/scripts/axis.py
index 9611a27..693db12 100755
--- a/src/emc/usr_intf/axis/scripts/axis.py
+++ b/src/emc/usr_intf/axis/scripts/axis.py
@@ -2906,6 +2906,7 @@ def units(s, d=1.0):
         return unit_values.get(s, d)
 
 random_toolchanger = int(inifile.find("EMCIO", "RANDOM_TOOLCHANGER") or 0)
+standard_tooltip_orientation = int(inifile.find("EMCIO", "STANDARD_TOOLTIP_ORIENTATION") or 0)
 vars.emcini.set(sys.argv[2])
 open_directory = inifile.find("DISPLAY", "PROGRAM_PREFIX")
 vars.machine.set(inifile.find("EMC", "MACHINE"))
