Salome HOME
db65c7d924d8ffa4f406092376eb3d326e97019b
[modules/kernel.git] / doc / salome / examples / example19
1
2 #======================================================================
3 #1. Create Study and add some components to it
4 #======================================================================
5
6 #=================================
7 #       create AttributeReal      
8 #=================================
9 A = batchmode_geompy.myBuilder.FindOrCreateAttribute(batchmode_geompy.father, "AttributeReal")
10 if A == None :
11         raise  RuntimeError, "Can't create AttributeReal attribute"
12 A = A._narrow(SALOMEDS.AttributeReal)
13 A.SetValue(0.0001)
14 if A.Value() != 0.0001:
15         raise  RuntimeError, "Error : wrong value of  AttributeReal"
16
17 # ===  Geometry  ==================================================
18
19 import batchmode_geompy
20  
21 #===================================
22 #     define a box
23 #===================================
24
25 box = batchmode_geompy.MakeBox(0., 0., 0., 100., 200., 300.)
26 idbox = batchmode_geompy.addToStudy(box,"box")
27
28
29 geom = batchmode_geompy.geom
30 import SMESH
31 import batchmode_smesh
32 ShapeType = batchmode_smesh.ShapeType
33
34 # ---- add first face of box in study
35
36 subShapeList = batchmode_geompy.SubShapeAll(box,ShapeType["Face"])
37 face=subShapeList[0]
38 name = "box_face"
39 print name
40 idface= batchmode_geompy.addToStudyInFather(box,face,name)
41
42 # ---- add shell from box  in study
43
44 subShellList= batchmode_geompy.SubShapeAll(box,ShapeType["Shell"])
45 shell = subShellList[0]
46 name = "box_shell"
47 print name
48 idshell= batchmode_geompy.addToStudyInFather(box,shell,name)
49
50 # ---- add first edge of face in study
51
52 edgeList =  batchmode_geompy.SubShapeAll(face,ShapeType["Edge"])
53 edge=edgeList[0];
54 name = "face_edge"
55 print name
56 idedge= batchmode_geompy.addToStudyInFather(face,edge,name)
57
58 # ---- launch SMESH, init a Mesh with the box
59
60 smesh =  batchmode_smesh.smesh
61 #  --  Init  --
62 shape = batchmode_geompy.IDToObject(idbox)
63 mesh=smesh.Init(geom, batchmode_geompy.myStudyId, shape)
64
65 orb = batchmode_geompy.orb
66
67 idmesh = batchmode_smesh.AddNewMesh( orb.object_to_string(mesh) )
68 batchmode_smesh.SetName(idmesh, "Meshbox");
69 batchmode_smesh.SetShape(idbox, idmesh);
70
71 # ---- create Hypothesis
72
73 print "-------------------------- create Hypothesis ----------------------"
74 print "-------------------------- LocalLength"
75 hyp1 = smesh.CreateHypothesis("LocalLength", batchmode_geompy.myStudyId )
76 hypLen1 = hyp1._narrow(SMESH.SMESH_LocalLength)
77 hypLen1.SetLength(100)
78 print hypLen1.GetName()
79 print hypLen1.GetId()
80 print hypLen1.GetLength()
81
82 idlength = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypLen1) );
83 batchmode_smesh.SetName(idlength, "Local_Length_100");
84
85 print "-------------------------- NumberOfSegments"
86 hyp2 =  smesh.CreateHypothesis("NumberOfSegments", batchmode_geompy.myStudyId )
87 hypNbSeg1=hyp2._narrow(SMESH.SMESH_NumberOfSegments)
88 hypNbSeg1.SetNumberOfSegments(7)
89 print hypNbSeg1.GetName()
90 print hypNbSeg1.GetId()
91 print hypNbSeg1.GetNumberOfSegments()
92
93 idseg =  batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypNbSeg1) );
94 batchmode_smesh.SetName(idseg, "NumberOfSegments_7");
95
96 print "-------------------------- MaxElementArea"
97 hyp3 = smesh.CreateHypothesis("MaxElementArea",  batchmode_geompy.myStudyId)
98 hypArea1=hyp3._narrow(SMESH.SMESH_MaxElementArea)
99 hypArea1.SetMaxElementArea(2500)
100 print hypArea1.GetName()
101 print hypArea1.GetId()
102 print hypArea1.GetMaxElementArea()
103
104 idarea1 = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypArea1) );
105 batchmode_smesh.SetName(idarea1, "MaxElementArea_2500");
106
107 print "-------------------------- MaxElementArea"
108 hyp3 = smesh.CreateHypothesis("MaxElementArea",  batchmode_geompy.myStudyId)
109 hypArea2 = hyp3._narrow(SMESH.SMESH_MaxElementArea)
110 hypArea2.SetMaxElementArea(500)
111 print hypArea2.GetName()
112 print hypArea2.GetId()
113 print hypArea2.GetMaxElementArea()
114
115 idarea2 = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypArea2) );
116 batchmode_smesh.SetName(idarea2, "MaxElementArea_500");
117
118 print "-------------------------- Regular_1D"
119 alg1 = smesh.CreateHypothesis("Regular_1D",  batchmode_geompy.myStudyId)
120 algo1 = alg1._narrow(SMESH.SMESH_Algo)
121 listHyp = algo1.GetCompatibleHypothesis()
122 for hyp in listHyp:
123     print hyp
124 algoReg=alg1._narrow(SMESH.SMESH_Regular_1D)
125 print algoReg.GetName()
126 print algoReg.GetId()
127
128 idreg = batchmode_smesh.AddNewAlgorithms( orb.object_to_string(algoReg) );
129 batchmode_smesh.SetName(idreg, "Regular_1D");
130
131 print "-------------------------- MEFISTO_2D"
132 alg2 = smesh.CreateHypothesis("MEFISTO_2D",  batchmode_geompy.myStudyId)
133 algo2 = alg2._narrow(SMESH.SMESH_Algo)
134 listHyp=algo2.GetCompatibleHypothesis()
135 for hyp in listHyp:
136     print hyp
137 algoMef=alg2._narrow(SMESH.SMESH_MEFISTO_2D)
138 print algoMef.GetName()
139 print algoMef.GetId()
140
141 idmef = batchmode_smesh.AddNewAlgorithms( orb.object_to_string(algoMef) );
142 batchmode_smesh.SetName(idmef, "MEFISTO_2D");
143
144
145 # ---- add hypothesis to edge
146
147 print "-------------------------- add hypothesis to edge"
148 edge = batchmode_geompy.IDToObject(idedge)
149 submesh = mesh.GetElementsOnShape(edge)
150 ret = mesh.AddHypothesis(edge,algoReg)
151 print ret
152 ret=mesh.AddHypothesis(edge,hypLen1)
153 print ret
154
155 idsm1 = batchmode_smesh.AddSubMeshOnShape( idmesh,
156                                            idedge,
157                                            orb.object_to_string(submesh),
158                                            ShapeType["Edge"] )
159 batchmode_smesh.SetName(idsm1, "SubMeshEdge")
160 batchmode_smesh.SetAlgorithms( idsm1, idreg );
161 batchmode_smesh.SetHypothesis( idsm1, idlength );
162
163 print "-------------------------- add hypothesis to face"
164 face=batchmode_geompy.IDToObject(idface)
165 submesh=mesh.GetElementsOnShape(face)
166 ret=mesh.AddHypothesis(face,hypArea2)
167 print ret
168
169 idsm2 = batchmode_smesh.AddSubMeshOnShape( idmesh,
170                                     idface,
171                                     orb.object_to_string(submesh),
172                                     ShapeType["Face"] )
173 batchmode_smesh.SetName(idsm2, "SubMeshFace")
174 batchmode_smesh.SetHypothesis( idsm2, idarea2 );
175
176 # ---- add hypothesis to box
177
178 print "-------------------------- add hypothesis to box"
179 box=batchmode_geompy.IDToObject(idbox)
180 submesh=mesh.GetElementsOnShape(box)
181 ret=mesh.AddHypothesis(box,algoReg)
182 print ret
183 ret=mesh.AddHypothesis(box,hypNbSeg1)
184 print ret
185 ret=mesh.AddHypothesis(box,algoMef)
186 print ret
187 ret=mesh.AddHypothesis(box,hypArea1)
188 print ret
189
190 batchmode_smesh.SetAlgorithms( idmesh, idreg );
191 batchmode_smesh.SetHypothesis( idmesh, idseg );
192 batchmode_smesh.SetAlgorithms( idmesh, idmef );
193 batchmode_smesh.SetHypothesis( idmesh, idarea1 );
194
195 # ---- compute box
196
197 print "-------------------------- compute box"
198 ret=smesh.Compute(mesh,box)
199 print ret
200 log=mesh.GetLog(0); # no erase trace
201 for linelog in log:
202     print linelog
203
204
205 #=======================================================
206 #    add  SuperVision component
207 #=======================================================
208
209 from batchmode_SuperV import *
210 import os
211 dir= os.getenv("SUPERV_ROOT_DIR")
212 if dir == None:
213         raise RuntimeError, "SUPERV_ROOT_DIR is not defined"
214 xmlfile = dir +"/examples/GraphEssai.xml"
215 print "--------------\n"+xmlfile+"\n--------------\n"
216
217 myGraph = Graph ( xmlfile )
218
219 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
220 print myGraph.IsValid()
221
222 # Get Nodes
223 myGraph.PrintNodes()
224 Add,Sub,Mul,Div = myGraph.Nodes()
225
226 # Load Datas
227 Addx = Add.Input("x",3.)
228 Addy = Add.Input("y",4.5)
229 Subx = Sub.Input("x",1.5)
230
231 # Get Output Port
232 Addz = Add.Port('z')
233 Subz = Sub.Port('z')
234 Mulz = Mul.Port('z')
235 Divz = Div.Port('z')
236
237 # This DataFlow is "executable" : all pending Ports are defined with Datas
238 print myGraph.IsExecutable()
239
240 # Starts only execution of that DataFlow and gets control immediately
241 print myGraph.Run()
242
243 # That DataFlow is running ==> 0 (false)
244 print myGraph.IsDone()
245
246 # Events of execution :
247 aStatus,aNode,anEvent,aState = myGraph.Event()
248 while aStatus :
249     print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
250     aStatus,aNode,anEvent,aState = myGraph.Event()
251 print myGraph.IsDone()
252
253 # Wait for Completion (but it is already done after event loop ...)
254 print "Done : ",myGraph.DoneW()
255
256 # Get result
257 print "Result : ",Divz.ToString()
258
259 # Intermediate results :
260 print "Intermediate Result Add\z : ",Addz.ToString()
261 print "Intermediate Result Sub\z : ",Subz.ToString()
262 print "Intermediate Result Mul\z : ",Mulz.ToString()
263
264 print " "
265 #print "Type : print myGraph.IsDone()"
266 #print "       If execution is finished ==> 1 (true)"
267 res=myGraph.IsDone()
268 if res != 1:
269         raise RuntimeError, "myGraph.Run() is not done"
270
271 print " "
272 print "Type : print Divz.ToString()"
273 print "       You will get the result"
274 Divz.ToString()
275
276 print " "
277 print "Type : myGraph.PrintPorts()"
278 print "       to see input and output values of the graph"
279 myGraph.PrintPorts()
280
281 print " "
282 print "Type : Add.PrintPorts()"
283 Add.PrintPorts()
284
285 print "Type : Sub.PrintPorts()"
286 Sub.PrintPorts()
287
288 print "Type : Mul.PrintPorts()"
289 Mul.PrintPorts()
290
291 print "Type : Div.PrintPorts()"
292 print "       to see input and output values of nodes"
293 Div.PrintPorts()
294
295 # Export will create newsupervisionexample.xml and the corresponding .py file
296 tmpdir=os.getenv("TmpDir")
297 if tmpdir is None:
298         tmpdir="/tmp"
299 file = tmpdir + "/newsupervisionexample"
300 print "--------------\n"+file+"\n--------------\n"
301 myGraph.Export(file)
302
303 ior = batchmode_geompy.orb.object_to_string(myGraph.G)
304 addStudy(ior)
305
306 GraphName = myGraph.Name()
307 print "Befor save ",
308 nodes = myGraph.Nodes()
309 length_bs = len(nodes)
310 print "ListOfNodes length = ", length_bs
311 names=[]
312 for node in nodes:
313         names.append(node.Name())
314 print names
315
316
317 #=================================
318 #      save / restore study      
319 #=================================
320 str= os.getenv("TmpDir")
321 if str == None:  
322         str = "/tmp"
323 file = str+"/test.hdf"
324
325
326 #==================================================
327 #1. SaveAs
328 #==================================================
329 print " -------  We will save to", file, "-----------"
330
331 batchmode_geompy.myStudyManager.SaveAs(file, batchmode_geompy.myStudy)
332 batchmode_geompy.myStudyManager.Close(batchmode_geompy.myStudy)
333
334 #rename the file and try to reread it again
335
336 os.mkdir(str + "/test_dir")
337
338 #2.==================================================
339 print " ------- We rename the file as " + str + "/test_dir/test_new.hdf"
340 os.rename(file, str+"/test_dir/test_new.hdf")
341
342 #==================================================
343 #3. Open
344 #==================================================
345 print " ------- We try to open " + str + "/test_dir/test_new.hdf"
346
347 try:
348         openedStudy=batchmode_geompy.myStudyManager.Open(str+"/test_dir/test_new.hdf")
349 except Exception:
350         raise  RuntimeError, "Can't open saved study!"
351
352 if openedStudy == None:
353         raise  RuntimeError, "Can't open saved study!"
354
355 father = openedStudy.FindComponent("GEOM")
356 if father is None:
357          raise  RuntimeError, "GEOM component is not found!  Wrong study is opened." 
358
359
360 #=================================
361 #     find AttributeReal
362 #=================================
363
364 res,A=father.FindAttribute("AttributeReal")
365 if res == 0 or A == None:
366         raise  RuntimeError, "Error:  not found AttributeReal"
367
368 A = A._narrow(SALOMEDS.AttributeReal)
369 if A.Value() != 0.0001:
370         raise  RuntimeError, "Error : wrong value of  AttributeReal"
371
372 #==================================================
373 #     find  box
374 #==================================================
375 box = openedStudy.FindObject("box")
376 if box is None :
377         raise  RuntimeError, "box was not found! Wrong study is opened."
378
379 edge=openedStudy.FindObject("face_edge")
380 if edge is None :
381         raise  RuntimeError, "face_edge was not found! Wrong study is opened."
382
383
384 father = openedStudy.FindComponent("MESH")
385 if father is None:
386          raise  RuntimeError, "MESH component is not found!  Wrong study is opened." 
387
388 hp=openedStudy.FindObject("Meshbox")
389 if hp is None:
390         raise  RuntimeError, "Meshbox object was not found! Wrong study is opened."
391
392 hp = openedStudy.FindObject("Algorithms Definition")
393 if hp is None:
394         raise  RuntimeError, "Algorithms Definition object was not found! Wrong study is opened."
395
396 hp = openedStudy.FindObject("MEFISTO_2D")
397 if hp is None:
398         raise  RuntimeError, "MEFISTO_2D object was not found! Wrong study is opened."
399
400 hp = openedStudy.FindObject("Hypothesis Definition")
401 if hp is None:
402         raise  RuntimeError, "Algoriths Definition was not found! Wrong study is opened."
403
404 #==================================================
405 # Find Supervisor
406 #==================================================
407 father = openedStudy.FindComponent("SUPERV")
408 if father is None: 
409          raise  RuntimeError, "SUPERV component is not found!  Wrong study is opened." 
410
411 SuperV = lcc.FindOrLoadComponent("SuperVisionContainer","Supervision")
412 Builder = openedStudy.NewBuilder()
413 Builder.LoadWith(father, SuperV)
414 import SALOMEDS
415
416 aChildIterator = openedStudy.NewChildIterator(father)
417
418 #while aChildIterator.More():
419 anSObject = aChildIterator.Value()
420 #print "iterate: ", anSObject.GetID()
421 res, anAttr=anSObject.FindAttribute("AttributeIOR")
422 if res :
423         anAttr=anAttr._narrow(SALOMEDS.AttributeIOR)
424         ior = anAttr.Value()
425         Graph=SuperV.getGraph(ior)
426         ListOfNodes=Graph.Nodes()
427         length_as= len(ListOfNodes)
428         print "ListOfNodes length = ", length_as
429         if length_as != length_bs:
430                 raise RuntimeErrror, "defferent length of nodes after study open"
431 #aChildIterator.Next()
432
433 Names = []
434 for node in ListOfNodes:
435         Names.append(node.Name())
436 names.sort()
437 Names.sort()
438 if names != Names :
439         raise RuntimeError, "List of dataflow nodes after save differs from one befor save operation"
440
441 #==================================================
442 #4. Save
443 #==================================================
444 batchmode_geompy.myStudyManager.Save(openedStudy)
445 batchmode_geompy.myStudyManager.Close(openedStudy)
446 #==================================================
447 #5. Open
448 #==================================================
449 try:
450         openedStudy = batchmode_geompy.myStudyManager.Open(str+"/test_dir/test_new.hdf")
451 except Exception:
452         raise  RuntimeError, "Can't open saved study!"
453
454
455
456 if openedStudy == None:
457         raise  RuntimeError, "Can't open saved study!"
458
459 father = openedStudy.FindComponent("GEOM")
460 if father is None:
461          raise  RuntimeError, "Geom component is not found!  Wrong study is opened." 
462
463
464 #find AttributeReal
465 res,A=father.FindAttribute("AttributeReal")
466 if res == 0 or A == None:
467         raise  RuntimeError, "Error:  not found AttributeReal"
468
469 A = A._narrow(SALOMEDS.AttributeReal)
470 if A.Value() != 0.0001:
471         raise  RuntimeError, "Error : wrong value of  AttributeReal"
472 #==================================================
473 #     find  box
474 #==================================================
475 box = openedStudy.FindObject("box")
476 if box is None :
477         raise  RuntimeError, "box was not found! Wrong study is opened."
478
479 edge=openedStudy.FindObject("face_edge")
480 if edge is None :
481         raise  RuntimeError, "face_edge was not found! Wrong study is opened."
482
483
484 father = openedStudy.FindComponent("MESH")
485 if father is None:
486          raise  RuntimeError, "MESH component is not found!  Wrong study is opened." 
487
488 hp=openedStudy.FindObject("Meshbox")
489 if hp is None:
490         raise  RuntimeError, "Meshbox object was not found! Wrong study is opened."
491
492 hp = openedStudy.FindObject("Algorithms Definition")
493 if hp is None:
494         raise  RuntimeError, "Algorithms Definition object was not found! Wrong study is opened."
495
496 hp = openedStudy.FindObject("MEFISTO_2D")
497 if hp is None:
498         raise  RuntimeError, "MEFISTO_2D object was not found! Wrong study is opened."
499
500 hp = openedStudy.FindObject("Hypothesis Definition")
501 if hp is None:
502         raise  RuntimeError, "Algoriths Definition was not found! Wrong study is opened."
503
504 #==================================================
505 # Find Supervisor
506 #==================================================
507 father = openedStudy.FindComponent("SUPERV")
508 if father is None:
509          raise  RuntimeError, "SUPERV component is not found!  Wrong study is opened." 
510
511 SuperV = lcc.FindOrLoadComponent("SuperVisionContainer","Supervision")
512 Builder = openedStudy.NewBuilder()
513 Builder.LoadWith(father, SuperV)
514 import SALOMEDS
515
516 aChildIterator = openedStudy.NewChildIterator(father)
517
518 #while aChildIterator.More():
519 anSObject = aChildIterator.Value()
520
521 res, anAttr=anSObject.FindAttribute("AttributeIOR")
522 if res :
523         anAttr=anAttr._narrow(SALOMEDS.AttributeIOR)
524         ior = anAttr.Value()
525         Graph=SuperV.getGraph(ior)
526         ListOfNodes=Graph.Nodes()
527         length_as= len(ListOfNodes)
528         print "ListOfNodes length = ", length_as
529         if length_as != length_bs:
530                 raise RuntimeErrror, "defferent length of nodes after study open"
531 #aChildIterator.Next()
532
533 Names = []
534 for node in ListOfNodes:
535         Names.append(node.Name())
536 names.sort()
537 Names.sort()
538 if names != Names :
539         raise RuntimeError, "List of dataflow nodes after save differs from one befor save operation"
540