From: regrain Date: Mon, 28 Nov 2005 17:47:38 +0000 (+0000) Subject: * Add the CommandPy and CommandManager to the python wrapping X-Git-Tag: Version1.3~18 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=4f40a99daaac8192cf47e0cfb026c6a23bbe22ca;p=gdcm.git * Add the CommandPy and CommandManager to the python wrapping -- BeNours --- diff --git a/gdcmPython/CMakeLists.txt b/gdcmPython/CMakeLists.txt index ba695739..8f1623a4 100644 --- a/gdcmPython/CMakeLists.txt +++ b/gdcmPython/CMakeLists.txt @@ -2,6 +2,7 @@ INCLUDE_DIRECTORIES( ${GDCM_SOURCE_DIR}/src + ${GDCM_SOURCE_DIR}/gdcmPython ${PYTHON_INCLUDE_PATH} ${GDCM_BINARY_DIR}/ ) diff --git a/gdcmPython/demo/CMakeLists.txt b/gdcmPython/demo/CMakeLists.txt index 4c76a3ba..eaae23a8 100644 --- a/gdcmPython/demo/CMakeLists.txt +++ b/gdcmPython/demo/CMakeLists.txt @@ -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 index 00000000..d8a6f02b --- /dev/null +++ b/gdcmPython/demo/MakeDicomDir.py.in @@ -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() + diff --git a/gdcmPython/demo/PrintDicomDir.py.in b/gdcmPython/demo/PrintDicomDir.py.in index dcd47fef..266adb19 100644 --- a/gdcmPython/demo/PrintDicomDir.py.in +++ b/gdcmPython/demo/PrintDicomDir.py.in @@ -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(): diff --git a/gdcmPython/gdcm.i b/gdcmPython/gdcm.i index 26ec3cc1..87da7195 100644 --- a/gdcmPython/gdcm.i +++ b/gdcmPython/gdcm.i @@ -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" @@ -38,45 +42,6 @@ #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 index 00000000..fb00b8ad --- /dev/null +++ b/gdcmPython/gdcmCommandPy.h @@ -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