Skip to content

Commit 4aa17e7

Browse files
authored
Merge pull request #65 from tpaviot/review/fixes
Review/fixes
2 parents 1480b14 + b9638cf commit 4aa17e7

17 files changed

+166
-79
lines changed

assets/models/2CylinderEngine.glb

1.75 MB
Binary file not shown.

examples/core_display_activate_manipulator.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import os
44
import sys
5-
from OCC.Core.BRepPrimAPI import (
6-
BRepPrimAPI_MakeBox,
7-
BRepPrimAPI_MakeSphere)
5+
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere
86
from OCC.Core.gp import gp_Pnt
97
from OCC.Core.AIS import AIS_Manipulator
108
from OCC.Extend.LayerManager import Layer
119

1210
from OCC.Display.backend import load_backend
11+
1312
load_backend("qt-pyqt5")
1413
import OCC.Display.qtDisplay as qtDisplay
1514

@@ -23,6 +22,7 @@
2322
QVBoxLayout,
2423
)
2524

25+
2626
class App(QDialog):
2727
def __init__(self):
2828
super().__init__()
@@ -86,7 +86,10 @@ def activate_manipulator(self):
8686
selected = self.display.GetSelectedShape()
8787
if selected is not None:
8888
# retrieve the AIS_Shape from the selected TopoDS_Shape
89-
self.ais_element_manip, self.index_element_manip = self.layer.get_aisshape_from_topodsshape(selected)
89+
(
90+
self.ais_element_manip,
91+
self.index_element_manip,
92+
) = self.layer.get_aisshape_from_topodsshape(selected)
9093
self.shape_element_manip = selected
9194
# Create and attach a Manipulator to AIS_Shape
9295
self.manip = AIS_Manipulator()
@@ -100,8 +103,10 @@ def activate_manipulator(self):
100103
# Get the transformations done with the manipulator
101104
trsf = self.canvas.get_trsf_from_manip()
102105
# Apply the transformation to the TopoDS_Shape and replace it in the layer
103-
self.layer.update_trsf_shape(self.shape_element_manip, self.index_element_manip, trsf)
104-
self.manip.Detach()
106+
self.layer.update_trsf_shape(
107+
self.shape_element_manip, self.index_element_manip, trsf
108+
)
109+
self.manip.Detach()
105110

106111

107112
if __name__ == "__main__":

examples/core_display_manipulator.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,6 @@
1515
##You should have received a copy of the GNU Lesser General Public License
1616
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
1717

18-
"""
19-
The very first pythonocc example. This used to be the script
20-
used to check the following points:
21-
22-
pythonocc installation is correct, i.e. pythonocc modules are found
23-
and properly imported
24-
25-
a GUI manager is installed. Whether it is wxpython or pyqt/pyside, it's necessary
26-
to display a 3d window
27-
28-
the rendering window can be initialized and set up, that is to say the
29-
graphic driver and OpenGl works correctly.
30-
31-
If this example runs on your machine, that means you're ready to explore the wide
32-
pythonocc world and run all the other examples.
33-
"""
34-
3518
from OCC.Display.SimpleGui import init_display
3619
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
3720
from OCC.Core.AIS import AIS_Manipulator

examples/core_export_gltf_single_shape.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
1717

