Extension settings in C++ code

Operating system: window 10
Slicer version: 5.2.1

Hi, slicer users

I know how to extend python code in 3D slicer. but this time I need to add C++ code to the 3d slicer…
I know that there is cmake in 3d slicer, but I am not familiar with it, so I ask a question.

  1. How to add c++ code in 3d slicer
  2. How to run custom c++ code in 3d slicer ( for testing )

Are there any resources I can refer to?

The 3D Slicer documentation includes details about developing C++ Loadable modules as part of an extension. Extensions — 3D Slicer documentation

Hi, @jamesobutler

I asked the question too roughly.

I know about python extension method for 3d slicer.
In the above method, I had to combine the custom code I implemented into one file.

convert file below

data_preprocessing.py 

def preprocessing():
...

def show():                                          
...

def set_domain_knowledge():
 ...

data_training.py

def train() : 
...

def val():
...

to

extension_code.py
def preprocessing():
...
def show():                                          
...
def set_domain_knowledge():
 ...
def train() : 
...
def val():
...

In order to extend a module composed of multiple files in python and c++ extension to 3d slicer, do I always have to combine them into one file?
( It does not mean that python files and cpp files are mixed, but means the case of the extension of the python version and the extension of the cpp version. )

image

Can I paste only the main part of the custom code I developed at the bottom of custom_extension.py in the above file, and add the necessary additional modules in the form of files to the custom_extension folder?

I do not quite understand what you are asking.

A Slicer extension can contain multiple Slicer modules of various types (python scripted loadable module, c++ loadable module, or CLI module). The ExtensionWizard module can help set up the necessary template of files needed for each Slicer module type.

You will observe that when you create the template of files for a c++ loadable module that there are multiple files created. You should follow this organization. A scripted loadable module can be a single .py file or it can also have a supporting library of .py files. However you will need to be careful with imports and modify the module’s CMakeLists.txt to make sure you include those supporting library files. That type of code organization for a scripted loadable module is not shown in the template of files created by the ExtensionWizard.

@jamesobutler

To clarify the question, let’s assume that we are going through an extension with a python version.

Through the extension wizard, you can create the following structure through the process of creating an extension and adding a module.

image

Now I need to add my module to custom_extenion.py .

If I didn’t misunderstand your previous reply, I understood that I had to keep the framework for class (ScriptedLoadableModule/ScriptedLoadableModuleWidget/ScriptedLoadableModuleLogic) of custom_extension.py and paste my code below it.

Therefore, I pasted my code in the empty space above(red rectangle) and declared a shortcut key function to perform the extension of the python version. To achieve the above method, I went through the process of modifying my module, which was previously composed of several files, into a single file.

I was curious about how to extend without merging into one file. Isn’t the cmakelist.txt you mentioned used when building in code using c++?

Are you saying that when adding a module composed of multiple files to 3d slicer, regardless of the version(python, c++ ), all cmakelist.txt should be modified?

There is a CMakeLists.txt for each module whether it is python based or C++ based. It is all necesssary for building the extension and creating the “.zip” file in the case of windows which is the final package of the extension. To create this “.zip” file, even python based Slicer scripted loadable modules have to be built against a local Slicer build.

Going back to your original post, are you creating a C++ Slicer Loadable module in your extension?
Have you been able to build the Slicer Loadable module template created by the ExtensionWizard?

1 Like

I haven’t tried extending the C++ code yet.

In c++, the build proceeds by reading cmakelist through cmake.

I understood what you said if it is correct to proceed with extensions for modules composed of multiple files in the same way as above in 3d slicer.

Yes, you can have many modules in an extension; and you may implement a Python scripted module in one or many files.

I would advise to implement your Python scripted module in a single file if the module is not too large or complex. If the file grows bigger than 30-40kB then it may make sense to split it up to more files. If you already have a Python package/library then you don’t need to merge them and move everything into the Slicer module file, you can keep it in a subfolder as @jamesobutler explained above.

1 Like