Salome HOME
PR: needed by merge C. CAREMOLI branch CCAR_br1
[modules/smesh.git] / src / SMESH_SWIG / batchmode_smesh.py
1 #==============================================================================
2 #  File      : batchmode_smesh.py
3 #  Created   : Fri Nov 15 13:51:00 2002
4 #  Author    : Oksana TCHEBANOVA
5 #  Project   : SALOME
6 #  Copyright : OPEN CASCADE 2002
7 #  $Header$
8 #==============================================================================
9
10 #--------------------------------------------------------------------------
11
12 from batchmode_salome import *
13 import SMESH
14
15 #--------------------------------------------------------------------------
16 modulecatalog = naming_service.Resolve("/Kernel/ModulCatalog")
17
18 smesh = lcc.FindOrLoadComponent("FactoryServer", "SMESH")
19 myStudyBuilder = myStudy.NewBuilder()
20
21 if myStudyBuilder is None:
22         raise RuntimeError, " Null myStudyBuilder"
23
24 father = myStudy.FindComponent("MESH")
25 if father is None:
26         father = myStudyBuilder.NewComponent("MESH")
27         A1 = myStudyBuilder.FindOrCreateAttribute(father, "AttributeName");
28         FName = A1._narrow(SALOMEDS.AttributeName)
29 #        FName.SetValue("Mesh")
30
31         Comp = modulecatalog.GetComponent( "SMESH" )
32         FName.SetValue( Comp._get_componentusername() )
33
34         A2 = myStudyBuilder.FindOrCreateAttribute(father, "AttributePixMap");
35         aPixmap = A2._narrow(SALOMEDS.AttributePixMap);
36         aPixmap.SetPixMap( "ICON_OBJBROWSER_Mesh" );
37
38 myStudyBuilder.DefineComponentInstance(father,smesh)
39
40 mySComponentMesh = father._narrow(SALOMEDS.SComponent)
41
42 Tag_HypothesisRoot  = 1;
43 Tag_AlgorithmsRoot  = 2;
44   
45 Tag_RefOnShape      = 1;
46 Tag_RefOnAppliedHypothesis = 2;
47 Tag_RefOnAppliedAlgorithms = 3;
48   
49 Tag_SubMeshOnVertex = 4;
50 Tag_SubMeshOnEdge = 5;
51 Tag_SubMeshOnFace = 6;
52 Tag_SubMeshOnSolid = 7;
53 Tag_SubMeshOnCompound = 8;
54
55 Tag = {"HypothesisRoot":1,"AlgorithmsRoot":2,"RefOnShape":1,"RefOnAppliedHypothesis":2,"RefOnAppliedAlgorithms":3,"SubMeshOnVertex":4,"SubMeshOnEdge":5,"SubMeshOnFace":6,"SubMeshOnSolid":7,"SubMeshOnCompound":8}
56
57 # -- enumeration --
58 ShapeTypeCompSolid = 1
59 ShapeTypeSolid     = 2
60 ShapeTypeShell     = 3
61 ShapeTypeFace      = 4
62 ShapeTypeWire      = 5
63 ShapeTypeEdge      = 6
64 ShapeTypeVertex    = 7
65
66 # -- enumeration ShapeType as a dictionary --
67 ShapeType = {"CompSolid":1, "Solid":2, "Shell":3, "Face":4, "Wire":5, "Edge":6, "Vertex":7}
68
69 #------------------------------------------------------------
70 def Init():
71          pass
72 #------------------------------------------------------------
73 def AddNewMesh(IOR):
74         res,HypothesisRoot = mySComponentMesh.FindSubObject ( Tag_HypothesisRoot )
75         if HypothesisRoot is None or res == 0:
76                 HypothesisRoot = myStudyBuilder.NewObjectToTag(mySComponentMesh, Tag_HypothesisRoot)
77                 anAttr = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributeName")
78                 aName = anAttr._narrow(SALOMEDS.AttributeName)
79                 aName.SetValue("Hypothesis Definition")
80                 anAttr = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributePixMap")
81                 aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap)
82                 aPixmap.SetPixMap( "mesh_tree_hypo.png" )
83                 anAttr = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributeSelectable")
84                 aSelAttr = anAttr._narrow(SALOMEDS.AttributeSelectable)
85                 aSelAttr.SetSelectable(0);
86
87         res, AlgorithmsRoot = mySComponentMesh.FindSubObject (Tag_AlgorithmsRoot)
88         if AlgorithmsRoot is None  or res == 0:
89                 AlgorithmsRoot = myStudyBuilder.NewObjectToTag (mySComponentMesh, Tag_AlgorithmsRoot)
90                 anAttr = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributeName")
91                 aName = anAttr._narrow(SALOMEDS.AttributeName)
92                 aName.SetValue("Algorithms Definition");
93                 anAttr = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributePixMap");
94                 aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap);
95                 aPixmap.SetPixMap( "mesh_tree_algo.png" );
96                 anAttr = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributeSelectable");
97                 aSelAttr = anAttr._narrow(SALOMEDS.AttributeSelectable);
98                 aSelAttr.SetSelectable(0);
99
100         HypothesisRoot = HypothesisRoot._narrow(SALOMEDS.SObject)
101         newMesh = myStudyBuilder.NewObject(mySComponentMesh)
102         newMesh = newMesh._narrow(SALOMEDS.SObject)
103         anAttr = myStudyBuilder.FindOrCreateAttribute(newMesh, "AttributePixMap")
104         aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap)
105         aPixmap.SetPixMap( "mesh_tree_mesh.png" )
106         anAttr = myStudyBuilder.FindOrCreateAttribute(newMesh, "AttributeIOR")
107         anIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
108         anIOR.SetValue(IOR)
109         return newMesh.GetID()
110
111 #------------------------------------------------------------   
112 def AddNewHypothesis(IOR):
113         res, HypothesisRoot = mySComponentMesh.FindSubObject (Tag_HypothesisRoot)
114         if HypothesisRoot is None or res == 0:
115                 HypothesisRoot = myStudyBuilder.NewObjectToTag (mySComponentMesh, Tag_HypothesisRoot)
116                 anAttr = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributeName");
117                 aName = anAttr._narrow(SALOMEDS.AttributeName);
118                 aName.SetValue("Hypothesis Definition");
119                 anAttr = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributeSelectable");
120                 aSelAttr = anAttr._narrow(SALOMEDS.AttributeSelectable);
121                 aSelAttr.SetSelectable(0);
122                 anAttr = myStudyBuilder.FindOrCreateAttribute(HypothesisRoot, "AttributePixMap");
123                 aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap);
124                 aPixmap.SetPixMap( "mesh_tree_hypo.png" );
125  
126         # Add New Hypothesis
127         newHypo = myStudyBuilder.NewObject(HypothesisRoot)
128         newHypo = newHypo._narrow(SALOMEDS.SObject)
129         anAttr  = myStudyBuilder.FindOrCreateAttribute(newHypo, "AttributePixMap")
130         aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap)
131         H = orb.string_to_object(IOR)
132         H = H._narrow( SMESH.SMESH_Hypothesis );
133         aType = H.GetName();
134         aPixmap.SetPixMap( "mesh_tree_hypo.png_" + aType );
135         anAttr = myStudyBuilder.FindOrCreateAttribute(newHypo, "AttributeIOR");
136         anIOR = anAttr._narrow(SALOMEDS.AttributeIOR);
137         anIOR.SetValue(IOR);
138         return newHypo.GetID();
139
140 #------------------------------------------------------------
141 def AddNewAlgorithms(IOR):
142         res, AlgorithmsRoot = mySComponentMesh.FindSubObject (Tag_AlgorithmsRoot)
143         if  AlgorithmsRoot is None or res == 0:
144                 AlgorithmsRoot = myStudyBuilde.NewObjectToTag (mySComponentMesh, Tag_AlgorithmsRoot)
145                 anAttr = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributeName")
146                 aName = anAttr._narrow(SALOMEDS.AttributeName);
147                 aName.SetValue("Algorithms Definition");
148                 anAttr = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributeSelectable")
149                 aSelAttr = anAttr._narrow(SALOMEDS.AttributeSelectable);
150                 aSelAttr.SetSelectable(0);
151                 anAttr = myStudyBuilder.FindOrCreateAttribute(AlgorithmsRoot, "AttributePixMap");
152                 aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap);
153                 aPixmap.SetPixMap( "mesh_tree_algo.png" );
154
155   # Add New Algorithms
156         newHypo = myStudyBuilder.NewObject(AlgorithmsRoot)
157         newHypo = newHypo._narrow(SALOMEDS.SObject)
158         anAttr = myStudyBuilder.FindOrCreateAttribute(newHypo, "AttributePixMap");
159         aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap);
160         H = orb.string_to_object(IOR)
161         H = H._narrow( SMESH.SMESH_Hypothesis);
162         aType = H.GetName();    #QString in fact
163         aPixmap.SetPixMap( "mesh_tree_algo.png_" + aType );
164         anAttr = myStudyBuilder.FindOrCreateAttribute(newHypo, "AttributeIOR");
165         anIOR = anAttr._narrow(SALOMEDS.AttributeIOR);
166         anIOR.SetValue(IOR);
167         return newHypo.GetID();
168
169
170 #------------------------------------------------------------
171 def SetShape(ShapeEntry, MeshEntry):
172
173         SO_MorSM = myStudy.FindObjectID( MeshEntry )
174         SO_MorSM = SO_MorSM._narrow(SALOMEDS.SObject)
175         SO_GeomShape = myStudy.FindObjectID( ShapeEntry );
176         SO_GeomShape = SO_GeomShape._narrow(SALOMEDS.SObject)
177
178         if SO_MorSM is not None and SO_GeomShape is not None :
179                 SO = myStudyBuilder.NewObjectToTag (SO_MorSM, Tag_RefOnShape);
180                 SO = SO._narrow(SALOMEDS.SObject)
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                 anAttr = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributeName");
196                 aName = anAttr._narrow(SALOMEDS.AttributeName);
197                 aName.SetValue("Applied Hypothesis");
198                 anAttr = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributeSelectable");
199                 aSelAttr = anAttr._narrow(SALOMEDS.AttributeSelectable);
200                 aSelAttr.SetSelectable(0);
201                 anAttr = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributePixMap");
202                 aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap);
203                 aPixmap.SetPixMap( "mesh_tree_hypo.png" );
204                 
205                 SO = myStudyBuilder.NewObject(AHR);
206                 SO = SO._narrow(SALOMEDS.SObject)
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                 anAttr = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributeName");
219                 aName = anAttr._narrow(SALOMEDS.AttributeName);
220                 aName.SetValue("Applied Algorithm");
221                 anAttr = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributeSelectable");
222                 aSelAttr = anAttr._narrow(SALOMEDS.AttributeSelectable);
223                 aSelAttr.SetSelectable(0);
224                 anAttr = myStudyBuilder.FindOrCreateAttribute(AHR, "AttributePixMap");
225                 aPixmap = anAttr._narrow(SALOMEDS.AttributePixMap);
226                 aPixmap.SetPixMap( "mesh_tree_algo.png" );
227     
228         SO = myStudyBuilder.NewObject(AHR);
229         myStudyBuilder.Addreference (SO,SO_Algorithms);
230   
231
232 #------------------------------------------------------------
233 def UnSetHypothesis( Applied_Hypothesis_Entry ):
234         SO_Applied_Hypothesis = myStudy.FindObjectID( Applied_Hypothesis_Entry );
235         if SO_Applied_Hypothesis : 
236                 myStudyBuilder.RemoveObject(SO_Applied_Hypothesis);
237         
238
239 #------------------------------------------------------------
240 def AddSubMesh ( SO_Mesh_Entry, SM_IOR, ST):
241         SO_Mesh = myStudy.FindObjectID( SO_Mesh_Entry )
242         if ( SO_Mesh ) : 
243     
244                 if  ST == ShapeTypeCompSolid : 
245                         Tag_Shape = Tag_SubMeshOnSolid;    
246                         Name = "SubMeshes On Solid";
247                 elif ST == ShapeTypeFace :
248                         Tag_Shape = Tag_SubMeshOnFace;     
249                         Name = "SubMeshes On Face";
250                 elif ST == ShapeTypeEdge :
251                         Tag_Shape = Tag_SubMeshOnEdge;     
252                         Name = "SubMeshes On Edge";
253                 elif ST == ShapeTypeVertex :
254                         Tag_Shape = Tag_SubMeshOnVertex;   
255                         Name = "SubMeshes On Vertex";
256                 else :
257                         Tag_Shape = Tag_SubMeshOnCompound; 
258                         Name = "SubMeshes On Compound";
259                 
260                 res, SubmeshesRoot = SO_Mesh.FindSubObject (Tag_Shape)
261                 if SubmeshesRoot is None or res == 0:
262                         SubmeshesRoot = myStudyBuilder.NewObjectToTag (SO_Mesh, Tag_Shape);
263                         anAttr = myStudyBuilder.FindOrCreateAttribute(SubmeshesRoot, "AttributeName");
264                         
265                         aName = anAttr._narrow(SALOMEDS.AttributeName);
266                         aName.SetValue(Name);
267                         anAttr = myStudyBuilder.FindOrCreateAttribute(SubmeshesRoot, "AttributeSelectable");
268                         aSelAttr = anAttr._narrow(SALOMEDS.AttributeSelectable);
269                         aSelAttr.SetSelectable(0);
270                 
271                 SO = myStudyBuilder.NewObject (SubmeshesRoot); 
272                 SO = SO._narrow(SALOMEDS.SObject)
273                 anAttr = myStudyBuilder.FindOrCreateAttribute(SO, "AttributeIOR");
274                 anIOR = anAttr._narrow(SALOMEDS.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   SO_GeomShape = myStudy.FindObjectID( GeomShape_Entry );
283   if  SO_GeomShape != None : 
284         SM_Entry = AddSubMesh (Mesh_Entry,SM_IOR,ST);
285         SO_SM = myStudy.FindObjectID( SM_Entry );
286
287         if  SO_SM != None :
288                 SetShape (GeomShape_Entry, SM_Entry);
289                 return SO_SM.GetID();
290     
291   return None;
292
293
294 #------------------------------------------------------------
295 def SetName(Entry, Name):
296         SO = myStudy.FindObjectID( Entry );
297         if SO != None : 
298                 anAttr = myStudyBuilder.FindOrCreateAttribute(SO, "AttributeName");
299                 aName = anAttr._narrow(SALOMEDS.AttributeName);
300                 aName.SetValue(Name);
301   
302