1818
from OCC.Core.TDocStd import TDocStd_Document
19-
from OCC.Core.TCollection import TCollection_ExtendedString, TCollection_AsciiString
19+
from OCC.Core.TCollection import TCollection_AsciiString
2020
from OCC.Core.XCAFDoc import (
2121
XCAFDoc_DocumentTool_ShapeTool,
2222
XCAFDoc_DocumentTool_LayerTool,
@@ -34,7 +34,7 @@
3434
shp = BRepPrimAPI_MakeSphere(60.0).Shape()
3535

3636
# create a document
37-
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
37+
doc = TDocStd_Document("pythonocc-doc")
3838
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
3939
layer_tool = XCAFDoc_DocumentTool_LayerTool(doc.Main())
4040

@@ -60,7 +60,7 @@
6060
# Binary export
6161
#
6262
binary = True
63-
binary_rwgltf_writer = RWGltf_CafWriter(TCollection_AsciiString("box.glb"), binary)
63+
binary_rwgltf_writer = RWGltf_CafWriter("box.glb", binary)
6464
binary_rwgltf_writer.SetTransformationFormat(a_format)
6565
binary_rwgltf_writer.SetForcedUVExport(force_uv_export)
6666
pr = Message_ProgressRange() # this is required
@@ -70,7 +70,7 @@
7070
# Ascii export
7171
#
7272
binary = False
73-
ascii_rwgltf_writer = RWGltf_CafWriter(TCollection_AsciiString("box.gla"), binary)
73+
ascii_rwgltf_writer = RWGltf_CafWriter("box.gla", binary)
7474
ascii_rwgltf_writer.SetTransformationFormat(a_format)
7575
ascii_rwgltf_writer.SetForcedUVExport(force_uv_export)
7676
pr = Message_ProgressRange() # this is required

examples/core_export_ply_single_shape.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
shp = BRepPrimAPI_MakeSphere(60.0).Shape()
3535

3636
# create a document
37-
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
37+
doc = TDocStd_Document("pythonocc-doc")
3838
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
3939
layer_tool = XCAFDoc_DocumentTool_LayerTool(doc.Main())
4040

@@ -53,6 +53,6 @@
5353
TCollection_AsciiString("Authors"), TCollection_AsciiString("pythonocc")
5454
)
5555

56-
rwply_writer = RWPly_CafWriter(TCollection_AsciiString("sphere.ply"))
56+
rwply_writer = RWPly_CafWriter("sphere.ply")
5757
pr = Message_ProgressRange()
5858
rwply_writer.Perform(doc, a_file_info, pr)

examples/core_geometry_face_recognition_from_stepfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ def exit(event=None):
138138
shp = read_step_file(
139139
os.path.join("..", "assets", "models", "face_recognition_sample_part.stp")
140140
)
141-
display.DisplayShape(shp, update=True)
141+
display.DisplayShape(shp, update=True)
142142
display.SetSelectionModeFace() # switch to Face selection mode
143143
add_menu("recognition")
144144
add_function_to_menu("recognition", recognize_batch)
145-
start_display()
145+
start_display()

examples/core_geometry_oriented_bounding_box.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,64 @@
1414
##
1515
##You should have received a copy of the GNU Lesser General Public License
1616
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
17-
1817
import random
1918

2019
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
2120
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_Dir, gp_XYZ
22-
from OCC.Core.BRepBndLib import brepbndlib_AddOBB
21+
from OCC.Core.BRepBndLib import brepbndlib
2322
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeVertex
2423
from OCC.Core.Bnd import Bnd_OBB
24+
from OCC.Core.BRepTools import breptools_Read
25+
from OCC.Core.TopoDS import TopoDS_Shape
26+
from OCC.Core.BRep import BRep_Builder
2527

2628
from OCC.Display.SimpleGui import init_display
2729

2830
display, start_display, add_menu, add_function_to_menu = init_display()
2931

3032

31-
def ConvertBndToShape(theBox):
32-
aBaryCenter = theBox.Center()
33-
aXDir = theBox.XDirection()
34-
aYDir = theBox.YDirection()
35-
aZDir = theBox.ZDirection()
36-
aHalfX = theBox.XHSize()
37-
aHalfY = theBox.YHSize()
38-
aHalfZ = theBox.ZHSize()
33+
def convert_bnd_to_shape(the_box):
34+
"""Converts a bounding box to a box shape."""
35+
barycenter = the_box.Center()
36+
x_dir = the_box.XDirection()
37+
y_dir = the_box.YDirection()
38+
z_dir = the_box.ZDirection()
39+
half_x = the_box.XHSize()
40+
half_y = the_box.YHSize()
41+
half_z = the_box.ZHSize()
3942

