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