Salome HOME
b9edf64140ac20c00c88580d32110af21d5ae391
[modules/shaper_study.git] / src / PY / SHAPERSTUDY_IOperations.py
1 # Copyright (C) 2019-2020  CEA/DEN, EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 import SHAPERSTUDY_ORB__POA
21 import SHAPERSTUDY_ORB
22 import SHAPERSTUDY_Object
23 import GEOM
24 import salome
25 from SHAPERSTUDY_utils import getStudy
26
27 import StudyData_Swig
28
29 class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
30                                     SHAPERSTUDY_Object.SHAPERSTUDY_GenericObject):
31     """
32     Construct an instance of SHAPERSTUDY IShapesOperations.
33     """
34     def __init__ ( self, *args):
35         SHAPERSTUDY_Object.SHAPERSTUDY_GenericObject.__init__(self)
36         self.done = False
37         self.myop = StudyData_Swig.StudyData_Operation()
38         self.errorcode = ""
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 = True
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 = True
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 = True
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 ExtractSubShapes(self, aShape, aType, isSorted):
97         """
98         Extract shapes (excluding the main shape) of given type.
99
100         Parameters:
101             aShape The shape.
102             aType  The shape type (see geompy.ShapeType)
103             isSorted Boolean flag to switch sorting on/off.
104
105         Returns:
106             List of sub-shapes of type aType, contained in aShape.
107         """
108         shapes = self.myop.ExtractSubShapes( aShape.getShape(), aType, isSorted )
109         resultList = []
110         for s in shapes:
111             aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
112             aShapeObj.SetShapeByPointer( s )
113             resultList.append( aShapeObj._this() )
114         self.done = True
115         return resultList
116
117
118     def NumberOfEdges(self, theShape):
119         """
120         Gives quantity of edges in the given shape.
121
122         Parameters:
123             theShape Shape to count edges of.
124
125         Returns:
126             Quantity of edges.
127         """
128         nb = self.myop.NumberOfEdges( theShape.getShape() )
129         self.done = ( nb >= 0 )
130         return nb
131
132     def NumberOfFaces(self, theShape):
133         """
134         Gives quantity of faces in the given shape.
135
136         Parameters:
137             theShape Shape to count faces of.
138
139         Returns:
140             Quantity of faces.
141         """
142         nb = self.myop.NumberOfFaces( theShape.getShape() )
143         self.done = ( nb >= 0 )
144         return nb
145
146     def MakeAllSubShapes(self, aShape, aType):
147         """
148         Explode a shape on sub-shapes of a given type.
149         If the shape itself matches the type, it is also returned.
150
151         Parameters:
152             aShape Shape to be exploded.
153             aType Type of sub-shapes to be retrieved (see geompy.ShapeType)
154
155         Returns:
156             List of sub-shapes of type theShapeType, contained in theShape.
157         """
158         self.done = True
159         return self.myop.MakeAllSubShapes(aShape.getShape(), aType)
160
161     def MakeSubShapes(self, aShape, anIDs):
162         """
163         Get a set of sub-shapes defined by their unique IDs inside theMainShape
164
165         Parameters:
166             aShape Main shape.
167             anIDs List of unique IDs of sub-shapes inside theMainShape.
168
169         Returns:
170             List of GEOM.GEOM_Object, corresponding to found sub-shapes.
171         """
172         self.done = True
173         return self.myop.MakeSubShapes(aShape.getShape(), anIDs)
174
175     def GetExistingSubObjects(self, theShape, theGroupsOnly = False):
176         """
177         Get all sub-shapes and groups of theShape,
178         that were created already by any other methods.
179
180         Parameters:
181             theShape Any shape.
182             theGroupsOnly If this parameter is TRUE, only groups will be
183                              returned, else all found sub-shapes and groups.
184
185         Returns:
186             List of existing sub-objects of theShape.
187         """
188         ListObj = []
189         self.done = False
190         SObj = salome.ObjectToSObject( theShape )
191         if not SObj: return ListObj
192         soIter = salome.myStudy.NewChildIterator( SObj )
193         while soIter.More():
194             soChild = soIter.Value()
195             soIter.Next()
196             obj = soChild.GetObject()
197             if theGroupsOnly:
198                 if isinstance( obj, SHAPERSTUDY_ORB._objref_SHAPER_Group):
199                     ListObj.append( obj )
200             elif isinstance( obj, SHAPERSTUDY_ORB._objref_SHAPER_Object ):
201                 ListObj.append( obj )
202         self.done = True
203         return ListObj
204
205     def GetTopologyIndex(self, aMainObj, aSubObj):
206         """
207         Return index of a sub-shape
208         """
209         i = self.myop.GetTopologyIndex(aMainObj.getShape(), aSubObj.getShape())
210         self.done = ( i > 0 )
211         return i
212
213     def GetShapeTypeString(self,aSubObj):
214         """
215         Return a shape type as a string
216         """
217         s = "%s" % aSubObj.GetShapeType()
218         t = s[5:]
219         return t
220         
221
222     def GetInPlace( self, theShapeWhere, theShapeWhat ):
223         """
224         Get sub-shape(s) of \a theShapeWhere, which are
225         coincident with \a theShapeWhat or could be a part of it.
226         """
227         self.done = False
228         self.errorcode = "Not implemented"
229         return SHAPERSTUDY_Object()._this()
230         
231     def GetInPlaceMap( self, theShapeWhere, theShapeWhat ):
232         """
233         A sort of GetInPlace functionality, returning for each sub-shape ID of
234         \a theShapeWhat a list of corresponding sub-shape IDs of \a theShapeWhere.
235         """
236         self.done = False
237         self.errorcode = "Not implemented"
238         return [[]]
239
240     def IsDone( self ):
241         """
242         To know, if the operation was successfully performed
243         """
244         return self.done
245
246     pass
247
248     def GetErrorCode( self ):
249         """
250         To know a failure reason
251         """
252         return self.errorcode
253
254     pass
255
256 class SHAPERSTUDY_IGroupOperations(SHAPERSTUDY_ORB__POA.IGroupOperations,
257                                    SHAPERSTUDY_IShapesOperations):
258     """
259     Construct an instance of SHAPERSTUDY IShapesOperations.
260     """
261     def __init__ ( self, *args):
262         SHAPERSTUDY_IShapesOperations.__init__(self)
263         self.done = False
264         pass
265
266     def CreateGroup( self, theMainShape, theShapeType ):
267         """
268         Creates a new group which will store sub-shapes of theMainShape
269         """
270         if theShapeType != 7 and theShapeType != 6 and theShapeType != 4 and theShapeType != 2: 
271           print("Error: You could create group of only next type: vertex, edge, face or solid")
272           return None
273
274         aGroup = SHAPERSTUDY_Object.SHAPERSTUDY_Group()
275         aGroupPtr = aGroup._this()
276         aGroup.SetSelectionType(theShapeType) # create a new field specific for the group python object
277         self.done = True
278         return aGroupPtr
279
280     def FindGroup(self, theOwner, theEntry):
281         """
282         Searches existing group of theOwner shape by the entry. Returns NULL if can not find.
283         """
284         aStudy = getStudy()
285         anIter = aStudy.NewChildIterator(theOwner.GetSO())
286         while anIter.More():
287           aGroupObj = anIter.Value().GetObject()
288           if aGroupObj:
289             if aGroupObj.GetEntry() == theEntry:
290               self.done = True
291               return aGroupObj
292           anIter.Next()
293         self.done = False
294         return None # not found
295
296     def UnionList( self, theGroup, theSubShapes ):
297         """
298         Adds to the group all the given shapes. No errors, if some shapes are already included.
299         """
300         self.done = False
301         indices = theGroup.GetSelection()
302         mainShape = self.GetMainShape( theGroup )
303         if not mainShape:
304             self.errorcode = "No main shape"
305             return
306         groupType = self.GetType( theGroup )
307         from shaperBuilder import EnumToLong
308         for shape in theSubShapes:
309             shapeType = EnumToLong( shape.GetShapeType() )
310             if not groupType == shapeType:
311                 self.errorcode = "Group type and shape type mismatch"
312                 return
313             i = self.myop.GetSubShapeIndex( mainShape.getShape(), shape.getShape() )
314             if not i in indices:
315                 indices.append( i )
316         theGroup.SetSelection( indices )
317         self.done = True
318         return
319
320     def GetMainShape( self, theGroup ):
321         """
322         Returns a main shape associated with the group
323         """
324         aSO = theGroup.GetSO()
325         if not aSO:
326             return None
327         aFatherSO = aSO.GetFather()
328         if not aFatherSO:
329             return None
330         anObj = aFatherSO.GetObject()
331         if isinstance( anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object ):
332             return anObj
333         else:
334             return None
335
336     def GetType( self, theGroup ):
337         """
338         Returns a type (int) of sub-objects stored in the group
339         """
340         return theGroup.GetSelectionType()
341
342     def GetObjects( self, theGroup ):
343         """
344         Returns a list of sub-objects ID stored in the group
345         """
346         return theGroup.GetSelection()
347
348     pass
349
350 class SHAPERSTUDY_IFieldOperations(SHAPERSTUDY_ORB__POA.IFieldOperations,
351                                    SHAPERSTUDY_IShapesOperations):
352     """
353     Construct an instance of SHAPERSTUDY IFieldOperations.
354     """
355     def __init__ ( self, *args):
356         SHAPERSTUDY_IShapesOperations.__init__(self)
357         pass
358
359     def CreateFieldByType( self, theMainShape, theShapeType):
360         """
361         Creates a new group which will store sub-shapes of theMainShape
362         """
363         if theShapeType != 8 and theShapeType != 7 and theShapeType != 6 and theShapeType != 4 and theShapeType != 2: 
364           print("Error: You could create field of only next type: vertex, edge, face or solid, whole part")
365           return None
366
367         aField = SHAPERSTUDY_Object.SHAPERSTUDY_Field()
368         aFieldPtr = aField._this()
369         aField.SetSelectionType(theShapeType) # create a new field specific for the group python object
370         return aFieldPtr
371
372     def FindField(self, theOwner, theEntry):
373         """
374         Searches existing field of theOwner shape by the entry. Returns NULL if can not find.
375         """
376         aStudy = getStudy()
377         anIter = aStudy.NewChildIterator(theOwner.GetSO())
378         while anIter.More():
379           if len(anIter.Value().GetIOR()):
380             aFieldObj = anIter.Value().GetObject()
381             if aFieldObj:
382               if aFieldObj.GetEntry() == theEntry:
383                 return aFieldObj
384           anIter.Next()
385         return None # not found
386
387
388     def GetFields( self, shape ):
389         """
390         Returns all fields on a shape
391         """
392         aResList = []
393         aStudy = getStudy()
394         anIter = aStudy.NewChildIterator(shape.GetSO())
395         while anIter.More():
396           aFieldObj = anIter.Value().GetObject()
397           if aFieldObj and isinstance(aFieldObj, SHAPERSTUDY_ORB._objref_SHAPER_Field):
398             aResList.append(aFieldObj)
399           anIter.Next()
400         return aResList
401
402     pass
403
404
405 class SHAPERSTUDY_IMeasureOperations(SHAPERSTUDY_ORB__POA.IMeasureOperations,
406                                      SHAPERSTUDY_IShapesOperations):
407     """
408     Construct an instance of SHAPERSTUDY IMeasureOperations.
409     """
410     def __init__ ( self, *args):
411         SHAPERSTUDY_IShapesOperations.__init__(self)
412         self.myop = StudyData_Swig.StudyData_Operation()
413         pass
414
415     def GetVertexByIndex( self, theShape, theIndex, theUseOri ):
416         """
417         Get a vertex sub-shape by index.
418
419         Parameters:
420         theShape Shape to find sub-shape.
421         theIndex Index to find vertex by this index (starting from zero)
422         theUseOri To consider edge/wire orientation or not
423         """
424         v = self.myop.GetVertexByIndex( theShape.getShape(), theIndex, theUseOri )
425         self.done = ( v > 0 )
426         if self.done:
427             aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
428             aShapeObj.SetShapeByPointer( v )
429             return aShapeObj._this()
430         return None
431
432     def GetMinDistance(self, theShape1, theShape2):
433         """
434         Get minimal distance between the given shapes.
435
436         Parameters:
437             theShape1,theShape2 Shapes to find minimal distance between.
438
439         Returns:
440             Value of the minimal distance between the given shapes.
441         """
442         d = self.myop.MinDistance(theShape1.getShape(), theShape2.getShape())
443         self.done = ( d >= 0 )
444         return d, 0,0,0, 0,0,0
445
446     def PointCoordinates(self,Point):
447         """
448         Get point coordinates
449
450         Returns:
451             [x, y, z]
452         """
453         d = self.myop.PointCoordinates(Point.getShape())
454         self.done = len( d )
455         if self.done == 3:
456             return d[0],d[1],d[2]
457         return [0,0,0]
458
459     def GetTolerance(self,theShape):
460         """
461         Get min and max tolerances of sub-shapes of theShape
462
463         Parameters:
464             theShape Shape, to get tolerances of.
465
466         Returns:
467             [FaceMin,FaceMax, EdgeMin,EdgeMax, VertMin,VertMax]
468              FaceMin,FaceMax: Min and max tolerances of the faces.
469              EdgeMin,EdgeMax: Min and max tolerances of the edges.
470              VertMin,VertMax: Min and max tolerances of the vertices.
471         """
472         tol = self.myop.GetTolerance(theShape.getShape())
473         self.done = tol > 0;
474         return tol,tol, tol,tol, tol,tol
475
476     pass