40-
ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
41-
ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
42-
az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())
43-
p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z())
44-
anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir))
45-
anAxes.SetLocation(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
46-
aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY, 2.0 * aHalfZ).Shape()
47-
return aBox
43+
x_vec = gp_XYZ(x_dir.X(), x_dir.Y(), x_dir.Z())
44+
y_vec = gp_XYZ(y_dir.X(), y_dir.Y(), y_dir.Z())
45+
z_vec = gp_XYZ(z_dir.X(), z_dir.Y(), z_dir.Z())
46+
point = gp_Pnt(barycenter.X(), barycenter.Y(), barycenter.Z())
47+
axes = gp_Ax2(point, gp_Dir(z_dir), gp_Dir(x_dir))
48+
axes.SetLocation(
49+
gp_Pnt(point.XYZ() - x_vec * half_x - y_vec * half_y - z_vec * half_z)
50+
)
51+
box = BRepPrimAPI_MakeBox(axes, 2.0 * half_x, 2.0 * half_y, 2.0 * half_z).Shape()
52+
return box
4853

4954

5055
# compute the oriented bounding box of a point cloud
5156
obb1 = Bnd_OBB()
52-
n = 10
53-
for _ in range(n):
57+
num_points = 10
58+
for _ in range(num_points):
5459
x = random.uniform(100, 500)
5560
y = random.uniform(100, 500)
5661
z = random.uniform(100, 500)
5762
p = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, z)).Shape()
5863
display.DisplayShape(p)
59-
brepbndlib_AddOBB(p, obb1)
60-
obb_shape1 = ConvertBndToShape(obb1)
64+
brepbndlib.AddOBB(p, obb1)
65+
obb_shape1 = convert_bnd_to_shape(obb1)
6166
display.DisplayShape(obb_shape1, transparency=0.5)
6267

6368
# then loads a brep file and computes the optimal bounding box
64-
from OCC.Core.BRepTools import breptools_Read
65-
from OCC.Core.TopoDS import TopoDS_Shape
66-
from OCC.Core.BRep import BRep_Builder
67-
6869
cylinder_head = TopoDS_Shape()
6970
builder = BRep_Builder()
7071
breptools_Read(cylinder_head, "../assets/models/cylinder_head.brep", builder)
7172
obb2 = Bnd_OBB()
72-
brepbndlib_AddOBB(cylinder_head, obb2, True, True, True)
73-
obb_shape2 = ConvertBndToShape(obb2)
73+
brepbndlib.AddOBB(cylinder_head, obb2, True, True, True)
74+
obb_shape2 = convert_bnd_to_shape(obb2)
7475
display.DisplayShape(cylinder_head)
7576
display.DisplayShape(obb_shape2, transparency=0.5, update=True)
7677

examples/core_load_gltf_ocaf.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
##Copyright 2023 Thomas Paviot (tpaviot@gmail.com)
2+
##
3+
##This file is part of pythonOCC.
4+
##
5+
##pythonOCC is free software: you can redistribute it and/or modify
6+
##it under the terms of the GNU Lesser General Public License as published by
7+
##the Free Software Foundation, either version 3 of the License, or
8+
##(at your option) any later version.
9+
##
10+
##pythonOCC is distributed in the hope that it will be useful,
11+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
##GNU Lesser General Public License for more details.
14+
##
15+
##You should have received a copy of the GNU Lesser General Public License
16+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
17+
18+
from OCC.Core.TDocStd import TDocStd_Document
19+
from OCC.Core.Message import Message_ProgressRange
20+
from OCC.Core.RWGltf import RWGltf_CafReader
21+
from OCC.Core.IFSelect import IFSelect_RetDone
22+
23+
from OCC.Display.SimpleGui import init_display
24+
25+
filename = "../assets/models/2CylinderEngine.glb"
26+
27+
# create an handle to a document
28+
doc = TDocStd_Document("pythonocc-doc")
29+
30+
gltf_reader = RWGltf_CafReader()
31+
32+
# gltf_reader.SetSystemLengthUnit(aScaleFactorM)
33+
# gltf_reader.SetSystemCoordinateSystem(RWMesh_CoordinateSystem_Zup)
34+
gltf_reader.SetDocument(doc)
35+
# gltf_reader.SetParallel(True)
36+
# gltf_reader.SetDoublePrecision(True)
37+
# gltf_reader.SetToSkipLateDataLoading(True)
38+
# gltf_reader.SetToKeepLateData(True)
39+
# gltf_reader.SetToPrintDebugMessages(True)
40+
# gltf_reader.SetLoadAllScenes(True)
41+
status = gltf_reader.Perform(filename, Message_ProgressRange())
42+
43+
assert status == IFSelect_RetDone
44+
45+
shp = gltf_reader.SingleShape()
46+
47+
#
48+
# Display
49+
#
50+
display, start_display, add_menu, add_function_to_menu = init_display()
51+
display.DisplayShape(shp, update=True)
52+
start_display()

