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