diff --git a/src/addons/DataExchange/STEP.py b/src/addons/DataExchange/STEP.py index 8f41cd60e..136e2a9f6 100644 --- a/src/addons/DataExchange/STEP.py +++ b/src/addons/DataExchange/STEP.py @@ -137,7 +137,7 @@ def set_tolerance(self, tolerance=0.0001): def add_shape(self, aShape): # First check the shape if aShape.IsNull(): - raise Assertion("STEPExporter Error: the shape is NULL") + raise AssertionError("STEPExporter Error: the shape is NULL") else: self._shapes.append(aShape) @@ -145,8 +145,8 @@ def write_file(self): # workaround for an OCC bug: temporarily changing the locale in order to # avoid issues when exporting, see: # http://tracker.dev.opencascade.org/view.php?id=22898 - loc = locale.getlocale() - locale.setlocale(locale.LC_ALL, 'C') + # loc = locale.getlocale() + # locale.setlocale(locale.LC_ALL, 'C') for shp in self._shapes: status = self.stepWriter.Transfer(shp, STEPControl_AsIs ) if status == IFSelect_RetDone: @@ -154,7 +154,7 @@ def write_file(self): else: return False # restoring the old locale - locale.setlocale(locale.LC_ALL, loc) + # locale.setlocale(locale.LC_ALL, loc) if self.verbose: self.stepWriter.PrintStatsTransfer() diff --git a/src/addons/DataExchange/utils.py b/src/addons/DataExchange/utils.py index b5e4e7551..9560f7a89 100644 --- a/src/addons/DataExchange/utils.py +++ b/src/addons/DataExchange/utils.py @@ -47,7 +47,7 @@ def shape_to_file(shape, pth, filename, format='iges'): def file_to_shape(pth): '''get a Shape from an .iges or .step file''' assert os.path.isfile(pth), '%s is not a valid file' % (pth) - ext = os.path.splitext(pth)[1] + ext = os.path.splitext(pth)[1].lower() print 'ext', ext assert ext in ['.iges', '.igs', '.stp', '.step', '.brep', '.stl'], '%s is not an readable format' % ( ext ) @@ -71,7 +71,7 @@ def file_to_shape(pth): stl_reader = StlAPI.StlAPI_Reader() stl_reader.Read(shape,pth) return shape - + reader.ReadFile(pth) n_translated = reader.TransferRoots() shape = reader.OneShape() diff --git a/src/addons/Display/OCCViewer.py b/src/addons/Display/OCCViewer.py index 70a50b09d..ff2dd5241 100644 --- a/src/addons/Display/OCCViewer.py +++ b/src/addons/Display/OCCViewer.py @@ -23,6 +23,7 @@ import itertools from OCC.AIS import AIS_MultipleConnectedInteractive +from OCC.MeshVS import MeshVS_Mesh, Handle_MeshVS_Mesh from OCC.TopoDS import * from OCC.Utils.Common import to_string, color from OCC.Utils.Construct import gp_Dir @@ -49,6 +50,9 @@ def set_CSF_GraphicShr(): modes = itertools.cycle([TopAbs.TopAbs_FACE, TopAbs.TopAbs_EDGE, TopAbs.TopAbs_VERTEX, TopAbs.TopAbs_SHELL, TopAbs.TopAbs_SOLID, ]) +def get_color_name(color): + return color.Name(color.Red(), color.Green(), color.Blue()) + class BaseDriver(object): """ The base driver class @@ -77,7 +81,8 @@ def SetWindow(self,window_handle): self._window_handle = window_handle def Create(self, create_default_lights = True): - if sys.platform!='win32': + # applies merely for X11 + if sys.platform!='win32' and sys.platform != "darwin": try: os.environ['DISPLAY'] except KeyError: @@ -179,10 +184,12 @@ def SetBackgroundImage(self, filename, Stretch = True): else: self.View.SetBackgroundImage(filename, OCC.Aspect.Aspect_FM_NONE, True ) - def DisplayVector(self, vec, pnt, update=False): + def DisplayVector(self, vec, pnt, update=False, veccolor=None): if self._inited: aPresentation = Prs3d.Prs3d_Presentation(self._struc_mgr) arrow = Prs3d_Arrow() + if veccolor is not None: + aPresentation.Color(get_color_name(veccolor)) arrow.Draw( aPresentation.GetHandle(), (pnt.as_vec() + vec).as_pnt(), @@ -206,7 +213,7 @@ def DisplayMessage(self,point,text_to_write, message_color=None, update=False): text_aspect = Prs3d.Prs3d_TextAspect() if message_color is not None: - text_aspect.SetColor(color(*message_color)) + aPresentation.Color(get_color_name(message_color)) text = Prs3d.Prs3d_Text() text.Draw(aPresentation.GetHandle(), @@ -247,6 +254,10 @@ def DisplayShape(self, shapes, material=None, texture=None, color=None, transpar elif material: shape_to_display = OCC.AIS.AIS_Shape(shape) shape_to_display.SetMaterial(material) + if isinstance(shape, Handle_MeshVS_Mesh): + shape_to_display = shape + if isinstance(shape, MeshVS_Mesh): + shape_to_display = shape.GetHandle() else: # TODO: can we use .Set to attach all TopoDS_Shapes to this AIS_Shape instance? shape_to_display = OCC.AIS.AIS_Shape(shape) @@ -308,17 +319,25 @@ def DisplayColoredShape(self, shapes, color='YELLOW', update=False, ): 'WHITE':OCC.Quantity.Quantity_NOC_WHITE, 'BLACK':OCC.Quantity.Quantity_NOC_BLACK, 'ORANGE':OCC.Quantity.Quantity_NOC_ORANGE, } + clr = Quantity_Color( dict_color[color] ) elif isinstance(color, Quantity_Color): clr = color else: - raise ValueError('color should either be a string ( "BLUE" ) or a Quantity_Color(0.1, 0.8, 0.1) got %s' % color) + msg = 'color should either be a string ( "BLUE" ) or a Quantity_Color(0.1, 0.8, 0.1) got %s' % color + raise ValueError(msg) - return self.DisplayShape(shapes, color=clr, update=update) + return self.DisplayShape(shapes, color=clr, update=update) def DisplayTriedron(self): - self.View.TriedronDisplay(OCC.Aspect.Aspect_TOTP_RIGHT_LOWER, OCC.Quantity.Quantity_NOC_BLACK, 0.08, OCC.V3d.V3d_WIREFRAME) + R,G,B = OCC.Quantity.Quantity_NOC_RED,OCC.Quantity.Quantity_NOC_GREEN,OCC.Quantity.Quantity_NOC_BLUE1 + size_ratio = 0.8 + axis_diameter = 0.05 + n_facettes = 18 + scale = 0.08 + self.View.ZBufferTriedronSetup(R,G,B ,size_ratio,axis_diameter,n_facettes) + self.View.TriedronDisplay(OCC.Aspect.Aspect_TOTP_RIGHT_LOWER,OCC.Quantity.Quantity_NOC_WHITE,scale,OCC.V3d.V3d_ZBUFFER); self.Repaint() def EnableAntiAliasing(self): @@ -351,16 +370,8 @@ def SetSelectionMode(self, mode=None): else: self.Context.ActivateStandardMode(mode) self.Context.UpdateSelected() + return topo_lut[topo_level] - def SetSelectionModeVertex(self): - self.SetSelectionMode(OCC.TopAbs.TopAbs_VERTEX) - - def SetSelectionModeEdge(self): - self.SetSelectionMode(OCC.TopAbs.TopAbs_EDGE) - - def SetSelectionModeFace(self): - self.SetSelectionMode(OCC.TopAbs.TopAbs_FACE) - def SetSelectionModeShape(self): self.Context.CloseAllContexts() @@ -390,9 +401,12 @@ def SelectArea(self,Xmin,Ymin,Xmax,Ymax): def Select(self,X,Y): self.Context.Select() self.Context.InitSelected() + + self.selected_shapes = [] if self.Context.MoreSelected(): if self.Context.HasSelectedShape(): self.selected_shape = self.Context.SelectedShape() + self.selected_shapes.append(self.selected_shape) print "Current selection (single):",self.selected_shape else: self.selected_shape = None diff --git a/src/addons/Display/qtDisplay.py b/src/addons/Display/qtDisplay.py index dbff052a5..f9893e5af 100644 --- a/src/addons/Display/qtDisplay.py +++ b/src/addons/Display/qtDisplay.py @@ -17,12 +17,12 @@ ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see . -import os - -import sys -from PyQt4 import QtCore, QtGui, QtOpenGL +import os,sys +from PyQt4 import Qt, QtCore, QtGui, QtOpenGL import OCCViewer +from OpenGL.GL import * + class point(object): def __init__(self,obj=None): self.x = 0 @@ -34,30 +34,55 @@ def set(self,obj): self.x = obj.x() self.y = obj.y() + class qtBaseViewer(QtOpenGL.QGLWidget): +# class qtBaseViewer(QtGui.QWidget): ''' The base Qt Widget for an OCC viewer ''' def __init__(self, parent = None): - QtOpenGL.QGLWidget.__init__(self,parent) + # super(qtBaseViewer, self).__init__(QtOpenGL.QGLFormat(QtOpenGL.QGL.SampleBuffers), parent) + super(qtBaseViewer, self).__init__(parent) + # self.makeCurrent() + super(qtBaseViewer, self).initializeGL() + + # super(qtBaseViewer, self).initializeOverlayGL() + + # both required for drawing boxes over the OpenGL image + + # These are correct for the OCC viewer + # self.setAutoFillBackground(False) + # self.setAutoBufferSwap(True) + + self.setAutoFillBackground(False) + # self.setAutoBufferSwap(True) + + + # ol = self.overlayContext() + # format_ = QtOpenGL.QGLFormat() + # f = format_.defaultOverlayFormat() + # f.setDoubleBuffer(True) + # ol.format().defaultFormat(format_) + # f.hasOverlay() + # f.setOverlay() + self._display = None self._inited = False self.setMouseTracking(True) #enable Mouse Tracking self.setFocusPolicy(QtCore.Qt.WheelFocus)#Strong focus # On X11, setting this attribute will disable all double buffering - self.setAttribute(QtCore.Qt.WA_PaintOnScreen) - # setting this flag implicitly disables double buffering for the widget - self.setAttribute(QtCore.Qt.WA_NoSystemBackground) + # self.setAttribute(QtCore.Qt.WA_PaintOnScreen) + + def initializeGL(self): + glEnable(GL_MULTISAMPLE) def GetHandle(self): return int(self.winId()) def resizeEvent(self, event): if self._inited: + print "resizeEvent" self._display.OnResize() - def paintEngine(self): - return None - class qtViewer3d(qtBaseViewer): def __init__(self, *kargs): qtBaseViewer.__init__(self, *kargs) @@ -108,25 +133,38 @@ def Test(self): self._display.Test() def focusInEvent(self, event): - #print 'focus in!!' + print 'focus in!!' if self._inited: self._display.Repaint() def focusOutEvent(self, event): - #print 'focus out' + print 'focus out' if self._inited: self._display.Repaint() #print 'repainted' + def resizeGL(self, width, height): + print "resizeGL" + self.setupViewport(width, height) + def paintEvent(self, event): + print "paint event qt viewer" if self._inited: - self._display.Repaint() + # the jitteriness is because of the many + # and quite likely unnessecary updates of the viewer + # if you skip the update, redrawing will be much faster + # problem is that all previous rectangles will be still there too... + self._display.Context.UpdateCurrentViewer() + self.swapBuffers() + if self._drawbox: + self.makeCurrent() painter = QtGui.QPainter(self) painter.setPen(QtGui.QPen(QtGui.QColor(0,0,0), 1)) rect = QtCore.QRect(*self._drawbox) painter.drawRect(rect) painter.end() + self.doneCurrent() def ZoomAll(self, evt): self._display.Zoom_FitAll() @@ -140,7 +178,8 @@ def wheelEvent(self,event): self._display.ZoomFactor(zoom_factor) def dragMoveEvent(self,event): - pass#print 'dragmove event' + #print 'dragmove event' + pass def mousePressEvent(self, event): #print 'mouse press event' @@ -157,7 +196,12 @@ def mouseReleaseEvent(self, event): selected_shapes = self._display.SelectArea(Xmin,Ymin,Xmin+dx,Ymin+dy) self._select_area = False else: - self._display.Select(pt.x,pt.y) + # multiple select if shift is pressed + if QtCore.Qt.ShiftModifier: + self._display.ShiftSelect(pt.x,pt.y) + else: + # single select otherwise + self._display.Select(pt.x, pt.y) elif event.button() == QtCore.Qt.RightButton: if self._zoom_area: [Xmin, Ymin, dx, dy] = self._drawbox @@ -171,9 +215,9 @@ def DrawBox(self, event): dy = pt.y - self.dragStartPos.y if abs( dx ) <= tolerance and abs( dy ) <= tolerance: return - self.repaint() self._drawbox = [self.dragStartPos.x, self.dragStartPos.y , dx, dy] - + self.update() + def mouseMoveEvent(self, evt): pt = point(evt.pos()) buttons = int(evt.buttons()) diff --git a/src/addons/Display/traitDisplay.py b/src/addons/Display/traitDisplay.py index 2e74e530e..dcacc4713 100644 --- a/src/addons/Display/traitDisplay.py +++ b/src/addons/Display/traitDisplay.py @@ -21,11 +21,11 @@ from itertools import izip -import os +import random, os try: if os.environ["ETS_TOOLKIT"] == "wx": - print "ETS_TOOLKIT variable has been set to wx, though this module explicitly uses the Qt backed\ + print "ETS_TOOLKIT variable has been set to wx, though this example explicitly uses the Qt backed\ setting the backend to Qt4..." except KeyError: pass @@ -35,7 +35,12 @@ from pyface.qt import QtGui from traits.api import HasTraits, Any, Button, List, Instance, Str, Bool -from traitsui.api import EditorFactory, Editor +from traitsui.api import Item, View, EditorFactory, Editor +from OCC.gp import gp_Trsf, gp_Vec +from OCC.TopLoc import TopLoc_Location +from OCC.Display.qtDisplay import qtViewer3d +from OCC.BRepPrimAPI import BRepPrimAPI_MakeCylinder +from OCC.Display import OCCViewer class TraitOCCViewer3d(OCCViewer.Viewer3d): ''' @@ -144,6 +149,8 @@ def _shapes_items_changed(self, name, nothing, change): self.control._display.EraseShape(s) self.control._display.FitAll() + + class ToolkitEditorFactory ( EditorFactory ): selection = Str display = Str @@ -151,3 +158,54 @@ def _get_simple_editor_class(self): return OCCEditor OCCEditorFactory = ToolkitEditorFactory + +if __name__ == '__main__': + #Todo move this to examples + class Example(HasTraits): + shapes = List + + add_stuff = Button + remove_stuff = Button + + #these are synced with the OCCEditor, name is refered to in OCCEditorFactory + selection = List + display = Instance(OCCViewer) + + view = View( + #set editor for the shapelist + #optional selection and display can set to the string name of the display instance and selection list + Item('shapes', editor=OCCEditorFactory(selection='selection', display='display'), show_label=False), + + + Item('add_stuff', show_label=False), + Item('remove_stuff', show_label=False), + width= 0.8, + height=0.8, + resizable=True + ) + + def _selection_items_changed(self, name, undefined, list_change): + print "selection trait changed", list_change.added, self.display + + def _add_stuff_changed(self, old, new): + for i in xrange(20): + brep = BRepPrimAPI_MakeCylinder(random.random()*50, random.random()*50).Shape() + trsf = gp_Trsf() + trsf.SetTranslation(gp_Vec(random.random()*100, random.random()*100, random.random()*100)) + brep.Move(TopLoc_Location(trsf)) + self.shapes.append(brep) + + def _remove_stuff_changed(self, old, new): + if len(self.selection) >= 1: + for shape in self.selection: + self.shapes.remove(shape) + self.selection = [] + else: + print "nothing selected so removing an arbitrary number of shapes" + for i in range(random.randint(1, 8)): + if len(self.shapes) > 0: + self.shapes.pop() + else: + print "you should add some if you want me to remove stuff..." + + Example(shapes=[]).configure_traits() diff --git a/src/addons/Display/wxDisplay.py b/src/addons/Display/wxDisplay.py index d0068c0d8..2397dd2b1 100644 --- a/src/addons/Display/wxDisplay.py +++ b/src/addons/Display/wxDisplay.py @@ -201,7 +201,11 @@ def OnLeftUp(self,evt): selected_shapes = self._display.SelectArea(Xmin,Ymin,Xmin+dx,Ymin+dy) self._select_area = False else: - self._display.Select(pt.x,pt.y) + shift_down = wx.GetMouseState().ShiftDown() + if shift_down: + self._display.ShiftSelect(pt.x,pt.y) + else: + self._display.Select(pt.x,pt.y) def OnRightUp(self,evt): if self._zoom_area: diff --git a/src/addons/KBE/edge.py b/src/addons/KBE/edge.py index 9371994c2..4a0568830 100644 --- a/src/addons/KBE/edge.py +++ b/src/addons/KBE/edge.py @@ -16,7 +16,7 @@ from OCC.Utils.Common import vertex2pnt, minimum_distance from OCC.Utils.Construct import make_edge, fix_continuity from OCC.Utils.Context import assert_isdone -from OCC.KBE.vertex import Vertex +# from OCC.KBE.vertex import Vertex from OCC.KBE.types_lut import geom_lut from OCC.KBE.base import KbeObject @@ -253,7 +253,7 @@ def _local_properties(self): self._lprops_curve_tool = GeomLProp_CurveTool() self._local_properties_init = True - def domain(self): + def domain(self, trimmed_domain=False): '''returns the u,v domain of the curve''' return self.adaptor.FirstParameter(), self.adaptor.LastParameter() diff --git a/src/addons/KBE/face.py b/src/addons/KBE/face.py index 5d601e45b..b718ae26f 100644 --- a/src/addons/KBE/face.py +++ b/src/addons/KBE/face.py @@ -1,3 +1,4 @@ +import itertools from OCC.BRep import BRep_Tool_Surface, BRep_Tool from OCC.BRepIntCurveSurface import BRepIntCurveSurface_Inter from OCC.BRepTopAdaptor import BRepTopAdaptor_FClass2d @@ -16,7 +17,6 @@ from OCC.ShapeAnalysis import ShapeAnalysis_Surface from OCC.GeomProjLib import GeomProjLib from OCC.Adaptor3d import Adaptor3d_IsoCurve - from OCC.KBE.base import Display, KbeObject, GlobalProperties from OCC.KBE.edge import Edge from OCC.Utils.Construct import * @@ -190,12 +190,15 @@ class Face(KbeObject, TopoDS_Face): object is a Face iff part of a Solid otherwise the same methods do apply, apart from the topology obviously """ + + counter = itertools.count() + def __init__(self, face): ''' ''' - KbeObject.__init__(self, name='face') + KbeObject.__init__(self) TopoDS_Face.__init__(self, face) - + self.name = "face {0}".format(self.counter.next()) # cooperative classes self.DiffGeom = DiffGeomSurface(self) @@ -255,8 +258,7 @@ def mid_point(self): :return: the parameter at the mid point of the face, and its corresponding gp_Pnt """ u_min, u_max, v_min, v_max = self.domain() - u_mid = (u_min + u_max) / 2. - v_mid = (v_min + v_max) / 2. + u_mid, v_mid = u_min + (u_max - u_min) / 2., v_min + (v_max - v_min) / 2. pnt = self.parameter_to_point(u_mid, v_mid) return ( (u_mid, v_mid), self.adaptor.Value(u_mid, v_mid) ) @@ -441,6 +443,7 @@ def iso_curve(self, u_or_v, param): """ uv = 0 if u_or_v == 'u' else 1 # TODO: REFACTOR, part of the Face class now... + # TODO: should use param_start & param_end to extract the iso curve given a specific domain iso = Adaptor3d_IsoCurve(self.adaptor_handle.GetHandle(), uv, param) return iso diff --git a/src/addons/KBE/vertex.py b/src/addons/KBE/vertex.py index 7a8e308f0..b46b47b1e 100644 --- a/src/addons/KBE/vertex.py +++ b/src/addons/KBE/vertex.py @@ -7,7 +7,7 @@ from OCC.BRepTools import BRepTools_TrsfModification from OCC.ShapeBuild import ShapeBuild_ReShape -from base import KbeObject +from OCC.KBE.base import KbeObject class Vertex(KbeObject, TopoDS_Vertex): """ diff --git a/src/addons/Utils/Common.py b/src/addons/Utils/Common.py index d3bd0a1c8..476ed35b2 100644 --- a/src/addons/Utils/Common.py +++ b/src/addons/Utils/Common.py @@ -130,9 +130,9 @@ def _Tcol_dim_1(li, _type): # return pts def point_list_to_TColgp_Array1OfPnt(li): - pts = TColgp_Array1OfPnt(0, len(li)-1) + pts = TColgp_Array1OfPnt(1, len(li)+1) for n,i in enumerate(li): - pts.SetValue(n,i) + pts.SetValue(n+1,i) return pts def point2d_list_to_TColgp_Array1OfPnt2d(li): @@ -402,6 +402,38 @@ def intersection_from_three_planes( planeA, planeB, planeC): pnt = intersection_planes.Value() return pnt +def intersect_plane_line(pln, li): + + """ + + :param pln: gp_Pln or Geom_Plane + :param li: gp_Lin + """ + + from OCC.Geom import Geom_Plane, Geom_Line + _li = Geom_Line(li) + + if isinstance(pln, gp_Pln): + _pln = Geom_Plane(pln) + elif isinstance(pln, Geom_Plane): + _pln = pln + else: + raise ValueError("the `pln` argument either is a gp_Pln or a Geom_Plane, but a {0}\ + argument was supplied".format(pln.__class__)) + + intersect_ = GeomAPI_IntCS() + intersect_.Perform(_li.GetHandle(), _pln.GetHandle()) + if intersect_.IsDone(): + if intersect_.NbPoints() == 0: + return None + elif intersect_.NbPoints() ==1: + return intersect_.Point(1) + else: + raise ValueError("found more than one intersection of a line and a plane...\ + expected just a single intersection") + else: + return None + def intersect_shape_by_line(topods_shape, line, low_parameter=0.0, hi_parameter=float("+inf")): """ finds the intersection of a shape and a line diff --git a/src/addons/Utils/Construct.py b/src/addons/Utils/Construct.py index b4277b45c..dab099399 100644 --- a/src/addons/Utils/Construct.py +++ b/src/addons/Utils/Construct.py @@ -503,7 +503,7 @@ def make_n_sided(edges, points, continuity=GeomAbs_C0): :return: TopoDS_Face """ from OCC.BRepFill import BRepFill_Filling - n_sided = BRepFill_Filling(NbIter=6) + n_sided = BRepFill_Filling()#NbIter=6) #n_sided.SetApproxParam( 6, 40) #n_sided.SetResolParam( 3, 20, 20, False) for edg in edges: @@ -844,6 +844,7 @@ def fit_plane_through_face_vertices(_face): """ from OCC.GeomPlate import GeomPlate_BuildAveragePlane from OCC.Utils.Topology import Topo + from OCC.Utils.Common import to_tcol uvs_from_vertices = [_face.project_vertex(vertex2pnt(i)) for i in Topo(_face).vertices()] diff --git a/src/addons/Utils/Topology.py b/src/addons/Utils/Topology.py index 6502e0bc4..3f8b814f1 100644 --- a/src/addons/Utils/Topology.py +++ b/src/addons/Utils/Topology.py @@ -1 +1 @@ -# -*- coding: iso-8859-1 -*- #!/usr/bin/env python ##Copyright 2008-2011 Jelle Feringa (jelleferinga@gmail.com) ## ##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 . ''' TODO: Hide WireExplorer... BRepTools.Map3DEdges() History: 20-01-2009: initial version 23-03-2009: completed and updated for modular pythonOCC build 23-04-2009: fixed a reference issue ( fixed using ReInit ) ''' from OCC.TopAbs import * from OCC.TopExp import * from OCC.TopoDS import * from OCC.TopTools import * from OCC.BRepTools import * from OCC.BRep import * import sys, itertools __all__ = ['Topo', 'WireExplorer'] class WireExplorer(object): ''' ''' def __init__(self, wire): assert isinstance(wire, TopoDS_Wire), 'not a TopoDS_Wire' self.wire = wire self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _reinitialize(self): self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _loop_topo(self, edges=True): if self.done: self._reinitialize() topologyType = TopoDS_edge if edges else TopoDS_vertex seq = [] hashes = [] #list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while self.wire_explorer.More(): # loop edges if edges: current_item = self.wire_explorer.Current() # loop vertices else: current_item = self.wire_explorer.CurrentVertex() current_item_hash = current_item.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) occ_seq.Append(current_item) self.wire_explorer.Next() # Convert occ_seq to python list occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = topologyType(occ_iterator.Value()) seq.append(topo_to_add) occ_iterator.Next() return iter(seq) self.done = True def ordered_edges(self): return self._loop_topo(edges=True) def ordered_vertices(self): return self._loop_topo(edges=False) class Topo(object): ''' sketch for a pythonic topology wrapper note that `myShape` should be self, which is in return a occ.TopoShape with this ''' def __init__(self, myShape): self.myShape = myShape def _loop_topo(self, topologyType, topologicalEntity=None, topologyTypeToAvoid=None): ''' this could be a faces generator for a python TopoShape class that way you can just do: for face in srf.faces: processFace(face) ''' topoTypes = { TopAbs_VERTEX: TopoDS_vertex, TopAbs_EDGE: TopoDS_edge, TopAbs_FACE: TopoDS_face, TopAbs_WIRE: TopoDS_wire, TopAbs_SHELL: TopoDS_shell, TopAbs_SOLID: TopoDS_solid, TopAbs_COMPOUND: TopoDS_compound, TopAbs_COMPSOLID: TopoDS_compsolid} assert topologyType in topoTypes.keys(), '%s not one of %s' %( topologyType, topoTypes.keys() ) self.topExp = TopExp_Explorer() # use self.myShape if nothing is specified if topologicalEntity is None and topologyTypeToAvoid is None: self.topExp.Init(self.myShape, topologyType) elif topologicalEntity is None and topologyTypeToAvoid is not None: self.topExp.Init(self.myShape, topologyType, topologyTypeToAvoid) elif topologyTypeToAvoid is None: self.topExp.Init(topologicalEntity, topologyType ) elif topologyTypeToAvoid: self.topExp.Init(topologicalEntity, topologyType, topologyTypeToAvoid) topo_set = set() seq = [] hashes = [] #list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while self.topExp.More(): current_item = self.topExp.Current() current_item_hash = current_item.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) occ_seq.Append(current_item) self.topExp.Next() # Convert occ_seq to python list occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = topoTypes[topologyType](occ_iterator.Value()) seq.append(topo_to_add) occ_iterator.Next() return iter(seq) def faces(self): ''' loops over all faces ''' return self._loop_topo(TopAbs_FACE) def _number_of_topo(self, iterable): n = 0 for i in iterable: n+=1 return n def number_of_faces(self): return self._number_of_topo(self.faces()) def vertices(self): ''' loops over all vertices ''' return self._loop_topo(TopAbs_VERTEX) def number_of_vertices(self): return self._number_of_topo(self.vertices()) def edges(self): ''' loops over all edges ''' return self._loop_topo(TopAbs_EDGE) def number_of_edges(self): return self._number_of_topo(self.edges()) def wires(self): ''' loops over all wires ''' return self._loop_topo(TopAbs_WIRE) def number_of_wires(self): return self._number_of_topo(self.wires()) def shells(self): ''' loops over all shells ''' return self._loop_topo(TopAbs_SHELL, None) def number_of_shells(self): return self._number_of_topo(self.shells()) def solids(self): ''' loops over all solids ''' return self._loop_topo(TopAbs_SOLID, None) def number_of_solids(self): return self._number_of_topo(self.solids()) def comp_solids(self): ''' loops over all compound solids ''' return self._loop_topo(TopAbs_COMPSOLID) def number_of_comp_solids(self): return self._number_of_topo(self.comp_solids()) def compounds(self): ''' loops over all compounds ''' return self._loop_topo(TopAbs_COMPOUND) def number_of_compounds(self): return self._number_of_topo(self.compounds()) def ordered_vertices_from_wire(self, wire): ''' @param wire: TopoDS_Wire ''' we = WireExplorer(wire) return we.ordered_vertices() def number_of_ordered_vertices_from_wire(self, wire): return self._number_of_topo(self.ordered_vertices_from_wire(wire)) def ordered_edges_from_wire(self, wire): ''' @param wire: TopoDS_Wire ''' we = WireExplorer(wire) return we.ordered_edges() def number_of_ordered_edges_from_wire(self, wire): return self._number_of_topo(self.ordered_edges_from_wire(wire)) def _map_shapes_and_ancestors(self, topoTypeA, topoTypeB, topologicalEntity): ''' using the same method @param topoTypeA: @param topoTypeB: @param topologicalEntity: ''' topo_set = set() _map = TopTools_IndexedDataMapOfShapeListOfShape() TopExp_MapShapesAndAncestors(self.myShape, topoTypeA, topoTypeB, _map) results = _map.FindFromKey(topologicalEntity) if results.IsEmpty(): yield None topoTypes = { TopAbs_VERTEX: TopoDS_vertex, TopAbs_EDGE: TopoDS_edge, TopAbs_FACE: TopoDS_face, TopAbs_WIRE: TopoDS_wire, TopAbs_SHELL: TopoDS_shell, TopAbs_SOLID: TopoDS_solid, TopAbs_COMPOUND: TopoDS_compound, TopAbs_COMPSOLID: TopoDS_compsolid} topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): topo_entity = topoTypes[topoTypeB](topology_iterator.Value()) # return the entity if not in set # to assure we're not returning entities several times if not topo_entity in topo_set: yield topo_entity topo_set.add(topo_entity) topology_iterator.Next() def _number_shapes_ancestors(self, topoTypeA, topoTypeB, topologicalEntity): '''returns the number of shape ancestors If you want to know how many edges a faces has: _number_shapes_ancestors(self, TopAbs_EDGE, TopAbs_FACE, edg) will return the number of edges a faces has @param topoTypeA: @param topoTypeB: @param topologicalEntity: ''' topo_set = set() _map = TopTools_IndexedDataMapOfShapeListOfShape() TopExp_MapShapesAndAncestors(self.myShape, topoTypeA, topoTypeB, _map) results = _map.FindFromKey(topologicalEntity) if results.IsEmpty(): return None topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): topo_set.add(topology_iterator.Value()) topology_iterator.Next() return len(topo_set) #=============================================================================== # EDGE <-> FACE #=============================================================================== def faces_from_edge(self, edge): return self._map_shapes_and_ancestors(TopAbs_EDGE,TopAbs_FACE,edge) def number_of_faces_from_edge(self, edge): return self._number_shapes_ancestors(TopAbs_EDGE,TopAbs_FACE,edge) def edges_from_face(self, face): return self._loop_topo(TopAbs_EDGE, face) def number_of_edges_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_EDGE, face): cnt += 1 return cnt #=============================================================================== # VERTEX <-> EDGE #=============================================================================== def vertices_from_edge(self, edg): return self._loop_topo(TopAbs_VERTEX, edg) def number_of_vertices_from_edge(self, edg): cnt = 0 for i in self._loop_topo(TopAbs_VERTEX, edg): cnt += 1 return cnt def edges_from_vertex(self, vertex): return self._map_shapes_and_ancestors(TopAbs_VERTEX, TopAbs_EDGE,vertex) def number_of_edges_from_vertex(self, vertex): return self._number_shapes_ancestors(TopAbs_VERTEX,TopAbs_EDGE,vertex) #=============================================================================== # WIRE <-> EDGE #=============================================================================== def edges_from_wire(self, wire): return self._loop_topo(TopAbs_EDGE, wire) def number_of_edges_from_wire(self, wire): cnt = 0 for i in self._loop_topo(TopAbs_EDGE, wire): cnt += 1 return cnt def wires_from_edge(self, edg): return self._map_shapes_and_ancestors(TopAbs_EDGE, TopAbs_WIRE, edg) def number_of_wires_from_edge(self, edg): return self._number_shapes_ancestors(TopAbs_EDGE,TopAbs_WIRE, edg) #=============================================================================== # WIRE <-> FACE #=============================================================================== def wires_from_face(self, face): return self._loop_topo(TopAbs_WIRE, face) def number_of_wires_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_WIRE, face): cnt += 1 return cnt def faces_from_wire(self, wire): return self._map_shapes_and_ancestors(TopAbs_WIRE, TopAbs_FACE, wire) def number_of_faces_from_wires(self, wire): return self._number_shapes_ancestors(TopAbs_WIRE,TopAbs_FACE, wire) #=============================================================================== # VERTEX <-> FACE #=============================================================================== def faces_from_vertex(self, vertex): return self._map_shapes_and_ancestors(TopAbs_VERTEX,TopAbs_FACE,vertex) def number_of_faces_from_vertex(self, vertex): return self._number_shapes_ancestors(TopAbs_VERTEX,TopAbs_FACE,vertex) def vertices_from_face(self, face): return self._loop_topo(TopAbs_VERTEX, face) def number_of_vertices_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_VERTEX, face): cnt += 1 return cnt #=============================================================================== # FACE <-> SOLID #=============================================================================== def solids_from_face(self, face): return self._map_shapes_and_ancestors(TopAbs_FACE,TopAbs_SOLID,face) def number_of_solids_from_face(self, face): return self._number_shapes_ancestors(TopAbs_FACE,TopAbs_SOLID,face) def faces_from_solids(self, solid): return self._loop_topo(TopAbs_FACE, solid) def number_of_faces_from_solids(self, solid): cnt = 0 for i in self._loop_topo(TopAbs_FACE, solid): cnt += 1 return cnt def dumpTopology(shape,level=0): """ Print the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() ts = TopoDS.TopoDS() print print "." * level,shapeTypeString(shape), if s == TopAbs_VERTEX: pnt = brt.Pnt(ts.Vertex(shape)) print "" % (pnt.X(), pnt.Y(), pnt.Z()) it = TopoDS.TopoDS_Iterator(shape) while it.More(): shp = it.Value() it.Next() dumpTopology(shp,level + 1 ) def shapeTypeString(shape): st = shape.ShapeType() s = "?" if st == TopAbs_VERTEX: s = "Vertex" if st == TopAbs_SOLID: s = "Solid" if st == TopAbs_EDGE: s = "Edge" if st == TopAbs_FACE: s = "Face" if st == TopAbs_SHELL: s = "Shell" if st == TopAbs_WIRE: s = "Wire" if st == TopAbs_COMPOUND: s = "Compound." if st == TopAbs_COMPSOLID: s = "Compsolid." return s + ":" + str(shape.HashCode(23232232)) \ No newline at end of file +# -*- coding: iso-8859-1 -*- #!/usr/bin/env python ##Copyright 2008-2011 Jelle Feringa (jelleferinga@gmail.com) ## ##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 . ''' TODO: Hide WireExplorer... BRepTools.Map3DEdges() History: 20-01-2009: initial version 23-03-2009: completed and updated for modular pythonOCC build 23-04-2009: fixed a reference issue ( fixed using ReInit ) ''' from OCC.TopAbs import * from OCC.TopExp import * from OCC.TopoDS import * from OCC.TopTools import * from OCC.BRepTools import * from OCC.BRep import * from OCC.Utils.Construct import vertex2pnt import sys, itertools __all__ = ['Topo', 'WireExplorer'] class WireExplorer(object): ''' ''' def __init__(self, wire, kbe_types=False): assert isinstance(wire, TopoDS_Wire), 'not a TopoDS_Wire' self.kbe_types = kbe_types self.wire = wire self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _reinitialize(self): self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _loop_topo(self, edges=True): if self.done: self._reinitialize() topologyType = TopoDS_edge if edges else TopoDS_vertex seq = [] hashes = [] #list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while self.wire_explorer.More(): # loop edges if edges: current_item = self.wire_explorer.Current() # loop vertices else: current_item = self.wire_explorer.CurrentVertex() current_item_hash = current_item.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) occ_seq.Append(current_item) self.wire_explorer.Next() # Convert occ_seq to python list occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = topologyType(occ_iterator.Value()) if self.kbe_types and edges: edge = Edge(topo_to_add) seq.append(edge) else: seq.append(topo_to_add) occ_iterator.Next() self.done = True return iter(seq) def ordered_edges(self): return self._loop_topo(edges=True) def ordered_vertices(self): return self._loop_topo(edges=False) class Topo(object): ''' Topology traversal ''' def __init__(self, myShape, kbe_types=False, ignore_orientation=False): """ implements topology traversal from any TopoDS_Shape this class lets you find how various topological entities are connected from one to another find the faces connected to an edge, find the vertices this edge is made from, get all faces connected to a vertex, and find out how many topological elements are connected from a source *note* when traversing TopoDS_Wire entities, its advised to use the specialized ``WireExplorer`` class, which will return the vertices / edges in the expected order :param myShape: the shape which topology will be traversed :param kbe_types: whether to return OCC.KBE topology types KBE types offers a more consistent and pythonic API, since many useful methods are bound to these objects :param ignore_orientation: filter out TopoDS_* entities of similar TShape but different Orientation for instance, a cube has 24 edges, 4 edges for each of 6 faces that results in 48 vertices, while there are only 8 vertices that have a unique geometric coordinate in certain cases ( computing a graph from the topology ) its preferable to return topological entities that share similar geometry, though differ in orientation by setting the ``ignore_orientation`` variable to True, in case of a cube, just 12 edges and only 8 vertices will be returned for further reference see TopoDS_Shape IsEqual / IsSame methods """ self.myShape = myShape self.kbe_types = kbe_types self.ignore_orientation = ignore_orientation self.topoTypes = { TopAbs_VERTEX: TopoDS_vertex, TopAbs_EDGE: TopoDS_edge, TopAbs_FACE: TopoDS_face, TopAbs_WIRE: TopoDS_wire, TopAbs_SHELL: TopoDS_shell, TopAbs_SOLID: TopoDS_solid, TopAbs_COMPOUND: TopoDS_compound, TopAbs_COMPSOLID: TopoDS_compsolid } if self.kbe_types: from OCC.KBE.types_lut import topo_lut, shape_lut from OCC.KBE.vertex import Vertex from OCC.KBE.edge import Edge from OCC.KBE.face import Face from OCC.KBE.solid import Shell, Solid self.kbeTypes = { TopAbs_VERTEX: Vertex, TopAbs_EDGE: Edge, TopAbs_FACE: Face, TopAbs_SHELL: Shell, TopAbs_SOLID: Solid, } def _loop_topo(self, topologyType, topologicalEntity=None, topologyTypeToAvoid=None): ''' this could be a faces generator for a python TopoShape class that way you can just do: for face in srf.faces: processFace(face) ''' topoTypes = { TopAbs_VERTEX: TopoDS_vertex, TopAbs_EDGE: TopoDS_edge, TopAbs_FACE: TopoDS_face, TopAbs_WIRE: TopoDS_wire, TopAbs_SHELL: TopoDS_shell, TopAbs_SOLID: TopoDS_solid, TopAbs_COMPOUND: TopoDS_compound, TopAbs_COMPSOLID: TopoDS_compsolid} assert topologyType in topoTypes.keys(), '%s not one of %s' %( topologyType, topoTypes.keys() ) self.topExp = TopExp_Explorer() # use self.myShape if nothing is specified if topologicalEntity is None and topologyTypeToAvoid is None: self.topExp.Init(self.myShape, topologyType) elif topologicalEntity is None and topologyTypeToAvoid is not None: self.topExp.Init(self.myShape, topologyType, topologyTypeToAvoid) elif topologyTypeToAvoid is None: self.topExp.Init(topologicalEntity, topologyType ) elif topologyTypeToAvoid: self.topExp.Init(topologicalEntity, topologyType, topologyTypeToAvoid) topo_set = set() seq = [] hashes = [] #list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while self.topExp.More(): current_item = self.topExp.Current() current_item_hash = current_item.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) occ_seq.Append(current_item) self.topExp.Next() # Convert occ_seq to python list occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = self.topoTypes[topologyType](occ_iterator.Value()) # return a KBE type if self.kbe_types: if topologyType in self.kbeTypes: seq.append(self.kbeTypes[topologyType](topo_to_add)) # otherwise a plain TopoDS_Shape subclass else: seq.append(topo_to_add) occ_iterator.Next() if self.ignore_orientation: # filter out those entities that share the same TShape # but do *not* share the same orientation filter_orientation_seq = [] for i in seq: _present = False for j in filter_orientation_seq: if i.IsSame(j): _present = True break if _present is False: filter_orientation_seq.append(i) return filter_orientation_seq else: return iter(seq) def faces(self): ''' loops over all faces ''' return self._loop_topo(TopAbs_FACE) def _number_of_topo(self, iterable): n = 0 for i in iterable: n+=1 return n def number_of_faces(self): return self._number_of_topo(self.faces()) def vertices(self): ''' loops over all vertices ''' return self._loop_topo(TopAbs_VERTEX) def number_of_vertices(self): return self._number_of_topo(self.vertices()) def edges(self): ''' loops over all edges ''' return self._loop_topo(TopAbs_EDGE) def number_of_edges(self): return self._number_of_topo(self.edges()) def wires(self): ''' loops over all wires ''' return self._loop_topo(TopAbs_WIRE) def number_of_wires(self): return self._number_of_topo(self.wires()) def shells(self): ''' loops over all shells ''' return self._loop_topo(TopAbs_SHELL, None) def number_of_shells(self): return self._number_of_topo(self.shells()) def solids(self): ''' loops over all solids ''' return self._loop_topo(TopAbs_SOLID, None) def number_of_solids(self): return self._number_of_topo(self.solids()) def comp_solids(self): ''' loops over all compound solids ''' return self._loop_topo(TopAbs_COMPSOLID) def number_of_comp_solids(self): return self._number_of_topo(self.comp_solids()) def compounds(self): ''' loops over all compounds ''' return self._loop_topo(TopAbs_COMPOUND) def number_of_compounds(self): return self._number_of_topo(self.compounds()) def ordered_vertices_from_wire(self, wire): ''' @param wire: TopoDS_Wire ''' we = WireExplorer(wire) return we.ordered_vertices() def number_of_ordered_vertices_from_wire(self, wire): return self._number_of_topo(self.ordered_vertices_from_wire(wire)) def ordered_edges_from_wire(self, wire): ''' @param wire: TopoDS_Wire ''' we = WireExplorer(wire) return we.ordered_edges() def number_of_ordered_edges_from_wire(self, wire): return self._number_of_topo(self.ordered_edges_from_wire(wire)) def _map_shapes_and_ancestors(self, topoTypeA, topoTypeB, topologicalEntity): ''' using the same method @param topoTypeA: @param topoTypeB: @param topologicalEntity: ''' topo_set = set() _map = TopTools_IndexedDataMapOfShapeListOfShape() TopExp_MapShapesAndAncestors(self.myShape, topoTypeA, topoTypeB, _map) results = _map.FindFromKey(topologicalEntity) if results.IsEmpty(): yield None topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): topo_entity = self.topoTypes[topoTypeB](topology_iterator.Value()) if self.kbe_types: if topoTypeB in self.kbeTypes: topo_entity = self.kbeTypes[topoTypeB](topo_entity) else: raise ValueError('none KBE types called') # return the entity if not in set # to assure we're not returning entities several times if not topo_entity in topo_set: if self.ignore_orientation: unique=True for i in topo_set: if i.IsSame(topo_entity): unique=False break if unique: yield topo_entity else: yield topo_entity topo_set.add(topo_entity) topology_iterator.Next() def _number_shapes_ancestors(self, topoTypeA, topoTypeB, topologicalEntity): '''returns the number of shape ancestors If you want to know how many edges a faces has: _number_shapes_ancestors(self, TopAbs_EDGE, TopAbs_FACE, edg) will return the number of edges a faces has @param topoTypeA: @param topoTypeB: @param topologicalEntity: ''' topo_set = set() _map = TopTools_IndexedDataMapOfShapeListOfShape() TopExp_MapShapesAndAncestors(self.myShape, topoTypeA, topoTypeB, _map) results = _map.FindFromKey(topologicalEntity) if results.IsEmpty(): return None topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): topo_set.add(topology_iterator.Value()) topology_iterator.Next() return len(topo_set) #=============================================================================== # EDGE <-> FACE #=============================================================================== def faces_from_edge(self, edge): """ :param edge: :return: """ return self._map_shapes_and_ancestors(TopAbs_EDGE,TopAbs_FACE,edge) def number_of_faces_from_edge(self, edge): """ :param edge: :return: """ return self._number_shapes_ancestors(TopAbs_EDGE,TopAbs_FACE,edge) def edges_from_face(self, face): """ :param face: :return: """ return self._loop_topo(TopAbs_EDGE, face) def number_of_edges_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_EDGE, face): cnt += 1 return cnt #=============================================================================== # VERTEX <-> EDGE #=============================================================================== def vertices_from_edge(self, edg): return self._loop_topo(TopAbs_VERTEX, edg) def number_of_vertices_from_edge(self, edg): cnt = 0 for i in self._loop_topo(TopAbs_VERTEX, edg): cnt += 1 return cnt def edges_from_vertex(self, vertex): return self._map_shapes_and_ancestors(TopAbs_VERTEX, TopAbs_EDGE,vertex) def number_of_edges_from_vertex(self, vertex): return self._number_shapes_ancestors(TopAbs_VERTEX,TopAbs_EDGE,vertex) #=============================================================================== # WIRE <-> EDGE #=============================================================================== def edges_from_wire(self, wire): return self._loop_topo(TopAbs_EDGE, wire) def number_of_edges_from_wire(self, wire): cnt = 0 for i in self._loop_topo(TopAbs_EDGE, wire): cnt += 1 return cnt def wires_from_edge(self, edg): return self._map_shapes_and_ancestors(TopAbs_EDGE, TopAbs_WIRE, edg) def wires_from_vertex(self, edg): return self._map_shapes_and_ancestors(TopAbs_VERTEX, TopAbs_WIRE, edg) def number_of_wires_from_edge(self, edg): return self._number_shapes_ancestors(TopAbs_EDGE,TopAbs_WIRE, edg) #=============================================================================== # WIRE <-> FACE #=============================================================================== def wires_from_face(self, face): return self._loop_topo(TopAbs_WIRE, face) def number_of_wires_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_WIRE, face): cnt += 1 return cnt def faces_from_wire(self, wire): return self._map_shapes_and_ancestors(TopAbs_WIRE, TopAbs_FACE, wire) def number_of_faces_from_wires(self, wire): return self._number_shapes_ancestors(TopAbs_WIRE,TopAbs_FACE, wire) #=============================================================================== # VERTEX <-> FACE #=============================================================================== def faces_from_vertex(self, vertex): return self._map_shapes_and_ancestors(TopAbs_VERTEX,TopAbs_FACE,vertex) def number_of_faces_from_vertex(self, vertex): return self._number_shapes_ancestors(TopAbs_VERTEX,TopAbs_FACE,vertex) def vertices_from_face(self, face): return self._loop_topo(TopAbs_VERTEX, face) def number_of_vertices_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_VERTEX, face): cnt += 1 return cnt #=============================================================================== # FACE <-> SOLID #=============================================================================== def solids_from_face(self, face): return self._map_shapes_and_ancestors(TopAbs_FACE,TopAbs_SOLID,face) def number_of_solids_from_face(self, face): return self._number_shapes_ancestors(TopAbs_FACE,TopAbs_SOLID,face) def faces_from_solids(self, solid): return self._loop_topo(TopAbs_FACE, solid) def number_of_faces_from_solids(self, solid): cnt = 0 for i in self._loop_topo(TopAbs_FACE, solid): cnt += 1 return cnt def dump_topology(shape,level=0): """ Print the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() ts = TopoDS.TopoDS() print print "." * level,topo_lut[shape.ShapeType()], if s == TopAbs_VERTEX: pnt = vertex2pnt(shape_lut[shape]) print "" % (pnt.X(), pnt.Y(), pnt.Z()) it = TopoDS.TopoDS_Iterator(shape) while it.More(): shp = it.Value() it.Next() dump_topology(shp,level + 1 ) def shapeTypeString(shape): st = shape.ShapeType() s = "?" if st == TopAbs_VERTEX: s = "Vertex" if st == TopAbs_SOLID: s = "Solid" if st == TopAbs_EDGE: s = "Edge" if st == TopAbs_FACE: s = "Face" if st == TopAbs_SHELL: s = "Shell" if st == TopAbs_WIRE: s = "Wire" if st == TopAbs_COMPOUND: s = "Compound." if st == TopAbs_COMPSOLID: s = "Compsolid." return s + ":" + str(shape.HashCode(23232232)) \ No newline at end of file diff --git a/src/contrib/smesh-5.1.2.2/inc/SMESH_MeshVSLink.hxx b/src/contrib/smesh-5.1.2.2/inc/SMESH_MeshVSLink.hxx index 9c1123d3a..d4f6962d4 100755 --- a/src/contrib/smesh-5.1.2.2/inc/SMESH_MeshVSLink.hxx +++ b/src/contrib/smesh-5.1.2.2/inc/SMESH_MeshVSLink.hxx @@ -1,27 +1,27 @@ // SMESH SMESH_MeshVSLink : Connection of SMESH with MeshVS from OCC -// -// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, -// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS -// -// This library 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 2.1 of the License. -// -// This library 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 this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -// -// File : SMESH_MeshVSLink.cxx -// Created : Mon Dec 1 09:00:00 2008 -// Author : Sioutis Fotios +// +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library 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 2.1 of the License. +// +// This library 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 this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SMESH_MeshVSLink.cxx +// Created : Mon Dec 1 09:00:00 2008 +// Author : Sioutis Fotios // Module : SMESH #ifndef _SMESH_MeshVSLink_HeaderFile @@ -75,11 +75,14 @@ class SMESH_MeshVSLink : public MeshVS_DataSource3D { //! Constructor
Standard_EXPORT SMESH_MeshVSLink(const SMESH_Mesh *aMesh); + Standard_EXPORT Standard_Boolean GetNodeNormal(const Standard_Integer ranknode,const Standard_Integer ElementId,Standard_Real& nx,Standard_Real& ny,Standard_Real& nz) const; //Not implemented yet /* - Standard_EXPORT Standard_Boolean GetNodeNormal(const Standard_Integer ranknode,const Standard_Integer ElementId,Standard_Real& nx,Standard_Real& ny,Standard_Real& nz) const; + + + Standard_EXPORT Standard_Boolean GetNormalsByElement(const Standard_Integer Id,const Standard_Boolean IsNodal,const Standard_Integer MaxNodes,Handle(TColStd_HArray1OfReal)& Normals) const; Standard_EXPORT void GetAllGroups(TColStd_PackedMapOfInteger& Ids) const; Standard_EXPORT Standard_Boolean GetGroup(const Standard_Integer Id,MeshVS_EntityType& Type,TColStd_PackedMapOfInteger& Ids) const; diff --git a/src/contrib/smesh-5.1.2.2/src/SMESH/SMESH_MeshVSLink.cpp b/src/contrib/smesh-5.1.2.2/src/SMESH/SMESH_MeshVSLink.cpp index 0f3991aa0..dd560598c 100755 --- a/src/contrib/smesh-5.1.2.2/src/SMESH/SMESH_MeshVSLink.cpp +++ b/src/contrib/smesh-5.1.2.2/src/SMESH/SMESH_MeshVSLink.cpp @@ -1,4 +1,4 @@ -// SMESH SMESH_MeshVSLink : Connection of SMESH with MeshVS from OCC +// SMESH SMESH_MeshVSLink : Connection of SMESH with MeshVS from OCC // // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -22,29 +22,29 @@ // File : SMESH_MeshVSLink.cxx // Created : Mon Dec 1 09:00:00 2008 // Author : Sioutis Fotios -// Module : SMESH - -//local headers -#include -#include - -//occ headers -#include -#include -#include - -//BEGIN sortNodes CHANGE /* -#include - -#include -#include -#include - -#define MAX_SORT_NODE_COUNT 12 - -typedef std::map T_Double_NodeID_Map; - -//======================================================================= +// Module : SMESH + +//local headers +#include +#include + +//occ headers +#include +#include +#include + +//BEGIN sortNodes CHANGE /* +#include + +#include +#include +#include + +#define MAX_SORT_NODE_COUNT 12 + +typedef std::map T_Double_NodeID_Map; + +//======================================================================= //function : sortNodes //purpose : //======================================================================= @@ -88,47 +88,53 @@ bool sortNodes (const SMDS_MeshElement* theTool, const int* idNodes, int theNode myMap.insert( make_pair(myAngleList.Value(i), idNodes[i-1])); int resID = 0; T_Double_NodeID_Map::iterator it; - for(it = myMap.begin(); it!= myMap.end(); ++it) + for(it = myMap.begin(); it!= myMap.end(); ++it) myResult[resID++] = it->second; return true; -} -//END sortNodes CHANGE */ - -//================================================================ -// Function : Constructor -// Purpose : -//================================================================ -SMESH_MeshVSLink::SMESH_MeshVSLink(const SMESH_Mesh *aMesh) -{ - myMesh = (SMESH_Mesh*) aMesh; - //add the nodes - SMDS_NodeIteratorPtr aNodeIter = myMesh->GetMeshDS()->nodesIterator(); - for(;aNodeIter->more();) { +} +//END sortNodes CHANGE */ + +//================================================================ +// Function : Constructor +// Purpose : +//================================================================ +SMESH_MeshVSLink::SMESH_MeshVSLink(const SMESH_Mesh *aMesh) +{ + myMesh = (SMESH_Mesh*) aMesh; + //add the nodes + SMDS_NodeIteratorPtr aNodeIter = myMesh->GetMeshDS()->nodesIterator(); + for(;aNodeIter->more();) { const SMDS_MeshNode* aNode = aNodeIter->next(); myNodes.Add( aNode->GetID() ); - } - //add the edges - SMDS_EdgeIteratorPtr anEdgeIter = myMesh->GetMeshDS()->edgesIterator(); + } + //add the edges + SMDS_EdgeIteratorPtr anEdgeIter = myMesh->GetMeshDS()->edgesIterator(); for(;anEdgeIter->more();) { const SMDS_MeshEdge* anElem = anEdgeIter->next(); myElements.Add( anElem->GetID() ); - } - //add the faces - SMDS_FaceIteratorPtr aFaceIter = myMesh->GetMeshDS()->facesIterator(); + } + //add the faces + SMDS_FaceIteratorPtr aFaceIter = myMesh->GetMeshDS()->facesIterator(); for(;aFaceIter->more();) { const SMDS_MeshFace* anElem = aFaceIter->next(); myElements.Add( anElem->GetID() ); } - //add the volumes - SMDS_VolumeIteratorPtr aVolumeIter = myMesh->GetMeshDS()->volumesIterator(); - for(;aVolumeIter->more();) { + + //add the volumes + + SMDS_VolumeIteratorPtr aVolumeIter = myMesh->GetMeshDS()->volumesIterator(); + + for(;aVolumeIter->more();) { const SMDS_MeshVolume* anElem = aVolumeIter->next(); myElements.Add( anElem->GetID() ); } - //add the groups - const std::set& groups = myMesh->GetMeshDS()->GetGroups(); - if (!groups.empty()) { + + //add the groups + + const std::set& groups = myMesh->GetMeshDS()->GetGroups(); + + if (!groups.empty()) { std::set::const_iterator GrIt = groups.begin(); for (; GrIt != groups.end(); GrIt++) { SMESHDS_Group* grp = dynamic_cast(*GrIt); @@ -136,195 +142,212 @@ SMESH_MeshVSLink::SMESH_MeshVSLink(const SMESH_Mesh *aMesh) myGroups.Add(grp->GetID()); } } - } - -//================================================================ -// Function : GetGeom -// Purpose : -//================================================================ -Standard_Boolean SMESH_MeshVSLink::GetGeom - ( const Standard_Integer ID, const Standard_Boolean IsElement, - TColStd_Array1OfReal& Coords, Standard_Integer& NbNodes, - MeshVS_EntityType& Type ) const -{ - if( IsElement ) { - const SMDS_MeshElement* myElem = myMesh->GetMeshDS()->FindElement(ID); - if (!myElem) return Standard_False; - if (myElem->GetType() == SMDSAbs_Edge) - Type = MeshVS_ET_Link; - else if (myElem->GetType() == SMDSAbs_Face) - Type = MeshVS_ET_Face; - else if (myElem->GetType() == SMDSAbs_Volume) - Type = MeshVS_ET_Volume; - else - Type = MeshVS_ET_Element; - NbNodes = myElem->NbNodes(); - int nbCoord = 1; - for(Standard_Integer i = 0; i < NbNodes; i++ ) { - Coords(nbCoord++) = myElem->GetNode(i)->X(); - Coords(nbCoord++) = myElem->GetNode(i)->Y(); - Coords(nbCoord++) = myElem->GetNode(i)->Z(); - } - } - else { - const SMDS_MeshNode* myNode = myMesh->GetMeshDS()->FindNode(ID); - if (!myNode) return Standard_False; - if (myNode->GetType() == SMDSAbs_Node) - Type = MeshVS_ET_Node; - else - Type = MeshVS_ET_0D; - NbNodes = 1; - Coords(1) = myNode->X(); - Coords(2) = myNode->Y(); - Coords(3) = myNode->Z(); - } - return Standard_True; -} - -//================================================================ -// Function : Get3DGeom -// Purpose : -//================================================================ -Standard_Boolean SMESH_MeshVSLink::Get3DGeom - ( const Standard_Integer ID, Standard_Integer& NbNodes, - Handle(MeshVS_HArray1OfSequenceOfInteger)& Data) const -{ - //check validity of element - const SMDS_MeshElement* myVolume = myMesh->GetMeshDS()->FindElement(ID); - if (!myVolume) return Standard_False; - if (myVolume->GetType() != SMDSAbs_Volume) return Standard_False; - - //initialize VolumeTool - SMDS_VolumeTool aTool; - aTool.Set(myVolume); - //set the nodes number - NbNodes = aTool.NbNodes();// myVolume->NbNodes(); - //check validity or create Data - int NbFaces = aTool.NbFaces(); - if (Data.IsNull()) - Data = new MeshVS_HArray1OfSequenceOfInteger(1, NbFaces); - else if (Data->Length() != NbFaces) { - Data.Nullify(); - Data = new MeshVS_HArray1OfSequenceOfInteger(1, NbFaces); - } - //iterate the faces and their nodes and add them to Data - for (int itr=0;itr < NbFaces;itr++) { - int NbThisFaceNodeCount = aTool.NbFaceNodes(itr); - const int *FaceIndices = aTool.GetFaceNodesIndices(itr); - int sortedFaceIndices[MAX_SORT_NODE_COUNT]; - TColStd_SequenceOfInteger aSeq; - if (sortNodes(myVolume, FaceIndices, NbThisFaceNodeCount, sortedFaceIndices)) { - for (int itrX=0;itrX < NbThisFaceNodeCount;itrX++) - aSeq.Append(sortedFaceIndices[itrX]); - } else { - for (int itrX=0;itrX < NbThisFaceNodeCount;itrX++) - aSeq.Append(FaceIndices[itrX]); - } - Data->SetValue(itr+1, aSeq); - } - return Standard_True; -} - -//================================================================ -// Function : GetGeomType -// Purpose : -//================================================================ -Standard_Boolean SMESH_MeshVSLink::GetGeomType - ( const Standard_Integer ID, - const Standard_Boolean IsElement, - MeshVS_EntityType& Type ) const -{ - if( IsElement ) { - const SMDS_MeshElement* myElem = myMesh->GetMeshDS()->FindElement(ID); - if (!myElem) return Standard_False; - if (myElem->GetType() == SMDSAbs_Edge) - Type = MeshVS_ET_Link; - else if (myElem->GetType() == SMDSAbs_Face) - Type = MeshVS_ET_Face; - else if (myElem->GetType() == SMDSAbs_Volume) - Type = MeshVS_ET_Volume; - else - Type = MeshVS_ET_Element; - } - else { - const SMDS_MeshNode* myNode = myMesh->GetMeshDS()->FindNode(ID); - if (!myNode) return Standard_False; - if (myNode->GetType() == SMDSAbs_Node) - Type = MeshVS_ET_Node; - else - Type = MeshVS_ET_0D; - } - return Standard_True; -} - -//================================================================ -// Function : GetAddr -// Purpose : -//================================================================ -Standard_Address SMESH_MeshVSLink::GetAddr - ( const Standard_Integer, const Standard_Boolean ) const -{ - return NULL; -} - -//================================================================ -// Function : GetNodesByElement -// Purpose : -//================================================================ -Standard_Boolean SMESH_MeshVSLink::GetNodesByElement - ( const Standard_Integer ID,TColStd_Array1OfInteger& NodeIDs,Standard_Integer& NbNodes ) const -{ - const SMDS_MeshElement* myElem = myMesh->GetMeshDS()->FindElement(ID); - if (!myElem) return Standard_False; - NbNodes = myElem->NbNodes(); - for(Standard_Integer i = 0; i < NbNodes; i++ ) { - const SMDS_MeshNode* aNode = myElem->GetNode(i); - if (!aNode) return Standard_False; - NodeIDs.SetValue(i+1, aNode->GetID()); - } - return Standard_True; -} - -//================================================================ -// Function : GetAllNodes -// Purpose : -//================================================================ -const TColStd_PackedMapOfInteger& SMESH_MeshVSLink::GetAllNodes() const -{ - return myNodes; -} - -//================================================================ -// Function : GetAllElements -// Purpose : -//================================================================ -const TColStd_PackedMapOfInteger& SMESH_MeshVSLink::GetAllElements() const -{ - return myElements; -} - -//================================================================ -// Function : GetAllElements -// Purpose : -//================================================================ -void SMESH_MeshVSLink::GetAllGroups(TColStd_PackedMapOfInteger& Ids) const -{ - Ids = myGroups; -} - -//================================================================ -// Function : GetNormal -// Purpose : -//================================================================ -Standard_Boolean SMESH_MeshVSLink::GetNormal - ( const Standard_Integer Id, const Standard_Integer Max, - Standard_Real& nx, Standard_Real& ny,Standard_Real& nz ) const -{ - if(Max<3) return Standard_False; - const SMDS_MeshElement* myElem = myMesh->GetMeshDS()->FindElement(Id); - if(!myElem) return Standard_False; - if(myElem->NbNodes() < 3) return Standard_False; - gp_XYZ normal; + +} + +//================================================================ +// Function : GetGeom +// Purpose : +//================================================================ +Standard_Boolean SMESH_MeshVSLink::GetGeom + ( const Standard_Integer ID, const Standard_Boolean IsElement, + TColStd_Array1OfReal& Coords, Standard_Integer& NbNodes, + MeshVS_EntityType& Type ) const +{ + if( IsElement ) { + const SMDS_MeshElement* myElem = myMesh->GetMeshDS()->FindElement(ID); + if (!myElem) return Standard_False; + if (myElem->GetType() == SMDSAbs_Edge) + Type = MeshVS_ET_Link; + else if (myElem->GetType() == SMDSAbs_Face) + Type = MeshVS_ET_Face; + else if (myElem->GetType() == SMDSAbs_Volume) + Type = MeshVS_ET_Volume; + else + Type = MeshVS_ET_Element; + NbNodes = myElem->NbNodes(); + int nbCoord = 1; + for(Standard_Integer i = 0; i < NbNodes; i++ ) { + Coords(nbCoord++) = myElem->GetNode(i)->X(); + Coords(nbCoord++) = myElem->GetNode(i)->Y(); + Coords(nbCoord++) = myElem->GetNode(i)->Z(); + } + } + else { + const SMDS_MeshNode* myNode = myMesh->GetMeshDS()->FindNode(ID); + if (!myNode) return Standard_False; + if (myNode->GetType() == SMDSAbs_Node) + Type = MeshVS_ET_Node; + else + Type = MeshVS_ET_0D; + NbNodes = 1; + Coords(1) = myNode->X(); + Coords(2) = myNode->Y(); + Coords(3) = myNode->Z(); + } + return Standard_True; +} + +//================================================================ +// Function : Get3DGeom +// Purpose : +//================================================================ +Standard_Boolean SMESH_MeshVSLink::Get3DGeom + ( const Standard_Integer ID, Standard_Integer& NbNodes, + Handle(MeshVS_HArray1OfSequenceOfInteger)& Data) const +{ + //check validity of element + const SMDS_MeshElement* myVolume = myMesh->GetMeshDS()->FindElement(ID); + if (!myVolume) return Standard_False; + if (myVolume->GetType() != SMDSAbs_Volume) return Standard_False; + + //initialize VolumeTool + SMDS_VolumeTool aTool; + aTool.Set(myVolume); + //set the nodes number + NbNodes = aTool.NbNodes();// myVolume->NbNodes(); + //check validity or create Data + int NbFaces = aTool.NbFaces(); + if (Data.IsNull()) + Data = new MeshVS_HArray1OfSequenceOfInteger(1, NbFaces); + else if (Data->Length() != NbFaces) { + Data.Nullify(); + Data = new MeshVS_HArray1OfSequenceOfInteger(1, NbFaces); + } + //iterate the faces and their nodes and add them to Data + for (int itr=0;itr < NbFaces;itr++) { + int NbThisFaceNodeCount = aTool.NbFaceNodes(itr); + const int *FaceIndices = aTool.GetFaceNodesIndices(itr); + int sortedFaceIndices[MAX_SORT_NODE_COUNT]; + TColStd_SequenceOfInteger aSeq; + if (sortNodes(myVolume, FaceIndices, NbThisFaceNodeCount, sortedFaceIndices)) { + for (int itrX=0;itrX < NbThisFaceNodeCount;itrX++) + aSeq.Append(sortedFaceIndices[itrX]); + } else { + for (int itrX=0;itrX < NbThisFaceNodeCount;itrX++) + aSeq.Append(FaceIndices[itrX]); + } + Data->SetValue(itr+1, aSeq); + } + return Standard_True; +} + +//================================================================ +// Function : GetGeomType +// Purpose : +//================================================================ +Standard_Boolean SMESH_MeshVSLink::GetGeomType + ( const Standard_Integer ID, + const Standard_Boolean IsElement, + MeshVS_EntityType& Type ) const +{ + if( IsElement ) { + const SMDS_MeshElement* myElem = myMesh->GetMeshDS()->FindElement(ID); + if (!myElem) return Standard_False; + if (myElem->GetType() == SMDSAbs_Edge) + Type = MeshVS_ET_Link; + else if (myElem->GetType() == SMDSAbs_Face) + Type = MeshVS_ET_Face; + else if (myElem->GetType() == SMDSAbs_Volume) + Type = MeshVS_ET_Volume; + else + Type = MeshVS_ET_Element; + } + else { + const SMDS_MeshNode* myNode = myMesh->GetMeshDS()->FindNode(ID); + if (!myNode) return Standard_False; + if (myNode->GetType() == SMDSAbs_Node) + Type = MeshVS_ET_Node; + else + Type = MeshVS_ET_0D; + } + return Standard_True; +} + +//================================================================ +// Function : GetAddr +// Purpose : +//================================================================ +Standard_Address SMESH_MeshVSLink::GetAddr + ( const Standard_Integer, const Standard_Boolean ) const +{ + return NULL; +} + +// patches from Mark Blome + +//Standard_Boolean SMESH_MeshVSLink::GetNodeNormal( const Standard_Integer ranknode, const Standard_Integer Id, Standard_Real &nx, Standard_Real &ny, Standard_Real &nz) const +//{ +// return myElements.GetNodeNormal(ranknode-1, Id, nx, ny, nz); +//} +// +//Standard_Boolean SMESH_MeshVSLink::GetNormal( const Standard_Integer Id, const Standard_Integer Max, +// Standard_Real& nx, Standard_Real& ny,Standard_Real& nz ) const +//{ +// if(Max<3) return Standard_False; +// return myElements.GetNormal(Id, nx, ny, nz); +//} + + +//================================================================ +// Function : GetNodesByElement +// Purpose : +//================================================================ +Standard_Boolean SMESH_MeshVSLink::GetNodesByElement + ( const Standard_Integer ID,TColStd_Array1OfInteger& NodeIDs,Standard_Integer& NbNodes ) const +{ + const SMDS_MeshElement* myElem = myMesh->GetMeshDS()->FindElement(ID); + if (!myElem) return Standard_False; + NbNodes = myElem->NbNodes(); + for(Standard_Integer i = 0; i < NbNodes; i++ ) { + const SMDS_MeshNode* aNode = myElem->GetNode(i); + if (!aNode) return Standard_False; + NodeIDs.SetValue(i+1, aNode->GetID()); + } + return Standard_True; +} + +//================================================================ +// Function : GetAllNodes +// Purpose : +//================================================================ +const TColStd_PackedMapOfInteger& SMESH_MeshVSLink::GetAllNodes() const +{ + return myNodes; +} + +//================================================================ +// Function : GetAllElements +// Purpose : +//================================================================ +const TColStd_PackedMapOfInteger& SMESH_MeshVSLink::GetAllElements() const +{ + return myElements; +} + +//================================================================ +// Function : GetAllElements +// Purpose : +//================================================================ +void SMESH_MeshVSLink::GetAllGroups(TColStd_PackedMapOfInteger& Ids) const +{ + Ids = myGroups; +} + + +//================================================================ +// Function : GetNormal +// Purpose : +//================================================================ +Standard_Boolean SMESH_MeshVSLink::GetNormal + ( const Standard_Integer Id, const Standard_Integer Max, + Standard_Real& nx, Standard_Real& ny,Standard_Real& nz ) const +{ + if(Max<3) return Standard_False; + const SMDS_MeshElement* myElem = myMesh->GetMeshDS()->FindElement(Id); + if(!myElem) return Standard_False; + if(myElem->NbNodes() < 3) return Standard_False; + gp_XYZ normal; gp_XYZ nodes[3]; for (int itr = 0;itr < 3;itr++) nodes[itr] = gp_XYZ(myElem->GetNode(itr)->X(), myElem->GetNode(itr)->Y(), myElem->GetNode(itr)->Z()); @@ -334,5 +357,5 @@ Standard_Boolean SMESH_MeshVSLink::GetNormal nx = normal.X(); ny = normal.Y(); nz = normal.Z(); - return Standard_True; -} + return Standard_True; +} diff --git a/src/examples/Concurrency/parallel_slicer.py b/src/examples/Concurrency/parallel_slicer.py index d7367c616..aa6a82d08 100644 --- a/src/examples/Concurrency/parallel_slicer.py +++ b/src/examples/Concurrency/parallel_slicer.py @@ -3,6 +3,7 @@ from OCC.BRepBuilderAPI import * from OCC.Display.SimpleGui import * +display, start_display, add_menu, add_function_to_menu = init_display() import time, numpy, os, sys if sys.version_info[:3] >= (2,6,0): @@ -31,6 +32,7 @@ def vectorized_slicer( li ): _slices.append( section.Shape() ) else: pass + #print 'damn it' section.Destroy() return _slices @@ -40,6 +42,7 @@ def vectorized_slicer( li ): def run( n_procs, compare_by_number_of_processors=False ): shape = get_brep() + display.DisplayShape(shape) x_min, y_min, z_min, x_max, y_max, z_max = get_boundingbox(shape) z_delta = abs( z_min - z_max ) @@ -101,10 +104,6 @@ def arguments(n_slices, n_procs): print '\n\n\n DONE SLICING ON %i CORES \n\n\n'%nprocs time.sleep(3) - # Display result - display, start_display, add_menu, add_function_to_menu = init_display() - print 'displaying original shape' - display.DisplayShape(shape) for n, result_shp in enumerate(_results): print 'displaying results from process {0}'.format(n) display.DisplayShape(result_shp, update=True) diff --git a/src/examples/DYN/dyn_demo.py b/src/examples/DYN/dyn_demo.py index a4e1ed53e..e039c29f8 100644 --- a/src/examples/DYN/dyn_demo.py +++ b/src/examples/DYN/dyn_demo.py @@ -311,7 +311,8 @@ def hinge(event=None): display.EraseAll() # Create the dynamic context dyn_context = DynamicSimulationContext() - dyn_context.set_display(display, safe_yield) + dyn_context.set_display(display, None) + # dyn_context.set_display(display, safe_yield) dyn_context.enable_collision_detection() dyn_context.enable_gravity() # create the table @@ -386,7 +387,8 @@ def adhesion(event=None): display.EraseAll() # Create the dynamic context dyn_context = DynamicSimulationContext() - dyn_context.set_display(display, safe_yield) + # dyn_context.set_display(display, safe_yield) + dyn_context.set_display(display, None) dyn_context.enable_collision_detection() dyn_context.enable_gravity() # create the table @@ -464,5 +466,5 @@ def exit(event=None): add_function_to_menu('rigid body simulation sample', collisions) add_function_to_menu('rigid body simulation sample', hinge) add_function_to_menu('rigid body simulation sample', adhesion) - adhesion() + # adhesion() start_display() diff --git a/src/examples/Display/lightened_shape.py b/src/examples/Display/lightened_shape.py index 2a502bcc8..b3780ed09 100644 --- a/src/examples/Display/lightened_shape.py +++ b/src/examples/Display/lightened_shape.py @@ -22,10 +22,8 @@ from OCC.Graphic3d import * from OCC.BRepPrimAPI import * -from OCC.BRepBuilderAPI import * from OCC.V3d import * from OCC.Quantity import * -from OCC.gp import * # diff --git a/src/examples/Display/qt_demo.py b/src/examples/Display/qt_demo.py index 9529bb86e..2213f5e3c 100644 --- a/src/examples/Display/qt_demo.py +++ b/src/examples/Display/qt_demo.py @@ -17,10 +17,13 @@ ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see . -from OCC.BRepPrimAPI import * from OCC.Display.SimpleGui import * +display, start_display, add_menu, add_function_to_menu = init_display() -display, start_display, add_menu, add_function_to_menu = init_display('pyqt4') +#from OCC.Graphic3d import * +from OCC.BRepPrimAPI import * +#from OCC.Utils.Image import Texture +#import os, os.path def simple_test(event=None): display.Test() @@ -37,4 +40,6 @@ def simple_cylinder(event=None): # # Display settings # +display.View_Iso() +display.FitAll() start_display() diff --git a/src/examples/Display/wx_demo.py b/src/examples/Display/wx_demo.py index c4e34e5b1..fcd00a44f 100644 --- a/src/examples/Display/wx_demo.py +++ b/src/examples/Display/wx_demo.py @@ -20,7 +20,12 @@ from OCC.BRepPrimAPI import * from OCC.Display.SimpleGui import * -display, start_display, add_menu, add_function_to_menu = init_display('wx') +display, start_display, add_menu, add_function_to_menu = init_display() + +#from OCC.Graphic3d import * +from OCC.BRepPrimAPI import * +#from OCC.Utils.Image import Texture +#import os, os.path def simple_test(event=None): display.Test() @@ -37,4 +42,7 @@ def simple_cylinder(event=None): # # Display settings # +display.View_Iso() +display.FitAll() start_display() + diff --git a/src/examples/GEOM/geom.py b/src/examples/GEOM/geom.py index f011dfd7d..541ef1c15 100644 --- a/src/examples/GEOM/geom.py +++ b/src/examples/GEOM/geom.py @@ -1,117 +1,28 @@ -from OCC.XCAFDoc import XCAFDoc_DocumentTool +from OCC.Display.SimpleGui import init_display +from OCC.PAF.TopologyParameterizer import geomobject_from_topods from OCC.Utils.Construct import * from OCC.PAF.Context import Parameters, ParametricModelingContext -from OCC.KBE.base import Display +#=============================================================================== +# the objective here is to make TopoDS_Shape subclasses ( think TopoDS_Vertex ) +# instances available to the parametric +# context, which requires a SGEOM.GEOM_Object, rather than a TopoDS_* instance +#=============================================================================== + +# create TopoDS_Vertex, TopoDS_Edge vertex = make_vertex( gp_Pnt() ) lineA = make_line( gp_Pnt(-1,0,0), gp_Pnt(1,0,0) ) lineB = make_line( gp_Pnt(0,-1,0), gp_Pnt(0,1,0) ) -from itertools import count - -cntr = count() - - - -#GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType) -#: _label(theEntry), _ior(""), _docID(-1) -#{ -# Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data()); -# if(!aDoc.IsNull()) { -# Handle(TDataStd_Integer) anID; -# if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get(); -# } -# -# theEntry.ForgetAllAttributes(Standard_True); -# -# if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root)) -# _root = TDataStd_TreeNode::Set(theEntry); -# -# TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType); -# -# TDataStd_UAttribute::Set(theEntry, GetObjectID()); -#} - - - - -def geomobject_from_topods(topods, pmc): - """ - returns a GEOM_Object that contains a TopoDS_Shape - :param topods: the TopoDS_Shape to add to the GEOM_Object - :param pmc: the parametric modeling context - :return: a GEOM_Object that contains :topods: - """ - # creates a GEOM_Object in the appropriate context - -# def GetObject(self, *args): -# """GetObject(self, int arg0, char theEntry) -> Handle_GEOM_Object""" -# return _SGEOM.GEOM_Engine_GetObject(self, *args) -# -# def AddObject(self, *args): -# """AddObject(self, int arg0, int arg1) -> Handle_GEOM_Object""" -# return _SGEOM.GEOM_Engine_AddObject(self, *args) - - - from OCC.SGEOM import * - - #geomobject = pmc.myEngine.AddObject(pmc.docId,cntr.next()).GetObject() - doc_tool = XCAFDoc_DocumentTool().ShapeTool(pmc.root).GetObject() - shape = TopoDS_Shape(topods) - label = doc_tool.AddShape(shape, False) - tt = GEOM_Object(label, topods.ShapeType()) - assert tt.GetEntry().IsEqual(label) == True - print 'did we fail?', tt.GetValue().IsNull(), ' should be 0' - - # the problem is that GEOM_Object.GetValue() returns the value of the - # this is the value returned by tt.GetValue(), its just an alias for: - # tt.GetLastFunction().GetObject().GetValue() # -> segfaults... - - gf = tt.GetLastFunction().GetObject() # GEOM_Function - gf.IsNull() # yep... - gf.SetValue(shape) # segfault; this is the shape that tt.GetValue() will return! - - tt.GetValue().IsNull() - - if geomobject.GetValue().IsNull(): - raise ValueError('TopoDS_Shape was not set...') - - return tt.GetHandle() +display, start_display, add_menu, add_function_to_menu = init_display() p = Parameters() pmc = ParametricModelingContext(p) pmc.register_operations(pmc.boolean_operations) +pmc.set_display(display) -v, lA, lB = map(lambda x: geomobject_from_topods(x, pmc), (vertex, lineA, lineB)) - -try: - qq = pmc.boolean_operations.MakeBoolean(lA,lB,3) - qqq = qq.GetObject().GetValue() -except Exception, e: - print e - -Display()(qqq) - -# pmc.boolean_operations.MakePartition() - -#Handle_TColStd_HSequenceOfTransient theTools, -#Handle_TColStd_HSequenceOfTransient theKeepInside, -#Handle_TColStd_HSequenceOfTransient theRemoveInside, -#Standard_Integer theLimit, -#Standard_Boolean theRemoveWebs, -#Handle_TColStd_HArray1OfInteger theMaterials, -#Standard_Integer theKeepNonlimitShapes, -#Standard_Boolean thePerformSelfIntersections) -> Handle_GEOM_Object - - -pmc.boolean_operations.MakeBoolean(v,lA, 3) - - +v, lA, lB = map(lambda x: geomobject_from_topods(pmc, x), (vertex, lineA, lineB)) +pmc.boolean_operations.MakeBoolean(lA.GetHandle(),lB.GetHandle(),3, show=True) -#TopoDS_Shape s1 = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), gp_Pnt(10, 30, 20)); -#TopLoc_Location location1, location2; -# -#TDF_Label lab1 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->NewShape(); -#XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->SetShape(lab1, s1); -#TDataStd_Name::Set(lab1, "Box1"); +start_display() diff --git a/src/examples/GEOM/partition.py b/src/examples/GEOM/partition.py index 76e939721..7b79c82a1 100644 --- a/src/examples/GEOM/partition.py +++ b/src/examples/GEOM/partition.py @@ -1,13 +1,8 @@ -__author__ = 'jelle' - -import functools - +from OCC.Display.SimpleGui import init_display from OCC.Utils.Construct import * from OCC.Partition import * from OCC.TopAbs import * -from OCC.TopoDS import * from OCC.Utils.Topology import Topo -from OCC.KBE.base import Display liX = make_line(gp_Pnt(-10,0,0), gp_Pnt(10,0,0)) liY = make_line(gp_Pnt(0,-10,0), gp_Pnt(0,10,0)) @@ -37,16 +32,21 @@ spl.Compute(TopAbs_SHAPE) shp = spl.Shape() -shp = translate_topods_from_vector(shp, gp_Vec(30,0,0)) +# start gui +display, start_display, add_menu, add_function_to_menu = init_display() -disp = Display() -tp = Topo(shp) +# move the results a little such that we can clearly see it +shp = translate_topods_from_vector(shp, gp_Vec(30,0,0)) +# add the result to the viewer, in black +tp = Topo(shp) for i in tp.edges(): - disp(i) -disp((pl1,pl2)) + display.DisplayColoredShape(i, "BLACK") -print spl +display.DisplayShape((pl1,pl2)) +display.DisplayShape((f1,f2)) +display.FitAll() +start_display() diff --git a/src/examples/InteractiveFeatureTree/qt/__init__.py b/src/examples/InteractiveFeatureTree/qt/__init__.py new file mode 100644 index 000000000..ad7a9f511 --- /dev/null +++ b/src/examples/InteractiveFeatureTree/qt/__init__.py @@ -0,0 +1 @@ +__author__ = 'jelleferinga' diff --git a/src/examples/InteractiveFeatureTree/qt/canvas.py b/src/examples/InteractiveFeatureTree/qt/canvas.py new file mode 100644 index 000000000..ad7a9f511 --- /dev/null +++ b/src/examples/InteractiveFeatureTree/qt/canvas.py @@ -0,0 +1 @@ +__author__ = 'jelleferinga' diff --git a/src/examples/InteractiveFeatureTree/wx/__init__.py b/src/examples/InteractiveFeatureTree/wx/__init__.py new file mode 100644 index 000000000..ad7a9f511 --- /dev/null +++ b/src/examples/InteractiveFeatureTree/wx/__init__.py @@ -0,0 +1 @@ +__author__ = 'jelleferinga' diff --git a/src/examples/InteractiveFeatureTree/wx/canvas.py b/src/examples/InteractiveFeatureTree/wx/canvas.py new file mode 100644 index 000000000..ad7a9f511 --- /dev/null +++ b/src/examples/InteractiveFeatureTree/wx/canvas.py @@ -0,0 +1 @@ +__author__ = 'jelleferinga' diff --git a/src/examples/OCAFBrowser/ocaf_browser.py b/src/examples/OCAFBrowser/ocaf_browser.py index 500fd1cc8..03a4fa42d 100644 --- a/src/examples/OCAFBrowser/ocaf_browser.py +++ b/src/examples/OCAFBrowser/ocaf_browser.py @@ -17,10 +17,10 @@ ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see . -from enthought.traits.ui.api import View, Item, TreeEditor, TreeNode,\ +from traitsui.api import View, Item, TreeEditor, TreeNode,\ ListEditor, VGroup -from enthought.traits.api import (HasTraits, Property, Bool, +from traits.api import (HasTraits, Property, Bool, on_trait_change, cached_property, Instance, File, Float as _Float, List, Str, Enum, Int ) diff --git a/src/examples/PAF/test_box_booleans_fillets.py b/src/examples/PAF/test_box_booleans_fillets.py index 692767266..468b872ba 100644 --- a/src/examples/PAF/test_box_booleans_fillets.py +++ b/src/examples/PAF/test_box_booleans_fillets.py @@ -51,16 +51,17 @@ li1 = make_line(gp_Pnt(-10,0,0), gp_Pnt(10,0,0)) li2 = make_line(gp_Pnt(0,-10,0), gp_Pnt(0,10,0)) + +# geometry that are subclassed form TopoDS_Shape ( like TopoDS_Vertex ) +# can be transformed to SGEOM.GEOM_Object +# which is required to work in a parametric context li1_geom = geomobject_from_topods(my_context, li1) li2_geom = geomobject_from_topods(my_context, li2) -yy1 = TColStd_HSequenceOfTransient() -yy2 = TColStd_HSequenceOfTransient() -yy1.Append(li1_geom.GetHandle()) -yy2.Append(li2_geom.GetHandle()) - my_context.boolean_operations.MakeBoolean(li1_geom.GetHandle(),li2_geom.GetHandle(),3, show=True) -pres1, pres2, pres_fillet = my_context.get_presentation(box1), my_context.get_presentation(box2), my_context.get_presentation(fillet_box) +pres1 = my_context.get_presentation(box1) +pres2 = my_context.get_presentation(box2) +pres_fillet = my_context.get_presentation(fillet_box) pres1.SetTransparency(.8); pres1.SetColor(12) pres2.SetTransparency(.8); pres1.SetColor(12) pres_fillet.SetColor(1) diff --git a/src/examples/WebGl/webgl_geomplate.py b/src/examples/WebGl/webgl_geomplate.py index 7a462e6bd..3548a42e6 100644 --- a/src/examples/WebGl/webgl_geomplate.py +++ b/src/examples/WebGl/webgl_geomplate.py @@ -14,7 +14,9 @@ ## ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see . -import os, types, sys, time +import os +import types +import sys from OCC.Utils.Construct import make_closed_polygon, make_n_sided, make_vertex, make_face, make_wire from OCC.gp import * @@ -23,17 +25,12 @@ from OCC.BRep import * from OCC.ShapeAnalysis import * from OCC.GeomLProp import * - from OCC.DataExchange.IGES import IGESImporter from OCC.BRepFill import * from OCC.GeomPlate import * from OCC.Display.WebGl import webgl_renderer +from scipy.optimize import fsolve -try: - from scipy.optimize import fsolve -except ImportError: - print 'scipy not installed, will not be able to run the solve radius example' - sys.exit(0) def geom_plate(event=None): p1,p2,p3,p4,p5 = gp_Pnt(0,0,0),gp_Pnt(0,100,0),gp_Pnt(0,100,100),gp_Pnt(0,0,100),gp_Pnt(50,50,50) diff --git a/src/wrapper/Visualization/Display3d.cpp b/src/wrapper/Visualization/Display3d.cpp index 959b61212..de9bca230 100644 --- a/src/wrapper/Visualization/Display3d.cpp +++ b/src/wrapper/Visualization/Display3d.cpp @@ -44,6 +44,7 @@ void Display3d::Init(long window_handle) #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) gd = new Graphic3d_GraphicDevice(std::getenv("DISPLAY")); printf("OSX - Graphic device created.\n"); + // no support for HiDPI / Retina devices myWindow = new Cocoa_Window (reinterpret_cast(window_handle)); printf("Cocoa window created.\n"); #else