# Running Cross-section analysis via CLI Slicer Python Console

**URL:** <https://discourse.slicer.org/t/running-cross-section-analysis-via-cli-slicer-python-console/38269>\
**Category:** Support\
**Created:** [September 8, 2024, 12:20am UTC](https://discourse.slicer.org/t/running-cross-section-analysis-via-cli-slicer-python-console/38269 "2024-09-08T00:20:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![klchtsk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/klchtsk/32/77849_2.png) [@klchtsk](https://discourse.slicer.org/u/klchtsk)\
**Post date:** [September 8, 2024, 12:20am UTC](https://discourse.slicer.org/t/running-cross-section-analysis-via-cli-slicer-python-console/38269/1 "2024-09-08T00:20:48Z")

</div>

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.

```auto

# 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

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [September 8, 2024, 9:09am UTC](https://discourse.slicer.org/t/running-cross-section-analysis-via-cli-slicer-python-console/38269/2 "2024-09-08T09:09:08Z")

</div>

1. You must import the module before creating an instance of the logic class.

```auto
import CrossSectionAnalysis
logic = CrossSectionAnalysis.CrossSectionAnalysisLogic()

```

1. Create the output _table_ and _plot_ nodes **before** calling _logic.run()_
2. You may also call _logic.getNumberOfPoints()_.

---

<div class="post-metadata">

**Author:** ![klchtsk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/klchtsk/32/77849_2.png) [@klchtsk](https://discourse.slicer.org/u/klchtsk)\
**Post date:** [September 8, 2024, 9:57am UTC](https://discourse.slicer.org/t/running-cross-section-analysis-via-cli-slicer-python-console/38269/3 "2024-09-08T09:57:07Z")

</div>

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!
