]> Creatis software - gdcm.git/commitdiff
* Add the CommandPy and CommandManager to the python wrapping
authorregrain <regrain>
Mon, 28 Nov 2005 17:47:38 +0000 (17:47 +0000)
committerregrain <regrain>
Mon, 28 Nov 2005 17:47:38 +0000 (17:47 +0000)
   -- BeNours

gdcmPython/CMakeLists.txt
gdcmPython/demo/CMakeLists.txt
gdcmPython/demo/MakeDicomDir.py.in [new file with mode: 0644]
gdcmPython/demo/PrintDicomDir.py.in
gdcmPython/gdcm.i
gdcmPython/gdcmCommandPy.h [new file with mode: 0644]

index ba6957394782755a5969540ee17088996ffde128..8f1623a413140f696bc4c982ca013b8bc7a607a0 100644 (file)
@@ -2,6 +2,7 @@
 
 INCLUDE_DIRECTORIES(
   ${GDCM_SOURCE_DIR}/src
+  ${GDCM_SOURCE_DIR}/gdcmPython
   ${PYTHON_INCLUDE_PATH}
   ${GDCM_BINARY_DIR}/
 )
index 4c76a3bae078854150d723300c8a8789d1eee888..eaae23a84fd9853c42c3f0c0aa52322c8a310768 100644 (file)
@@ -25,6 +25,9 @@ IF(PYTHON_EXECUTABLE)
   ADD_TEST(Python-PrintDicomDir ${PYTHON_EXECUTABLE}
     ${GDCM_BINARY_DIR}/gdcmPython/demo/PrintDicomDir.py
     )
