]> SALOME platform Git repositories - modules/shaper_study.git/blob - src/PY/SHAPERSTUDY.py
Salome HOME
Implementation of python dump of SHAPER STUDY for dead fields using XAO file format.
[modules/shaper_study.git] / src / PY / SHAPERSTUDY.py
1 # Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
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.
10 #
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.
15 #
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
19 #
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22
23 import SHAPERSTUDY_ORB__POA
24 import SHAPERSTUDY_ORB
25 import SALOME_ComponentPy
26 import SALOME_DriverPy
27 import SALOMEDS
28 from SHAPERSTUDY_utils import findOrCreateComponent, moduleName, getStudy, getORB
29 import salome
30 import SHAPERSTUDY_Object
31 import SHAPERSTUDY_IOperations
32 import GEOM
33 import SMESH
34
35 import StudyData_Swig
36
37 __entry2IOR__ = {}
38 __entry2DumpName__ = {}
39
40 class SHAPERSTUDY(SHAPERSTUDY_ORB__POA.Gen,
41                   SALOME_ComponentPy.SALOME_ComponentPy_i,
42                   SALOME_DriverPy.SALOME_DriverPy_i):
43
44
45     ShapeType = {"AUTO":-1, "COMPOUND":0, "COMPSOLID":1, "SOLID":2, "SHELL":3, "FACE":4, "WIRE":5, "EDGE":6, "VERTEX":7, "SHAPE":8, "FLAT":9}
46     
47     ShaperIcons = {GEOM.COMPOUND:"SHAPER_ICON_COMPSOLID",
48         GEOM.COMPSOLID:"SHAPER_ICON_COMPSOLID",
49         GEOM.SOLID:"SHAPER_ICON_SOLID",
50         GEOM.SHELL:"SHAPER_ICON_SHELL",
51         GEOM.FACE:"SHAPER_ICON_FACE",
52         GEOM.WIRE:"SHAPER_ICON_WIRE",
53         GEOM.EDGE:"SHAPER_ICON_EDGE",
54         GEOM.VERTEX:"SHAPER_ICON_VERTEX",
55         GEOM.SHAPE:"SHAPER_ICON_SOLID",
56         GEOM.FLAT:"SHAPER_ICON_FACE"
57         }
58
59     def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ):
60         """
61         Construct an instance of SHAPERSTUDY module engine.
62         The class SHAPERSTUDY implements CORBA interface Gen (see SHAPERSTUDY_Gen.idl).
63         It is inherited (via GEOM_Gen) from the classes SALOME_ComponentPy_i (implementation of
64         Engines::EngineComponent CORBA interface - SALOME component) and SALOME_DriverPy_i
65         (implementation of SALOMEDS::Driver CORBA interface - SALOME module's engine).
66         """
67         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
68                     contID, containerName, instanceName, interfaceName, False)
69         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
70         #
71         #self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
72         #
73         pass
74
75     def FindOrCreateShape( self, theInternalEntry ):
76         """
77         Searches existing or creates a new SHAPERSTUDY_Object to interact with SHAPER
78         """
79         # Searching in the study tree
80         aComponent = findOrCreateComponent()
81         aSOIter = getStudy().NewChildIterator(aComponent)
82         while aSOIter.More():
83           aSO = aSOIter.Value()
84           anIOR = aSO.GetIOR()
85           anObj = salome.orb.string_to_object(anIOR)
86           if isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
87             if anObj.GetEntry() == theInternalEntry:
88               return anObj
89           aSOIter.Next()
90
91         aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
92         aShapeObj.SetEntry(theInternalEntry)
93         return aShapeObj._this()
94
95     def AddInStudy( self, theObject, theName, theFather ):
96         """
97         Adds in theStudy a object theObject under theFather with a name theName,
98         if theFather is not NULL the object is placed under theFather's SObject.
99         Returns a SObject where theObject is placed
100         """
101         if not theObject.GetEntry():
102             return None # object not existing in shaper
103         aStudy = getStudy()
104         aBuilder = aStudy.NewBuilder()
105         isGroup = theObject.GetType() == 37 or theObject.GetType() == 52
106         if not theFather:
107           if isGroup:
108             return None # Group may be added only under the shape-father
109           theFatherSO = findOrCreateComponent()
110         else:
111           theFatherSO = theFather.GetSO()
112         aResultSO = None
113         if isGroup: # add group to the third sub-label or later to keep space for reference and "History"
114           aTag = 3
115           anIter = aStudy.NewChildIterator(theFatherSO)
116           while anIter.More():
117             aCurrentTag = anIter.Value().Tag() + 1
118             if aTag < aCurrentTag:
119               aTag = aCurrentTag
120             anIter.Next()
121           aResultSO = aBuilder.NewObjectToTag(theFatherSO, aTag)
122         else:
123           aResultSO = aBuilder.NewObject(theFatherSO);
124         aResultSO.SetAttrString("AttributeName", theName)
125         if theObject is not None:
126             anIOR = salome.orb.object_to_string(theObject)
127             aResultSO.SetAttrString("AttributeIOR", anIOR)
128             theObject.SetSO(aResultSO)
129           
130             aAttr = aBuilder.FindOrCreateAttribute(aResultSO, "AttributePixMap")
131             aPixmap = aAttr._narrow(salome.SALOMEDS.AttributePixMap)
132             aType = 0
133             if isGroup:
134               aType = SHAPERSTUDY_Object.__shape_types__[theObject.GetSelectionType()]
135             else:
136               aType = theObject.GetShapeType()
137             aPixmap.SetPixMap(self.ShaperIcons[aType])
138             
139         # add a red-reference that means that this is an active reference to SHAPER result
140         if not isGroup:
141           aSub = aBuilder.NewObjectToTag(aResultSO, 1)
142           aBuilder.Addreference(aSub, aResultSO)
143
144         return aResultSO
145
146     def AddSubShape( theMainShape, theIndices ):
147         """
148         Add a sub-shape defined by indices in theIndices
149         (contains unique IDs of sub-shapes inside theMainShape)
150         """
151         # no sub-shapes for the moment
152         go = SHAPERSTUDY_Object()._this()
153         return go
154
155     # For now it is impossible to remove anything from the SHAPER-STUDY
156     def RemoveObject( self, theObject ):
157         """
158         Removes the object from the component
159         """
160         # can not be removed for the moment
161         return
162
163     def GetIFieldOperations( self ):
164         """
165         """
166         return SHAPERSTUDY_IOperations.SHAPERSTUDY_IFieldOperations()._this()
167
168     def GetIGroupOperations( self ):
169         """
170         """
171         return SHAPERSTUDY_IOperations.SHAPERSTUDY_IGroupOperations()._this()
172
173     def GetIShapesOperations( self ):
174         """
175         """
176         return SHAPERSTUDY_IOperations.SHAPERSTUDY_IShapesOperations()._this()
177
178     def GetIMeasureOperations( self ):
179         """
180         """
181         return SHAPERSTUDY_IOperations.SHAPERSTUDY_IMeasureOperations()._this()
182
183     def GetStringFromIOR( self, theObject ):
184         """
185         Returns a string which contains an IOR of the SHAPERSTUDY_Object
186         """
187         IOR = ""
188         if theObject and getORB():
189             IOR = getORB().object_to_string( theObject )
190             pass
191         return IOR
192
193     def Save( self, component, URL, isMultiFile ):
194         """
195         Saves data: all objects into one file
196         """
197         aResult = "" # string-pairs of internal entries and shape streams
198         aStudy = getStudy()
199         # get all sub-SObjects with IOR defined
200         anIters = [aStudy.NewChildIterator(findOrCreateComponent())]
201         aSOList = []
202         while len(anIters):
203           aLast = anIters[len(anIters) - 1]
204           if aLast.More():
205             aSO = aLast.Value()
206             anIOR = aSO.GetIOR()
207             if len(anIOR):
208               aSOList.append(aSO)
209             anIters.append(aStudy.NewChildIterator(aSO))
210             aLast.Next()
211           else:
212             anIters.remove(aLast)
213
214         for aSO in aSOList: # for each sobject export shapes stream if exists
215           anIOR = aSO.GetIOR()
216           if not len(anIOR):
217             continue
218           anObj = salome.orb.string_to_object(anIOR)
219           if type(anObj) == SHAPERSTUDY_ORB._objref_SHAPER_Group:
220             if len(aResult):
221               aResult += '|'
222             # store internal entry, type and list of indices of the group selection (separated by spaces)
223             aResult += anObj.GetEntry() + "|" + str(anObj.GetSelectionType())
224             aSelList = anObj.GetSelection()
225             aResult += "|" + str(' '.join(str(anI) for anI in aSelList))
226           elif type(anObj) == SHAPERSTUDY_ORB._objref_SHAPER_Field:
227             if len(aResult):
228               aResult += '|'
229             # same as for group, but in addition to the second string part - field specifics
230             aResult += anObj.GetEntry() + "|" + str(anObj.GetSelectionType())
231             aResult += " " + str(anObj.GetDataType()) # values type
232             aSteps = anObj.GetSteps()
233             aResult += " " + str(len(aSteps)) # number of steps
234             aComps = anObj.GetComponents()
235             aResult += " " + str(len(aComps)) # number of components
236             for aComp in aComps: # components strings: but before remove spaces and '|'
237               aCoded = aComp.replace(" ", "__space__").replace("|", "__vertical_bar__")
238               aResult += " " + aCoded
239             for aStepNum in range(len(aSteps)):
240               aVals = anObj.GetStep(aStepNum + 1).GetValues()
241               if aStepNum == 0:
242                 aResult += " " + str(len(aVals)) # first the number of values in the step
243               aResult += " " + str(anObj.GetStep(aStepNum + 1).GetStamp()) # ID of stamp in step
244               for aVal in aVals:
245                 aResult += " " + str(aVal) # all values of step
246             aSelList = anObj.GetSelection()
247             aResult += "|" + str(' '.join(str(anI) for anI in aSelList))
248           elif isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
249             if len(aResult):
250               aResult += '|'
251             # store internal entry, current and old shapes in BRep format
252             aResult += anObj.GetEntry() + "|" + anObj.GetShapeStream().decode()
253             aResult += "|" + anObj.GetOldShapeStream().decode()
254
255         return aResult.encode()
256
257     def Load( self, component, stream, URL, isMultiFile ):
258         """
259         Loads data
260         """
261         global __entry2IOR__
262         __entry2IOR__.clear()
263         aList=stream.decode().split('|')
264         aSubNum = 1
265         anId = ""
266         aNewShapeStream = ""
267         for aSub in aList:
268           if aSubNum == 1:
269             anId = aSub
270             aSubNum = 2
271           elif aSubNum == 2:
272             aNewShapeStream = aSub
273             aSubNum = 3
274           else: # create objects by 3 arguments
275             anObj = None
276             if anId.startswith('group') or (anId.startswith('dead') and anId.count("group") > 0): # group object
277               anObj = SHAPERSTUDY_Object.SHAPERSTUDY_Group()
278               if len(aSub):
279                 anObj.SetSelection([int(anI) for anI in aSub.split(' ')])
280               anObj.SetSelectionType(int(aNewShapeStream))
281             elif anId.startswith('field') or (anId.startswith('dead') and anId.count("field") > 0): # field object
282               anObj = SHAPERSTUDY_Object.SHAPERSTUDY_Field()
283               if len(aSub):
284                 anObj.SetSelection([int(anI) for anI in aSub.split(' ')])
285               aParams = aNewShapeStream.split(" ")
286               anObj.SetSelectionType(int(aParams[0]))
287               aTypeStr = aParams[1]
288               if (aTypeStr == "FDT_Bool"):
289                 anObj.SetValuesType(0)
290               elif (aTypeStr == "FDT_Int"):
291                 anObj.SetValuesType(1)
292               elif (aTypeStr == "FDT_Double"):
293                 anObj.SetValuesType(2)
294               elif (aTypeStr == "FDT_String"):
295                 anObj.SetValuesType(3)
296               aSteps = []
297               aNumSteps = int(aParams[2])
298               for aVal in range(aNumSteps):
299                 aSteps.append(aVal + 1)
300               anObj.SetSteps(aSteps)
301               aCompNum = int(aParams[3])
302               aCompNames = []
303               for aCompNameIndex in range(aCompNum):
304                 aCompName = aParams[4 + aCompNameIndex].replace("__space__", " ").replace("__vertical_bar__", "|")
305                 aCompNames.append(aCompName)
306               anObj.SetComponents(aCompNames)
307               aNumValsInStep = int(aParams[4 + aCompNum])
308               for aStepNum in range(aNumSteps):
309                 aStepStartIndex = 4 + aCompNum + aStepNum * (aNumValsInStep + 1) + 1
310                 aStampId = int(aParams[aStepStartIndex])
311                 aVals = []
312                 for aValIndex in range(aNumValsInStep):
313                   aVals.append(float(aParams[aStepStartIndex + aValIndex + 1]))
314                 anObj.AddFieldStep(aStampId, aStepNum + 1, aVals)
315             else: # shape object by BRep in the stream: set old first then new
316               anObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
317               if len(aSub):
318                 anObj.SetShapeByStream(aSub)
319               anObj.SetShapeByStream(aNewShapeStream)
320             if anObj:
321               anObj.SetEntry(anId)
322               anIOR = salome.orb.object_to_string(anObj._this())
323               __entry2IOR__[anId] = anIOR
324             aSubNum = 1
325         return 1
326         
327     def IORToLocalPersistentID(self, sobject, IOR, isMultiFile, isASCII):
328         """
329         Gets persistent ID for the CORBA object.
330         The internal entry of the Object is returned.
331         """
332         anObj = salome.orb.string_to_object(IOR)
333         if anObj and (isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object) or \
334                       isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Field)):
335           return anObj.GetEntry()
336         return ""
337
338     def LocalPersistentIDToIOR(self, sobject, persistentID, isMultiFile, isASCII):
339         "Converts persistent ID of the object to its IOR."
340         global __entry2IOR__
341         if persistentID in __entry2IOR__:
342           aRes = __entry2IOR__[persistentID]
343           if len(aRes): # set SO from the study, the sobject param is temporary, don't store it
344             salome.orb.string_to_object(aRes).SetSO(getStudy().FindObjectID(sobject.GetID()))
345           return aRes
346         return ""
347     
348     def UniqueDumpName( self, theBaseName, theID ):
349         """
350         Returns a unique name from the theBaseName. Keeps theBaseName if it was not used yet.
351         Stores the newly generated name into the global map __entry2DumpName__.
352         """
353         global __entry2DumpName__
354         aPrefix = 1
355         # to avoid spaces and parenthesis in the variable name
356         aBaseName = theBaseName.replace(" ", "_").replace("(", "").replace(")", "")
357         aName = aBaseName
358         while aName in __entry2DumpName__.values():
359           aName = aBaseName + "_" + str(aPrefix)
360           aPrefix = aPrefix + 1
361         __entry2DumpName__[theID] = aName
362         return aName
363
364
365     def DumpPython( self, isPublished, isMultiFile ):
366         """
367         Dump module data to the Python script.
368         """
369         global __entry2DumpName__
370         __entry2DumpName__.clear()
371         anArchiveNum = 1
372         # collect all shape-objects in the SHAPERSTUDY tree
373         aShapeObjects = []
374         aStudy = getStudy()
375         aRoots = aStudy.NewChildIterator(findOrCreateComponent())
376         while aRoots.More():
377           aSO = aRoots.Value()
378           anIOR = aSO.GetIOR()
379           if len(anIOR):
380             anObj = salome.orb.string_to_object(anIOR)
381             if anObj and type(anObj) == SHAPERSTUDY_ORB._objref_SHAPER_Object:
382               aShapeObjects.append(anObj)
383           aRoots.Next()
384         script = []
385         if len(aShapeObjects):
386           script.append("if 'model' in globals():")
387           script.append("\tmodel.publishToShaperStudy()")
388           script.append("import SHAPERSTUDY")
389           for aShapeObj in aShapeObjects:
390             # check this shape also has sub-groups and fields
391             aGroupVarNames = []
392             aSOIter = aStudy.NewChildIterator(anObj.GetSO())
393             while aSOIter.More():
394               aGroupSO = aSOIter.Value()
395               anIOR = aGroupSO.GetIOR()
396               if len(anIOR):
397                 aGroup = salome.orb.string_to_object(anIOR)
398                 if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group) or \
399                    isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field):
400                   aGroupVarName = self.UniqueDumpName(aGroup.GetName(), aGroupSO.GetID())
401                   aGroupVarNames.append(aGroupVarName)
402               aSOIter.Next()
403             aShapeVar = self.UniqueDumpName(anObj.GetName(), anObj.GetSO().GetID())
404             aShapeStr = aShapeVar + ", "
405             for aGName in aGroupVarNames:
406               aShapeStr += aGName + ", "
407             aShapeStr += "= SHAPERSTUDY.shape(\"" + anObj.GetEntry() +"\")"
408             script.append(aShapeStr)
409             # dump also dead-shapes with groups and fields in the XAO format
410             aRes, aHistSO = aShapeObj.GetSO().FindSubObject(2) # the History folder
411             if aRes:
412               aDeads = aStudy.NewChildIterator(aHistSO)
413               while aDeads.More():
414                 aDSO = aDeads.Value()
415                 aDIOR = aDSO.GetIOR()
416                 if len(aDIOR):
417                   aDeadShape = salome.orb.string_to_object(aDIOR)
418                   if aDeadShape and type(aDeadShape) == SHAPERSTUDY_ORB._objref_SHAPER_Object:
419                     aDeadString = ""
420                     aXAO = StudyData_Swig.StudyData_XAO()
421                     aXAO.SetShape(aDeadShape.getShape())
422                     anArchiveName = "archive_" + str(anArchiveNum) + ".xao"
423                     anArchiveNum += 1
424                     aDeadVarName = self.UniqueDumpName(aDeadShape.GetName(), aDSO.GetID())
425                     aDeadString += aDeadVarName + ", "
426                     aDGroupIter = aStudy.NewChildIterator(aDSO)
427
428                     while aDGroupIter.More():
429                       aDeadGroup = aDGroupIter.Value().GetObject()
430                       if isinstance(aDeadGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group):
431                         aDGroupVarName = self.UniqueDumpName(aDeadGroup.GetName(), aDGroupIter.Value().GetID())
432                         aDeadString += aDGroupVarName + ", "
433                         aGroupID = aXAO.AddGroup(aDeadGroup.GetSelectionType(), aDGroupVarName)
434                         for aSel in aDeadGroup.GetSelection():
435                           aXAO.AddGroupSelection(aGroupID, aSel)
436                       elif isinstance(aDeadGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field):
437                         aDeadField = aDeadGroup
438                         aDFieldVarName = self.UniqueDumpName(aDeadField.GetName(), aDGroupIter.Value().GetID())
439                         aDeadString += aDFieldVarName + ", "
440                         aComponents = aDeadField.GetComponents()
441                         aFieldID = aXAO.AddField(aDeadField.GetValuesType(), aDeadField.GetSelectionType(), \
442                           len(aComponents), aDFieldVarName)
443                         for aCompIndex in range(len(aComponents)):
444                           aXAO.SetFieldComponent(aFieldID, aCompIndex, aComponents[aCompIndex])
445                         aSteps = aDeadField.GetSteps()
446                         for aStep in aSteps:
447                           aFieldStep = aDeadField.GetStep(aStep)
448                           aXAO.AddStep(aFieldID, aStep, aFieldStep.GetStamp())
449                           aStepVals = aFieldStep.GetValues()
450                           for aValue in aStepVals:
451                             aXAO.AddStepValue(aFieldID, aStep, str(aValue))
452                       aDGroupIter.Next()
453                     aXAO.Export(anArchiveName)
454                     aDeadString += " = SHAPERSTUDY.archive(" + aShapeVar + ", \"" + anArchiveName + "\")"
455                     script.append(aDeadString)
456                 aDeads.Next()
457           pass
458         
459         script.append("") # to have an end-line in the end
460         result_str = "\n".join(script)
461         encoded_str = result_str.encode() + b'\0' # to avoid garbage symbols in the end
462         return (encoded_str, 1)
463
464     def GetAllDumpNames( self ):
465         """
466         Returns all names with which Object's was dumped
467         into python script to avoid the same names in SMESH script
468         """
469         global __entry2DumpName__
470         aResultList = []
471         for anEntry in __entry2DumpName__:
472           aResultList.append(__entry2DumpName__[anEntry])
473         return aResultList
474
475     def GetDumpName( self, theStudyEntry ):
476         """
477         Returns a name with which a GEOM_Object was dumped into python script
478
479         Parameters:
480             theStudyEntry is an entry of the Object in the study
481         """
482         global __entry2DumpName__
483         if theStudyEntry in __entry2DumpName__:
484           return __entry2DumpName__[theStudyEntry]
485         return ""
486
487     def IsFather(theFather, theChild):
488         """
489         Returns true if theChild SObject is a child of theFather SObject
490         """
491         aChild = theChild.GetFather()
492         while aChild.Depth() > theFather.Depth():
493           aChild = aChild.GetFather()
494         return aChild.GetID() == theFather.GetID()
495
496     def BreakLink(self, theEntry):
497         """
498         Breaks links to not-dead shape, make the shape as dead
499         """
500         aStudy = getStudy()
501         aSO = aStudy.FindObjectID(theEntry)
502         if not aSO:
503           return
504         aRes, aSSO = aSO.ReferencedObject()
505         if not aRes:
506           return # only SObjects referenced to the SHAPEr STUDY objects are allowed
507         anIOR = aSSO.GetIOR()
508         if not anIOR:
509           return # must be referenced to the SHAPER STUDY shape
510         anObj = salome.orb.string_to_object(anIOR)
511         if not anObj or not isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
512           return
513         if anObj.IsDead():
514           return # do nothing for reference to already dead shape
515         aDeadShape = anObj.MakeDead()
516         
517         aMeshSObject = aSO.GetFather()
518         aMeshObject = aMeshSObject.GetObject()
519
520         aBuilder = aStudy.NewBuilder()
521         aBuilder.RemoveReference(aSO) # reset reference to the dead shape
522         aBuilder.Addreference(aSO, aDeadShape.GetSO())
523
524         # check also sub-structure of the mesh to find references to sub-objects that become dead
525         aRoot = aSO.GetFather()
526         anIters = [aStudy.NewChildIterator(aRoot)]
527         aSubList = []
528         while len(anIters):
529           aLast = anIters[len(anIters) - 1]
530           if aLast.More():
531             aSub = aLast.Value()
532             aRes, aSubRef = aSub.ReferencedObject()
533             if aRes and SHAPERSTUDY.IsFather(aSSO, aSubRef):
534               aReferenced = aSubRef.GetObject()
535               if aReferenced and not aReferenced.IsDead():
536                 aSubList.append(aSub)
537             anIters.append(aStudy.NewChildIterator(aSub))
538             aLast.Next()
539           else:
540             anIters.remove(aLast)
541         if len(aSubList):
542           # associate the number of sub-objects of the referenced objects
543           aMapSubEntryToIndex = {}
544           aSSOIter = aStudy.NewChildIterator(aSSO)
545           anIndex = 1
546           while aSSOIter.More():
547             aSub = aSSOIter.Value()
548             if aSub.GetIOR():
549               aMapSubEntryToIndex[aSub.GetID()] = anIndex
550               anIndex = anIndex + 1
551             aSSOIter.Next()
552           for aSubSO in aSubList:
553             aRes, aSubRef = aSubSO.ReferencedObject()
554             if aRes and aSubRef.GetID() in aMapSubEntryToIndex:
555               anIndex = aMapSubEntryToIndex[aSubRef.GetID()]
556               aDeadIter = aStudy.NewChildIterator(aDeadShape.GetSO())
557               while aDeadIter.More(): # iterate dead subs to find object with the same index
558                 aDeadSubSO = aDeadIter.Value()
559                 if aDeadSubSO.GetIOR():
560                   anIndex = anIndex - 1
561                   if anIndex == 0:
562                     # for a submesh there is no ReplaceShape, but the shape is not updated
563                     # anyway, so no need to update it here
564                     #aSubMeshSO = aSubSO.GetFather() # Replace shape object in the parent mesh
565                     #aSubMeshObject = aSubMeshSO.GetObject()
566                     #if aSubMeshObject:
567                     #  aSubMeshObject.ReplaceShape(aDeadSubSO.GetObject())
568                     aBuilder.RemoveReference(aSubSO) # reset reference to the dead shape
569                     aBuilder.Addreference(aSubSO, aDeadSubSO)
570                 aDeadIter.Next()
571
572         # Replace shape object in the parent mesh
573         aMeshObject.ReplaceShape(aDeadShape)
574
575 def shape(theEntry):
576   """
577   Searches a shape object by the SHAPER entry. Used in the python dump script
578   """
579   aStudy = getStudy()
580   aRoots = aStudy.NewChildIterator(findOrCreateComponent())
581   while aRoots.More():
582     aSO = aRoots.Value()
583     anIOR = aSO.GetIOR()
584     if len(anIOR):
585       anObj = salome.orb.string_to_object(anIOR)
586       if anObj and type(anObj) == SHAPERSTUDY_ORB._objref_SHAPER_Object and anObj.GetEntry() == theEntry:
587         aRes = (anObj,)
588         # add groups and fields to the result
589         aSOIter = aStudy.NewChildIterator(aSO)
590         while aSOIter.More():
591           aGroupSO = aSOIter.Value()
592           anIOR = aGroupSO.GetIOR()
593           if len(anIOR):
594             aGroup = salome.orb.string_to_object(anIOR)
595             if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group) or \
596                isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field):
597               aRes = aRes + (aGroup,)
598           aSOIter.Next()
599         return aRes
600   return None # not found
601
602 def archive(theShape, theXAOFile):
603   """
604   Creates a dead shapes under the theShape and restores these dead objects state basing on theXAOFile
605   """
606   theShape.MakeDead()
607   aStudy = getStudy()
608   # searching for the last dead
609   aDeads = aStudy.NewChildIterator(theShape.GetSO().FindSubObject(2)[1])
610   aLastDeadSO = aDeads.Value()
611   while aDeads.More():
612     aLastDeadSO = aDeads.Value()
613     aDeads.Next()
614
615   aDShape = aLastDeadSO.GetObject()
616   if aDShape:
617     aXAO = StudyData_Swig.StudyData_XAO()
618     anError = aXAO.Import(theXAOFile)
619     if (len(anError)):
620       print("Error of XAO file import: " + anError)
621       return None
622     aDShape.SetShapeByPointer(aXAO.GetShape())
623     aRes = (aDShape,)
624     # add groups and fields to the result
625     aGroupIndex = 0
626     aFieldIndex = 0
627     aSOIter = aStudy.NewChildIterator(aLastDeadSO)
628     while aSOIter.More():
629       aGroupSO = aSOIter.Value()
630       anIOR = aGroupSO.GetIOR()
631       if len(anIOR):
632         aGroup = salome.orb.string_to_object(anIOR)
633         if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group):
634           aRes += (aGroup,)
635           aGroup.SetSelectionType(aXAO.GetGroupDimension(aGroupIndex))
636           aSelection = []
637           for aSel in aXAO.GetGroupSelection(aGroupIndex):
638             aSelection.append(aSel)
639           aGroup.SetSelection(aSelection)
640           aGroupIndex += 1
641         elif isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field):
642           aField = aGroup
643           aRes += (aField,)
644           aField.SetValuesType(aXAO.GetValuesType(aFieldIndex))
645           aField.SetSelectionType(aXAO.GetSelectionType(aFieldIndex))
646           aField.SetComponents(aXAO.GetComponents(aFieldIndex))
647           aField.ClearFieldSteps()
648           for aStepIndex in range(aXAO.GetStepsNum(aFieldIndex)):
649             aField.AddFieldStep(aXAO.GetStamp(aFieldIndex, aStepIndex), \
650                                 aXAO.GetStepIndex(aFieldIndex, aStepIndex), \
651                                 aXAO.GetValues(aFieldIndex, aStepIndex))
652           aFieldIndex += 1
653       aSOIter.Next()
654     return aRes
655   return None # not found