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

**URL:** <https://discourse.slicer.org/t/how-to-display-a-line-with-projection-in-2dslicer-by-python/17518>\
**Category:** Support\
**Created:** [May 8, 2021, 11:22am UTC](https://discourse.slicer.org/t/how-to-display-a-line-with-projection-in-2dslicer-by-python/17518 "2021-05-08T11:22:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [May 8, 2021, 11:22am UTC](https://discourse.slicer.org/t/how-to-display-a-line-with-projection-in-2dslicer-by-python/17518/1 "2021-05-08T11:22:26Z")

</div>

```auto
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?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/8/c8087794ae2d35ada211ec083c48cdbe8201bea1.jpeg)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/a/6a673aba214546279024829829c96488d5466bc8.jpeg)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 10, 2021, 4:55am UTC](https://discourse.slicer.org/t/how-to-display-a-line-with-projection-in-2dslicer-by-python/17518/2 "2021-05-10T04:55:52Z")

</div>

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

```python
modelDisplay.SetSliceDisplayModeToProjection()

```

See documentation of the display node [here](http://apidocs.slicer.org/master/classvtkMRMLModelDisplayNode.html).

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](https://github.com/lassoan/PedicleScrewSimulator) interesting:

[![](https://img.youtube.com/vi/XN3Tp8jaYdQ/maxresdefault.jpg "3D Slicer - Pedicle Screw Surgical Simulator") ](https://www.youtube.com/watch?v=XN3Tp8jaYdQ)

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [May 10, 2021, 2:02pm UTC](https://discourse.slicer.org/t/how-to-display-a-line-with-projection-in-2dslicer-by-python/17518/3 "2021-05-10T14:02:52Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/f/bf57de67e4a0775ac79ad94cf6d1272637a8f9b6.jpeg)  
谢谢老师!你太好了!!
