Running Cross-section analysis via CLI Slicer Python Console

Dear community,

I’m building a code, which relies on cross-section analysis. The output I’m interested in is the standard output as in GUI: table and plot of diameters. I’m struggling to achieve the same via CLI.


# Step 4: Cross-Sectional Analysis
def cross_sectional_analysis(centerline_model_node, segmentation_node):

    # Initialize the CrossSectionAnalysis logic
    logic = CrossSectionAnalysis.CrossSectionAnalysisLogic()

    # Set the inputs for the CrossSectionAnalysis logic
    logic.setInputCenterlineNode(centerline_model_node)
    logic.setLumenSurface(segmentation_node, 'Segment_1')
    
    # Run the cross-sectional analysis
    logic.run()

    # Create and set the output table node if it does not exist
    if not logic.outputTableNode:
        output_table_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode")
        logic.setOutputTableNode(output_table_node)
        print("Created and set new output table node.")

    # Create and set the output plot series node if it does not exist
    if not logic.outputPlotSeriesNode:
        output_plot_series_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotSeriesNode")
        logic.setOutputPlotSeriesNode(output_plot_series_node)
        print("Created and set new output plot series node.")

    # Determine the number of control points
    if centerline_model_node.IsTypeOf("vtkMRMLModelNode"):
        number_of_points = centerline_model_node.GetPolyData().GetNumberOfPoints()
    elif centerline_model_node.IsTypeOf("vtkMRMLMarkupsShapeNode"):
        trimmed_spline = vtk.vtkPolyData()
        if not centerline_model_node.GetTrimmedSplineWorld(trimmed_spline):
            number_of_points = centerline_model_node.GetSplineWorld().GetNumberOfPoints()
        else:
            number_of_points = trimmed_spline.GetNumberOfPoints()
    else:
        number_of_points = centerline_model_node.GetCurvePointsWorld().GetNumberOfPoints()

    print(f"Number of control points: {number_of_points}")

#GetArray?

#UpdateTable?

Thank you in advance!

Bohdan

  1. You must import the module before creating an instance of the logic class.
import CrossSectionAnalysis
logic = CrossSectionAnalysis.CrossSectionAnalysisLogic()
  1. Create the output table and plot nodes before calling logic.run()
  2. You may also call logic.getNumberOfPoints().

It worked! I didn’t see that!

As for Import - I have listed all imports on top of the whole project, so that I wasn’t copied over. Sorry that was misleading from my side.

And for some reason, if I use GetNumberOfPoints directly without check for type, it ends un in error, so I looked up the source code and used it directly in mine. Probably not elegant solution, but it works.

Thank you!