From f97cd976bad813834baaa338430553982bb6aaa3 Mon Sep 17 00:00:00 2001
From: Filipe Tomaz <filipetomaz@portugalmail.pt>
Date: Sun, 29 Dec 2013 03:37:21 -0800
Subject: [PATCH 1/2] Added functionality to swap the tool tip direction when reading the tool table.
 The intention is to be able to have a tooltable similar to the "Fanuc type".
 This changes do not change how linuxcnc deals with the orientation, they only change how
 linuxcnc reads the tooltable.

---
 src/emc/rs274ngc/tool_parse.cc |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/emc/rs274ngc/tool_parse.cc b/src/emc/rs274ngc/tool_parse.cc
index cad4b13..cfad0c9 100644
--- a/src/emc/rs274ngc/tool_parse.cc
+++ b/src/emc/rs274ngc/tool_parse.cc
@@ -92,11 +92,20 @@ static bool scan_old_style(
     return false;
 }
 
+
+// Loads the tooltable from file
+//
+// If fanuc_tooltip_orientation is true, and since linuxcnc implements it diferently than the
+// FANUC style, when reading the tooltable, the orientation is mapped to the linuxcnc style.
+// This way, the user can use the FANUC style, and internally linuxcnc will carry using the
+// implemented style
+
 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,7 +262,18 @@ int loadToolTable(const char *filename,
             toolTable[pocket].diameter = diameter;
             toolTable[pocket].frontangle = frontangle;
             toolTable[pocket].backangle = backangle;
-            toolTable[pocket].orientation = orientation;
+		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;
+		if (toolTable[pocket].orientation != orientation)
+		 printf ("\nSwapping tool orientation for Fanuc style on tool %d, from orientation %d to orientation %d.", toolno, orientation, toolTable[pocket].orientation);
+		} else toolTable[pocket].orientation = orientation;
 
             if (ttcomments && comment)
                 strcpy(ttcomments[pocket], comment);
-- 
1.7.0.4


From 3a02547ec50fab960a0a2ab9f5d39abf425397cb Mon Sep 17 00:00:00 2001
From: Filipe Tomaz <filipetomaz@portugalmail.pt>
Date: Sun, 29 Dec 2013 04:00:23 -0800
Subject: [PATCH 2/2] Added functionality to swap the tool tip direction when reading the tool table.
  The intention is to be able to have a tooltable similar to the "Fanuc type".
  This changes do not change how linuxcnc deals with the orientation, they only change how
  linuxcnc reads the tooltable.

---
 configs/sim/axis/lathe.ini     |    4 +++-
 src/emc/iotask/ioControl.cc    |    9 +++++----
 src/emc/iotask/ioControl_v2.cc |    9 ++++++---
 src/emc/rs274ngc/tool_parse.h  |    3 ++-
 src/emc/sai/driver.cc          |    2 +-
 5 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/configs/sim/axis/lathe.ini b/configs/sim/axis/lathe.ini
index b951964..5816823 100644
--- a/configs/sim/axis/lathe.ini
+++ b/configs/sim/axis/lathe.ini
@@ -178,6 +178,8 @@ EMCIO = 		io
 CYCLE_TIME =    0.100
 
 # tool table file
-TOOL_TABLE = lathe.tbl
+TOOL_TABLE = dlathe.tbl
 TOOL_CHANGE_POSITION = 2 0 1
 TOOL_CHANGE_WITH_SPINDLE_ON = 1
+FANUC_TOOLTIP_ORIENTATION = 1
+
diff --git a/src/emc/iotask/ioControl.cc b/src/emc/iotask/ioControl.cc
index 133c320..26c1dd4 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 */
@@ -242,6 +242,7 @@ 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();
@@ -748,7 +749,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");
     }
 
@@ -817,7 +818,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;
 
@@ -903,7 +904,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 85d9a10..161d531 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,
@@ -337,6 +338,8 @@ 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");
+printf ("Fetching FANUC_TOOLTIP_ORIENTATION = %d", fanuc_tooltip_orientation);
 
     // close it
     inifile.Close();
@@ -866,7 +869,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);
     }
 
@@ -969,7 +972,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;
 
@@ -1091,7 +1094,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.h b/src/emc/rs274ngc/tool_parse.h
index 684407b..f80e9e4 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_orientation // If true will use the Fanuc tool tip directions
 	);
 
 #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);
 }
 
 /************************************************************************/
-- 
1.7.0.4

