Salome HOME
b441acd71f9ce23f75396aff6901383df729d7cd
[modules/shaper_study.git] / src / PY / SHAPERSTUDY_Object.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
24 import SHAPERSTUDY_ORB__POA
25 import GEOM
26 from SHAPERSTUDY_utils import getEngine, getStudy
27 import salome
28
29 import StudyData_Swig
30
31 # converter from the integer values to idl shape_type enumerations
32 __shape_types__ = {
33   0:GEOM.COMPOUND, 1:GEOM.COMPSOLID, 2:GEOM.SOLID,
34   3:GEOM.SHELL, 4:GEOM.FACE, 5:GEOM.WIRE,
35   6:GEOM.EDGE, 7:GEOM.VERTEX, 8:GEOM.SHAPE, 9:GEOM.FLAT}
36
37 class SHAPERSTUDY_GenericObject:
38     """
39     Implement methods of SALOME::GenericObj
40     """
41
42     def Register(self):
43         """
44         Increase the reference count (mark as used by another object).
45         """
46         return
47
48     def UnRegister(self):
49         """
50         Decrease the reference count (release by another object)
51         """
52         return
53
54     def Destroy(self):
55         """
56         Obsolete, left for compatibility reasons only. Use UnRegister() instead
57         """
58         return
59
60     pass
61
62
63 class SHAPERSTUDY_Object(SHAPERSTUDY_ORB__POA.SHAPER_Object,
64                          SHAPERSTUDY_GenericObject):
65     """
66     Construct an instance of SHAPERSTUDY Object.
67     """
68     def __init__ ( self, *args):
69         self.SO = None
70         self.data = None
71         self.entry = None
72         self.type = 1 # by default it is a shape (Import feature in GEOMImpl_Types.hxx)
73         pass
74
75     def GetShapeType( self ):
76         """
77         Get a GEOM.shape_type of the object value.
78         """
79         if self.data is None:
80             return GEOM.SHAPE
81         global __shape_types__
82         return __shape_types__[self.data.type()];
83
84     def IsMainShape( self ):
85         """
86         Returns True if this object is not a sub-shape of another object.
87         """
88         return True
89
90     def GetSubShapeIndices( self ):
91         """
92         Get a list of ID's of sub-shapes in the main shape.
93         """
94         return []
95
96     def GetMainShape( self ):
97         """
98         Get a main shape object to which this object is a sub-shape.
99         """
100         return getShape()
101
102     def getShape( self ):
103         """
104         Get the TopoDS_Shape, for collocated case only.
105         Called by GEOM_Client to get TopoDS_Shape pointer
106         """
107         if self.data is None:
108             return 0
109         return self.data.shape()
110
111     def GetShapeStream( self ):
112         """
113         Get geometric shape of the object as a byte stream in BRep format
114         """
115         if self.data is None:
116             return b''
117         return self.data.shapeStream().encode()
118
119     def GetOldShapeStream( self ):
120         """
121         Get geometric shape of the object as a byte stream in BRep format
122         """
123         if self.data is None:
124             return b''
125         return self.data.oldShapeStream().encode()
126
127     def SetShapeByStream(self, theStream):
128         """
129         Sets geometric shape content of the object as a byte stream in BRep format
130         """
131         if self.data:
132           self.data.updateShape(theStream)
133         else:
134           self.data = StudyData_Swig.StudyData_Object(theStream)
135
136     """
137     Methods from BaseObject
138     """
139     def GetName( self ):
140         """
141         Get name of the object associated with this object.
142         """
143         return self.SO.GetName()
144
145     def SetEntry( self, theInternalEntry ):
146         """
147         Sets internal (unique) entry of the object in the component's data tree.
148         """
149         self.entry = theInternalEntry
150
151     def GetEntry( self ):
152         """
153         Get internal (unique) entry of the object in the component's data tree.
154         """
155         return self.entry
156
157     def GetType( self ):
158         """
159         Get internal type of operation created this object.
160         In SMESH is used to find out if an object is GROUP (type == 37)
161         """
162         return self.type
163
164     def SetType( self, theType ):
165         """
166         Sets internal type of operation created this object.
167         In SMESH is used to find out if an object is GROUP (type == 37, for shape it is default=1)
168         """
169         self.type = theType
170
171     def GetTick( self ):
172         """
173         Get value of a modification counter of the object
174         """
175         if self.data:
176           return self.data.getTick()
177         return 0
178
179     def GetStudyEntry( self ):
180         """
181         Get a Study entry where this object was published.
182         """
183         return self.SO.GetID()
184
185     def IsShape( self ):
186         """
187         Return true if geom object represents a shape.
188         For example, method return false for GEOM_MARKER
189         """
190         return True
191
192     def IsSame( self, other ):
193         """
194         Return true if passed object is identical to this object
195         """
196         return self.GetType() == other.GetType() and self.GetEntry() == other.GetEntry()
197
198     def GetGen( self ):
199         """
200         Return the engine creating this object
201         """
202         return getEngine()
203
204     def SetSO( self, theSO ):
205         """
206         Sets SObject of this object (when it is published)
207         """
208         self.SO = theSO
209         
210     def GetSO( self ):
211         """
212         Returns SObject of this object
213         """
214         return self.SO
215
216     def IsParametrical(self):
217         """
218         Returns true if the current object has connection to a parametrical model
219         which can be modified by parameters change.
220         """
221         return not self.IsDead() and self.type == 1 # only break link for shapes are accessible now
222
223     def IsDead(self):
224         """
225         Returns true if the shape is dead - no parametrical link to the SHAPER exists
226         """
227         return self.GetEntry().startswith("dead")
228
229     def MakeDead(self):
230         """
231         Makes the dead-copy of the shape and returns it.
232         """
233         aStudy = getStudy()
234         aBuilder = aStudy.NewBuilder()
235         aRes, aHistSO = self.SO.FindSubObject(2)
236         if not aRes: # create a "history" folder if it does not exist
237           aHistSO = aBuilder.NewObjectToTag(self.SO, 2)
238           aHistSO.SetAttrString("AttributeName", "History")
239
240         aDeadSO = aBuilder.NewObject(aHistSO)
241         anIndex = aDeadSO.Tag()
242         aDeadSO.SetAttrString("AttributeName", self.SO.GetName() + " (" + str(anIndex) + ")")
243         aDead = SHAPERSTUDY_Object()
244         aDeadEntry = "dead" + str(anIndex) + "_" + self.GetEntry()
245         aDead.SetEntry(aDeadEntry)
246         aDead.SetShapeByStream(self.data.oldShapeStream())
247         aDeadObj = aDead._this()
248         anIOR = salome.orb.object_to_string(aDeadObj)
249         aDeadSO.SetAttrString("AttributeIOR", anIOR)
250         aDead.SetSO(aDeadSO)
251         if self.GetTick() > 2:
252           aDead.data.setTick(self.GetTick() - 1) # set the tick of an old shape
253         # make dead-copy also sub-groups
254         aSOIter = aStudy.NewChildIterator(self.SO)
255         while aSOIter.More():
256           aGroupSO = aSOIter.Value()
257           anIOR = aGroupSO.GetIOR()
258           if len(anIOR):
259             aGroup = salome.orb.string_to_object(anIOR)
260             if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group):
261               aDeadGroup = SHAPERSTUDY_Group()
262               aDeadGroupEntry = "dead" + str(anIndex) + "_" + aGroup.GetEntry()
263               aDeadGroup.SetEntry(aDeadGroupEntry)
264               aDeadGroup.SetShapeByPointer(aGroup.getShape())
265               aDeadGroup.SetSelectionType(aGroup.GetSelectionType())
266               aDeadGroup.SetSelection(aGroup.GetSelection())
267               aDeadGroupSO = aBuilder.NewObject(aDeadSO)
268               aDeadGroup.SetSO(aDeadGroupSO)
269               aDeadGroupSO.SetAttrString("AttributeName", aGroupSO.GetName() + " (" + str(anIndex) + ")")
270               aDeadGroupObj = aDeadGroup._this()
271               anIOR = salome.orb.object_to_string(aDeadGroupObj)
272               aDeadGroupSO.SetAttrString("AttributeIOR", anIOR)
273           aSOIter.Next()
274
275         return aDeadObj
276     
277     def SetShapeByPointer(self, theShape):
278         """
279         Sets the shape by the pointer to the TopoDS_Shape
280         """
281         if not self.data:
282           self.data = StudyData_Swig.StudyData_Object()
283         self.data.SetShapeByPointer(theShape)
284
285     pass
286
287 class SHAPERSTUDY_Group(SHAPERSTUDY_ORB__POA.SHAPER_Group, SHAPERSTUDY_Object):
288     """
289     Construct an instance of SHAPERSTUDY Group
290     """
291     def __init__ ( self, *args):
292         self.seltype = None
293         self.selection = []
294         self.SO = None
295         self.data = None
296         self.entry = None
297         self.type = 37 # a group type
298         pass
299
300     def SetSelectionType(self, theType):
301         """
302         Sets what is returned in the GEOM_IGroupOperations::GetType
303         """
304         self.seltype = theType
305
306     def GetSelectionType(self):
307         """
308         Returns the type of the selected sub-shapes
309         """
310         return self.seltype
311
312     def SetSelection(self, theSelection):
313         """
314         Sets what is returned in the GEOM_IGroupOperations::GetObjects
315         """
316         self.data = None # nullify the cashed shape when selection is changed
317         self.selection = theSelection
318
319     def GetSelection(self):
320         """
321         Returns the selected sub-shapes indices
322         """
323         return self.selection
324
325     def GetMainShape( self ):
326         """
327         Main shape is groups owner
328         """
329         return self.SO.GetFather().GetObject()
330
331     def getShape( self ):
332         """
333         Redefinition of the getShape method: here it creates a shape by the
334         main shape and the group index.
335         """
336         if not self.data:
337           self.data = StudyData_Swig.StudyData_Object()
338         # convert selection to long list
339         anArg = StudyData_Swig.LongList()
340         for l in self.selection:
341           anArg.append(l)
342         return self.data.groupShape(self.GetMainShape().getShape(), anArg)
343
344     pass
345
346 class SHAPERSTUDY_Field(SHAPERSTUDY_ORB__POA.SHAPER_Field, SHAPERSTUDY_Group):
347     """
348     Construct an instance of SHAPERSTUDY Field (inherits selection from a Group object)
349     """
350     def __init__ ( self, *args):
351         self.seltype = None
352         self.selection = []
353         self.SO = None
354         self.data = None
355         self.entry = None
356         self.type = 52 # a field type
357         self.valtype = None # type of the values
358         self.steps = [] # list of long
359         self.components = [] # string array, names of the components
360         self.name = None # name, string
361         pass
362
363     def SetValuesType( self, theType ):
364       """
365       Sets the type of values in the field
366       """
367       self.valtype = theType
368
369     def GetDataType( self ):
370       """
371       Returns the type of values in the field in terms of GEOM enumeration
372       """
373       if self.valtype == 0:
374         return GEOM.FDT_Bool
375       elif self.valtype == 1:
376         return GEOM.FDT_Int
377       elif self.valtype == 2:
378         return GEOM.FDT_Double
379       elif self.valtype == 3:
380         return GEOM.FDT_String
381       return None # unknown case
382
383     def SetSteps( self, theSteps ):
384       self.steps = theSteps
385
386     def GetSteps( self ):
387       return self.steps
388
389     def SetComponents( self, theComponents ):
390       self.components = theComponents
391     
392     def GetComponents( self ):
393       return self.components
394
395     def GetDimension( self ):
396       aShapeType = SHAPERSTUDY_Group.GetSelectionType()
397       if aShapeType == 8:
398         return -1 # whole part
399       elif aShapeType == 7:
400         return 0 # vertex
401       elif aShapeType == 6:
402         return 1 # edge
403       elif aShapeType == 4:
404         return 2 # face
405       elif aShapeType == 2:
406         return 3 # solid
407       return None # unknown case
408
409     def GetShape( self ):
410       return SHAPERSTUDY_Group.getShape()
411
412     pass