examples/core_load_step_ap203_ocaf.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
##You should have received a copy of the GNU Lesser General Public License
1616
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
1717

18-
from __future__ import print_function
19-
20-
21-
from OCC.Core.TCollection import TCollection_ExtendedString
22-
2318
from OCC.Core.TDocStd import TDocStd_Document
2419
from OCC.Core.XCAFDoc import (
2520
XCAFDoc_DocumentTool_ShapeTool,
@@ -37,7 +32,7 @@
3732
_shapes = []
3833

3934
# create an handle to a document
40-
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
35+
doc = TDocStd_Document("pythonocc-doc")
4136

4237
# Get root assembly
4338
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
##Copyright 2023 Thomas Paviot (tpaviot@gmail.com)
2+
##
3+
##This file is part of pythonOCC.
4+
##
5+
##pythonOCC is free software: you can redistribute it and/or modify
6+
##it under the terms of the GNU Lesser General Public License as published by
7+
##the Free Software Foundation, either version 3 of the License, or
8+
##(at your option) any later version.
9+
##
10+
##pythonOCC is distributed in the hope that it will be useful,
11+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
##GNU Lesser General Public License for more details.
14+
##
15+
##You should have received a copy of the GNU Lesser General Public License
16+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
17+
18+
from OCC.Core.TDocStd import TDocStd_Document
19+
from OCC.Core.XCAFDoc import (
20+
XCAFDoc_DocumentTool_MaterialTool,
21+
)
22+
from OCC.Core.STEPCAFControl import STEPCAFControl_Reader
23+
from OCC.Core.IFSelect import IFSelect_RetDone
24+
from OCC.Core.TDF import TDF_LabelSequence
25+
26+
filename = "../assets/models/eight_cyl.stp"
27+
28+
# create an handle to a document
29+
doc = TDocStd_Document("pythonocc-doc")
30+
31+
# Get root assembly
32+
mat_tool = XCAFDoc_DocumentTool_MaterialTool(doc.Main())
33+
34+
step_reader = STEPCAFControl_Reader()
35+
36+
status = step_reader.ReadFile(filename)
37+
if status == IFSelect_RetDone:
38+
step_reader.Transfer(doc)
39+
40+
material_labels = TDF_LabelSequence()
41+
42+
mat_tool.GetMaterialLabels(material_labels)
43+
44+
# materials
45+
for i in range(1, material_labels.Length() + 1):
46+
(
47+
ok,
48+
material_name,
49+
material_description,
50+
material_density,
51+
material_densname,
52+
material_densvaltype,
53+
) = mat_tool.GetMaterial(material_labels.Value(i))
54+
55+
print(f"Material name: {material_name}")
56+
print(f"Material description: {material_description}")
57+
print(f"Material density: {material_density}")
58+
print(f"Material densname: {material_densname}")
59+
print(f"Material_densvaltype: {material_densvaltype}\n")

0 commit comments

Comments
 (0)