Salome HOME
Copyright update 2021
[modules/shaper_study.git] / src / PY / shaperBuilder.py
1 # Copyright (C) 2019-2021  CEA/DEN, EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
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
24 #
25 # Typical use is:
26 #     import shaperBuilder
27 #     shaper = shaperBuilder.New()
28 #     from salome.smesh import smeshBuilder
29 #     smesh = smeshBuilder.New( instanceGeom = shaper )
30 #
31 # ======================================================================================
32
33 import SHAPERSTUDY
34 import GEOM
35 import SALOME_DriverPy
36
37 # Warning: geom is a singleton
38 geom = None
39 engine = None
40 doLcc = False
41 created = False
42
43 class shaperBuilder(SHAPERSTUDY.SHAPERSTUDY):
44
45     ## Enumeration ShapeType as a dictionary
46     ShapeType = SHAPERSTUDY.SHAPERSTUDY.ShapeType
47
48     def __new__(cls, *args):
49         global engine
50         global geom
51         global doLcc
52         global created
53         if geom is None:
54             # geom engine is either retrieved from engine, or created
55             geom = engine
56             # Following test avoids a recursive loop
57             if doLcc:
58                 if geom is not None:
59                     # geom engine not created: existing engine found
60                     doLcc = False
61                 if doLcc and not created:
62                     doLcc = False
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" )
68             else:
69                 # FindOrLoadComponent not called
70                 if geom is None:
71                     # geomBuilder instance is created from lcc.FindOrLoadComponent
72                     geom = super(shaperBuilder,cls).__new__(cls)
73                 else:
74                     # geom engine not created: existing engine found
75                     pass
76             return geom
77
78         return geom
79
80     def __init__(self, *args):
81         global created
82         if not created:
83             created = True
84             GEOM._objref_GEOM_Gen.__init__(self, *args)
85             from SHAPERSTUDY_utils import moduleName
86             SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, moduleName())
87             self.BasicOp  = None
88             self.CurvesOp = None
89             self.PrimOp   = None
90             self.ShapesOp = self.GetIShapesOperations()
91             self.HealOp   = None
92             self.InsertOp = None
93             self.BoolOp   = None
94             self.TrsfOp   = None
95             self.LocalOp  = None
96             self.MeasuOp  = self.GetIMeasureOperations()
97             self.BlocksOp = None
98             self.GroupOp  = self.GetIGroupOperations()
99             self.FieldOp  = None
100             pass
101
102     def init_geom(self):
103         return
104
105     def CreateGroup(self, theMainShape, theShapeType, theName=None):
106         """
107         Creates a new group which will store sub-shapes of theMainShape
108         """
109         # used in Mesh.GetFailedShapes()
110         anObj = self.GroupOp.CreateGroup(theMainShape, theShapeType)
111         RaiseIfFailed("CreateGroup", self.GroupOp)
112         return anObj
113
114     def ExtractShapes(self, aShape, aType, isSorted = False, theName=None):
115         """
116         Extract shapes (excluding the main shape) of given type.
117         """
118         ListObj = self.ShapesOp.ExtractSubShapes(aShape, EnumToLong( aType ), isSorted)
119         RaiseIfFailed("ExtractSubShapes", self.ShapesOp)
120         return ListObj
121
122     def GetSubShape(self, aShape, ListOfID, theName=None):
123         """
124         Obtain a composite sub-shape of aShape, composed from sub-shapes
125         of aShape, selected by their unique IDs inside aShape
126         """
127         # used in Mesh.GetFailedShapes() and Mesh.GetSubShapeName() to get a sub-shape by index
128         anObj = self.ShapesOp.GetSubShape(aShape,ListOfID[0])
129         return anObj
130
131     def GetSubShapeID(self, aShape, aSubShape):
132         """
133         Obtain unique ID of sub-shape aSubShape inside aShape
134         of aShape, selected by their unique IDs inside aShape
135         """
136         anID = self.ShapesOp.GetSubShapeIndex(aShape, aSubShape)
137         RaiseIfFailed("GetSubShapeIndex", self.ShapesOp)
138         return anID
139
140     def MinDistance(self, theVertex1, theVertex2):
141         """
142         Get minimal distance between the given vertices.
143         """
144         # used in Mesh_Algorithm.ReversedEdgeIndices() to get distance between two vertices
145         aTuple = self.MeasuOp.GetMinDistance(theVertex1, theVertex2)
146         RaiseIfFailed("GetMinDistance", self.MeasuOp)
147         return aTuple[0]
148
149     def NumberOfEdges(self, theShape):
150         """
151         Gives quantity of edges in the given shape.
152         """
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)
156         return nb_edges
157
158     def NumberOfFaces(self, theShape):
159         """
160         Gives quantity of faces in the given shape.
161         """
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)
165         return nb_faces
166
167     def PointCoordinates(self,Point):
168         """
169         Get point coordinates
170         """
171         aTuple = self.MeasuOp.PointCoordinates(Point)
172         RaiseIfFailed("PointCoordinates", self.MeasuOp)
173         return aTuple
174
175     def SubShapeAllIDs(self, aShape, aType):
176         """
177         Explode a shape on sub-shapes of a given type.
178         """
179         ListObj = self.ShapesOp.GetAllSubShapesIDs(aShape, EnumToLong( aType ), False)
180         RaiseIfFailed("SubShapeAllIDs", self.ShapesOp)
181         return ListObj
182
183     def SubShapeAll(self, aShape, aType, theName=None):
184         """
185         Explode a shape on sub-shapes of a given type.
186         If the shape itself matches the type, it is also returned.
187         """
188         ListObj = self.ShapesOp.ExtractSubShapes(aShape, EnumToLong( aType ), False)
189         RaiseIfFailed("SubShapeAll", self.ShapesOp)
190         return ListObj
191
192     def SubShapeName(self,aSubObj, aMainObj):
193         """
194         Get name for sub-shape aSubObj of shape aMainObj
195         """
196         index = self.ShapesOp.GetTopologyIndex(aMainObj, aSubObj)
197         name = self.ShapesOp.GetShapeTypeString(aSubObj) + "_%d"%(index)
198         return name
199
200     def Tolerance(self,theShape):
201         """
202         Get min and max tolerances of sub-shapes of theShape
203         """
204         # used in Mesh_Algorithm.ReversedEdgeIndices() to get tolerance of a vertex
205         aTuple = self.MeasuOp.GetTolerance(theShape)
206         RaiseIfFailed("GetTolerance", self.MeasuOp)
207         return aTuple
208
209     def GetVertexByIndex(self, theShape, theIndex, theUseOri=True, theName=None):
210         """
211         Get a vertex sub-shape by index.
212         """
213         # used in Mesh_Algorithm.ReversedEdgeIndices()
214         anObj = self.MeasuOp.GetVertexByIndex(theShape, theIndex, theUseOri)
215         RaiseIfFailed("GetVertexByIndex", self.MeasuOp)
216         return anObj
217
218     def UnionList (self,theGroup, theSubShapes):
219         """
220         Adds to the group all the given shapes. No errors, if some shapes are already included.
221         """
222         # used in Mesh.GetFailedShapes()
223         self.GroupOp.UnionList(theGroup, theSubShapes)
224         RaiseIfFailed("UnionList", self.GroupOp)
225         pass
226
227     def addToStudy(self, aShape, aName, doRestoreSubShapes=False,
228                    theArgs=[], theFindMethod=None, theInheritFirstArg=False):
229         """
230         Publish in study aShape with name aName
231         """
232         # used to publish Mesh.geom and shapes that are filter criteria
233         #return ""
234         try:
235             from SHAPERSTUDY_utils import getEngine
236             aSObject = getEngine().AddInStudy(aShape, aName, None)
237             if aSObject and aName: aSObject.SetAttrString("AttributeName", aName)
238         except:
239             print("addToStudy() failed")
240             return ""
241         return aShape.GetStudyEntry()
242
243     def addToStudyInFather(self, aFather, aShape, aName):
244         """
245         Publish in study aShape with name aName as sub-object of previously published aFather
246         """
247         #return ""
248         try:
249             from SHAPERSTUDY_utils import getEngine
250             aSObject = getEngine().AddInStudy(aShape, aName, aFather)
251             if aSObject and aName: aSObject.SetAttrString("AttributeName", aName)
252         except:
253             print("addToStudy() failed")
254             return ""
255         return aShape.GetStudyEntry()
256
257 import omniORB
258 omniORB.registerObjref(SHAPERSTUDY.SHAPERSTUDY._NP_RepositoryId, shaperBuilder)
259 omniORB.registerObjref(GEOM._objref_GEOM_Gen._NP_RepositoryId, shaperBuilder)
260
261 def New( instance=None ):
262     """
263     Create a new shaperBuilder instance
264     """
265     global engine
266     global geom
267     global doLcc
268     engine = instance
269     if engine is None:
270       doLcc = True
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__
273     #geom.init_geom()
274     return geom
275
276
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())
282
283 def EnumToLong(theItem):
284     """
285     Returns a long value from enumeration type
286     Can be used for CORBA enumerator types like geomBuilder.ShapeType
287
288     Parameters:
289         theItem enumeration type
290     """
291     ret = theItem
292     if hasattr(theItem, "_v"): ret = theItem._v
293     return ret
294