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