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 import SHAPERSTUDY_ORB
21 import SHAPERSTUDY_ORB__POA
23 from SHAPERSTUDY_utils import getEngine, getStudy
28 # converter from the integer values to idl shape_type enumerations
30 0:GEOM.COMPOUND, 1:GEOM.COMPSOLID, 2:GEOM.SOLID,
31 3:GEOM.SHELL, 4:GEOM.FACE, 5:GEOM.WIRE,
32 6:GEOM.EDGE, 7:GEOM.VERTEX, 8:GEOM.SHAPE, 9:GEOM.FLAT}
34 class SHAPERSTUDY_GenericObject:
36 Implement methods of SALOME::GenericObj
43 Increase the reference count (mark as used by another object).
45 #print(self.GetEntry())
47 #print("Register() --------- ", id(self), self.cnt)
52 Decrease the reference count (release by another object)
55 #print("UnRegister() --------- ", id(self), self.cnt)
57 from SHAPERSTUDY_utils import getPOA
59 oid=poa.servant_to_id(self)
60 poa.deactivate_object(oid)
61 if hasattr(self,"SetSO"):
62 self.SetSO(None) # release a GenericObject SO
63 #print("UnRegister() --------- OK")
68 Obsolete, left for compatibility reasons only. Use UnRegister() instead
76 class SHAPERSTUDY_Object(SHAPERSTUDY_ORB__POA.SHAPER_Object,
77 SHAPERSTUDY_GenericObject):
79 Constructs an instance of SHAPERSTUDY Object.
81 def __init__ ( self, *args):
82 SHAPERSTUDY_GenericObject.__init__(self)
86 self.type = 1 # by default it is a shape (Import feature in GEOMImpl_Types.hxx)
89 def GetShapeType( self ):
91 Get a GEOM.shape_type of the object value.
95 global __shape_types__
96 return __shape_types__[self.data.type()];
98 def IsMainShape( self ):
100 Returns True if this object is not a sub-shape of another object.
104 def GetSubShapeIndices( self ):
106 Get a list of ID's of sub-shapes in the main shape.
110 def GetMainShape( self ):
112 Get a main shape object to which this object is a sub-shape.
116 def getShape( self ):
118 Get the TopoDS_Shape, for collocated case only.
119 Called by GEOM_Client to get TopoDS_Shape pointer
121 if self.data is None:
123 return self.data.shape()
125 def GetShapeStream( self ):
127 Get geometric shape of the object as a byte stream in BRep format
129 if self.data is None:
131 return self.data.shapeStream().encode()
133 def GetOldShapeStream( self ):
135 Get geometric shape of the object as a byte stream in BRep format
137 if self.data is None:
139 return self.data.oldShapeStream().encode()
141 def SetShapeByStream(self, theStream):
143 Sets geometric shape content of the object as a byte stream in BRep format
146 self.data.updateShape(theStream)
148 self.data = StudyData_Swig.StudyData_Object(theStream)
151 Methods from BaseObject
155 Get name of the object associated with this object.
157 return self.SO.GetName()
159 def SetEntry( self, theInternalEntry ):
161 Sets internal (unique) entry of the object in the component's data tree.
163 self.entry = theInternalEntry
165 def GetEntry( self ):
167 Get internal (unique) entry of the object in the component's data tree.
173 Get internal type of operation created this object.
174 In SMESH is used to find out if an object is GROUP (type == 37)
178 def SetType( self, theType ):
180 Sets internal type of operation created this object.
181 In SMESH is used to find out if an object is GROUP (type == 37, for shape it is default=1)
187 Get value of a modification counter of the object
190 return self.data.getTick()
193 def SetTick( self, theValue ):
195 Sets value of a modification counter of the object
198 return self.data.setTick(theValue)
201 def GetStudyEntry( self ):
203 Get a Study entry where this object was published.
206 return self.SO.GetID()
211 Return true if geom object represents a shape.
212 For example, method return false for GEOM_MARKER
216 def IsSame( self, other ):
218 Return true if passed object is identical to this object
220 return self.GetType() == other.GetType() and self.GetEntry() == other.GetEntry()
224 Return the engine creating this object
227 return e._duplicate( e )
229 def SetSO( self, theSO ):
231 Sets SObject of this object (when it is published)
234 theSO.Register() # I hold a GenericObject!
241 Returns SObject of this object
245 def IsParametrical(self):
247 Returns true if the current object has connection to a parametrical model
248 which can be modified by parameters change.
250 return not self.IsDead() and self.type == 1 # only break link for shapes are accessible now
254 Returns true if the shape is dead - no parametrical link to the SHAPER exists
256 return self.GetEntry().startswith("dead")
260 Makes the dead-copy of the shape and returns it.
263 aBuilder = aStudy.NewBuilder()
264 aRes, aHistSO = self.SO.FindSubObject(10000)
265 if not aRes: # create a "history" folder if it does not exist
266 aHistSO = aBuilder.NewObjectToTag(self.SO, 10000)
267 aHistSO.SetAttrString("AttributeName", "History")
269 aDeadSO = aBuilder.NewObject(aHistSO)
270 anIndex = aDeadSO.Tag()
271 aDeadSO.SetAttrString("AttributeName", self.SO.GetName() + " (" + str(anIndex) + ")")
272 aRes, aPixMap = aBuilder.FindAttribute(self.SO, "AttributePixMap")
274 aDeadPixMap = aBuilder.FindOrCreateAttribute(aDeadSO, "AttributePixMap")
275 aDeadPixMap.SetPixMap(aPixMap.GetPixMap())
276 aDead = SHAPERSTUDY_Object()
277 aDeadEntry = "dead" + str(anIndex) + "_" + self.GetEntry()
278 aDead.SetEntry(aDeadEntry)
279 aDead.SetShapeByStream(self.data.oldShapeStream())
280 aDeadObj = aDead._this()
281 anIOR = salome.orb.object_to_string(aDeadObj)
282 aDeadSO.SetAttrString("AttributeIOR", anIOR)
284 if self.GetTick() > 2:
285 aDead.data.setTick(self.GetTick() - 1) # set the tick of an old shape
286 # make dead-copy also sub-groups
287 aSOIter = aStudy.NewChildIterator(self.SO)
288 while aSOIter.More():
289 aGroupSO = aSOIter.Value()
290 anIOR = aGroupSO.GetIOR()
292 aGroup = salome.orb.string_to_object(anIOR)
293 if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group) or \
294 isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field):
295 if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group):
296 aDeadGroup = SHAPERSTUDY_Group()
298 aDeadGroup = SHAPERSTUDY_Field()
299 aDeadGroupEntry = "dead" + str(anIndex) + "_" + aGroup.GetEntry()
300 aDeadGroup.SetEntry(aDeadGroupEntry)
301 aDeadGroup.SetSelectionType(aGroup.GetSelectionType())
302 aDeadGroup.SetSelection(aGroup.GetSelectionOld())
303 if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field): # additional field data
304 aDeadGroup.SetValuesType(aGroup.GetValuesType())
305 aDeadGroup.SetSteps(aGroup.GetSteps())
306 aDeadGroup.SetComponents(aGroup.GetComponents())
307 for aStep in aGroup.GetSteps():
308 aStepObj = aGroup.GetStep(aStep)
309 aDeadGroup.AddFieldStep(aStepObj.GetStamp(), aStep, aStepObj.GetValues())
310 aDeadGroupSO = aBuilder.NewObject(aDeadSO)
311 aDeadGroup.SetSO(aDeadGroupSO)
312 # 15.01.20 groups and fields names stays the same
313 #aDeadGroupSO.SetAttrString("AttributeName", aGroupSO.GetName() + " (" + str(anIndex) + ")")
314 aDeadGroupSO.SetAttrString("AttributeName", aGroupSO.GetName())
315 aRes, aPixMap = aBuilder.FindAttribute(aGroupSO, "AttributePixMap")
317 aDeadPixMap = aBuilder.FindOrCreateAttribute(aDeadGroupSO, "AttributePixMap")
318 aDeadPixMap.SetPixMap(aPixMap.GetPixMap())
319 aDeadGroupObj = aDeadGroup._this()
320 anIOR = salome.orb.object_to_string(aDeadGroupObj)
321 aDeadGroupSO.SetAttrString("AttributeIOR", anIOR)
326 def SetShapeByPointer(self, theShape):
328 Sets the shape by the pointer to the TopoDS_Shape
331 self.data = StudyData_Swig.StudyData_Object()
332 self.data.SetShapeByPointer(theShape)
336 class SHAPERSTUDY_Group(SHAPERSTUDY_ORB__POA.SHAPER_Group, SHAPERSTUDY_Object):
338 Constructs an instance of SHAPERSTUDY Group
340 def __init__ ( self, *args):
341 SHAPERSTUDY_GenericObject.__init__(self)
344 self.selectionTick = -2 # tick of the main shape when the current selection is set
345 self.selectionOld = [] # keep selection for breaking link
349 self.type = 37 # a group type
352 def SetSelectionType(self, theType):
354 Sets what is returned in the GEOM_IGroupOperations::GetType
356 self.seltype = theType
358 def GetSelectionType(self):
360 Returns the type of the selected sub-shapes
364 def SetSelection(self, theSelection):
366 Sets what is returned in the GEOM_IGroupOperations::GetObjects
370 aMainShape = self.GetMainShape()
372 aTick = aMainShape.GetTick()
373 if aTick != self.selectionTick or aTick == -2: # the last condition is for load: restore old and new
374 self.selectionOld = self.selection
375 self.selection = theSelection
376 self.selectionTick = aTick
377 #print("Set selection " + self.entry + " old = " + str(self.selectionOld) + " new = " + str(self.selection) + " tick = " + str(aTick))
379 self.selection = theSelection
380 #print("Set selection " + self.entry + " tick = " + str(aTick))
383 def GetSelection(self):
385 Returns the selected sub-shapes indices
387 return self.selection
389 def GetSelectionOld(self):
391 Returns the previously selected sub-shapes indices
393 print("get selection OLD " + self.entry + " old = " + str(self.selectionOld) + " new = " + str(self.selection))
394 return self.selectionOld
396 def IsMainShape( self ):
398 Returns True if this object is not a sub-shape of another object.
402 def GetMainShape( self ):
404 Main shape is groups owner
406 return self.SO.GetFather().GetObject()
408 def GetSubShapeIndices( self ):
410 Get a list of ID's of sub-shapes in the main shape.
412 return self.selection
414 def getShape( self ):
416 Redefinition of the getShape method: here it creates a shape by the
417 main shape and the group index.
420 self.data = StudyData_Swig.StudyData_Object()
421 # convert selection to long list
422 anArg = StudyData_Swig.LongList()
423 for l in self.selection:
425 return self.data.groupShape(self.GetMainShape().getShape(), anArg)
427 def GetShapeType( self ):
429 Group shape type is always compound.
431 return GEOM.COMPOUND;
435 class SHAPERSTUDY_Field(SHAPERSTUDY_ORB__POA.SHAPER_Field, SHAPERSTUDY_Group):
437 Constructs an instance of SHAPERSTUDY Field (inherits selection from a Group object)
439 def __init__ ( self, *args):
440 SHAPERSTUDY_GenericObject.__init__(self)
443 self.selectionTick = -2 # tick of the main shape when the current selection is set
444 self.selectionOld = [] # keep selection for breaking link
448 self.type = 52 # a field type
449 self.valtype = None # type of the values
450 self.steps = [] # list of long
451 self.components = [] # string array, names of the components
452 self.name = None # name, string
453 self.fieldsteps = {} # FieldSteps objects identified by step ID
456 def SetValuesType( self, theType ):
458 Sets the type of values in the field
460 self.valtype = theType
462 def GetValuesType( self ):
464 Returns the type of values in the field
469 def GetDataType( self ):
471 Returns the type of values in the field in terms of GEOM enumeration
473 if self.valtype == 0:
475 elif self.valtype == 1:
477 elif self.valtype == 2:
478 return GEOM.FDT_Double
479 elif self.valtype == 3:
480 return GEOM.FDT_String
481 return None # unknown case
483 def GetShape ( self ):
485 Returns the shape the field lies on
487 return super().GetMainShape()
489 def SetSteps( self, theSteps ):
490 self.steps = theSteps
492 def GetSteps( self ):
495 def SetComponents( self, theComponents ):
496 self.components = theComponents
498 def GetComponents( self ):
499 return self.components
501 def GetDimension( self ):
502 aShapeType = super().GetSelectionType()
504 return -1 # whole part
505 elif aShapeType == 7:
507 elif aShapeType == 6:
509 elif aShapeType == 4:
511 elif aShapeType == 2:
513 return None # unknown case
515 def ClearFieldSteps( self ):
518 def AddFieldStep( self, theStampID, theStepID, theValues):
520 if self.valtype == 0:
521 aFieldStep = SHAPER_BoolFieldStep()
522 elif self.valtype == 1:
523 aFieldStep = SHAPER_IntFieldStep()
524 elif self.valtype == 2:
525 aFieldStep = SHAPER_DoubleFieldStep()
527 aFieldStep.SetStep(theStampID, theStepID, theValues)
528 self.fieldsteps[theStepID] = aFieldStep._this()
530 def GetStep( self, theStepID ):
531 return self.fieldsteps[theStepID]
535 class SHAPER_FieldStep:
537 Base class for all step-classes
539 def __init__ ( self, *args):
540 self.stamp = None # long, ID of stamp
541 self.step = None # long, ID of step
542 self.values = None # array of values of the needed type
545 Defines all parameters of the step
547 def SetStep( self, theStamp, theStep, theValues ):
548 self.stamp = theStamp
550 self.values = theValues
555 def GetStamp( self ):
563 Returns a name of a sub-shape if the sub-shape is published in the study
565 def GetSubShape(self, theSubID):
566 # the SHAPER study does not support sub-shapes for now
570 class SHAPER_DoubleFieldStep(SHAPERSTUDY_ORB__POA.SHAPER_DoubleFieldStep, SHAPER_FieldStep):
572 Constructs an instance of SHAPERSTUDY Field step of type Double
574 def __init__ ( self, *args):
578 Returns values as an array of the needed type
580 def GetValues( self ):
581 aResult = [] # to make any type of result, create a corba-type
582 for i in self.values:
583 aResult.append(float(i))
588 class SHAPER_IntFieldStep(SHAPERSTUDY_ORB__POA.SHAPER_IntFieldStep, SHAPER_FieldStep):
590 Constructs an instance of SHAPERSTUDY Field step of type Double
592 def __init__ ( self, *args):
596 Returns values as an array of the needed type
598 def GetValues( self ):
599 aResult = [] # to make any type of result, create a corba-type
600 for i in self.values:
601 aResult.append(int(i))
606 class SHAPER_BoolFieldStep(SHAPERSTUDY_ORB__POA.SHAPER_BoolFieldStep, SHAPER_FieldStep):
608 Constructs an instance of SHAPERSTUDY Field step of type Double
610 def __init__ ( self, *args):
614 Returns values as an array of the needed type
616 def GetValues( self ):
617 aResult = [] # to make any type of result, create a corba-type
618 for i in self.values:
619 aResult.append(int(i))