+  ADD_TEST(Python-MakeDicomDir ${PYTHON_EXECUTABLE}
+    ${GDCM_BINARY_DIR}/gdcmPython/demo/PrintDicomDir.py
+    )
 ELSE(PYTHON_EXECUTABLE)
   MESSAGE(FATAL_ERROR "You requested testing for python, but python was not
     found")
@@ -48,6 +51,11 @@ CONFIGURE_FILE(
     ${GDCM_BINARY_DIR}/gdcmPython/demo/PrintDicomDir.py
 )
 
+CONFIGURE_FILE(
+    ${GDCM_SOURCE_DIR}/gdcmPython/demo/MakeDicomDir.py.in
+    ${GDCM_BINARY_DIR}/gdcmPython/demo/MakeDicomDir.py
+)
+
 CONFIGURE_FILE(
     ${GDCM_SOURCE_DIR}/gdcmPython/demo/PrintDict.py.in
     ${GDCM_BINARY_DIR}/gdcmPython/demo/PrintDict.py
diff --git a/gdcmPython/demo/MakeDicomDir.py.in b/gdcmPython/demo/MakeDicomDir.py.in
new file mode 100644 (file)
index 0000000..d8a6f02
--- /dev/null
@@ -0,0 +1,62 @@
+import sys
+import os.path
+from gdcmConfigDemo import *
+from gdcmPython.core import *
+
+def PrintUse():
+   print ""
+   print "Use :"
+   print "-----"
+   print "%s" % sys.argv[0]
+   print "   Create the DicomDir from the GDCM_DATA_ROOT path"
+   print ""
+   print ""
+
+### Progress methods
+def startMethod():
+   print "Start"
+def progressMethod():
+   print "Progress",dicomdir.GetProgress()
+def endMethod():
+   print "End"
+
+### Get filename from command line or default it
+try:
+   fileName = sys.argv[1]
+except IndexError:
+   fileName = os.path.join(GDCM_DATA_ROOT, "DICOMDIR")
+   if( not os.path.isfile(fileName) ):
+      fileName=os.path.join(os.path.split(sys.argv[0])[0],"DICOMDIR")
+
+try:
+   printLevel = int(sys.argv[2])
+except IndexError:
+   printLevel = 1
+
+### Build the DicomDir element list
+dicomdir = gdcm.DicomDir.New()
+
+cmd=gdcm.CommandPy.New()
+cmd.SetCallback(startMethod)
+gdcm.CommandManager.SetCommand(dicomdir,gdcm.CMD_STARTPROGRESS,cmd)
+cmd=gdcm.CommandPy.New()
+cmd.SetCallback(endMethod)
+gdcm.CommandManager.SetCommand(dicomdir,gdcm.CMD_ENDPROGRESS,cmd)
+cmd=gdcm.CommandPy.New()
+cmd.SetCallback(progressMethod)
+gdcm.CommandManager.SetCommand(dicomdir,gdcm.CMD_PROGRESS,cmd)
+
+dicomdir.SetDirectoryName(GDCM_DATA_ROOT)
+dicomdir.Load()
+if not dicomdir.IsReadable():
+   PrintUse()
+   raise RuntimeError,"The '%s' DicomDir is not readable with gdcm." % fileName
+
+print "DICOMDIR -->",fileName
+print "##############################################################"
+print "## Display all the elements and their respective values"
+print "## found in the ", fileName, " file."
+print "##############################################################"
+dicomdir.SetPrintLevel(-1)
+dicomdir.Print()
+
index dcd47fef32a27530b8e3407318497e67d5c46637..266adb19dfdbe1047734df02a01b6ebaccb42a01 100644 (file)
@@ -12,14 +12,6 @@ def PrintUse():
    print ""
    print ""
 
-### Progress methods
-def startMethod():
-   print "Start"
-def progressMethod():
-   print "Progress",dicomdir.GetProgress()
-def endMethod():
-   print "End"
-
 ### Get filename from command line or default it
 try:
    fileName = sys.argv[1]
@@ -36,10 +28,6 @@ except IndexError:
 ### Build the DicomDir element list
 dicomdir = gdcm.DicomDir.New()
 
-dicomdir.SetStartMethod(startMethod)
-dicomdir.SetProgressMethod(progressMethod)
-dicomdir.SetEndMethod(endMethod)
-
 dicomdir.SetFileName(fileName)
 dicomdir.Load()
 if not dicomdir.IsReadable():
index 26ec3cc1de6be00c0ac260965aec8441e8ab0a38..87da7195634f175d59d13bc8862cf94928ccaee5 100644 (file)
@@ -6,6 +6,10 @@
 #include "gdcmCommon.h"
 #include "gdcmBase.h"
 #include "gdcmRefCounter.h"
+#include "gdcmCommand.h"
+#include "gdcmCommandPy.h"
+#include "gdcmDebug.h"
+#include "gdcmCommandManager.h"
 #include "gdcmTagKey.h"
 #include "gdcmVRKey.h"
 #include "gdcmDict.h"
 #include "gdcmTS.h"
 #include "gdcmDictGroupName.h"
 
-////////////////////////////////////////////////////////////////////////////
-/// Refer (below) to the definition of multi-argument typemap
-///   %typemap(python, in)
-///      ( gdcm::DicomDir::Method*, void*, gdcm::DicomDir::Method*)
-/// for detail on gdcmPythonVoidFunc() and gdcmPythonVoidFuncArgDelete().
-void gdcmPythonVoidFunc(void *arg)
-{
-   PyObject *arglist, *result;
-   PyObject *func = (PyObject *)arg;
-
-   arglist = Py_BuildValue("()");
-
-   result = PyEval_CallObject(func, arglist);
-   Py_DECREF(arglist);
-
-   if (result)
-   {
-      Py_XDECREF(result);
-   }
-   else
-   {
-      if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
-      {
-         std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
-         Py_Exit(1);
-      }
-      PyErr_Print();
-   }
-}
-
-void gdcmPythonVoidFuncArgDelete(void *arg)
-{
-   PyObject *func = (PyObject *)arg;
-   if (func)
-   {
-      Py_DECREF(func);
-   }
-}
-
 /// This is required in order to avoid %including all the gdcm include files.
 using namespace gdcm;
 %}
@@ -117,36 +82,6 @@ typedef unsigned long long uint64_t;
    $result = newEntry;
 }
 
