Skip to content

Commit

Permalink
Merge pull request #65 from tpaviot/review/fixes
Browse files Browse the repository at this point in the history
Review/fixes
  • Loading branch information
tpaviot authored May 12, 2023
2 parents 1480b14 + b9638cf commit 4aa17e7
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 79 deletions.
Binary file added assets/models/2CylinderEngine.glb
Binary file not shown.
17 changes: 11 additions & 6 deletions examples/core_display_activate_manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import os
import sys
from OCC.Core.BRepPrimAPI import (
BRepPrimAPI_MakeBox,
BRepPrimAPI_MakeSphere)
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere
from OCC.Core.gp import gp_Pnt
from OCC.Core.AIS import AIS_Manipulator
from OCC.Extend.LayerManager import Layer

from OCC.Display.backend import load_backend

load_backend("qt-pyqt5")
import OCC.Display.qtDisplay as qtDisplay

Expand All @@ -23,6 +22,7 @@
QVBoxLayout,
)


class App(QDialog):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -86,7 +86,10 @@ def activate_manipulator(self):
selected = self.display.GetSelectedShape()
if selected is not None:
# retrieve the AIS_Shape from the selected TopoDS_Shape
self.ais_element_manip, self.index_element_manip = self.layer.get_aisshape_from_topodsshape(selected)
(
self.ais_element_manip,
self.index_element_manip,
) = self.layer.get_aisshape_from_topodsshape(selected)
self.shape_element_manip = selected
# Create and attach a Manipulator to AIS_Shape
self.manip = AIS_Manipulator()
Expand All @@ -100,8 +103,10 @@ def activate_manipulator(self):
# Get the transformations done with the manipulator
trsf = self.canvas.get_trsf_from_manip()
# Apply the transformation to the TopoDS_Shape and replace it in the layer
self.layer.update_trsf_shape(self.shape_element_manip, self.index_element_manip, trsf)
self.manip.Detach()
self.layer.update_trsf_shape(
self.shape_element_manip, self.index_element_manip, trsf
)
self.manip.Detach()


if __name__ == "__main__":
Expand Down
17 changes: 0 additions & 17 deletions examples/core_display_manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.

"""
The very first pythonocc example. This used to be the script
used to check the following points:
pythonocc installation is correct, i.e. pythonocc modules are found
and properly imported
a GUI manager is installed. Whether it is wxpython or pyqt/pyside, it's necessary
to display a 3d window
the rendering window can be initialized and set up, that is to say the
graphic driver and OpenGl works correctly.
If this example runs on your machine, that means you're ready to explore the wide
pythonocc world and run all the other examples.
"""

from OCC.Display.SimpleGui import init_display
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.AIS import AIS_Manipulator
Expand Down
8 changes: 4 additions & 4 deletions examples/core_export_gltf_single_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.

from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.TCollection import TCollection_ExtendedString, TCollection_AsciiString
from OCC.Core.TCollection import TCollection_AsciiString
from OCC.Core.XCAFDoc import (
XCAFDoc_DocumentTool_ShapeTool,
XCAFDoc_DocumentTool_LayerTool,
Expand All @@ -34,7 +34,7 @@
shp = BRepPrimAPI_MakeSphere(60.0).Shape()

# create a document
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
doc = TDocStd_Document("pythonocc-doc")
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
layer_tool = XCAFDoc_DocumentTool_LayerTool(doc.Main())

Expand All @@ -60,7 +60,7 @@
# Binary export
#
binary = True
binary_rwgltf_writer = RWGltf_CafWriter(TCollection_AsciiString("box.glb"), binary)
binary_rwgltf_writer = RWGltf_CafWriter("box.glb", binary)
binary_rwgltf_writer.SetTransformationFormat(a_format)
binary_rwgltf_writer.SetForcedUVExport(force_uv_export)
pr = Message_ProgressRange() # this is required
Expand All @@ -70,7 +70,7 @@
# Ascii export
#
binary = False
ascii_rwgltf_writer = RWGltf_CafWriter(TCollection_AsciiString("box.gla"), binary)
ascii_rwgltf_writer = RWGltf_CafWriter("box.gla", binary)
ascii_rwgltf_writer.SetTransformationFormat(a_format)
ascii_rwgltf_writer.SetForcedUVExport(force_uv_export)
pr = Message_ProgressRange() # this is required
Expand Down
4 changes: 2 additions & 2 deletions examples/core_export_ply_single_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
shp = BRepPrimAPI_MakeSphere(60.0).Shape()

# create a document
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
doc = TDocStd_Document("pythonocc-doc")
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
layer_tool = XCAFDoc_DocumentTool_LayerTool(doc.Main())

Expand All @@ -53,6 +53,6 @@
TCollection_AsciiString("Authors"), TCollection_AsciiString("pythonocc")
)

