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