From 42fad8a754613305df0fbbd267807fe3a4b9131f Mon Sep 17 00:00:00 2001
From: Miguel Angel Ajo <miguelangel@nbee.es>
Date: Fri, 20 Apr 2012 23:20:56 +0200
Subject: [PATCH] Exceptions handled on board Load/Save

---
 pcbnew/scripting/pcbnew.i                     |  5 +++++
 pcbnew/scripting/pcbnew_scripting_helpers.cpp | 19 ++++++++++++++-----
 scripting/kicad.i                             |  3 ++-
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/pcbnew/scripting/pcbnew.i b/pcbnew/scripting/pcbnew.i
index ece9340af0..9851e518cd 100644
--- a/pcbnew/scripting/pcbnew.i
+++ b/pcbnew/scripting/pcbnew.i
@@ -39,6 +39,9 @@
 %ignore BOARD_ITEM::ZeroOffset;
 %ignore D_PAD::m_PadSketchModePenSize;
 
+// rename the Add method of classes to Add native, so we will handle
+// the Add method in python 
+
 %rename(AddNative) *::Add;
 
 // this is what it must be included in the wrapper .cxx code to compile
@@ -102,6 +105,8 @@
 %include <pcbnew_scripting_helpers.h>
 
 #ifdef BUILD_WITH_PLUGIN
+  // ignore RELEASER as nested classes are still unsupported by swig
+  %ignore IO_MGR::RELEASER;
   %include <io_mgr.h>
   %include <kicad_plugin.h>
 #endif
diff --git a/pcbnew/scripting/pcbnew_scripting_helpers.cpp b/pcbnew/scripting/pcbnew_scripting_helpers.cpp
index 89b678e35a..086238c2b8 100644
--- a/pcbnew/scripting/pcbnew_scripting_helpers.cpp
+++ b/pcbnew/scripting/pcbnew_scripting_helpers.cpp
@@ -27,6 +27,7 @@
  * @brief Scripting helper functions for pcbnew functionality
  */
 
+#include <Python.h>
 
 #include <pcbnew_scripting_helpers.h>
 #include <pcbnew.h>
@@ -35,6 +36,8 @@
 #include <class_board.h>
 #include <kicad_string.h>
 #include <io_mgr.h>
+#include <macros.h>
+#include <stdlib.h>
 
 static PCB_EDIT_FRAME *PcbEditFrame=NULL;
 
@@ -56,16 +59,19 @@ BOARD* LoadBoard(wxString& aFileName)
 
 BOARD* LoadBoard(wxString& aFileName,IO_MGR::PCB_FILE_T aFormat)
 {
+	static char ExceptionError[256];
 #ifdef USE_NEW_PCBNEW_LOAD
 	try{
 	   return IO_MGR::Load(aFormat,aFileName);	
-	} catch (IO_ERROR)
+	} catch (IO_ERROR e)
 	{
+		sprintf(ExceptionError, "%s\n", TO_UTF8(e.errorText) );
+		PyErr_SetString(PyExc_IOError,ExceptionError);
 		return NULL;
 	}
 #else
   fprintf(stderr,"Warning, LoadBoard not implemented without USE_NEW_PCBNEW_LOAD\n");
-	return NULL;
+   return NULL;
 #endif
 }
 
@@ -77,7 +83,7 @@ bool SaveBoard(wxString& aFilename, BOARD* aBoard)
 bool SaveBoard(wxString& aFileName, BOARD* aBoard,
                 IO_MGR::PCB_FILE_T aFormat)
 {
-
+  static char ExceptionError[256];
 #ifdef USE_NEW_PCBNEW_LOAD
   aBoard->m_Status_Pcb &= ~CONNEXION_OK;
   aBoard->SynchronizeNetsAndNetClasses();
@@ -101,9 +107,12 @@ bool SaveBoard(wxString& aFileName, BOARD* aBoard,
      IO_MGR::Save( aFormat, aFileName, aBoard, &props );
      return true;
   } 
-  catch (IO_ERROR)
+  catch (IO_ERROR e)
   {
-  	 return false;
+	sprintf(ExceptionError, "%s\n", TO_UTF8(e.errorText) );
+	PyErr_SetString(PyExc_IOError,ExceptionError);
+
+        return false;
   }
 
 #else
diff --git a/scripting/kicad.i b/scripting/kicad.i
index 02003c53a8..9784ede559 100644
--- a/scripting/kicad.i
+++ b/scripting/kicad.i
@@ -82,6 +82,7 @@
 /* exception handling */
 
 /* the IO_ERROR exception handler, not working yet... */
+/*
 %exception
 {
   try {
@@ -92,7 +93,7 @@
     return NULL;
   }
 }
-
+*/
 
 
 %include <dlist.h>