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