diff --git a/Modules/Python/QmitkPythonVariableStackTableModel.cpp b/Modules/Python/QmitkPythonVariableStackTableModel.cpp index f4506ec4ef..55385f6607 100755 --- a/Modules/Python/QmitkPythonVariableStackTableModel.cpp +++ b/Modules/Python/QmitkPythonVariableStackTableModel.cpp @@ -1,163 +1,217 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkPythonVariableStackTableModel.h" #include #include #include #include #include #include +#include const QString QmitkPythonVariableStackTableModel::MITK_IMAGE_VAR_NAME = "mitkImage"; +const QString QmitkPythonVariableStackTableModel::MITK_SURFACE_VAR_NAME = "mitkSurface"; + QmitkPythonVariableStackTableModel::QmitkPythonVariableStackTableModel(QObject *parent) :QAbstractTableModel(parent) { mitk::ModuleContext* context = mitk::GetModuleContext(); mitk::ServiceReference serviceRef = context->GetServiceReference(); m_PythonService = context->GetService(serviceRef); m_PythonService->AddPythonCommandObserver( this ); } QmitkPythonVariableStackTableModel::~QmitkPythonVariableStackTableModel() { m_PythonService->RemovePythonCommandObserver( this ); } bool QmitkPythonVariableStackTableModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ) { // Early exit, returning true, but not actually doing anything (ignoring data). if (action == Qt::IgnoreAction) return true; // Note, we are returning true if we handled it, and false otherwise bool returnValue = false; if(data->hasFormat("application/x-mitk-datanodes")) { MITK_DEBUG("QmitkPythonVariableStackTableModel") << "dropped MITK DataNode"; returnValue = true; QString arg = QString(data->data("application/x-mitk-datanodes").data()); QStringList listOfDataNodeAddressPointers = arg.split(","); QStringList::iterator slIter; int i = 0; + int j = 0; for (slIter = listOfDataNodeAddressPointers.begin(); slIter != listOfDataNodeAddressPointers.end(); slIter++) { long val = (*slIter).toLong(); mitk::DataNode* node = static_cast((void*)val); mitk::Image* mitkImage = dynamic_cast(node->GetData()); -//QString::fromStdString((node->GetName()); - QString varName = MITK_IMAGE_VAR_NAME; - if( i > 0 ) - varName = QString("%1%2").arg(MITK_IMAGE_VAR_NAME).arg(i); - MITK_DEBUG("varName") << "varName" << varName; - m_PythonService->CopyToPythonAsItkImage( mitkImage, MITK_IMAGE_VAR_NAME ); - ++i; + + if( mitkImage ) + { + QString varName = MITK_IMAGE_VAR_NAME; + if( i > 0 ) + varName = QString("%1%2").arg(MITK_IMAGE_VAR_NAME).arg(i); + MITK_DEBUG("varName") << "varName" << varName; + + bool exportAsCvImage = m_PythonService->IsOpenCvPythonWrappingAvailable(); + + if( mitkImage->GetDimension() == 2 && exportAsCvImage ) + { + int ret = QMessageBox::question(this, "Export option", + "2D image detected. Export as OpenCV image to Python instead of an ITK image?", + QMessageBox::Yes|QMessageBox::No); + exportAsCvImage = ret == QMessageBox::Yes; + if(exportAsCvImage) + { + m_PythonService->CopyToPythonAsCvImage( mitkImage, MITK_IMAGE_VAR_NAME ); + ++i; + } + } + if( !exportAsCvImage ) + { + if( m_PythonService->IsItkPythonWrappingAvailable() ) + { + m_PythonService->CopyToPythonAsItkImage( mitkImage, MITK_IMAGE_VAR_NAME ); + ++i; + } + else + { + MITK_ERROR << "ITK Python wrapping not available. Skipping export for image " << node->GetName(); + } + } + } + else + { + mitk::Surface* surface = dynamic_cast(node->GetData()); + + if( surface ) + { + QString varName = MITK_SURFACE_VAR_NAME; + if( j > 0 ) + varName = QString("%1%2").arg(MITK_SURFACE_VAR_NAME).arg(j); + MITK_DEBUG("varName") << "varName" << varName; + + if( m_PythonService->IsVtkPythonWrappingAvailable() ) + { + m_PythonService->CopyToPythonAsVtkPolyData( surface, MITK_SURFACE_VAR_NAME ); + ++j; + } + else + { + MITK_ERROR << "VTK Python wrapping not available. Skipping export for surface " << node->GetName(); + } + } + } } } return returnValue; } QVariant QmitkPythonVariableStackTableModel::headerData(int section, Qt::Orientation orientation, int role) const { QVariant headerData; // show only horizontal header if ( role == Qt::DisplayRole ) { if( orientation == Qt::Horizontal ) { // first column: "Attribute" if(section == 0) headerData = "Attribute"; else if(section == 1) headerData = "Value"; else if(section == 2) headerData = "Type"; } } return headerData; } Qt::ItemFlags QmitkPythonVariableStackTableModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QAbstractItemModel::flags(index); if(index.isValid()) return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | flags; else return Qt::ItemIsDropEnabled | flags; } int QmitkPythonVariableStackTableModel::rowCount(const QModelIndex &) const { return m_VariableStack.size(); } int QmitkPythonVariableStackTableModel::columnCount(const QModelIndex &) const { return 3; } QVariant QmitkPythonVariableStackTableModel::data(const QModelIndex &index, int role) const { if (index.isValid() && !m_VariableStack.empty()) { if(role == Qt::DisplayRole) { mitk::PythonVariable item = m_VariableStack.at(index.row()); if(index.column() == 0) return item.m_Name; if(index.column() == 1) return item.m_Value; if(index.column() == 2) return item.m_Type; } } return QVariant(); } QStringList QmitkPythonVariableStackTableModel::mimeTypes() const { return QAbstractTableModel::mimeTypes(); QStringList types; types << "application/x-mitk-datanodes"; types << "application/x-qabstractitemmodeldatalist"; return types; } Qt::DropActions QmitkPythonVariableStackTableModel::supportedDropActions() const { return Qt::CopyAction | Qt::MoveAction; } void QmitkPythonVariableStackTableModel::CommandExecuted(const QString &pythonCommand) { MITK_DEBUG("QmitkPythonVariableStackTableModel") << "command was executed " << pythonCommand.toStdString(); m_VariableStack = m_PythonService->GetVariableStack(); QAbstractTableModel::reset(); } QList QmitkPythonVariableStackTableModel::GetVariableStack() const { return m_VariableStack; } diff --git a/Modules/Python/QmitkPythonVariableStackTableModel.h b/Modules/Python/QmitkPythonVariableStackTableModel.h index dc2f2aa9bb..8b9d933e85 100755 --- a/Modules/Python/QmitkPythonVariableStackTableModel.h +++ b/Modules/Python/QmitkPythonVariableStackTableModel.h @@ -1,60 +1,61 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef QmitkPythonVariableStackTableModel_h #define QmitkPythonVariableStackTableModel_h #include #include #include #include "mitkIPythonService.h" #include "mitkPythonExports.h" /// /// implements a table model to show the variables of the Python "__main__" dictionary /// furthermore implements dragging and dropping of datanodes (conversion from and to python) /// class MITK_PYTHON_EXPORT QmitkPythonVariableStackTableModel : public QAbstractTableModel, public mitk::PythonCommandObserver { Q_OBJECT public: static const QString MITK_IMAGE_VAR_NAME; + static const QString MITK_SURFACE_VAR_NAME; QmitkPythonVariableStackTableModel(QObject *parent = 0); virtual ~QmitkPythonVariableStackTableModel(); int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; Qt::ItemFlags flags( const QModelIndex& index ) const; virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; QStringList mimeTypes() const; bool dropMimeData ( const QMimeData *, Qt::DropAction, int, int, const QModelIndex & ); Qt::DropActions supportedDropActions() const; Qt::DropActions supportedDragActions() const; void CommandExecuted(const QString& pythonCommand); QList GetVariableStack() const; private: QList m_VariableStack; mitk::IPythonService* m_PythonService; }; #endif // QmitkPythonVariableStackTableModel_h diff --git a/Modules/Python/mitkIPythonService.h b/Modules/Python/mitkIPythonService.h index 4de02db8f5..5255aeeb63 100644 --- a/Modules/Python/mitkIPythonService.h +++ b/Modules/Python/mitkIPythonService.h @@ -1,118 +1,135 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef mitkIPythonService_h #define mitkIPythonService_h // mitk #include "mitkPythonExports.h" #include "mitkImage.h" //for microservices #include // Qt #include #include #include +#include "mitkSurface.h" namespace mitk { /// /// describes a python variable (data container) /// \see IPythonService::GetVariableStack() /// struct PythonVariable { QString m_Name; QString m_Type; QString m_Value; }; /// /// a PythonCommandObserver gets informed as soon as a python command was issued /// \see IPythonService::AddPythonCommandObserver() /// class PythonCommandObserver { public: virtual void CommandExecuted(const QString& pythonCommand) = 0; }; /// /// The central service for issuing Python Code /// The class also enables to transfer mitk images to python as itk::Image and vice versa /// \see IPythonService::GetVariableStack() /// class MITK_PYTHON_EXPORT IPythonService { public: /// /// Constant representing a single line command /// \see IPythonService::Execute() static const int SINGLE_LINE_COMMAND = 0; /// /// Constant representing a command in which the commands are seperated by new lines, i.e. "\\n" /// \see IPythonService::Execute() static const int MULTI_LINE_COMMAND = 1; /// /// Constant representing a single line command x which is run as "eval(x)" /// \see IPythonService::Execute() static const int EVAL_COMMAND = 2; /// /// Executes a python command. /// \return A variant containing the return value of the python code (if any) virtual QVariant Execute( const QString& pythonCommand, int commandType = SINGLE_LINE_COMMAND ) = 0; /// /// \return The list of variables in the __main__ namespace virtual QList GetVariableStack() const = 0; /// /// adds a command observer which is informed after a command was issued with "Execute" virtual void AddPythonCommandObserver( PythonCommandObserver* observer ) = 0; /// /// removes a specific command observer virtual void RemovePythonCommandObserver( PythonCommandObserver* observer ) = 0; /// /// notify all observer. this should only be used if it can be garantueed that the /// current python interpreter instance got another command from anywhere else /// the the Execute() method of this service, e.g. the shell widget uses this function /// since it does not use Execute() virtual void NotifyObserver( const QString& command ) = 0; + /// + /// \return true, if itk wrapping is available, false otherwise + virtual bool IsItkPythonWrappingAvailable() = 0; /// /// copies an mitk image as itk image into the python interpreter process /// the image will be available as "varName" in python if everythin worked /// \return true if image was copied, else false virtual bool CopyToPythonAsItkImage( mitk::Image* image, const QString& varName ) = 0; /// /// copies an itk image from the python process that is named "varName" /// \return the image or 0 if copying was not possible virtual mitk::Image::Pointer CopyItkImageFromPython( const QString& varName ) = 0; + /// + /// \return true, if OpenCv wrapping is available, false otherwise + virtual bool IsOpenCvPythonWrappingAvailable() = 0; /// /// \see CopyToPythonAsItkImage() virtual bool CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ) = 0; /// /// \see CopyCvImageFromPython() virtual mitk::Image::Pointer CopyCvImageFromPython( const QString& varName ) = 0; + /// + /// \return true, if vtk wrapping is available, false otherwise + virtual bool IsVtkPythonWrappingAvailable() = 0; + /// + /// \see CopyToPythonAsItkImage() + virtual bool CopyToPythonAsVtkPolyData( mitk::Surface* surface, const QString& varName ) = 0; + /// + /// \see CopyCvImageFromPython() + virtual mitk::Surface::Pointer CopyVtkPolyDataFromPython( const QString& varName ) = 0; + /// /// nothing to do here virtual ~IPythonService(); // leer in mitkIPythonService.cpp implementieren }; } US_DECLARE_SERVICE_INTERFACE(mitk::IPythonService, "org.mitk.services.IPythonService") #endif diff --git a/Modules/Python/mitkPythonService.cpp b/Modules/Python/mitkPythonService.cpp index 5f8d8d1c48..b45a36a4f7 100644 --- a/Modules/Python/mitkPythonService.cpp +++ b/Modules/Python/mitkPythonService.cpp @@ -1,297 +1,379 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkPythonService.h" #include #include #include #include const QString mitk::PythonService::m_TmpImageName("temp_mitk_image"); mitk::PythonService::PythonService() { m_PythonManager.initialize(); } mitk::PythonService::~PythonService() { } QVariant mitk::PythonService::Execute(const QString &pythonCommand, int commandType) { { MITK_DEBUG("mitk::PythonService") << "pythonCommand = " << pythonCommand.toStdString(); MITK_DEBUG("mitk::PythonService") << "commandType = " << commandType; } QVariant result; bool commandIssued = true; if(commandType == IPythonService::SINGLE_LINE_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::SingleInput ); else if(commandType == IPythonService::MULTI_LINE_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::FileInput ); else if(commandType == IPythonService::EVAL_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::EvalInput ); else commandIssued = false; if(commandIssued) this->NotifyObserver(pythonCommand); return result; } QList mitk::PythonService::GetVariableStack() const { QList list; PyObject* dict = PyImport_GetModuleDict(); PyObject* object = PyDict_GetItemString(dict, "__main__"); PyObject* dirMain = PyObject_Dir(object); PyObject* tempObject; if(dirMain) { QString attr, attrValue, attrType; for(int i = 0; iob_type->tp_name; if(PyUnicode_Check(tempObject) || PyString_Check(tempObject)) attrValue = PyString_AsString(tempObject); else attrValue = ""; mitk::PythonVariable var; var.m_Name = attr; var.m_Value = attrValue; var.m_Type = attrType; list.append(var); } } return list; } void mitk::PythonService::AddPythonCommandObserver(mitk::PythonCommandObserver *observer) { if(!m_Observer.contains(observer)) m_Observer.append(observer); } void mitk::PythonService::RemovePythonCommandObserver(mitk::PythonCommandObserver *observer) { m_Observer.removeOne(observer); } void mitk::PythonService::NotifyObserver(const QString &command) { MITK_DEBUG("mitk::PythonService") << "number of observer " << m_Observer.size(); foreach(mitk::PythonCommandObserver* observer, m_Observer) { observer->CommandExecuted(command); } } QString mitk::PythonService::GetTempImageName(const QString& ext) const { QString tmpFolder = QDir::tempPath(); QString fileName = tmpFolder + QDir::separator() + m_TmpImageName + ext; return fileName; } bool mitk::PythonService::CopyToPythonAsItkImage(mitk::Image *image, const QString &varName) { // save image QString fileName = this->GetTempImageName( QString::fromStdString(mitk::IOUtil::DEFAULTIMAGEEXTENSION) ); MITK_DEBUG("PythonService") << "Saving temporary file " << fileName.toStdString(); if( !mitk::IOUtil::SaveImage(image, fileName.toStdString()) ) { MITK_ERROR << "Temporary file could not be created."; } else { // TODO CORRECT TYPE SETUP, MAKE MITK_DEBUG("PythonService") MITK_DEBUG("PythonService") int dim = image->GetDimension(); mitk::PixelType pixelType = image->GetPixelType(); itk::ImageIOBase::IOPixelType ioPixelType = image->GetPixelType().GetPixelTypeId(); // default pixeltype: unsigned short QString type = "US"; if( ioPixelType == itk::ImageIOBase::SCALAR ) { if( pixelType.GetTypeId() == typeid(double) ) type = "D"; else if( pixelType.GetTypeId() == typeid(float) ) type = "F"; if( pixelType.GetTypeId() == typeid(long double) ) type = "LD"; else if( pixelType.GetTypeId() == typeid(short) ) type = "SS"; else if( pixelType.GetTypeId() == typeid(signed char) ) type = "SC"; else if( pixelType.GetTypeId() == typeid(signed int) ) type = "SI"; else if( pixelType.GetTypeId() == typeid(signed long) ) type = "SL"; else if( pixelType.GetTypeId() == typeid(signed short) ) type = "SS"; else if( pixelType.GetTypeId() == typeid(unsigned char) ) type = "UC"; else if( pixelType.GetTypeId() == typeid(unsigned int) ) type = "UI"; else if( pixelType.GetTypeId() == typeid(unsigned long) ) type = "UL"; else if( pixelType.GetTypeId() == typeid(unsigned short) ) type = "US"; } MITK_DEBUG("PythonService") << "Got mitk image with type " << type.toStdString() << " and dim " << dim; QString command; command.append( QString("import itk\n") ); command.append( QString("imageType = itk.Image[%1, %2]\n") .arg( type ).arg( dim ) ); command.append( QString("readerType = itk.ImageFileReader[imageType]\n") ); command.append( QString("reader = readerType.New()\n") ); command.append( QString("reader.SetFileName( \"%1\" )\n") .arg(fileName) ); command.append( QString("reader.Update()\n") ); command.append( QString("%1 = reader.GetOutput()\n").arg( varName ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); QFile file(fileName); MITK_DEBUG("PythonService") << "Removing file " << fileName.toStdString(); file.remove(); return true; } return false; } mitk::Image::Pointer mitk::PythonService::CopyItkImageFromPython(const QString &varName) { mitk::Image::Pointer mitkImage; QString command; QString fileName = GetTempImageName( QString::fromStdString( mitk::IOUtil::DEFAULTIMAGEEXTENSION ) ); MITK_DEBUG("PythonService") << "Saving temporary file with python itk code " << fileName.toStdString(); command.append( QString("import itk\n") ); command.append( QString( "writer = itk.ImageFileWriter[ %1 ].New()\n").arg( varName ) ); command.append( QString( "writer.SetFileName( \"%1\" )\n").arg(fileName) ); command.append( QString( "writer.SetInput( %1 )\n").arg(varName) ); command.append( QString( "writer.Update()\n" ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); try { MITK_DEBUG("PythonService") << "Loading temporary file " << fileName.toStdString() << " as MITK image"; mitkImage = mitk::IOUtil::LoadImage( fileName.toStdString() ); } catch(std::exception& e) { MITK_ERROR << e.what(); } QFile file(fileName); if( file.exists() ) { MITK_DEBUG("PythonService") << "Removing temporary file " << fileName.toStdString(); file.remove(); } return mitkImage; } + bool mitk::PythonService::CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ) { bool convert = false; if(image->GetDimension() != 2) { MITK_ERROR << "Only 2D images allowed for OpenCV images"; return convert; } // try to save mitk image QString fileName = this->GetTempImageName( QString::fromStdString(".bmp") ); MITK_DEBUG("PythonService") << "Saving temporary file " << fileName.toStdString(); if( !mitk::IOUtil::SaveImage(image, fileName.toStdString()) ) { MITK_ERROR << "Temporary file " << fileName.toStdString() << " could not be created."; return convert; } QString command; command.append( QString("import cv\n") ); command.append( QString("%1 = cv.LoadImage(\"%2\")\n") .arg( varName ).arg( fileName ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); MITK_DEBUG("PythonService") << "Removing file " << fileName.toStdString(); QFile file(fileName); file.remove(); - return true; - + convert = true; + return convert; } mitk::Image::Pointer mitk::PythonService::CopyCvImageFromPython( const QString& varName ) { mitk::Image::Pointer mitkImage; QString command; QString fileName = GetTempImageName( QString::fromStdString( ".bmp" ) ); MITK_DEBUG("PythonService") << "run python command to save image with opencv to " << fileName.toStdString(); command.append( QString("import cv\n") ); command.append( QString( "cv.SaveImage(\"%1\", %2)\n").arg( fileName ).arg( varName ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); try { MITK_DEBUG("PythonService") << "Loading temporary file " << fileName.toStdString() << " as MITK image"; mitkImage = mitk::IOUtil::LoadImage( fileName.toStdString() ); } catch(std::exception& e) { MITK_ERROR << e.what(); } QFile file(fileName); if( file.exists() ) { MITK_DEBUG("PythonService") << "Removing temporary file " << fileName.toStdString(); file.remove(); } return mitkImage; } ctkAbstractPythonManager *mitk::PythonService::GetPythonManager() { return &m_PythonManager; } +mitk::Surface::Pointer mitk::PythonService::CopyVtkPolyDataFromPython( const QString& varName ) +{ + mitk::Surface::Pointer newSurface; + + QString command; + QString fileName = GetTempImageName( ".stl" ); + + MITK_DEBUG("PythonService") << "run python command to save polydata with vtk to " << fileName.toStdString(); + command = QString ( + "vtkStlWriter = vtk.vtkSTLWriter()\n" + "vtkStlWriter.SetInput(%1)\n" + "vtkStlWriter.SetFileName(\"%2\")\n" + "vtkStlWriter.Write()\n").arg(varName).arg(fileName); + + MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); + this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); + + try + { + MITK_DEBUG("PythonService") << "Loading temporary file " << fileName.toStdString() << " as MITK Surface"; + newSurface = mitk::IOUtil::LoadSurface( fileName.toStdString() ); + } + catch(std::exception& e) + { + MITK_ERROR << e.what(); + } + + QFile file(fileName); + if( file.exists() ) + { + MITK_DEBUG("PythonService") << "Removing temporary file " << fileName.toStdString(); + file.remove(); + } + + return newSurface; +} + +bool mitk::PythonService::CopyToPythonAsVtkPolyData( mitk::Surface* surface, const QString& varName ) +{ + bool convert = false; + + // try to save mitk image + QString fileName = this->GetTempImageName( ".stl" ); + MITK_DEBUG("PythonService") << "Saving temporary file " << fileName.toStdString(); + if( !mitk::IOUtil::SaveSurface( surface, fileName.toStdString() ) ) + { + MITK_ERROR << "Temporary file " << fileName.toStdString() << " could not be created."; + return convert; + } + + QString command; + command.append( QString("import vtk\n") ); + command.append( QString("vtkStlReader = vtk.vtkSTLReader()\n") ); + command.append( QString("vtkStlReader.SetFileName(\"%1\")\n").arg( fileName ) ); + command.append( QString("%1 = vtkStlReader.GetOutput()").arg( fileName ) ); + MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); + this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); + + MITK_DEBUG("PythonService") << "Removing file " << fileName.toStdString(); + QFile file(fileName); + file.remove(); + convert = true; + return convert; +} + +bool mitk::PythonService::IsItkPythonWrappingAvailable() +{ + return true; //TODO +} + +bool mitk::PythonService::IsOpenCvPythonWrappingAvailable() +{ + return true; //TODO +} + +bool mitk::PythonService::IsVtkPythonWrappingAvailable() +{ + + return true; //TODO +} + diff --git a/Modules/Python/mitkPythonService.h b/Modules/Python/mitkPythonService.h index 48c52fd34c..687e31c163 100644 --- a/Modules/Python/mitkPythonService.h +++ b/Modules/Python/mitkPythonService.h @@ -1,53 +1,92 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef mitkPythonService_h #define mitkPythonService_h #include "mitkIPythonService.h" #include #include +#include "mitkSurface.h" namespace mitk { + /// + /// implementation of the IPythonService using ctkabstractpythonmanager + /// \see IPythonService class PythonService: public itk::LightObject, public mitk::IPythonService { public: + /// + /// instantiate python manager here PythonService(); - ~PythonService(); // leer in mitkIPythonService.cpp implementieren + /// + /// empty implementation... + ~PythonService(); + /// + /// \see IPythonService::Execute() QVariant Execute( const QString& pythonCommand, int commandType = SINGLE_LINE_COMMAND ); + /// + /// \see IPythonService::GetVariableStack() QList GetVariableStack() const; - + /// + /// \see IPythonService::AddPythonCommandObserver() void AddPythonCommandObserver( PythonCommandObserver* observer ); + /// + /// \see IPythonService::RemovePythonCommandObserver() void RemovePythonCommandObserver( PythonCommandObserver* observer ); + /// + /// \see IPythonService::NotifyObserver() void NotifyObserver( const QString& command ); - + /// + /// \see IPythonService::IsItkPythonWrappingAvailable() + bool IsItkPythonWrappingAvailable(); + /// + /// \see IPythonService::CopyToPythonAsItkImage() bool CopyToPythonAsItkImage( mitk::Image* image, const QString& varName ); + /// + /// \see IPythonService::CopyItkImageFromPython() mitk::Image::Pointer CopyItkImageFromPython( const QString& varName ); - + /// + /// \see IPythonService::IsOpenCvPythonWrappingAvailable() + bool IsOpenCvPythonWrappingAvailable(); + /// + /// \see IPythonService::CopyToPythonAsCvImage() bool CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ); + /// + /// \see IPythonService::CopyCvImageFromPython() mitk::Image::Pointer CopyCvImageFromPython( const QString& varName ); - + /// + /// \see IPythonService::IsVtkPythonWrappingAvailable() + bool IsVtkPythonWrappingAvailable(); + /// + /// \see IPythonService::CopyToPythonAsVtkPolyData() + bool CopyToPythonAsVtkPolyData( mitk::Surface* surface, const QString& varName ); + /// + /// \see IPythonService::CopyVtkPolyDataFromPython() + mitk::Surface::Pointer CopyVtkPolyDataFromPython( const QString& varName ); + /// + /// \return the ctk abstract python manager instance ctkAbstractPythonManager* GetPythonManager(); protected: QString GetTempImageName(const QString &ext) const; private: QList m_Observer; ctkAbstractPythonManager m_PythonManager; static const QString m_TmpImageName; }; } #endif