1 # Copyright (C) 2019-2020 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # ======================================================================================
21 # Module adding some methods to CORBA API of GEM_Gen, analogously to geomBuilder.py.
22 # WARNING: do not try to use it in the same way as geomBuilder because
23 # it provides limited functionality, it implements only methods used by smeshBuilder
26 # import shaperBuilder
27 # shaper = shaperBuilder.New()
28 # from salome.smesh import smeshBuilder
29 # smesh = smeshBuilder.New( instanceGeom = shaper )
31 # ======================================================================================
35 import SALOME_DriverPy
37 # Warning: geom is a singleton
43 class shaperBuilder(SHAPERSTUDY.SHAPERSTUDY):
45 ## Enumeration ShapeType as a dictionary
46 ShapeType = SHAPERSTUDY.SHAPERSTUDY.ShapeType
48 def __new__(cls, *args):
54 # geom engine is either retrieved from engine, or created
56 # Following test avoids a recursive loop
59 # geom engine not created: existing engine found
61 if doLcc and not created:
63 # FindOrLoadComponent called:
64 # 1. CORBA resolution of server
65 # 2. the __new__ method is called again
66 from SHAPERSTUDY_utils import getLCC
67 geom = getLCC().FindOrLoadComponent( "FactoryServer", "SHAPERSTUDY" )
69 # FindOrLoadComponent not called
71 # geomBuilder instance is created from lcc.FindOrLoadComponent
72 geom = super(shaperBuilder,cls).__new__(cls)
74 # geom engine not created: existing engine found
80 def __init__(self, *args):
84 GEOM._objref_GEOM_Gen.__init__(self, *args)
85 from SHAPERSTUDY_utils import moduleName
86 SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, moduleName())
90 self.ShapesOp = self.GetIShapesOperations()
96 self.MeasuOp = self.GetIMeasureOperations()
98 self.GroupOp = self.GetIGroupOperations()
105 def CreateGroup(self, theMainShape, theShapeType, theName=None):
107 Creates a new group which will store sub-shapes of theMainShape
109 # used in Mesh.GetFailedShapes()
110 anObj = self.GroupOp.CreateGroup(theMainShape, theShapeType)
111 RaiseIfFailed("CreateGroup", self.GroupOp)
114 def ExtractShapes(self, aShape, aType, isSorted = False, theName=None):
116 Extract shapes (excluding the main shape) of given type.
118 ListObj = self.ShapesOp.ExtractSubShapes(aShape, EnumToLong( aType ), isSorted)
119 RaiseIfFailed("ExtractSubShapes", self.ShapesOp)
122 def GetSubShape(self, aShape, ListOfID, theName=None):
124 Obtain a composite sub-shape of aShape, composed from sub-shapes
125 of aShape, selected by their unique IDs inside aShape
127 # used in Mesh.GetFailedShapes() and Mesh.GetSubShapeName() to get a sub-shape by index
128 anObj = self.ShapesOp.GetSubShape(aShape,ListOfID[0])
131 def GetSubShapeID(self, aShape, aSubShape):
133 Obtain unique ID of sub-shape aSubShape inside aShape
134 of aShape, selected by their unique IDs inside aShape
136 anID = self.ShapesOp.GetSubShapeIndex(aShape, aSubShape)
137 RaiseIfFailed("GetSubShapeIndex", self.ShapesOp)
140 def MinDistance(self, theVertex1, theVertex2):
142 Get minimal distance between the given vertices.
144 # used in Mesh_Algorithm.ReversedEdgeIndices() to get distance between two vertices
145 aTuple = self.MeasuOp.GetMinDistance(theVertex1, theVertex2)
146 RaiseIfFailed("GetMinDistance", self.MeasuOp)
149 def NumberOfEdges(self, theShape):
151 Gives quantity of edges in the given shape.
153 # used in Mesh.MeshDimension() to find out presence of edges in theShape
154 nb_edges = self.ShapesOp.NumberOfEdges(theShape)
155 RaiseIfFailed("NumberOfEdges", self.ShapesOp)
158 def NumberOfFaces(self, theShape):
160 Gives quantity of faces in the given shape.
162 # used in Mesh.MeshDimension() to find out presence of faces in theShape
163 nb_faces = self.ShapesOp.NumberOfFaces(theShape)
164 RaiseIfFailed("NumberOfFaces", self.ShapesOp)
167 def PointCoordinates(self,Point):
169 Get point coordinates
171 aTuple = self.MeasuOp.PointCoordinates(Point)
172 RaiseIfFailed("PointCoordinates", self.MeasuOp)
175 def SubShapeAllIDs(self, aShape, aType):
177 Explode a shape on sub-shapes of a given type.
179 ListObj = self.ShapesOp.GetAllSubShapesIDs(aShape, EnumToLong( aType ), False)
180 RaiseIfFailed("SubShapeAllIDs", self.ShapesOp)
183 def SubShapeAll(self, aShape, aType, theName=None):
185 Explode a shape on sub-shapes of a given type.
186 If the shape itself matches the type, it is also returned.
188 ListObj = self.ShapesOp.ExtractSubShapes(aShape, EnumToLong( aType ), False)
189 RaiseIfFailed("SubShapeAll", self.ShapesOp)
192 def SubShapeName(self,aSubObj, aMainObj):
194 Get name for sub-shape aSubObj of shape aMainObj
196 index = self.ShapesOp.GetTopologyIndex(aMainObj, aSubObj)
197 name = self.ShapesOp.GetShapeTypeString(aSubObj) + "_%d"%(index)
200 def Tolerance(self,theShape):
202 Get min and max tolerances of sub-shapes of theShape
204 # used in Mesh_Algorithm.ReversedEdgeIndices() to get tolerance of a vertex
205 aTuple = self.MeasuOp.GetTolerance(theShape)
206 RaiseIfFailed("GetTolerance", self.MeasuOp)
209 def GetVertexByIndex(self, theShape, theIndex, theUseOri=True, theName=None):
211 Get a vertex sub-shape by index.
213 # used in Mesh_Algorithm.ReversedEdgeIndices()
214 anObj = self.MeasuOp.GetVertexByIndex(theShape, theIndex, theUseOri)
215 RaiseIfFailed("GetVertexByIndex", self.MeasuOp)
218 def UnionList (self,theGroup, theSubShapes):
220 Adds to the group all the given shapes. No errors, if some shapes are already included.
222 # used in Mesh.GetFailedShapes()
223 self.GroupOp.UnionList(theGroup, theSubShapes)
224 RaiseIfFailed("UnionList", self.GroupOp)
227 def addToStudy(self, aShape, aName, doRestoreSubShapes=False,
228 theArgs=[], theFindMethod=None, theInheritFirstArg=False):
230 Publish in study aShape with name aName
232 # used to publish Mesh.geom and shapes that are filter criteria
235 from SHAPERSTUDY_utils import getEngine
236 aSObject = getEngine().AddInStudy(aShape, aName, None)
237 if aSObject and aName: aSObject.SetAttrString("AttributeName", aName)
239 print("addToStudy() failed")
241 return aShape.GetStudyEntry()
243 def addToStudyInFather(self, aFather, aShape, aName):
245 Publish in study aShape with name aName as sub-object of previously published aFather
249 from SHAPERSTUDY_utils import getEngine
250 aSObject = getEngine().AddInStudy(aShape, aName, aFather)
251 if aSObject and aName: aSObject.SetAttrString("AttributeName", aName)
253 print("addToStudy() failed")
255 return aShape.GetStudyEntry()
258 omniORB.registerObjref(SHAPERSTUDY.SHAPERSTUDY._NP_RepositoryId, shaperBuilder)
259 omniORB.registerObjref(GEOM._objref_GEOM_Gen._NP_RepositoryId, shaperBuilder)
261 def New( instance=None ):
263 Create a new shaperBuilder instance
271 geom = shaperBuilder()
272 assert isinstance(geom,shaperBuilder), "Geom engine class is %s but should be shaperBuilder.shaperBuilder. Import geomBuilder before creating the instance."%geom.__class__
277 ## Raise an Error, containing the Method_name, if Operation failed
278 ## @ingroup l1_geomBuilder_auxiliary
279 def RaiseIfFailed (Method_name, Operation):
280 if not Operation.IsDone() and Operation.GetErrorCode() != "NOT_FOUND_ANY":
281 raise RuntimeError(Method_name + " : " + Operation.GetErrorCode())
283 def EnumToLong(theItem):
285 Returns a long value from enumeration type
286 Can be used for CORBA enumerator types like geomBuilder.ShapeType
289 theItem enumeration type
292 if hasattr(theItem, "_v"): ret = theItem._v