Salome HOME
Make SHAPEr STUDY fields exported in SMESH into MED file
[modules/shaper_study.git] / src / PY / SHAPERSTUDY_IOperations.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 SHAPERSTUDY_Object
26 import GEOM
27 from SHAPERSTUDY_utils import getStudy
28
29 import StudyData_Swig
30
31 class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
32                                     SHAPERSTUDY_Object.SHAPERSTUDY_GenericObject):
33     """
34     Construct an instance of SHAPERSTUDY IShapesOperations.
35     """
36     def __init__ ( self, *args):
37         self.done = False
38         self.myop = StudyData_Swig.StudyData_Operation()
39         pass
40
41     def GetAllSubShapesIDs( self, theShape, theShapeType, isSorted ):
42         """
43         Explode a shape on sub-shapes of a given type.
44
45         Parameters:
46             theShape Shape to be exploded.
47             theShapeType Type of sub-shapes to be retrieved.
48             isSorted If this parameter is TRUE, sub-shapes will be
49             sorted by coordinates of their gravity centers.
50         """
51         aList = self.myop.GetAllSubShapesIDs(theShape.getShape(), theShapeType, isSorted)
52         self.done = not aList.empty()
53         aResult = []
54         for i in aList:
55           aResult.append(i)
56         return aResult
57
58     def GetSharedShapes( self, theShape1, theShape2, theShapeType ):
59         """
60         Get sub-shapes, shared by input shapes.
61
62         Parameters:
63             theShape1 Shape to find sub-shapes in.
64             theShape2 Shape to find shared sub-shapes with.
65             theShapeType Type of sub-shapes to be retrieved.
66         """
67         aList = self.myop.GetSharedShapes(theShape1.getShape(), theShape2.getShape(), theShapeType)
68         self.done = not aList.empty()
69         aResult = []
70         for i in aList:
71           aResult.append(i)
72         return aResult
73
74     def GetSubShapeIndex( self, theMainShape, theSubShape ):
75         """
76         Get global index of theSubShape in theMainShape.
77         """
78         anIndex = self.myop.GetSubShapeIndex(theMainShape.getShape(), theSubShape.getShape())
79         self.done = anIndex != 0
80         return anIndex
81
82     def GetSubShape( self, theMainShape, theID ):
83         """
84         Get a sub-shape defined by its unique ID within theMainShape
85         """
86         aShape = self.myop.GetSubShape(theMainShape.getShape(), theID)
87         self.done = aShape != 0
88         if not self.done:
89           return None
90
91         # create a shape-object that contain the internal shape only
92         aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
93         aShapeObj.SetShapeByPointer(aShape)
94         return aShapeObj._this()
95
96     def GetInPlace( self, theShapeWhere, theShapeWhat ):
97         """
98         Get sub-shape(s) of \a theShapeWhere, which are
99         coincident with \a theShapeWhat or could be a part of it.
100         """
101         # not done
102         return SHAPERSTUDY_Object()._this()
103         
104     def GetInPlaceMap( self, theShapeWhere, theShapeWhat ):
105         """
106         A sort of GetInPlace functionality, returning for each sub-shape ID of
107         \a theShapeWhat a list of corresponding sub-shape IDs of \a theShapeWhere.
108         """
109         # not done
110         return [[]]
111
112     def IsDone( self ):
113         """
114         To know, if the operation was successfully performed
115         """
116         return self.done
117
118     pass
119
120 class SHAPERSTUDY_IGroupOperations(SHAPERSTUDY_ORB__POA.IGroupOperations):
121     """
122     Construct an instance of SHAPERSTUDY IShapesOperations.
123     """
124     def __init__ ( self, *args):
125         self.done = False
126         pass
127
128     def CreateGroup( self, theMainShape, theShapeType ):
129         """
130         Creates a new group which will store sub-shapes of theMainShape
131         """
132         if theShapeType != 7 and theShapeType != 6 and theShapeType != 4 and theShapeType != 2: 
133           print("Error: You could create group of only next type: vertex, edge, face or solid")
134           return None
135
136         aGroup = SHAPERSTUDY_Object.SHAPERSTUDY_Group()
137         aGroupPtr = aGroup._this()
138         aGroup.SetSelectionType(theShapeType) # create a new field specific for the group python object
139         self.done = True
140         return aGroupPtr
141
142     def FindGroup(self, theOwner, theEntry):
143         """
144         Searches existing group of theOwner shape by the entry. Returns NULL if can not find.
145         """
146         aStudy = getStudy()
147         anIter = aStudy.NewChildIterator(theOwner.GetSO())
148         while anIter.More():
149           aGroupObj = anIter.Value().GetObject()
150           if aGroupObj:
151             if aGroupObj.GetEntry() == theEntry:
152               self.done = True
153               return aGroupObj
154           anIter.Next()
155         self.done = False
156         return None # not found
157
158     def UnionList( self, theGroup, theSubShapes ):
159         """
160         Adds to the group all the given shapes. No errors, if some shapes are already included.
161
162         Parameters:
163             theGroup is a GEOM group to which the new sub-shapes are added.
164             theSubShapes is a list of sub-shapes to be added.
165         """
166         # Not needed while SHAPER-STUDY has no sub-shapes in the structure, so, 
167         # theSubShapes can not be filled or treated
168         return
169
170     def IsDone( self ):
171         """
172         To know, if the operation was successfully performed
173         """
174         return self.done
175
176     def GetMainShape( self, theGroup ):
177         """
178         Returns a main shape associated with the group
179         """
180         aSO = theGroup.GetSO()
181         aFatherSO = aSO.GetFather()
182         return aFatherSO.GetObject()
183
184     def GetType( self, theGroup ):
185         """
186         Returns a type (int) of sub-objects stored in the group
187         """
188         return theGroup.GetSelectionType()
189
190     def GetObjects( self, theGroup ):
191         """
192         Returns a list of sub-objects ID stored in the group
193         """
194         return theGroup.GetSelection()
195
196     pass
197
198 class SHAPERSTUDY_IFieldOperations(SHAPERSTUDY_ORB__POA.IFieldOperations):
199     """
200     Construct an instance of SHAPERSTUDY IFieldOperations.
201     """
202     def __init__ ( self, *args):
203         pass
204
205     def CreateFieldByType( self, theMainShape, theShapeType):
206         """
207         Creates a new group which will store sub-shapes of theMainShape
208         """
209         if theShapeType != 8 and theShapeType != 7 and theShapeType != 6 and theShapeType != 4 and theShapeType != 2: 
210           print("Error: You could create field of only next type: vertex, edge, face or solid, whole part")
211           return None
212
213         aField = SHAPERSTUDY_Object.SHAPERSTUDY_Field()
214         aFieldPtr = aField._this()
215         aField.SetSelectionType(theShapeType) # create a new field specific for the group python object
216         return aFieldPtr
217
218     def FindField(self, theOwner, theEntry):
219         """
220         Searches existing field of theOwner shape by the entry. Returns NULL if can not find.
221         """
222         aStudy = getStudy()
223         anIter = aStudy.NewChildIterator(theOwner.GetSO())
224         while anIter.More():
225           if len(anIter.Value().GetIOR()):
226             aFieldObj = anIter.Value().GetObject()
227             if aFieldObj:
228               if aFieldObj.GetEntry() == theEntry:
229                 return aFieldObj
230           anIter.Next()
231         return None # not found
232
233
234     def GetFields( self, shape ):
235         """
236         Returns all fields on a shape
237         """
238         aResList = []
239         aStudy = getStudy()
240         anIter = aStudy.NewChildIterator(shape.GetSO())
241         while anIter.More():
242           aFieldObj = anIter.Value().GetObject()
243           if aFieldObj and isinstance(aFieldObj, SHAPERSTUDY_ORB._objref_SHAPER_Field):
244             aResList.append(aFieldObj)
245           anIter.Next()
246         return aResList
247
248     pass
249
250
251 class SHAPERSTUDY_IMeasureOperations(SHAPERSTUDY_ORB__POA.IMeasureOperations):
252     """
253     Construct an instance of SHAPERSTUDY IMeasureOperations.
254     """
255     def __init__ ( self, *args):
256         pass
257
258     def GetVertexByIndex( self, theShape, theIndex, theUseOri ):
259         """
260         Get a vertex sub-shape by index.
261
262         Parameters:
263         theShape Shape to find sub-shape.
264         theIndex Index to find vertex by this index (starting from zero)
265         theUseOri To consider edge/wire orientation or not
266         """
267         return [ SHAPERSTUDY_Field() ]
268
269     pass