Skip to content

Commit

Permalink
Implemented transparent handle
Browse files Browse the repository at this point in the history
The OCCT handle smart pointer API is a C++ specific detail
and should not be exposed to Python. Instead it should be
completely invisible.
I used the std::shared_ptr swig library as a basis for the
implementation.
To be compatible with most of the "old" code, GetHandle() and
GetObject() methods still exist but do nothing except from
a deprecation warning. In general, I tried to make sure
that the code is backwards compatible as possible.

Closes #539
  • Loading branch information
rainman110 committed Nov 16, 2018
1 parent bf47b0a commit 0ac6173
Show file tree
Hide file tree
Showing 652 changed files with 173,371 additions and 287,320 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ install(TARGETS _GEOMAlgo DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core )
# install Extend
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/Extend DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY} )

# install Utils
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/utils.py DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY})

# install deprecated modules
file(GLOB DEPRECATED_MODULES ${CMAKE_CURRENT_SOURCE_DIR}/src/SWIG_files/deprecated_modules/*.py)
install(FILES ${DEPRECATED_MODULES} DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY} )
81 changes: 27 additions & 54 deletions src/Display/OCCViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
BRepBuilderAPI_MakeFace)
from OCC.Core.TopAbs import (TopAbs_FACE, TopAbs_EDGE, TopAbs_VERTEX,
TopAbs_SHELL, TopAbs_SOLID)
from OCC.Core.Geom import Handle_Geom_Curve, Handle_Geom_Surface
from OCC.Core.Geom2d import Handle_Geom2d_Curve
from OCC.Core.Geom import Geom_Curve, Geom_Surface
from OCC.Core.Geom2d import Geom2d_Curve
from OCC.Core.Visualization import Display3d
from OCC.Core.V3d import (V3d_ZBUFFER, V3d_PHONG, V3d_Zpos, V3d_Zneg, V3d_Xpos,
V3d_Xneg, V3d_Ypos, V3d_Yneg, V3d_XposYnegZpos, V3d_TEX_ALL,
Expand Down Expand Up @@ -120,14 +120,10 @@ def __init__(self, window_handle):
self._window_handle = window_handle
self._inited = False
self._local_context_opened = False
self.Context_handle = None
self.Viewer_handle = None
self.View_handle = None
self.Context = None
self.Viewer = None
self.View = None
self.OverLayer = None
self.OverLayer_handle = None
self.selected_shape = None
self.default_drawer = None
self._struc_mgr = None
Expand All @@ -145,7 +141,7 @@ def register_overlay_item(self, overlay_item):
def GetOverLayer(self):
""" returns an handle to the current overlayer
"""
return self.OverLayer_handle
return self.OverLayer

def register_select_callback(self, callback):
""" Adds a callback that will be called each time a shape s selected
Expand All @@ -164,7 +160,7 @@ def unregister_callback(self, callback):
self._select_callbacks.remove(callback)

def MoveTo(self, X, Y):
self.Context.MoveTo(X, Y, self.View_handle)
self.Context.MoveTo(X, Y, self.View)

def FitAll(self):
self.View.ZFitAll()
Expand All @@ -178,17 +174,14 @@ def Create(self, create_default_lights=True, draw_face_boundaries=True, phong_sh
self.Init(self._window_handle)
self._is_offscreen = False

self.Context_handle = self.GetContext()
self.Viewer_handle = self.GetViewer()
self.View_handle = self.GetView()
self.Context = self.Context_handle.GetObject()
self.Viewer = self.Viewer_handle.GetObject()
self.Context = self.GetContext()
self.Viewer = self.GetViewer()
self.View = self.GetView()
if create_default_lights:
self.Viewer.SetDefaultLights()
self.Viewer.SetLightOn()
self.View = self.View_handle.GetObject()
self.camera = self.View.Camera().GetObject()
self.default_drawer = self.Context.DefaultDrawer().GetObject()
self.camera = self.View.Camera()
self.default_drawer = self.Context.DefaultDrawer()

# draw black contour edges, like other famous CAD packages
if draw_face_boundaries:
Expand All @@ -206,17 +199,16 @@ def Create(self, create_default_lights=True, draw_face_boundaries=True, phong_sh
# self.Context.SelectionColor(Quantity_NOC_ORANGE)

# necessary for text rendering
self._struc_mgr = self.Context.MainPrsMgr().GetObject().StructureManager()
self._struc_mgr = self.Context.MainPrsMgr().StructureManager()

# overlayer
self.OverLayer_handle = self.Viewer.Viewer().GetObject().OverLayer()
if self.OverLayer_handle.IsNull():
aMgr = V3d_LayerMgr(self.View_handle)
self.OverLayer_handle = aMgr.Overlay()
self.View.SetLayerMgr(aMgr.GetHandle())
self.OverLayer = self.OverLayer_handle.GetObject()
self.OverLayer = self.Viewer.Viewer().OverLayer()
if self.OverLayer is None:
aMgr = V3d_LayerMgr(self.View)
self.OverLayer = aMgr.Overlay()
self.View.SetLayerMgr(aMgr)
print("Layer manager created")
height, width = self.View.Window().GetObject().Size()
height, width = self.View.Window().Size()
print("Layer dimensions: %i, %i" % (height, width))
self.OverLayer.SetViewport(height, width)

Expand Down Expand Up @@ -289,7 +281,7 @@ def EnableTextureEnv(self, name_of_texture=Graphic3d_NOT_ENV_CLOUDS):
Graphic3d_NOT_ENV_UNKNOWN
"""
texture_env = Graphic3d_TextureEnv(name_of_texture)
self.View.SetTextureEnv(texture_env.GetHandle())
self.View.SetTextureEnv(texture_env)
self.View.SetSurfaceDetail(V3d_TEX_ENVIRONMENT)
self.View.Redraw()

Expand Down Expand Up @@ -386,7 +378,7 @@ def DisplayVector(self, vec, pnt, update=False):
pnt_start = gp_Pnt(start.X(), start.Y(), start.Z())

Prs3d_Arrow.Draw(
aPresentation.GetHandle(),
aPresentation,
pnt_start,
gp_Dir(vec),
math.radians(20),
Expand Down Expand Up @@ -414,8 +406,8 @@ def DisplayMessage(self, point, text_to_write, height=None, message_color=None,
text_aspect.SetHeight(height)
if isinstance(point, gp_Pnt2d):
point = gp_Pnt(point.X(), point.Y(), 0)
Prs3d_Text.Draw(aPresentation.GetHandle(),
text_aspect.GetHandle(),
Prs3d_Text.Draw(aPresentation,
text_aspect,
to_string(text_to_write),
point)
aPresentation.Display()
Expand All @@ -438,38 +430,19 @@ def DisplayShape(self, shapes, material=None, texture=None, color=None, transpar
vertex = BRepBuilderAPI_MakeVertex(gp_Pnt(shapes.X(), shapes.Y(), 0))
shapes = [vertex.Shape()]
SOLO = True
# if a Geom_Curve is passed
elif callable(getattr(shapes, "GetHandle", None)):
handle = shapes.GetHandle()
if issubclass(handle.__class__, Handle_Geom_Curve):
edge = BRepBuilderAPI_MakeEdge(handle)
shapes = [edge.Shape()]
SOLO = True
elif issubclass(handle.__class__, Handle_Geom2d_Curve):
edge2d = BRepBuilderAPI_MakeEdge2d(handle)
shapes = [edge2d.Shape()]
SOLO = True
elif issubclass(handle.__class__, Handle_Geom_Surface):
bounds = True
toldegen = 1e-6
face = BRepBuilderAPI_MakeFace()
face.Init(handle, bounds, toldegen)
face.Build()
shapes = [face.Shape()]
SOLO = True
elif isinstance(shapes, Handle_Geom_Surface):
elif isinstance(shapes, Geom_Surface):
bounds = True
toldegen = 1e-6
face = BRepBuilderAPI_MakeFace()
face.Init(shapes, bounds, toldegen)
face.Build()
shapes = [face.Shape()]
SOLO = True
elif isinstance(shapes, Handle_Geom_Curve):
elif isinstance(shapes, Geom_Curve):
edge = BRepBuilderAPI_MakeEdge(shapes)
shapes = [edge.Shape()]
SOLO = True
elif isinstance(shapes, Handle_Geom2d_Curve):
elif isinstance(shapes, Geom2d_Curve):
edge2d = BRepBuilderAPI_MakeEdge2d(shapes)
shapes = [edge2d.Shape()]
SOLO = True
Expand Down Expand Up @@ -499,7 +472,7 @@ def DisplayShape(self, shapes, material=None, texture=None, color=None, transpar
# to this AIS_Shape instance?
shape_to_display = AIS_Shape(shape)

ais_shapes.append(shape_to_display.GetHandle())
ais_shapes.append(shape_to_display)

if not SOLO:
# computing graphic properties is expensive
Expand Down Expand Up @@ -527,12 +500,12 @@ def DisplayShape(self, shapes, material=None, texture=None, color=None, transpar
shape_to_display.SetTransparency(transparency)
if update:
# only update when explicitely told to do so
self.Context.Display(shape_to_display.GetHandle(), False)
self.Context.Display(shape_to_display, False)
# especially this call takes up a lot of time...
self.FitAll()
self.Repaint()
else:
self.Context.Display(shape_to_display.GetHandle(), False)
self.Context.Display(shape_to_display, False)

if SOLO:
return ais_shapes[0]
Expand Down Expand Up @@ -611,7 +584,7 @@ def GetSelectedShape(self):
return self.selected_shape

def SelectArea(self, Xmin, Ymin, Xmax, Ymax):
self.Context.Select(Xmin, Ymin, Xmax, Ymax, self.View_handle)
self.Context.Select(Xmin, Ymin, Xmax, Ymax, self.View)
self.Context.InitSelected()
# reinit the selected_shapes list
self.selected_shapes = []
Expand Down
Loading

0 comments on commit 0ac6173

Please sign in to comment.