Standalone viewer under OCP

398 views
Skip to first unread message

nopria

unread,
Aug 2, 2020, 5:13:26 AM8/2/20
to CadQuery

Using module OCC I could display objects programmatically without cq-editor, for example:

from OCC.Display.SimpleGui import init_display
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox

display, start_display, add_menu, add_function_to_menu = init_display()
my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()

display.DisplayShape(my_box, update=True)
start_display()

Is it possible to do the same with OCP (current master branch) ?

Adam Urbanczyk

unread,
Aug 2, 2020, 5:42:00 AM8/2/20
to CadQuery
You could use this widget from CQ-editor - it only depends on PyQT and OCP:


but you'd need know how to use the OCP api (CQ-editor source could be an example). BTW: there is no extra python code in OCP by design - it is intended to "just" wrap what is available in OCCT.

nopria

unread,
Aug 2, 2020, 6:43:19 AM8/2/20
to CadQuery
This seems a bit too hard for me, I tried without success

import cadquery as cq
import occt_widget

myObject = cq.Workplane("XY").box(10,5,1)
init_display = occt_widget.OCCTWidget()
# returns
# QWidget: Must construct a QApplication before a QWidget

Alternatively, is there a way to display cq objects created by standalone code  through cq-editor?

Adam Urbanczyk

unread,
Aug 2, 2020, 10:35:25 AM8/2/20
to CadQuery
Here you have an example (taken from https://github.com/CadQuery/CQ-editor/blob/fdc97e6acce8358f52a3d377a5e67875ec993047/cq_editor/widgets/viewer.py#L341 , you'll need to import all relevant things first)

import sys

from OCP.BRepPrimAPI import BRepPrimAPI_MakeBox


app = QApplication(sys.argv)

viewer = OCCViewer()

viewer.show_line()


dlg = QDialog()

dlg.setFixedHeight(400)

dlg.setFixedWidth(600)


layout(dlg,(viewer,),dlg)

dlg.show()


box = BRepPrimAPI_MakeBox(20,20,30)

box_ais = AIS_ColoredShape(box.Shape())

viewer.display(box_ais)

sys.exit(app.exec_())

nopria

unread,
Aug 23, 2020, 12:55:53 PM8/23/20
to CadQuery
Since the relevant things are too many, I tried to import all of them through `from cq_editor.widgets import viewer as cqv` (see the following code)

import sys
from OCP.BRepPrimAPI import BRepPrimAPI_MakeBox
from cq_editor.widgets import viewer as cqv

app = cqv.QApplication(sys.argv)
viewer = cqv.OCCViewer()
viewer.show_line()
...

but at the last line above I get the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\NUC-W10\Miniconda3\envs\cqgui-master\lib\site-packages\cq_editor\widgets\viewer.py", line 292, in show_line
    self._display_ais(line)
  File "C:\Users\NUC-W10\Miniconda3\envs\cqgui-master\lib\site-packages\cq_editor\widgets\viewer.py", line 302, in _display_ais
    self._get_context().Display(ais)
TypeError: Display(): incompatible function arguments. The following argument types are supported:
    1. (self: OCP.AIS.AIS_InteractiveContext, theIObj: OCP.AIS.AIS_InteractiveObject, theToUpdateViewer: bool) -> None
    2. (self: OCP.AIS.AIS_InteractiveContext, theIObj: OCP.AIS.AIS_InteractiveObject, theDispMode: int, theSelectionMode: int, theToUpdateViewer: bool, theDispStatus: OCP.AIS.AIS_DisplayStatus = AIS_DisplayStatus.AIS_DS_None) -> None
    3. (self: OCP.AIS.AIS_InteractiveContext, theIObj: OCP.AIS.AIS_InteractiveObject, theDispMode: int, theSelectionMode: int, theToUpdateViewer: bool, theToAllowDecomposition: bool, theDispStatus: OCP.AIS.AIS_DisplayStatus = AIS_DisplayStatus.AIS_DS_None) -> None

Invoked with: <OCP.AIS.AIS_InteractiveContext object at 0x00000235D5D1D6B0>, <OCP.AIS.AIS_Line object at 0x00000235DAC8C6F0>

Adam Urbanczyk

unread,
Aug 23, 2020, 2:53:33 PM8/23/20
to CadQuery
That method seems to be broken - remove it from the code (I just did it in the repo).

Op zondag 23 augustus 2020 om 18:55:53 UTC+2 schreef nopria:

nopria

unread,
Aug 24, 2020, 4:40:24 AM8/24/20
to CadQuery
Thank you very much, the code is now working (see below), but how to display objects created with Cadquery? Method AIS_ColoredShape wants an OCP.TopoDS.TopoDS_Shape object, how to convert a Cadquery object into it?

I report below a working example of code able to display an OCP object directly from Python (from an environment including cq-editor, current master branch):

import sys
from OCP.BRepPrimAPI import BRepPrimAPI_MakeBox
from cq_editor.widgets import viewer as cqv

app = cqv.QApplication(sys.argv)
viewer = cqv.OCCViewer()

dlg = cqv.QDialog()
dlg.setFixedHeight(400)
dlg.setFixedWidth(600)

cqv.layout(dlg,(viewer,),dlg)
dlg.show()

box = BRepPrimAPI_MakeBox(20,20,30)
box_ais = cqv.AIS_ColoredShape(box.Shape())

viewer.display(box_ais)
sys.exit(app.exec_())

Jeremy Wright

unread,
Aug 24, 2020, 6:24:04 AM8/24/20
to CadQuery
This works for me.

    import sys
    from OCP.BRepPrimAPI import BRepPrimAPI_MakeBox
    from cq_editor.widgets import viewer as cqv
    import cadquery as cq


    app = cqv.QApplication(sys.argv)
    viewer = cqv.OCCViewer()

    dlg = cqv.QDialog()
    dlg.setFixedHeight(400)
    dlg.setFixedWidth(600)

    cqv.layout(dlg,(viewer,),dlg)
    dlg.show()

    #box = BRepPrimAPI_MakeBox(20,20,30)
    box = cq.Workplane("XY").box(20,20,30).val().wrapped
    box_ais = cqv.AIS_ColoredShape(box)

    viewer.display(box_ais)
    sys.exit(app.exec_())

Adam Urbanczyk

unread,
Aug 24, 2020, 7:24:43 AM8/24/20
to CadQuery
You can also use this helper:


Op maandag 24 augustus 2020 om 12:24:04 UTC+2 schreef Jeremy Wright:
Reply all
Reply to author
Forward
0 new messages