1 # Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 import SHAPERSTUDY_ORB__POA
24 import SHAPERSTUDY_ORB
25 import SALOME_ComponentPy
26 import SALOME_DriverPy
28 from SHAPERSTUDY_utils import findOrCreateComponent, moduleName, getStudy, getORB
30 import SHAPERSTUDY_Object
31 import SHAPERSTUDY_IOperations
38 class SHAPERSTUDY(SHAPERSTUDY_ORB__POA.Gen,
39 SALOME_ComponentPy.SALOME_ComponentPy_i,
40 SALOME_DriverPy.SALOME_DriverPy_i):
43 ShapeType = {"AUTO":-1, "COMPOUND":0, "COMPSOLID":1, "SOLID":2, "SHELL":3, "FACE":4, "WIRE":5, "EDGE":6, "VERTEX":7, "SHAPE":8, "FLAT":9}
45 ShaperIcons = {GEOM.COMPOUND:"SHAPER_ICON_COMPSOLID",
46 GEOM.COMPSOLID:"SHAPER_ICON_COMPSOLID",
47 GEOM.SOLID:"SHAPER_ICON_SOLID",
48 GEOM.SHELL:"SHAPER_ICON_SHELL",
49 GEOM.FACE:"SHAPER_ICON_FACE",
50 GEOM.WIRE:"SHAPER_ICON_WIRE",
51 GEOM.EDGE:"SHAPER_ICON_EDGE",
52 GEOM.VERTEX:"SHAPER_ICON_VERTEX",
53 GEOM.SHAPE:"SHAPER_ICON_SOLID",
54 GEOM.FLAT:"SHAPER_ICON_FACE"
57 def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ):
59 Construct an instance of SHAPERSTUDY module engine.
60 The class SHAPERSTUDY implements CORBA interface Gen (see SHAPERSTUDY_Gen.idl).
61 It is inherited (via GEOM_Gen) from the classes SALOME_ComponentPy_i (implementation of
62 Engines::EngineComponent CORBA interface - SALOME component) and SALOME_DriverPy_i
63 (implementation of SALOMEDS::Driver CORBA interface - SALOME module's engine).
65 SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
66 contID, containerName, instanceName, interfaceName, False)
67 SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
69 #self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
73 def FindOrCreateShape( self, theInternalEntry ):
75 Searches existing or creates a new SHAPERSTUDY_Object to interact with SHAPER
77 # Searching in the study tree
78 aComponent = findOrCreateComponent()
79 aSOIter = getStudy().NewChildIterator(aComponent)
83 anObj = salome.orb.string_to_object(anIOR)
84 if isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
85 if anObj.GetEntry() == theInternalEntry:
89 aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
90 aShapeObj.SetEntry(theInternalEntry)
91 return aShapeObj._this()
93 def AddInStudy( self, theObject, theName, theFather ):
95 Adds in theStudy a object theObject under theFather with a name theName,
96 if theFather is not NULL the object is placed under theFather's SObject.
97 Returns a SObject where theObject is placed
100 aBuilder = aStudy.NewBuilder()
101 isGroup = theObject.GetType() == 37 or theObject.GetType() == 52
104 return None # Group may be added only under the shape-father
105 theFather = findOrCreateComponent()
107 if isGroup: # add group to the third sub-label or later to keep space for reference and "History"
109 anIter = aStudy.NewChildIterator(theFather)
111 aCurrentTag = anIter.Value().Tag() + 1
112 if aTag < aCurrentTag:
115 aResultSO = aBuilder.NewObjectToTag(theFather, aTag)
117 aResultSO = aBuilder.NewObject(theFather);
118 aResultSO.SetAttrString("AttributeName", theName)
119 if theObject is not None:
120 anIOR = salome.orb.object_to_string(theObject)
121 aResultSO.SetAttrString("AttributeIOR", anIOR)
122 theObject.SetSO(aResultSO)
124 aAttr = aBuilder.FindOrCreateAttribute(aResultSO, "AttributePixMap")
125 aPixmap = aAttr._narrow(salome.SALOMEDS.AttributePixMap)
128 aType = SHAPERSTUDY_Object.__shape_types__[theObject.GetSelectionType()]
130 aType = theObject.GetShapeType()
131 aPixmap.SetPixMap(SHAPERSTUDY.ShaperIcons[aType])
133 # add a red-reference that means that this is an active reference to SHAPER result
135 aSub = aBuilder.NewObjectToTag(aResultSO, 1)
136 aBuilder.Addreference(aSub, aResultSO)
140 def AddSubShape( theMainShape, theIndices ):
142 Add a sub-shape defined by indices in theIndices
143 (contains unique IDs of sub-shapes inside theMainShape)
145 # no sub-shapes for the moment
146 go = SHAPERSTUDY_Object()._this()
149 # For now it is impossible to remove anything from the SHAPER-STUDY
150 def RemoveObject( self, theObject ):
152 Removes the object from the component
154 # can not be removed for the moment
157 def GetIFieldOperations( self ):
160 return SHAPERSTUDY_IOperations.SHAPERSTUDY_IFieldOperations()._this()
162 def GetIGroupOperations( self ):
165 return SHAPERSTUDY_IOperations.SHAPERSTUDY_IGroupOperations()._this()
167 def GetIShapesOperations( self ):
170 return SHAPERSTUDY_IOperations.SHAPERSTUDY_IShapesOperations()._this()
172 def GetIMeasureOperations( self ):
175 return SHAPERSTUDY_IOperations.SHAPERSTUDY_IMeasureOperations()._this()
177 def GetStringFromIOR( self, theObject ):
179 Returns a string which contains an IOR of the SHAPERSTUDY_Object
182 if theObject and getORB():
183 IOR = getORB().object_to_string( theObject )
187 def GetAllDumpNames( self ):
189 Returns all names with which Object's was dumped
190 into python script to avoid the same names in SMESH script
194 def GetDumpName( self, theStudyEntry ):
196 Returns a name with which a GEOM_Object was dumped into python script
199 theStudyEntry is an entry of the Object in the study
203 def Save( self, component, URL, isMultiFile ):
205 Saves data: all objects into one file
207 aResult = "" # string-pairs of internal entries and shape streams
209 # get all sub-SObjects with IOR defined
210 anIters = [aStudy.NewChildIterator(findOrCreateComponent())]
213 aLast = anIters[len(anIters) - 1]
219 anIters.append(aStudy.NewChildIterator(aSO))
222 anIters.remove(aLast)
224 for aSO in aSOList: # for each sobject export shapes stream if exists
226 anObj = salome.orb.string_to_object(anIOR)
227 if isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Group):
229 aResult = aResult + '|'
230 # store internal entry, type and list of indices of the group selection (separated by spaces)
231 aResult = aResult + anObj.GetEntry() + "|" + str(anObj.GetSelectionType())
232 aSelList = anObj.GetSelection()
233 aResult = aResult + "|" + str(' '.join(str(anI) for anI in aSelList))
234 elif isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
236 aResult = aResult + '|'
237 # store internal entry, current and old shapes in BRep format
238 aResult = aResult + anObj.GetEntry() + "|" + anObj.GetShapeStream().decode()
239 aResult = aResult + "|" + anObj.GetOldShapeStream().decode()
241 return aResult.encode()
243 def Load( self, component, stream, URL, isMultiFile ):
248 __entry2IOR__.clear()
249 aList=stream.decode().split('|')
258 aNewShapeStream = aSub
260 else: # create objects by 3 arguments
262 if anId.startswith('group'): # group object
263 anObj = SHAPERSTUDY_Object.SHAPERSTUDY_Group()
264 if len(aNewShapeStream):
265 anObj.SetSelection([int(anI) for anI in aSub.split(' ')])
266 anObj.SetSelectionType(int(aNewShapeStream))
267 else: # shape object by BRep in the stream: set old first then new
268 anObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
270 anObj.SetShapeByStream(aSub)
271 anObj.SetShapeByStream(aNewShapeStream)
274 anIOR = salome.orb.object_to_string(anObj._this())
275 __entry2IOR__[anId] = anIOR
279 def IORToLocalPersistentID(self, sobject, IOR, isMultiFile, isASCII):
281 Gets persistent ID for the CORBA object.
282 The internal entry of the Object is returned.
284 anObj = salome.orb.string_to_object(IOR)
285 if anObj and isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
286 return anObj.GetEntry()
289 def LocalPersistentIDToIOR(self, sobject, persistentID, isMultiFile, isASCII):
290 "Converts persistent ID of the object to its IOR."
292 if persistentID in __entry2IOR__:
293 aRes = __entry2IOR__[persistentID]
294 if len(aRes): # set SO from the study, the sobject param is temporary, don't store it
295 salome.orb.string_to_object(aRes).SetSO(getStudy().FindObjectID(sobject.GetID()))
299 def DumpPython( self, isPublished, isMultiFile ):
301 Dump module data to the Python script.
303 return ("".encode(), 1)
307 def CreateGroup( self, theMainShape, theShapeType ):
309 Creates a new group which will store sub-shapes of theMainShape
311 return GetIGroupOperations().CreateGroup( theMainShape, theShapeType );
313 def ExtractShapes( self, aShape, aType, isSorted = False ):
315 Extract shapes (excluding the main shape) of given type.
319 aType The shape type (see geompy.ShapeType)
320 isSorted Boolean flag to switch sorting on/off.
323 List of sub-shapes of type aType, contained in aShape.
325 return [ SHAPERSTUDY_Object()._this() ]
327 def GetSubShape( self, aShape, ListOfID ):
329 Obtain a composite sub-shape of aShape, composed from sub-shapes
330 of aShape, selected by their unique IDs inside aShape
333 aShape Shape to get sub-shape of.
334 ListOfID List of sub-shapes indices.
336 return SHAPERSTUDY_Object()._this()
338 def GetSubShapeID( self, aShape, aSubShape ):
340 Obtain unique ID of sub-shape aSubShape inside aShape
341 of aShape, selected by their unique IDs inside aShape
344 aShape Shape to get sub-shape of.
345 aSubShape Sub-shapes of aShape.
349 def MinDistance( self, theShape1, theShape2 ):
351 Get minimal distance between the given shapes.
355 def NumberOfEdges( self, theShape ):
357 Gives quantity of edges in the given shape.
361 def NumberOfFaces( self, ):
363 Gives quantity of faces in the given shape.
367 def PointCoordinates( self, theVertex ):
369 Get point coordinates
373 def SubShapeAll( self, aShape, aType ):
375 Explode a shape on sub-shapes of a given type.
376 If the shape itself matches the type, it is also returned.
378 return [ SHAPERSTUDY_Object()._this() ]
380 def SubShapeName( self, aSubObj, aMainObj ):
382 Get name for sub-shape aSubObj of shape aMainObj
386 def SubShapes( self, aShape, anIDs ):
388 Get a set of sub-shapes defined by their unique IDs inside theMainShape
390 return [ SHAPERSTUDY_Object()._this() ]
392 def Tolerance( self, theShape ):
394 Get min and max tolerances of sub-shapes of theShape
397 [FaceMin,FaceMax, EdgeMin,EdgeMax, VertMin,VertMax]
399 return [0,0, 0,0, 0,0]
401 def UnionList( self, theGroup, theSubShapes ):
403 Adds to the group all the given shapes. No errors, if some shapes are already included.
405 return GetIGroupOperations().UnionList( theGroup, theSubShapes )
407 def IsFather(theFather, theChild):
409 Returns true if theChild SObject is a child of theFather SObject
411 aChild = theChild.GetFather()
412 while aChild.Depth() > theFather.Depth():
413 aChild = aChild.GetFather()
414 return aChild.GetID() == theFather.GetID()
416 def BreakLink(self, theEntry):
418 Breaks links to not-dead shape, make the shape as dead
421 aSO = aStudy.FindObjectID(theEntry)
424 aRes, aSSO = aSO.ReferencedObject()
426 return # only SObjects referenced to the SHAPEr STUDY objects are allowed
427 anIOR = aSSO.GetIOR()
429 return # must be referenced to the SHAPER STUDY shape
430 anObj = salome.orb.string_to_object(anIOR)
431 if not anObj or not isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
434 return # do nothing for reference to already dead shape
435 aDeadShape = anObj.MakeDead()
437 # Replace shape object in the parent mesh
438 aMeshSObject = aSO.GetFather()
439 aMeshObject = aMeshSObject.GetObject()
440 aMeshObject.ReplaceShape(aDeadShape)
442 aBuilder = aStudy.NewBuilder()
443 aBuilder.RemoveReference(aSO) # reset reference to the dead shape
444 aBuilder.Addreference(aSO, aDeadShape.GetSO())
446 # check also sub-structure of the mesh to find references to sub-objects that become dead
447 aRoot = aSO.GetFather()
448 anIters = [aStudy.NewChildIterator(aRoot)]
451 aLast = anIters[len(anIters) - 1]
454 aRes, aSubRef = aSub.ReferencedObject()
455 if aRes and SHAPERSTUDY.IsFather(aSSO, aSubRef):
456 aReferenced = aSubRef.GetObject()
457 if aReferenced and not aReferenced.IsDead():
458 aSubList.append(aSub)
459 anIters.append(aStudy.NewChildIterator(aSub))
462 anIters.remove(aLast)
464 # associate the number of sub-objects of the referenced objects
465 aMapSubEntryToIndex = {}
466 aSSOIter = aStudy.NewChildIterator(aSSO)
468 while aSSOIter.More():
469 aSub = aSSOIter.Value()
471 aMapSubEntryToIndex[aSub.GetID()] = anIndex
472 anIndex = anIndex + 1
474 for aSubSO in aSubList:
475 aRes, aSubRef = aSubSO.ReferencedObject()
476 if aRes and aSubRef.GetID() in aMapSubEntryToIndex:
477 anIndex = aMapSubEntryToIndex[aSubRef.GetID()]
478 aDeadIter = aStudy.NewChildIterator(aDeadShape.GetSO())
479 while aDeadIter.More(): # iterate dead subs to find object with the same index
480 aDeadSubSO = aDeadIter.Value()
481 if aDeadSubSO.GetIOR():
482 anIndex = anIndex - 1
484 # for a submesh there is no ReplaceShape, but the shape is not updated
485 # anyway, so no need to update it here
486 #aSubMeshSO = aSubSO.GetFather() # Replace shape object in the parent mesh
487 #aSubMeshObject = aSubMeshSO.GetObject()
489 # aSubMeshObject.ReplaceShape(aDeadSubSO.GetObject())
490 aBuilder.RemoveReference(aSubSO) # reset reference to the dead shape
491 aBuilder.Addreference(aSubSO, aDeadSubSO)