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