rwply_writer = RWPly_CafWriter(TCollection_AsciiString("sphere.ply"))
rwply_writer = RWPly_CafWriter("sphere.ply")
pr = Message_ProgressRange()
rwply_writer.Perform(doc, a_file_info, pr)
4 changes: 2 additions & 2 deletions examples/core_geometry_face_recognition_from_stepfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def exit(event=None):
shp = read_step_file(
os.path.join("..", "assets", "models", "face_recognition_sample_part.stp")
)
display.DisplayShape(shp, update=True)
display.DisplayShape(shp, update=True)
display.SetSelectionModeFace() # switch to Face selection mode
add_menu("recognition")
add_function_to_menu("recognition", recognize_batch)
start_display()
start_display()
57 changes: 29 additions & 28 deletions examples/core_geometry_oriented_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,64 @@
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.

import random

from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_Dir, gp_XYZ
from OCC.Core.BRepBndLib import brepbndlib_AddOBB
from OCC.Core.BRepBndLib import brepbndlib
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeVertex
from OCC.Core.Bnd import Bnd_OBB
from OCC.Core.BRepTools import breptools_Read
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.BRep import BRep_Builder

from OCC.Display.SimpleGui import init_display

display, start_display, add_menu, add_function_to_menu = init_display()


def ConvertBndToShape(theBox):
aBaryCenter = theBox.Center()
aXDir = theBox.XDirection()
aYDir = theBox.YDirection()
aZDir = theBox.ZDirection()
aHalfX = theBox.XHSize()
aHalfY = theBox.YHSize()
aHalfZ = theBox.ZHSize()
def convert_bnd_to_shape(the_box):
"""Converts a bounding box to a box shape."""
barycenter = the_box.Center()
x_dir = the_box.XDirection()
y_dir = the_box.YDirection()
z_dir = the_box.ZDirection()
half_x = the_box.XHSize()
half_y = the_box.YHSize()
half_z = the_box.ZHSize()

ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())
p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z())
anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir))
anAxes.SetLocation(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY, 2.0 * aHalfZ).Shape()
return aBox
x_vec = gp_XYZ(x_dir.X(), x_dir.Y(), x_dir.Z())
y_vec = gp_XYZ(y_dir.X(), y_dir.Y(), y_dir.Z())
z_vec = gp_XYZ(z_dir.X(), z_dir.Y(), z_dir.Z())
point = gp_Pnt(barycenter.X(), barycenter.Y(), barycenter.Z())
axes = gp_Ax2(point, gp_Dir(z_dir), gp_Dir(x_dir))
axes.SetLocation(
gp_Pnt(point.XYZ() - x_vec * half_x - y_vec * half_y - z_vec * half_z)
)
box = BRepPrimAPI_MakeBox(axes, 2.0 * half_x, 2.0 * half_y, 2.0 * half_z).Shape()
return box


# compute the oriented bounding box of a point cloud
obb1 = Bnd_OBB()
n = 10
for _ in range(n):
num_points = 10
for _ in range(num_points):
x = random.uniform(100, 500)
y = random.uniform(100, 500)
z = random.uniform(100, 500)
p = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, z)).Shape()
display.DisplayShape(p)
brepbndlib_AddOBB(p, obb1)
obb_shape1 = ConvertBndToShape(obb1)
brepbndlib.AddOBB(p, obb1)
obb_shape1 = convert_bnd_to_shape(obb1)
display.DisplayShape(obb_shape1, transparency=0.5)

# then loads a brep file and computes the optimal bounding box
from OCC.Core.BRepTools import breptools_Read
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.BRep import BRep_Builder

cylinder_head = TopoDS_Shape()
builder = BRep_Builder()
breptools_Read(cylinder_head, "../assets/models/cylinder_head.brep", builder)
obb2 = Bnd_OBB()
brepbndlib_AddOBB(cylinder_head, obb2, True, True, True)
obb_shape2 = ConvertBndToShape(obb2)
brepbndlib.AddOBB(cylinder_head, obb2, True, True, True)
obb_shape2 = convert_bnd_to_shape(obb2)
display.DisplayShape(cylinder_head)
display.DisplayShape(obb_shape2, transparency=0.5, update=True)

Expand Down
52 changes: 52 additions & 0 deletions examples/core_load_gltf_ocaf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
##Copyright 2023 Thomas Paviot ([email protected])
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.

