]> SALOME platform Git repositories - modules/shaper_study.git/blob - src/PY/SHAPERSTUDY.py
Salome HOME
Implementation of Open/Save and Break-Link functionality on SHAPER shapes
[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 GEOM
32
33 __entry2IOR__ = {}
34
35
36 class SHAPERSTUDY(SHAPERSTUDY_ORB__POA.Gen,
37                   SALOME_ComponentPy.SALOME_ComponentPy_i,
38                   SALOME_DriverPy.SALOME_DriverPy_i):
39
40
41     ShapeType = {"AUTO":-1, "COMPOUND":0, "COMPSOLID":1, "SOLID":2, "SHELL":3, "FACE":4, "WIRE":5, "EDGE":6, "VERTEX":7, "SHAPE":8, "FLAT":9}
42     
43     ShaperIcons = {GEOM.COMPOUND:"SHAPER_ICON_COMPSOLID",
44         GEOM.COMPSOLID:"SHAPER_ICON_COMPSOLID",
45         GEOM.SOLID:"SHAPER_ICON_SOLID",
46         GEOM.SHELL:"SHAPER_ICON_SHELL",
47         GEOM.FACE:"SHAPER_ICON_FACE",
48         GEOM.WIRE:"SHAPER_ICON_WIRE",
49         GEOM.EDGE:"SHAPER_ICON_EDGE",
50         GEOM.VERTEX:"SHAPER_ICON_VERTEX",
51         GEOM.SHAPE:"SHAPER_ICON_SOLID",
52         GEOM.FLAT:"SHAPER_ICON_FACE"}
53
54     def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ):
55         """
56         Construct an instance of SHAPERSTUDY module engine.
57         The class SHAPERSTUDY implements CORBA interface Gen (see SHAPERSTUDY_Gen.idl).
58         It is inherited (via GEOM_Gen) from the classes SALOME_ComponentPy_i (implementation of
59         Engines::EngineComponent CORBA interface - SALOME component) and SALOME_DriverPy_i
60         (implementation of SALOMEDS::Driver CORBA interface - SALOME module's engine).
61         """
62         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
63                     contID, containerName, instanceName, interfaceName, False)
64         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
65         #
66         #self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
67         #
68         pass
69
70     def FindOrCreateShape( self, theInternalEntry ):
71         """
72         Searches existing or creates a new SHAPERSTUDY_Object to interact with SHAPER
73         """
74         # Searching in the study tree
75         aComponent = findOrCreateComponent()
76         aSOIter = getStudy().NewChildIterator(aComponent)
77         while aSOIter.More():
78           aSO = aSOIter.Value()
79           anIOR = aSO.GetIOR()
80           anObj = salome.orb.string_to_object(anIOR)
81           if isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
82             if anObj.GetEntry() == theInternalEntry:
83               return anObj
84           aSOIter.Next()
85
86         aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
87         aShapeObj.SetEntry(theInternalEntry)
88         return aShapeObj._this()
89
90     def AddInStudy( self, theObject, theName, theFather ):
91         """
92         Adds in theStudy a object theObject under theFather with a name theName,
93         if theFather is not NULL the object is placed under theFather's SObject.
94         Returns a SObject where theObject is placed
95         """
96         aStudy = getStudy()
97         aBuilder = aStudy.NewBuilder()
98         if not theFather:
99           theFather = findOrCreateComponent()
100         aResultSO = aBuilder.NewObject(theFather);
101         aResultSO.SetAttrString("AttributeName", theName)
102         
103         
104         if theObject is not None:
105             anIOR = salome.orb.object_to_string(theObject)
106             aResultSO.SetAttrString("AttributeIOR", anIOR)
107             theObject.SetSO(aResultSO)
108           
109             aType = theObject.GetShapeType()
110             aAttr = aBuilder.FindOrCreateAttribute(aResultSO, "AttributePixMap")
111             aPixmap = aAttr._narrow(salome.SALOMEDS.AttributePixMap)
112             aPixmap.SetPixMap(SHAPERSTUDY.ShaperIcons[aType])
113             
114         # add a red-reference that means that this is an active reference to SHAPER result
115         aSub = aBuilder.NewObject(aResultSO)
116         aBuilder.Addreference(aSub, aResultSO)
117
118         return aResultSO
119
120     def AddSubShape( theMainShape, theIndices ):
121         """
122         Add a sub-shape defined by indices in theIndices
123         (contains unique IDs of sub-shapes inside theMainShape)
124         """
125         go = SHAPERSTUDY_Object()._this()
126         return go
127
128     # For now it is impossible to remove anything from the SHAPER-STUDY
129     def RemoveObject( self, theObject ):
130         """
131         Removes the object from the component
132         """
133         return
134
135     def GetIFieldOperations( self ):
136         """
137         """
138         return SHAPERSTUDY_IFieldOperations()
139
140     def GetIGroupOperations( self ):
141         """
142         """
143         return SHAPERSTUDY_IGroupOperations()
144
145     def GetIShapesOperations( self ):
146         """
147         """
148         return SHAPERSTUDY_IShapesOperations()
149
150     def GetIMeasureOperations( self ):
151         """
152         """
153         return SHAPERSTUDY_IMeasureOperations()
154
155     def GetStringFromIOR( self, theObject ):
156         """
157         Returns a string which contains an IOR of the SHAPERSTUDY_Object
158         """
159         IOR = ""
160         if theObject and getORB():
161             IOR = getORB().object_to_string( theObject )
162             pass
163         return IOR
164
165     def GetAllDumpNames( self ):
166         """
167         Returns all names with which Object's was dumped
168         into python script to avoid the same names in SMESH script
169         """
170         return [""]
171
172     def GetDumpName( self, theStudyEntry ):
173         """
174         Returns a name with which a GEOM_Object was dumped into python script
175
176         Parameters:
177             theStudyEntry is an entry of the Object in the study
178         """
179         return "test"
180
181     def Save( self, component, URL, isMultiFile ):
182         """
183         Saves data: all objects into one file
184         """
185         aResult = "" # string-pairs of internal entries and shape streams
186         aStudy = getStudy()
187         aSOIter = aStudy.NewChildIterator(findOrCreateComponent())
188         while aSOIter.More():
189           aSOVal = aSOIter.Value()
190           aSOList = [aSOVal]
191           # collect also the history shape objects
192           aRes, aHistSO = aSOVal.FindSubObject(2)
193           if aRes:
194             aSOIter2 = aStudy.NewChildIterator(aHistSO)
195             while aSOIter2.More():
196               aSOList.append(aSOIter2.Value())
197               aSOIter2.Next()
198           # for each sobject export shapes stream if exists
199           for aSO in aSOList:
200             anIOR = aSO.GetIOR()
201             anObj = salome.orb.string_to_object(anIOR)
202             if isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
203               if len(aResult):
204                 aResult = aResult + '|'
205               aResult = aResult + anObj.GetEntry() + "|" + anObj.GetShapeStream().decode()
206               aResult = aResult + "|" + anObj.GetOldShapeStream().decode()
207
208           aSOIter.Next()
209         return aResult.encode()
210
211     def Load( self, component, stream, URL, isMultiFile ):
212         """
213         Loads data
214         """
215         global __entry2IOR__
216         __entry2IOR__.clear()
217         
218         aList=stream.decode().split('|')
219         aSubNum = 1
220         anId = ""
221         aNewShapeStream = ""
222         for aSub in aList:
223           if aSubNum == 1:
224             anId = aSub
225             aSubNum = 2
226           elif aSubNum == 2:
227             aNewShapeStream = aSub
228             aSubNum = 3
229           else: # create shapes by BRep in the stream: set old first then new
230             aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
231             if len(aSub):
232               aShapeObj.SetShapeByStream(aSub)
233             aShapeObj.SetShapeByStream(aNewShapeStream)
234             aShapeObj.SetEntry(anId)
235             anIOR = salome.orb.object_to_string(aShapeObj._this())
236             __entry2IOR__[anId] = anIOR
237             aSubNum = 1
238
239         return 1
240         
241     def IORToLocalPersistentID(self, sobject, IOR, isMultiFile, isASCII):
242         """
243         Gets persistent ID for the CORBA object.
244         The internal entry of the Object is returned.
245         """
246         anObj = salome.orb.string_to_object(IOR)
247         if anObj and isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
248           return anObj.GetEntry()
249         return ""
250
251     def LocalPersistentIDToIOR(self, sobject, persistentID, isMultiFile, isASCII):
252         "Converts persistent ID of the object to its IOR."
253         global __entry2IOR__
254         if persistentID in __entry2IOR__:
255           aRes = __entry2IOR__[persistentID]
256           if len(aRes): # set SO from the study, the sobject param is temporary, don't store it
257             salome.orb.string_to_object(aRes).SetSO(getStudy().FindObjectID(sobject.GetID()))
258           return aRes
259         return ""
260
261     def DumpPython( self, isPublished, isMultiFile ):
262         """
263         Dump module data to the Python script.
264         """
265         return ("".encode(), 1)
266
267
268
269     def CreateGroup( self, theMainShape, theShapeType ):
270         """
271         Creates a new group which will store sub-shapes of theMainShape
272         """
273         return GetIGroupOperations().CreateGroup( theMainShape, theShapeType );
274
275     def ExtractShapes( self, aShape, aType, isSorted = False ):
276         """
277         Extract shapes (excluding the main shape) of given type.
278
279         Parameters:
280             aShape The shape.
281             aType  The shape type (see geompy.ShapeType)
282             isSorted Boolean flag to switch sorting on/off.
283
284         Returns:
285             List of sub-shapes of type aType, contained in aShape.
286         """
287         return [ SHAPERSTUDY_Object()._this() ]
288
289     def GetSubShape( self, aShape, ListOfID ):
290         """
291         Obtain a composite sub-shape of aShape, composed from sub-shapes
292         of aShape, selected by their unique IDs inside aShape
293
294         Parameters:
295             aShape Shape to get sub-shape of.
296             ListOfID List of sub-shapes indices.
297         """
298         return SHAPERSTUDY_Object()._this()
299
300     def GetSubShapeID( self, aShape, aSubShape ):
301         """
302         Obtain unique ID of sub-shape aSubShape inside aShape
303         of aShape, selected by their unique IDs inside aShape
304
305         Parameters:
306            aShape Shape to get sub-shape of.
307            aSubShape Sub-shapes of aShape.
308         """
309         return 1
310
311     def MinDistance( self, theShape1, theShape2 ):
312         """
313         Get minimal distance between the given shapes.
314         """
315         return 0.
316
317     def NumberOfEdges( self, theShape ):
318         """
319         Gives quantity of edges in the given shape.
320         """
321         return 0
322
323     def NumberOfFaces( self,  ):
324         """
325         Gives quantity of faces in the given shape.
326         """
327         return 0
328
329     def PointCoordinates( self, theVertex ):
330         """
331         Get point coordinates
332         """
333         return 0,0,0
334
335     def SubShapeAll( self, aShape, aType ):
336         """
337         Explode a shape on sub-shapes of a given type.
338         If the shape itself matches the type, it is also returned.
339         """
340         return [ SHAPERSTUDY_Object()._this() ]
341
342     def SubShapeName( self, aSubObj, aMainObj ):
343         """
344         Get name for sub-shape aSubObj of shape aMainObj
345         """
346         return ""
347
348     def SubShapes( self, aShape, anIDs ):
349         """
350         Get a set of sub-shapes defined by their unique IDs inside theMainShape
351         """
352         return  [ SHAPERSTUDY_Object()._this() ]
353
354     def Tolerance( self, theShape ):
355         """
356         Get min and max tolerances of sub-shapes of theShape
357
358         Returns:
359             [FaceMin,FaceMax, EdgeMin,EdgeMax, VertMin,VertMax]
360         """
361         return [0,0, 0,0, 0,0]
362
363     def UnionList( self, theGroup, theSubShapes ):
364         """
365         Adds to the group all the given shapes. No errors, if some shapes are already included.
366         """
367         return GetIGroupOperations().UnionList( theGroup, theSubShapes )
368
369     def BreakLink(self, theEntry):
370         """
371         Breaks links to not-dead shape, make the shape as dead
372         """
373         aStudy = getStudy()
374         aSO = aStudy.FindObjectID(theEntry)
375         if not aSO:
376           return
377         aRes, aSSO = aSO.ReferencedObject()
378         if not aRes:
379           return # only SObjects referenced to the SHAPEr STUDY objects are allowed
380         anIOR = aSSO.GetIOR()
381         if not anIOR:
382           return # must be referenced to the SHAPER STUDY shape
383         anObj = salome.orb.string_to_object(anIOR)
384         if not anObj or not isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
385           return
386         if anObj.IsDead():
387           return # do nothing for reference to already dead shape
388         aDeadShape = anObj.MakeDead()
389         aBuilder = aStudy.NewBuilder()
390         aBuilder.RemoveReference(aSO) # reset reference to the dead shape
391         aBuilder.Addreference(aSO, aDeadShape.GetSO())