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