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