How to display a line with projection in 2Dslicer by python?

def p2pline(P0, P1, width=1,modelName="Line",color = "blue"):
    points = vtk.vtkPoints()
    points.SetNumberOfPoints(2)
    points.SetPoint(0,   P0[0], P0[1], P0[2])
    points.SetPoint(1,   P1[0], P1[1], P1[2])
    line = vtk.vtkLineSource()
    line.SetPoints(points)
    lineNode = slicer.modules.models.logic().AddModel(line.GetOutputPort())
    lineNode.SetName(modelName)
    modelDisplay = lineNode.GetDisplayNode()
    modelDisplay.SetColor(myColor(color)) # yellow
    modelDisplay.SetLineWidth(width)
    modelDisplay.SetOpacity(1)
    modelDisplay.SetVisibility2D(1)

以上的代码想用2点画一条直线,可不知道为啥在2D视图里看不到,我发现用projection可以看到,我该怎么实现呢?

The above code wants to draw a straight line with 2 points, but I don’t know why I can’t see it in 2D view, but I found that I can see it with projection, how can I achieve it?

You can set slice display mode to projection by this call:

modelDisplay.SetSliceDisplayModeToProjection()

See documentation of the display node here.

To display pedicle screw trajectory with label, interactively editable line endpoints, and many other display options, etc. you can use markups line instead of model.

You may also find the Pedicle Screw Simulator extension interesting:

1 Like


谢谢老师!你太好了!!