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