|
| 1 | +##Author github user @Tanneguydv, 2023 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from OCC.Core.BRepPrimAPI import ( |
| 6 | + BRepPrimAPI_MakeBox, |
| 7 | + BRepPrimAPI_MakeSphere) |
| 8 | +from OCC.Core.gp import gp_Pnt |
| 9 | +from OCC.Core.AIS import AIS_Manipulator |
| 10 | +from OCC.Extend.LayerManager import Layer |
| 11 | + |
| 12 | +from OCC.Display.backend import load_backend |
| 13 | +load_backend("qt-pyqt5") |
| 14 | +import OCC.Display.qtDisplay as qtDisplay |
| 15 | + |
| 16 | +from PyQt5.QtWidgets import ( |
| 17 | + QApplication, |
| 18 | + QWidget, |
| 19 | + QPushButton, |
| 20 | + QHBoxLayout, |
| 21 | + QGroupBox, |
| 22 | + QDialog, |
| 23 | + QVBoxLayout, |
| 24 | +) |
| 25 | + |
| 26 | +class App(QDialog): |
| 27 | + def __init__(self): |
| 28 | + super().__init__() |
| 29 | + self.title = "PyQt5 / pythonOCC / Manipulator" |
| 30 | + self.left = 300 |
| 31 | + self.top = 300 |
| 32 | + self.width = 800 |
| 33 | + self.height = 300 |
| 34 | + self.initUI() |
| 35 | + |
| 36 | + def initUI(self): |
| 37 | + self.setWindowTitle(self.title) |
| 38 | + self.setGeometry(self.left, self.top, self.width, self.height) |
| 39 | + self.createLayout() |
| 40 | + |
| 41 | + windowLayout = QVBoxLayout() |
| 42 | + windowLayout.addWidget(self.horizontalGroupBox) |
| 43 | + self.setLayout(windowLayout) |
| 44 | + self.show() |
| 45 | + |
| 46 | + def createLayout(self): |
| 47 | + self.horizontalGroupBox = QGroupBox("Display PythonOCC") |
| 48 | + layout_h = QHBoxLayout() |
| 49 | + layout_v = QVBoxLayout() |
| 50 | + |
| 51 | + self.activate_manip_button = QPushButton("Activate Manipulator", self) |
| 52 | + self.activate_manip_button.setCheckable(True) |
| 53 | + self.activate_manip_button.clicked.connect(self.activate_manipulator) |
| 54 | + layout_v.addWidget(self.activate_manip_button) |
| 55 | + |
| 56 | + self.show_layer_button = QPushButton("Show Layer", self) |
| 57 | + self.show_layer_button.setCheckable(True) |
| 58 | + self.show_layer_button.setChecked(True) |
| 59 | + self.show_layer_button.clicked.connect(self.show_layer) |
| 60 | + layout_v.addWidget(self.show_layer_button) |
| 61 | + layout_h.addLayout(layout_v) |
| 62 | + |
| 63 | + self.canvas = qtDisplay.qtViewer3dManip(self) |
| 64 | + layout_h.addWidget(self.canvas) |
| 65 | + self.horizontalGroupBox.setLayout(layout_h) |
| 66 | + |
| 67 | + self.canvas.InitDriver() |
| 68 | + self.display = self.canvas._display |
| 69 | + |
| 70 | + box = BRepPrimAPI_MakeBox(15.0, 15.0, 15.0).Shape() |
| 71 | + self.layer = Layer(self.display, box) |
| 72 | + sphere = BRepPrimAPI_MakeSphere(gp_Pnt(25, 25, 25), 5).Shape() |
| 73 | + self.layer.add_shape(sphere) |
| 74 | + self.show_layer() |
| 75 | + self.layer.merge() |
| 76 | + |
| 77 | + def show_layer(self): |
| 78 | + if self.show_layer_button.isChecked(): |
| 79 | + self.layer.show() |
| 80 | + self.display.FitAll() |
| 81 | + else: |
| 82 | + self.layer.hide() |
| 83 | + self.display.FitAll() |
| 84 | + |
| 85 | + def activate_manipulator(self): |
| 86 | + if self.activate_manip_button.isChecked(): |
| 87 | + selected = self.display.GetSelectedShape() |
| 88 | + if selected is not None: |
| 89 | + # retrieve the AIS_Shape from the selected TopoDS_Shape |
| 90 | + self.ais_element_manip, self.index_element_manip = self.layer.get_aisshape_from_topodsshape(selected) |
| 91 | + self.shape_element_manip = selected |
| 92 | + # Create and attach a Manipulator to AIS_Shape |
| 93 | + self.manip = AIS_Manipulator() |
| 94 | + self.manip.Attach(self.ais_element_manip) |
| 95 | + # Pass the Manipulator to the Qtviewer |
| 96 | + self.canvas.set_manipulator(self.manip) |
| 97 | + self.display.View.Redraw() |
| 98 | + else: |
| 99 | + self.activate_manip_button.setChecked(False) |
| 100 | + else: |
| 101 | + # Get the transformations done with the manipulator |
| 102 | + trsf = self.canvas.get_trsf_from_manip() |
| 103 | + # Apply the transformation to the TopoDS_Shape and replace it in the layer |
| 104 | + self.layer.update_trsf_shape(self.shape_element_manip, self.index_element_manip, trsf) |
| 105 | + self.manip.Detach() |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + app = QApplication(sys.argv) |
| 110 | + ex = App() |
| 111 | + if os.getenv("APPVEYOR") is None: |
| 112 | + sys.exit(app.exec_()) |
0 commit comments