From dfcd0b84f01eb96e43c2180a35d626cc123a7231 Mon Sep 17 00:00:00 2001 From: Eduardo DAVILA Date: Fri, 14 Apr 2023 16:42:01 +0200 Subject: [PATCH] #3498 Export to Python code for 3DSlicer --- kernel/appli/bbs2cpp/CMakeLists.txt | 4 +- .../appli/bbs2cpp/{bbs2cpp.cxx => bbs2.cxx} | 64 +++++-- kernel/src/bbtkInterpreterPy.cxx | 170 ++++++++++++++++++ kernel/src/bbtkInterpreterPy.h | 99 ++++++++++ packages/std/src/bbstdConcatStrings.h | 2 +- packages/std/src/bbstdDiv.xml | 2 +- packages/vtk/src/bbvtkMarchingCubes.h | 2 +- packages/vtk/src/bbvtkMetaImageReader.h | 2 +- packages/vtk/src/bbvtkPolyDataToActor.h | 3 +- packages/wx/src/bbwxSlider.h | 2 +- 10 files changed, 326 insertions(+), 24 deletions(-) rename kernel/appli/bbs2cpp/{bbs2cpp.cxx => bbs2.cxx} (60%) create mode 100644 kernel/src/bbtkInterpreterPy.cxx create mode 100644 kernel/src/bbtkInterpreterPy.h diff --git a/kernel/appli/bbs2cpp/CMakeLists.txt b/kernel/appli/bbs2cpp/CMakeLists.txt index f778ba7..b919bd0 100644 --- a/kernel/appli/bbs2cpp/CMakeLists.txt +++ b/kernel/appli/bbs2cpp/CMakeLists.txt @@ -25,8 +25,8 @@ -SET(SOURCES bbs2cpp ) -SET(EXENAME bbs2cpp ) +SET(SOURCES bbs2 ) +SET(EXENAME bbs2 ) #IF(BBTK_USE_WXWIDGETS AND WIN32) # ADD_EXECUTABLE(${EXENAME} WIN32 ${SOURCES}) diff --git a/kernel/appli/bbs2cpp/bbs2cpp.cxx b/kernel/appli/bbs2cpp/bbs2.cxx similarity index 60% rename from kernel/appli/bbs2cpp/bbs2cpp.cxx rename to kernel/appli/bbs2cpp/bbs2.cxx index 501a461..c193d24 100644 --- a/kernel/appli/bbs2cpp/bbs2cpp.cxx +++ b/kernel/appli/bbs2cpp/bbs2.cxx @@ -44,29 +44,63 @@ =========================================================================*/ +#include + #include "bbtkInterpreter.h" +#include "bbtkInterpreterPy.h" #include "bbtkUtilities.h" int main(int argc, char* argv[]) { - if (argc<2) return 1; - - std::string file,path; - file = bbtk::Utilities::ExtractScriptName(argv[1],path); - file += ".h"; - bbtk::Interpreter::Pointer I = bbtk::Interpreter::New(file); - - I->SetThrow(true); - - try + if (argc<3) { - I->InterpretFile(argv[1]); - } - catch (bbtk::Exception e) - { - e.Print(); + printf("bbs2 \n"); + return 1; } + std::string file,path; + std::string extention( argv[2] ); + file = bbtk::Utilities::ExtractScriptName(argv[1],path); + file += std::string(".")+extention; + if ( extention.compare("cxx")==0) + { + printf("bbs -> cxx\n"); + bbtk::Interpreter::Pointer I = bbtk::Interpreter::New(file); + I->SetThrow(true); + try + { + I->InterpretFile(argv[1]); + } catch (bbtk::Exception e) { + e.Print(); + } // try + } // if cxx + + if ( extention.compare("py")==0) + { + printf("bbs -> py\n"); + bbtk::InterpreterPy::Pointer I = bbtk::InterpreterPy::New(); + I->SetThrow(true); + try + { + I->InterpretFile(argv[1]); + std::vector pythonBBTK= ((bbtk::InterpreterPy*)(I.get()))->pythonBBTK ; + FILE *ff=fopen(file.c_str(),"w+"); + fprintf(ff,"from bbtk.bbtkBlackBox import *\n" ); + fprintf(ff,"class appliTest02:\n" ); + fprintf(ff," def Run(self):\n" ); + fprintf(ff," mCB = ComplexBlackBox('appliTest02')\n"); + int i,size=pythonBBTK.size(); + for (i=0; ipythonBBTK.push_back(code); + } + //========================================================================= + + + //========================================================================= + /// Connects the output boxOutput to the input boxInput + void InterpreterPy::commandConnection (const std::string &boxfrom, + const std::string &output, + const std::string &boxto, + const std::string &input) // virtual + { + //ex: mCB.Connection( "Box10" , "Out", "Box11", "In") + std::string code("Connection('"+boxfrom+"','"+output+"','"+boxto+"','"+input+"')"); + this->pythonBBTK.push_back(code); + } + //========================================================================= + + //========================================================================= + void InterpreterPy::commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help) + { + } + //========================================================================= + + //========================================================================= + void InterpreterPy::commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help) + { + } + //========================================================================= + + + //========================================================================= + /// sets the input of the box with the value + void InterpreterPy::commandSet(const std::string &box,const std::string &input,const std::string &value) // virtual + { + //ex: mCB.Set("Box10","In2","/hola.mhd") + std::string code("Set('"+box+"','"+input+"','"+value+"')"); + this->pythonBBTK.push_back(code); + } + //========================================================================= + + + //========================================================================= + void InterpreterPy::commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename) // virtual + { + } + //========================================================================= + + + //========================================================================= + void InterpreterPy::commandEndDefine() // virtual + { + } + //========================================================================= + + + //========================================================================= + void InterpreterPy::commandExec(const std::string &word) // virtual + { + //Ex: mCB.AddToExecutableLst("Box13") + std::string code("AddToExecutableLst('"+word+"')"); + this->pythonBBTK.push_back(code); + } + //========================================================================= + + + //========================================================================= + void InterpreterPy::commandAuthor(const std::string &author) // virtual + { + } + //========================================================================= + + //========================================================================= + void InterpreterPy::commandCategory(const std::string &categorytype) // virtual + { + } + //========================================================================= + + //========================================================================= + void InterpreterPy::commandDescription(const std::string &description) // virtual + { + } + //========================================================================= + +} // EO namespace bbtk + +// EOF + diff --git a/kernel/src/bbtkInterpreterPy.h b/kernel/src/bbtkInterpreterPy.h new file mode 100644 index 0000000..a65ecf6 --- /dev/null +++ b/kernel/src/bbtkInterpreterPy.h @@ -0,0 +1,99 @@ +/* +# --------------------------------------------------------------------- +# +# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image +# pour la Santé) +# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton +# +# This software is governed by the CeCILL-B license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL-B +# license as circulated by CEA, CNRS and INRIA at the following URL +# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +# or in the file LICENSE.txt. +# +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. +# +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL-B license and that you accept its terms. +# ------------------------------------------------------------------------ +*/ + +/*========================================================================= +Program: bbtk +Module: $RCSfile$ +Language: C++ +Date: $Date$ +Version: $Revision$ +=========================================================================*/ + + +#ifndef __bbtkBBPInterpreter_h__ +#define __bbtkBBPInterpreter_h__ + +//Includes bbtk +#include "bbtkInterpreterVirtual.h" + +//Includes std +#include +#include + + +//#include "bbtkSystem.h" +//#include "bbtkComplexBlackBox.h" + +namespace bbtk +{ + class BBTK_EXPORT InterpreterPy : public InterpreterVirtual + { + public: + static Pointer New(); + InterpreterPy(); + ~InterpreterPy(); + + //Public methods + + virtual void commandNew( const std::string& boxType, const std::string& boxName); + + virtual void commandConnection (const std::string &boxfrom, + const std::string &output, + const std::string &boxto, + const std::string &input); + virtual void commandSet(const std::string &box,const std::string &input,const std::string &value); + + + virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename); + virtual void commandEndDefine(); + + virtual void commandExec(const std::string &word); + + virtual void commandAuthor(const std::string &author); + virtual void commandCategory(const std::string &categorytype); + virtual void commandDescription(const std::string &description); + + virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help); + virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help); + + std::vector pythonBBTK; + + private: + + //Private Attributes + + //Private Methods + + protected: + + //Protected Attributes + + //Protected methods + + }; +} +// namespace bbtk +#endif + diff --git a/packages/std/src/bbstdConcatStrings.h b/packages/std/src/bbstdConcatStrings.h index f22f2f2..625ac86 100644 --- a/packages/std/src/bbstdConcatStrings.h +++ b/packages/std/src/bbstdConcatStrings.h @@ -67,7 +67,7 @@ namespace bbstd BBTK_NAME("ConcatStrings"); BBTK_AUTHOR("jean-pierre.roux@creatis.insa-lyon.fr"); BBTK_CATEGORY("misc"); - BBTK_DESCRIPTION("String concatenation"); + BBTK_DESCRIPTION("String concatenation (C++,Python)"); BBTK_INPUT(ConcatStrings,In1, "String 1", std::string,""); BBTK_INPUT(ConcatStrings,In2, "String 2", std::string,""); BBTK_INPUT(ConcatStrings,In3, "String 3", std::string,""); diff --git a/packages/std/src/bbstdDiv.xml b/packages/std/src/bbstdDiv.xml index 09686eb..ab02f2c 100644 --- a/packages/std/src/bbstdDiv.xml +++ b/packages/std/src/bbstdDiv.xml @@ -3,7 +3,7 @@ laurent.guigues@creatis.insa-lyon.fr - Divides its inputs + Divides its inputs (C++,Python) math diff --git a/packages/vtk/src/bbvtkMarchingCubes.h b/packages/vtk/src/bbvtkMarchingCubes.h index 011834a..c94d76f 100644 --- a/packages/vtk/src/bbvtkMarchingCubes.h +++ b/packages/vtk/src/bbvtkMarchingCubes.h @@ -111,7 +111,7 @@ namespace bbvtk BBTK_BEGIN_DESCRIBE_BLACK_BOX(MarchingCubes,bbtk::AtomicBlackBox); BBTK_NAME("MarchingCubes"); BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr"); - BBTK_DESCRIPTION("Extracts an iso-surface of an image using the marching cubes algorithm (bbfication of vtkMarchingCubes)"); + BBTK_DESCRIPTION("Extracts an iso-surface of an image using the marching cubes algorithm (bbfication of vtkMarchingCubes) (C++,Python)"); BBTK_CATEGORY("image;mesh"); BBTK_INPUT(MarchingCubes,Active,"Active true/false (default true)",bool,""); diff --git a/packages/vtk/src/bbvtkMetaImageReader.h b/packages/vtk/src/bbvtkMetaImageReader.h index e4e4de4..f00867c 100644 --- a/packages/vtk/src/bbvtkMetaImageReader.h +++ b/packages/vtk/src/bbvtkMetaImageReader.h @@ -34,7 +34,7 @@ class bbvtk_EXPORT MetaImageReader BBTK_BEGIN_DESCRIBE_BLACK_BOX(MetaImageReader,bbtk::AtomicBlackBox); BBTK_NAME("MetaImageReader"); BBTK_AUTHOR("Info-Dev"); - BBTK_DESCRIPTION("No Description."); + BBTK_DESCRIPTION("No Description. (C++,Python)"); BBTK_CATEGORY("empty"); BBTK_INPUT(MetaImageReader,In,"File Name",std::string,""); diff --git a/packages/vtk/src/bbvtkPolyDataToActor.h b/packages/vtk/src/bbvtkPolyDataToActor.h index 583f0f7..a01dcdb 100644 --- a/packages/vtk/src/bbvtkPolyDataToActor.h +++ b/packages/vtk/src/bbvtkPolyDataToActor.h @@ -77,7 +77,6 @@ namespace bbvtk vtkPolyDataMapper *polydatamapper; vtkActor *vtkactor; - BBTK_BLACK_BOX_INTERFACE(PolyDataToActor,bbtk::AtomicBlackBox); BBTK_DECLARE_INPUT(Active,bool); @@ -104,7 +103,7 @@ namespace bbvtk BBTK_BEGIN_DESCRIBE_BLACK_BOX(PolyDataToActor,bbtk::AtomicBlackBox); BBTK_NAME("PolyDataToActor"); BBTK_AUTHOR("eduardo.davila at creatis.insa-lyon.fr"); - BBTK_DESCRIPTION("takes a vtkPolyData object to insert into a 3D scene (e.g. a Viewer3D)"); + BBTK_DESCRIPTION("takes a vtkPolyData object to insert into a 3D scene (e.g. a Viewer3D) (C++,Python)"); typedef std::vector vectorcolour; BBTK_CATEGORY("3D object creator"); BBTK_INPUT(PolyDataToActor,In,"Input image",vtkPolyData*,""); diff --git a/packages/wx/src/bbwxSlider.h b/packages/wx/src/bbwxSlider.h index d75d02d..82fdeea 100644 --- a/packages/wx/src/bbwxSlider.h +++ b/packages/wx/src/bbwxSlider.h @@ -99,7 +99,7 @@ namespace bbwx BBTK_NAME("Slider"); BBTK_AUTHOR("eduardo.davila@creatis.insa-lyon.fr"); // Already inserted for any WxBlackBox BBTK_CATEGORY("widget"); - BBTK_DESCRIPTION("Slider widget (wxSlider)"); + BBTK_DESCRIPTION("Slider widget (wxSlider) (C++,Python)"); BBTK_INPUT(Slider,In, "Initial position of the slider (default 0)", int, ""); BBTK_INPUT(Slider,Min, "Minimum value of the slider (default 0)", int, ""); BBTK_INPUT(Slider,Max, "Maximum value of the slider (default 500)", int, ""); -- 2.47.1