-////////////////////////////////////////////////////////////////////////////
-// Multi-argument typemap designed for wrapping the progress related methods
-// in order to control from an external application the computation of
-// a DicomDir object (see DicomDir::SetStartMethod*,
-// DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
-// Motivation: since DicomDir parsing can be quite long, a GUI application
-//             needs to display the avancement and potentially offer a
-//             cancel method to the user (when this one feels things are
-//             longer than expected).
-// Example of usage: refer to demo/DicomDirProgressMethod.py
-// Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
-//       in the Swig verbatim section of this gdcm.i i.e. in the above section
-//       enclosed within the %{ ... %} scope operator ).
-%typemap(python, in) (void(*method)(void *),void *arg,void(*argDelete)(void *))
-{
-       if($input!=Py_None)
-       {
-               Py_INCREF($input);
-               $1=gdcmPythonVoidFunc;
-               $2=$input;
-               $3=gdcmPythonVoidFuncArgDelete;
-       }
-       else
-       {
-               $1=NULL;
-               $2=NULL;
-               $3=NULL;
-       }
-}
-
 ////////////////////  STL string versus Python str  ////////////////////////
 // Convertion returning a C++ string.
 %typemap(out) std::string
@@ -244,6 +179,10 @@ typedef unsigned long long uint64_t;
 %include "gdcmCommon.h"
 %include "gdcmBase.h"
 %include "gdcmRefCounter.h"
+%include "gdcmCommand.h"
+%include "gdcmCommandPy.h"
+%include "gdcmDebug.h"
+%include "gdcmCommandManager.h"
 %include "gdcmTagKey.h"
 %include "gdcmVRKey.h"
 %include "gdcmDicomEntry.h"
diff --git a/gdcmPython/gdcmCommandPy.h b/gdcmPython/gdcmCommandPy.h
new file mode 100644 (file)
index 0000000..fb00b8a
--- /dev/null
@@ -0,0 +1,86 @@
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: gdcmCommandPy.h,v $
+  Language:  C++
+  Date:      $Date: 2005/11/28 17:47:38 $
+  Version:   $Revision: 1.1 $
+                                                                                
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+                                                                                
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+                                                                                
+=========================================================================*/
+
+#ifndef GDCMCOMMANDPY_H
+#define GDCMCOMMANDPY_H
+
+#include "gdcmDebug.h"
+#include "gdcmCommand.h"
+#include "python.h"
+
+namespace gdcm 
+{
+//-----------------------------------------------------------------------------
+/**
+ * \brief CommandPy base class to react on a gdcm event
+ *
+ * \remarks The execution parameter depends on the
+ */
+class CommandPy : public Command
+{
+   gdcmTypeMacro(CommandPy);
+
+public:
+   static CommandPy *New() {return new CommandPy(); }
+
+   void SetCallback(PyObject *callback)
+   {
+      Callback = callback;
+      Py_INCREF(Callback);
+   }
+
+   virtual void Execute()
+   {
+      PyObject *arglist = Py_BuildValue("()");
+      PyObject *result = PyEval_CallObject(Callback, arglist);
+      Py_DECREF(arglist);
+
+      if (result)
+      {
+         Py_XDECREF(result);
+      }
+      else
+      {
+         if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
+         {
+            std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
+            Py_Exit(1);
+         }
+         PyErr_Print();
+      }
+   }
+
+protected:
+   CommandPy()
+   {
+      Callback = NULL;
+   }
+   virtual ~CommandPy() 
+   {
+      if (Callback)
+         Py_DECREF(Callback);
+   }
+
+private:
+   /// pointer to the initialisation method for any progress bar   
+   PyObject *Callback;
+};
+} // end namespace gdcm
+
+//-----------------------------------------------------------------------------
+#endif