From ac70f8068972ee8ee7480a49262e3b7fda5f4e97 Mon Sep 17 00:00:00 2001
From: Ineiev <ineiev@users.berlios.de>
Date: Tue, 27 Oct 2009 03:05:47 +0000
Subject: [PATCH] Add SwapSides_nogui action

The action is similar to SwapSides, but it
depends on no real GUI, so can have effect when
running an exporter from command line or
with batch GUI.
---
 src/action.c |   82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/src/action.c b/src/action.c
index 6b979d3..29979c3 100644
--- a/src/action.c
+++ b/src/action.c
@@ -6793,6 +6793,85 @@ ActionPSCalib (int argc, char **argv, int x, int y)
   return 0;
 }
 
+/* ---------------------------------------------------------------------- */
+static const char swapsides_syntax[] =
+"SwapSides_nogui(|v|h|r)";
+
+static const char swapsides_help[] =
+"Swaps the side of the board you're looking at.";
+
+/* %start-doc actions SwapSides_nogui
+
+This action shows the opposite side of the board: version
+for scripts when there is no GUI.
+
+Normally, this action changes which pads and silk layer are drawn as
+true silk, and which are drawn as the "invisible" layer.  It also
+determines which solder mask you see.
+
+As a special case, if the layer group for the side you're looking at
+is visible and currently active, and the layer group for the opposite
+is not visible (i.e. disabled), then this action will also swap which
+layer group is visible and active, effectively swapping the ``working
+side'' of the board.
+
+%end-doc */
+
+static int
+SwapSides (int argc, char **argv, int x, int y)
+{
+  int comp_group = GetLayerGroupNumberByNumber (max_layer + COMPONENT_LAYER);
+  int solder_group = GetLayerGroupNumberByNumber (max_layer + SOLDER_LAYER);
+  int active_group = GetLayerGroupNumberByNumber (LayerStack[0]);
+  int comp_showing =
+    PCB->Data->Layer[PCB->LayerGroups.Entries[comp_group][0]].On;
+  int solder_showing =
+    PCB->Data->Layer[PCB->LayerGroups.Entries[solder_group][0]].On;
+
+
+  if (argc > 0)
+    {
+     /* basic arguments handling for compatibility with GTK and Lesstif HIDs */
+      switch (argv[0][0]) {
+      case 'h':
+      case 'H':
+	break;
+      case 'v':
+      case 'V':
+	break;
+      case 'r':
+      case 'R':
+	break;
+      default:
+	return 1;
+      }
+    }
+
+  Settings.ShowSolderSide = !Settings.ShowSolderSide;
+  if (Settings.ShowSolderSide)
+    {
+      if (active_group == comp_group && comp_showing && !solder_showing)
+	{
+	  ChangeGroupVisibility (PCB->LayerGroups.Entries[comp_group][0], 0,
+				 0);
+	  ChangeGroupVisibility (PCB->LayerGroups.Entries[solder_group][0], 1,
+				 1);
+	}
+    }
+  else
+    {
+      if (active_group == solder_group && solder_showing && !comp_showing)
+	{
+	  ChangeGroupVisibility (PCB->LayerGroups.Entries[solder_group][0], 0,
+				 0);
+	  ChangeGroupVisibility (PCB->LayerGroups.Entries[comp_group][0], 1,
+				 1);
+	}
+    }
+
+  return 0;
+}
+
 /* --------------------------------------------------------------------------- */
 
 HID_Action action_action_list[] = {
@@ -6931,6 +7010,9 @@ HID_Action action_action_list[] = {
   {"SetValue", 0, ActionSetValue,
    setvalue_help, setvalue_syntax}
   ,
+  {"SwapSides_nogui", 0, SwapSides_nogui,
+   swapsides_help, swapsides_syntax}
+  ,
   {"ToggleHideName", 0, ActionToggleHideName,
    togglehidename_help, togglehidename_syntax}
   ,
-- 
1.6.4.1

