Salome HOME
- ## Returns the list of IDs of submesh elements with the given type
[modules/smesh.git] / src / SMESH_SWIG / batchmode_smesh.py
1 #  Copyright (C) 2007-2008  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.
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 #  File   : batchmode_smesh.py
23 #  Author : Oksana TCHEBANOVA
24 #  Module : SMESH
25 #  $Header$
26 #
27 from batchmode_salome import *
28 from batchmode_geompy import ShapeType
29 import SMESH
30
31 #--------------------------------------------------------------------------
32 modulecatalog = naming_service.Resolve("/Kernel/ModulCatalog")
33
34 smesh = lcc.FindOrLoadComponent("FactoryServer", "SMESH")
35 myStudyBuilder = myStudy.NewBuilder()
36
37 if myStudyBuilder is None:
38         raise RuntimeError, " Null myStudyBuilder"
39
40 father = myStudy.FindComponent("SMESH")
41 if father is None:
42         father = myStudyBuilder.NewComponent("SMESH")
43         FName = myStudyBuilder.FindOrCreateAttribute(father, "AttributeName")
44         Comp = modulecatalog.GetComponent("SMESH")
45         FName.SetValue(Comp._get_componentusername())
46         aPixmap = myStudyBuilder.FindOrCreateAttribute(father, "AttributePixMap")
47         aPixmap.SetPixMap("ICON_OBJBROWSER_Mesh")
48
49 myStudyBuilder.DefineComponentInstance(father,smesh)
50
51 mySComponentMesh = father._narrow(SALOMEDS.SComponent)
52
53 Tag_HypothesisRoot  = 1
54 Tag_AlgorithmsRoot  = 2
55   
56 Tag_RefOnShape      = 1
57 Tag_RefOnAppliedHypothesis = 2
58 Tag_RefOnAppliedAlgorithms = 3
59   
60 Tag_SubMeshOnVertex = 4
61 Tag_SubMeshOnEdge = 5
62 Tag_SubMeshOnFace = 6
63 Tag_SubMeshOnSolid = 7
64 Tag_SubMeshOnCompound = 8
65
66 Tag = {"HypothesisRoot":1,"AlgorithmsRoot":2,"RefOnShape":1,"RefOnAppliedHypothesis":2,"RefOnAppliedAlgorithms":3,"SubMeshOnVertex":4,"SubMeshOnEdge":5,"SubMeshOnFace":6,"SubMeshOnSolid":7,"SubMeshOnCompound":8}
67
68 #------------------------------------------------------------
69 def Init():
70          pass
71 #------------------------------------------------------------
72 def AddNewMesh(IOR):
73         # VSR: added temporarily - objects are published automatically by the engine
74         aSO = myStudy.FindObjectIOR( IOR )
75         if aSO is not None:
76                 return aSO.GetID()
77         # VSR ######################################################################
78         
79         res,HypothesisRoot = mySComponentMesh.FindSubObject ( Tag_HypothesisRoot )
80         if HypothesisRoot is None or res == 0:
81                 HypothesisRoot = myStudyBuilder.NewObjectToTag(mySComponentMesh, Tag_HypothesisRoot)
82                 aName = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributeName")
83                 aName.SetValue("Hypotheses")
84                 aPixmap = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributePixMap")
85                 aPixmap.SetPixMap( "mesh_tree_hypo.png" )
86                 aSelAttr = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributeSelectable")
87                 aSelAttr.SetSelectable(0)
88
89         res, AlgorithmsRoot = mySComponentMesh.FindSubObject (Tag_AlgorithmsRoot)
90         if AlgorithmsRoot is None  or res == 0:
91                 AlgorithmsRoot = myStudyBuilder.NewObjectToTag (mySComponentMesh, Tag_AlgorithmsRoot)
92                 aName = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributeName")
93                 aName.SetValue("Algorithms")
94                 aPixmap = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributePixMap")
95                 aPixmap.SetPixMap( "mesh_tree_algo.png" )
96                 aSelAttr = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributeSelectable")
97                 aSelAttr.SetSelectable(0)
98
99         HypothesisRoot = HypothesisRoot._narrow(SALOMEDS.SObject)
100         newMesh = myStudyBuilder.NewObject(mySComponentMesh)
101         aPixmap = myStudyBuilder.FindOrCreateAttribute(newMesh, "AttributePixMap")
102         aPixmap.SetPixMap( "mesh_tree_mesh.png" )
103         anIOR = myStudyBuilder.FindOrCreateAttribute(newMesh, "AttributeIOR")
104         anIOR.SetValue(IOR)
105         return newMesh.GetID()
106
107 #------------------------------------------------------------   
108 def AddNewHypothesis(IOR):
109         # VSR: added temporarily - objects are published automatically by the engine
110         aSO = myStudy.FindObjectIOR( IOR )
111         if aSO is not None:
112                 return aSO.GetID()
113         # VSR ######################################################################
114
115         res, HypothesisRoot = mySComponentMesh.FindSubObject (Tag_HypothesisRoot)
116         if HypothesisRoot is None or res == 0:
117                 HypothesisRoot = myStudyBuilder.NewObjectToTag (mySComponentMesh, Tag_HypothesisRoot)
118                 aName = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributeName")
119                 aName.SetValue("Hypotheses")
120                 aSelAttr = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributeSelectable")
121                 aSelAttr.SetSelectable(0)
122                 aPixmap = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributePixMap")
123                 aPixmap.SetPixMap( "mesh_tree_hypo.png" )
124  
125         # Add New Hypothesis
126         newHypo = myStudyBuilder.NewObject(HypothesisRoot)
127         aPixmap = myStudyBuilder.FindOrCreateAttribute(newHypo, "AttributePixMap")
128         H = orb.string_to_object(IOR)
129         aType = H.GetName()
130         aPixmap.SetPixMap( "mesh_tree_hypo.png_" + aType )
131         anIOR = myStudyBuilder.FindOrCreateAttribute(newHypo, "AttributeIOR")
132         anIOR.SetValue(IOR)
133         return newHypo.GetID()
134
135 #------------------------------------------------------------
136 def AddNewAlgorithms(IOR):
137         # VSR: added temporarily - objects are published automatically by the engine
138         aSO = myStudy.FindObjectIOR( IOR )
139         if aSO is not None:
140                 return aSO.GetID()
141         # VSR ######################################################################
142
143         res, AlgorithmsRoot = mySComponentMesh.FindSubObject (Tag_AlgorithmsRoot)
144         if  AlgorithmsRoot is None or res == 0:
145                 AlgorithmsRoot = myStudyBuilde.NewObjectToTag (mySComponentMesh, Tag_AlgorithmsRoot)
146                 aName = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributeName")
147                 aName.SetValue("Algorithms")
148                 aSelAttr = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributeSelectable")
149                 aSelAttr.SetSelectable(0)
150                 aPixmap = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributePixMap")
151                 aPixmap.SetPixMap( "mesh_tree_algo.png" )
152
153   # Add New Algorithms
154         newHypo = myStudyBuilder.NewObject(AlgorithmsRoot)
155         aPixmap = myStudyBuilder.FindOrCreateAttribute(newHypo, "AttributePixMap")
156         aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap)
157         H = orb.string_to_object(IOR)
158         aType = H.GetName();    #QString in fact
159         aPixmap.SetPixMap( "mesh_tree_algo.png_" + aType )
160         anIOR = myStudyBuilder.FindOrCreateAttribute(newHypo, "AttributeIOR")
161         anIOR.SetValue(IOR)
162         return newHypo.GetID()
163
164
165 #------------------------------------------------------------
166 def SetShape(ShapeEntry, MeshEntry):
167         SO_MorSM = myStudy.FindObjectID( MeshEntry )
168         SO_GeomShape = myStudy.FindObjectID( ShapeEntry )
169
170         if SO_MorSM is not None and SO_GeomShape is not None :
171                 # VSR: added temporarily - shape reference is published automatically by the engine
172                 res, Ref = SO_MorSM.FindSubObject( Tag_RefOnShape )
173                 if res == 1 :
174                         return
175                 # VSR ######################################################################
176         
177                 SO = myStudyBuilder.NewObjectToTag (SO_MorSM, Tag_RefOnShape)
178                 myStudyBuilder.Addreference (SO,SO_GeomShape)
179
180
181 #------------------------------------------------------------
182 def SetHypothesis(Mesh_Or_SubMesh_Entry, Hypothesis_Entry):
183   SO_MorSM = myStudy.FindObjectID( Mesh_Or_SubMesh_Entry )
184   SO_Hypothesis =  myStudy.FindObjectID( Hypothesis_Entry )
185
186   if  SO_MorSM is not None and SO_Hypothesis is not None : 
187     
188         #Find or Create Applied Hypothesis root
189         res, AHR = SO_MorSM.FindSubObject (Tag_RefOnAppliedHypothesis)
190         if  AHR is None or res == 0: 
191                 AHR = myStudyBuilder.NewObjectToTag (SO_MorSM, Tag_RefOnAppliedHypothesis)
192                 aName = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributeName")
193                 
194                 # The same name as in SMESH_Mesh_i::AddHypothesis() ##################
195                 aName.SetValue("Applied hypotheses")
196                 
197                 aSelAttr = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributeSelectable")
198                 aSelAttr.SetSelectable(0)
199                 aPixmap = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributePixMap")
200                 aPixmap.SetPixMap( "mesh_tree_hypo.png" )
201                 
202         # VSR: added temporarily - reference to applied hypothesis is published automatically by the engine
203         else :
204                 it = myStudy.NewChildIterator(AHR)
205                 while it.More() :
206                         res, Ref = it.Value().ReferencedObject()
207                         if res and Ref is not None and Ref.GetID() == Hypothesis_Entry :
208                                 return
209                         it.Next()
210         # VSR ######################################################################
211         
212         SO = myStudyBuilder.NewObject(AHR)
213         myStudyBuilder.Addreference (SO,SO_Hypothesis)
214
215 #------------------------------------------------------------
216 def SetAlgorithms(Mesh_Or_SubMesh_Entry, Algorithms_Entry):
217     SO_MorSM = myStudy.FindObjectID( Mesh_Or_SubMesh_Entry )
218     SO_Algorithms = myStudy.FindObjectID( Algorithms_Entry )
219     if  SO_MorSM != None and SO_Algorithms != None : 
220         #Find or Create Applied Algorithms root
221         res, AHR = SO_MorSM.FindSubObject (Tag_RefOnAppliedAlgorithms)
222         if AHR is None or res == 0: 
223                 AHR = myStudyBuilder.NewObjectToTag (SO_MorSM, Tag_RefOnAppliedAlgorithms)
224                 aName = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributeName")
225
226                 # The same name as in SMESH_Mesh_i::AddHypothesis() ##################
227                 aName.SetValue("Applied algorithms")
228                 
229                 aSelAttr = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributeSelectable")
230                 aSelAttr.SetSelectable(0)
231                 aPixmap = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributePixMap")
232                 aPixmap.SetPixMap( "mesh_tree_algo.png" )
233                         
234         # VSR: added temporarily - reference to applied hypothesis is published automatically by the engine
235         else :
236                 it = myStudy.NewChildIterator(AHR)
237                 while it.More() :
238                         res, Ref = it.Value().ReferencedObject()
239                         if res and Ref is not None and Ref.GetID() == Algorithms_Entry :
240                                 return
241                         it.Next()
242         # VSR ######################################################################
243         
244         SO = myStudyBuilder.NewObject(AHR)
245         myStudyBuilder.Addreference (SO,SO_Algorithms)
246   
247
248 #------------------------------------------------------------
249 def UnSetHypothesis( Applied_Hypothesis_Entry ):
250         SO_Applied_Hypothesis = myStudy.FindObjectID( Applied_Hypothesis_Entry )
251         if SO_Applied_Hypothesis : 
252                 myStudyBuilder.RemoveObject(SO_Applied_Hypothesis)
253         
254
255 #------------------------------------------------------------
256 def AddSubMesh ( SO_Mesh_Entry, SM_IOR, ST):
257         # VSR: added temporarily - objects are published automatically by the engine
258         aSO = myStudy.FindObjectIOR( SM_IOR )
259         if aSO is not None:
260                 return aSO.GetID()
261         # VSR ######################################################################
262         
263         SO_Mesh = myStudy.FindObjectID( SO_Mesh_Entry )
264         if ( SO_Mesh ) : 
265     
266                 if  ST == ShapeType["COMPSOLID"] : 
267                         Tag_Shape = Tag_SubMeshOnSolid
268                         Name = "SubMeshes on Solid"
269                 elif ST == ShapeType["FACE"] :
270                         Tag_Shape = Tag_SubMeshOnFace
271                         Name = "SubMeshes on Face"
272                 elif ST == ShapeType["EDGE"] :
273                         Tag_Shape = Tag_SubMeshOnEdge
274                         Name = "SubMeshes on Edge"
275                 elif ST == ShapeType["VERTEX"] :
276                         Tag_Shape = Tag_SubMeshOnVertex
277                         Name = "SubMeshes on Vertex"
278                 else :
279                         Tag_Shape = Tag_SubMeshOnCompound
280                         Name = "SubMeshes on Compound"
281                 
282                 res, SubmeshesRoot = SO_Mesh.FindSubObject (Tag_Shape)
283                 if SubmeshesRoot is None or res == 0:
284                         SubmeshesRoot = myStudyBuilder.NewObjectToTag (SO_Mesh, Tag_Shape)
285                         aName = myStudyBuilder.FindOrCreateAttribute(SubmeshesRoot, "AttributeName")
286                         aName.SetValue(Name)
287                         aSelAttr = myStudyBuilder.FindOrCreateAttribute(SubmeshesRoot, "AttributeSelectable")
288                         aSelAttr.SetSelectable(0)
289                 
290                 SO = myStudyBuilder.NewObject (SubmeshesRoot)
291                 anIOR = myStudyBuilder.FindOrCreateAttribute(SO, "AttributeIOR")
292                 anIOR.SetValue(SM_IOR)
293                 return  SO.GetID()
294
295         return None
296
297 #------------------------------------------------------------
298 def AddSubMeshOnShape (Mesh_Entry, GeomShape_Entry, SM_IOR, ST) :
299         # VSR: added temporarily - objects are published automatically by the engine
300         aSO = myStudy.FindObjectIOR( SM_IOR )
301         if aSO is not None:
302                 return aSO.GetID()
303         # VSR ######################################################################
304         SO_GeomShape = myStudy.FindObjectID( GeomShape_Entry )
305         if  SO_GeomShape != None : 
306                 SM_Entry = AddSubMesh (Mesh_Entry,SM_IOR,ST)
307                 SO_SM = myStudy.FindObjectID( SM_Entry )
308
309                 if  SO_SM != None :
310                         SetShape (GeomShape_Entry, SM_Entry)
311                         return SM_Entry
312
313         return None
314
315
316 #------------------------------------------------------------
317 def SetName(Entry, Name):
318         SO = myStudy.FindObjectID( Entry )
319         if SO != None : 
320                 aName = myStudyBuilder.FindOrCreateAttribute(SO, "AttributeName")
321                 aName.SetValue(Name)