import os
import sys
import ifcopenshell
import ifcopenshell.geom
from OCC.Display.SimpleGui import init_display
from OCC.Core.Graphic3d import *
from OCC.Core.gp import gp_Vec
from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QPushButton,
QHBoxLayout,
QGroupBox,
QDialog,
QVBoxLayout,
)
from PyQt5.QtCore import Qt
from OCC.Display.backend import load_backend
load_backend("pyqt5")
import OCC.Display.qtDisplay as qtDisplay
class App(QDialog):
def __init__(self):
super().__init__()
self.title = "PyQt5 / pythonOCC / Ionut Bim Studio"
self.left = 50
self.top = 50
self.width = 2000 # Increased width for more space for the display
self.height = 1200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createVerticalLayout()
windowLayout = QHBoxLayout()
windowLayout.addWidget(self.verticalGroupBox)
windowLayout.addWidget(self.canvas)
self.setLayout(windowLayout)
self.show()
self.canvas.InitDriver()
self.display = self.canvas._display
def createVerticalLayout(self):
self.verticalGroupBox = QGroupBox("Controls")
self.verticalGroupBox.setFixedWidth(200)
self.verticalGroupBox.setStyleSheet("background-color: #202020;")
layout = QVBoxLayout()
load_button = QPushButton("Load ifc", self)
load_button.setStyleSheet(
"background-color: #404040; color: orange; border: 1px solid #404040; padding: 5px;"
)
load_button.clicked.connect(self.displayIFC)
layout.addWidget(load_button)
erase_button = QPushButton("Unload ifc", self)
erase_button.setStyleSheet(
"background-color: #404040; color: orange; border: 1px solid #404040; padding: 5px;"
)
erase_button.clicked.connect(self.eraseIFC)
layout.addWidget(erase_button)
self.verticalGroupBox.setLayout(layout)
full_screen_button = QPushButton("Full Screen", self)
full_screen_button.setStyleSheet(
"background-color: #404040; color: orange; border: 1px solid #404040; padding: 5px;"
)
full_screen_button.clicked.connect(self.toggleFullScreen)
layout.addWidget(full_screen_button)
# Create the canvas for the 3D display
self.canvas = qtDisplay.qtViewer3d(self)
def displayIFC(self):
# Deschide fișierul IFC
ifc_file = ifcopenshell.open("test.ifc")
# Configurarea setărilor pentru PythonOCC
settings = ifcopenshell.geom.settings()
settings.set(settings.USE_PYTHON_OPENCASCADE, True)
# Parcurge entitățile din fișierul IFC și afișează-le în display
for product in ifc_file.by_type("IfcProduct"):
if product.Representation is not None: # some IfcProducts don't have any 3d representation
try:
shape = ifcopenshell.geom.create_shape(settings, inst=product)
r, g, b, a = shape.styles[0] # the shape color
color = Quantity_Color(abs(r), abs(g), abs(b), Quantity_TOC_RGB)
self.display.DisplayShape(shape.geometry, color=color, transparency=0.15)
except RuntimeError:
print("Failed to process shape geometry")
# Afișează rezultatul și inițializează display-ul
self.display.FitAll()
def eraseIFC(self):
self.display.EraseAll()
def eventFilter(self, obj, event):
if event.type() == Qt.MouseButtonPress:
self.canvas.StartRotation(event.x(), event.y())
elif event.type() == Qt.MouseMove:
if event.buttons() == Qt.LeftButton:
self.canvas.Rotation(event.x(), event.y())
elif event.type() == Qt.KeyPress:
if event.key() == Qt.Key_Left:
self.display.View.Rotate(gp_Vec(-1, 0, 0))
elif event.key() == Qt.Key_Right:
self.display.View.Rotate(gp_Vec(1, 0, 0))
elif event.key() == Qt.Key_Up:
self.display.View.Rotate(gp_Vec(0, 1, 0))
elif event.key() == Qt.Key_Down:
self.display.View.Rotate(gp_Vec(0, -1, 0))
return super().eventFilter(obj, event)
def toggleFullScreen(self):
if self.isFullScreen():
self.showNormal()
else:
self.showFullScreen()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
if os.getenv("APPVEYOR") is None:
sys.exit(app.exec_())