Documentation/Nightly/Developers/SlicerExecutionModel/Programmatic Invocation

From Slicer Wiki
Revision as of 18:00, 15 May 2008 by Marianna (talk | contribs) (1 revision)
Jump to: navigation, search
Home < Documentation < Nightly < Developers < SlicerExecutionModel < Programmatic Invocation


A large amount of functionality is available in command line modules for processing images and models. Some applications would like to use several modules together in a workflow or as a background processing task.

Implementation Example

This example shows calling the Model Maker (from the Editor). This example is in Tcl, but the same API can be used from C++ or Python. The idea is to query for the Model Maker module (and bail out if it is not loaded). Then to create a MRMLNode with the parameters you want for the model maker. These would be the same parameters you would set interactively. The model maker logic and gui classes are then told about the node and told to start processing. The processing then goes on in the background.

  # create a model using the command line module
  # based on the current editor parameters

  # get the image data for the label layer
  set sliceLogic [lindex [vtkSlicerSliceLogic ListInstances] 0]
  set layerLogic [$sliceLogic GetLabelLayer]
  set volumeNode [$layerLogic GetVolumeNode]
  if { $volumeNode == "" } {
    errorDialog "Cannot make model - no volume node for $layerLogic in $sliceLogic."

  # find the Model Maker
  # - call Enter to be sure GUI has been built
  # - find the GUI - for non-tcl use, try vtkSlicerApplication::GetModuleGUIByName
  set modelMaker ""
  foreach gui [vtkCommandLineModuleGUI ListInstances] {
    if { [$gui GetGUIName] == "Model Maker" } {
      set modelMaker $gui

  if { $modelMaker == "" } {
    errorDialog "Cannot make model: no Model Maker Module found."

  $modelMaker Enter

  # set up the model maker node
  set moduleNode [$::slicer3::MRMLScene CreateNodeByClass "vtkMRMLCommandLineModuleNode"]
  $::slicer3::MRMLScene AddNode $moduleNode
  $moduleNode SetModuleDescription "Model Maker"

  $moduleNode SetParameterAsString "Name" "Quick Model"
  $moduleNode SetParameterAsString "FilterType" "Sinc"
  $moduleNode SetParameterAsBool "GenerateAll" "0"
  $moduleNode SetParameterAsString "Labels" [EditorGetPaintLabel]
  $moduleNode SetParameterAsBool "JointSmooth" 1
  $moduleNode SetParameterAsBool "SplitNormals" 1
  $moduleNode SetParameterAsBool "PointNormals" 1
  $moduleNode SetParameterAsBool "SkipUnNamed" 1
  $moduleNode SetParameterAsInt "Start" -1
  $moduleNode SetParameterAsInt "End" -1
  if { [[$o(smooth) GetWidget] GetSelectedState] } {
    $moduleNode SetParameterAsDouble "Decimate" 0.25
    $moduleNode SetParameterAsDouble "Smooth" 10
  } else {
    $moduleNode SetParameterAsDouble "Decimate" 0
    $moduleNode SetParameterAsDouble "Smooth" 0

  $moduleNode SetParameterAsString "InputVolume" [$volumeNode GetID]

  # output 
  # - make a new hierarchy node if needed
  set outHierarchy [[$::slicer3::MRMLScene GetNodesByClassByName "vtkMRMLModelHierarchyNode" "Editor Models"] GetItemAsObject 0]
  if { $outHierarchy == "" } {
    set outHierarchy [$::slicer3::MRMLScene CreateNodeByClass "vtkMRMLModelHierarchyNode"]
    $outHierarchy SetScene $::slicer3::MRMLScene
    $outHierarchy SetName "Editor Models"
    $::slicer3::MRMLScene AddNode $outHierarchy

  $moduleNode SetParameterAsString "ModelSceneFile" [$outHierarchy GetID]

  # run the task (in the background)
  # - use the GUI to provide progress feedback
  # - use the GUI's Logic to invoke the task
  # - model will show up when the processing is finished
  $modelMaker SetCommandLineModuleNode $moduleNode
  [$modelMaker GetLogic] SetCommandLineModuleNode $moduleNode
  [$modelMaker GetLogic] Apply $moduleNode

  $this statusText "Model Making Started..."

  # clean up our references
  $moduleNode Delete
  $outHierarchy Delete
  $modelMaker Enter