from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.Message import Message_ProgressRange
from OCC.Core.RWGltf import RWGltf_CafReader
from OCC.Core.IFSelect import IFSelect_RetDone

from OCC.Display.SimpleGui import init_display

filename = "../assets/models/2CylinderEngine.glb"

# create an handle to a document
doc = TDocStd_Document("pythonocc-doc")

gltf_reader = RWGltf_CafReader()

# gltf_reader.SetSystemLengthUnit(aScaleFactorM)
# gltf_reader.SetSystemCoordinateSystem(RWMesh_CoordinateSystem_Zup)
gltf_reader.SetDocument(doc)
# gltf_reader.SetParallel(True)
# gltf_reader.SetDoublePrecision(True)
# gltf_reader.SetToSkipLateDataLoading(True)
# gltf_reader.SetToKeepLateData(True)
# gltf_reader.SetToPrintDebugMessages(True)
# gltf_reader.SetLoadAllScenes(True)
status = gltf_reader.Perform(filename, Message_ProgressRange())

assert status == IFSelect_RetDone

shp = gltf_reader.SingleShape()

#
# Display
#
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(shp, update=True)
start_display()
7 changes: 1 addition & 6 deletions examples/core_load_step_ap203_ocaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function


from OCC.Core.TCollection import TCollection_ExtendedString

from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.XCAFDoc import (
XCAFDoc_DocumentTool_ShapeTool,
Expand All @@ -37,7 +32,7 @@
_shapes = []

# create an handle to a document
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
doc = TDocStd_Document("pythonocc-doc")

# Get root assembly
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
Expand Down
59 changes: 59 additions & 0 deletions examples/core_load_step_ap214_with_materials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
##Copyright 2023 Thomas Paviot ([email protected])
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.

from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.XCAFDoc import (
XCAFDoc_DocumentTool_MaterialTool,
)
from OCC.Core.STEPCAFControl import STEPCAFControl_Reader
from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.TDF import TDF_LabelSequence

filename = "../assets/models/eight_cyl.stp"

# create an handle to a document
doc = TDocStd_Document("pythonocc-doc")

# Get root assembly
mat_tool = XCAFDoc_DocumentTool_MaterialTool(doc.Main())

step_reader = STEPCAFControl_Reader()

status = step_reader.ReadFile(filename)
if status == IFSelect_RetDone:
step_reader.Transfer(doc)

material_labels = TDF_LabelSequence()

mat_tool.GetMaterialLabels(material_labels)

# materials
for i in range(1, material_labels.Length() + 1):
(
ok,
material_name,
material_description,
material_density,
material_densname,
material_densvaltype,
) = mat_tool.GetMaterial(material_labels.Value(i))

print(f"Material name: {material_name}")
print(f"Material description: {material_description}")
print(f"Material density: {material_density}")
print(f"Material densname: {material_densname}")
print(f"Material_densvaltype: {material_densvaltype}\n")
2 changes: 1 addition & 1 deletion examples/core_meshDS_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def getMesh(X=100, Y=100):
x = np.linspace(-5, 5, X)
y = np.linspace(-5, 5, Y)
xx, yy = np.meshgrid(x, y, sparse=False)
z = np.sin(xx ** 2 + yy ** 2) / (xx ** 2 + yy ** 2)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
xyz = np.column_stack((xx.flatten(), yy.flatten(), z.flatten()))
tri = Delaunay(xyz[:, :2])
return xyz, tri.simplices
Expand Down
2 changes: 1 addition & 1 deletion examples/core_meshDS_numpy_face_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def getMesh(X=100, Y=100):
x = np.linspace(-5, 5, X)
y = np.linspace(-5, 5, Y)
xx, yy = np.meshgrid(x, y, sparse=False)
z = np.sin(xx ** 2 + yy ** 2) / (xx ** 2 + yy ** 2)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
xyz = np.column_stack((xx.flatten(), yy.flatten(), z.flatten()))
tri = Delaunay(xyz[:, :2])
return xyz, tri.simplices
Expand Down
2 changes: 1 addition & 1 deletion examples/core_meshDS_numpy_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def getMesh(X=100, Y=100):
x = np.linspace(-5, 5, X)
y = np.linspace(-5, 5, Y)
xx, yy = np.meshgrid(x, y, sparse=False)
z = np.sin(xx ** 2 + yy ** 2) / (xx ** 2 + yy ** 2)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
xyz = np.column_stack((xx.flatten(), yy.flatten(), z.flatten()))
tri = Delaunay(xyz[:, :2])
return xyz, tri.simplices
Expand Down
Loading

0 comments on commit 4aa17e7

Please sign in to comment.