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