1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
\r
3 <!-- hennerik CVSweb $Revision$ -->
\r
4 <TITLE>Exemple</TITLE></HEAD>
\r
5 <BODY BGCOLOR="#eeeeee">
\r
7 #^ SALOMEDS methods : FindOrCreateAttribute, Save, SaveAs, Close, Open, FindComponent, FindAttribute, FindObject, LoadWith^
\r
8 #^ Attribute methods : Value, SetValue^
\r
9 #======================================================================
\r
10 #1. Create Study and add some components to it
\r
11 #======================================================================
\r
13 #=================================
\r
14 # create AttributeReal
\r
15 #=================================
\r
16 A = batchmode_geompy.myBuilder.FindOrCreateAttribute(batchmode_geompy.father, "AttributeReal")
\r
18 raise RuntimeError, "Can't create AttributeReal attribute"
\r
19 A = A._narrow(SALOMEDS.AttributeReal)
\r
21 if A.Value() != 0.0001:
\r
22 raise RuntimeError, "Error : wrong value of AttributeReal"
\r
24 # === Geometry ==================================================
\r
26 import batchmode_geompy
\r
28 #===================================
\r
29 # define a box
\r
30 #===================================
\r
32 box = batchmode_geompy.MakeBox(0., 0., 0., 100., 200., 300.)
\r
33 idbox = batchmode_geompy.addToStudy(box,"box")
\r
36 geom = batchmode_geompy.geom
\r
38 import batchmode_smesh
\r
39 ShapeType = batchmode_smesh.ShapeType
\r
41 # ---- add first face of box in study
\r
43 subShapeList = batchmode_geompy.SubShapeAll(box,ShapeType["Face"])
\r
44 face=subShapeList[0]
\r
45 name = "box_face"
\r
47 idface= batchmode_geompy.addToStudyInFather(box,face,name)
\r
49 # ---- add shell from box in study
\r
51 subShellList= batchmode_geompy.SubShapeAll(box,ShapeType["Shell"])
\r
52 shell = subShellList[0]
\r
53 name = "box_shell"
\r
55 idshell= batchmode_geompy.addToStudyInFather(box,shell,name)
\r
57 # ---- add first edge of face in study
\r
59 edgeList = batchmode_geompy.SubShapeAll(face,ShapeType["Edge"])
\r
61 name = "face_edge"
\r
63 idedge= batchmode_geompy.addToStudyInFather(face,edge,name)
\r
65 # ---- launch SMESH, init a Mesh with the box
\r
67 smesh = batchmode_smesh.smesh
\r
68 # -- Init --
\r
69 shape = batchmode_geompy.IDToObject(idbox)
\r
70 mesh=smesh.Init(geom, batchmode_geompy.myStudyId, shape)
\r
72 orb = batchmode_geompy.orb
\r
74 idmesh = batchmode_smesh.AddNewMesh( orb.object_to_string(mesh) )
\r
75 batchmode_smesh.SetName(idmesh, "Meshbox");
\r
76 batchmode_smesh.SetShape(idbox, idmesh);
\r
78 # ---- create Hypothesis
\r
80 print "-------------------------- create Hypothesis ----------------------"
\r
81 print "-------------------------- LocalLength"
\r
82 hyp1 = smesh.CreateHypothesis("LocalLength", batchmode_geompy.myStudyId )
\r
83 hypLen1 = hyp1._narrow(SMESH.SMESH_LocalLength)
\r
84 hypLen1.SetLength(100)
\r
85 print hypLen1.GetName()
\r
86 print hypLen1.GetId()
\r
87 print hypLen1.GetLength()
\r
89 idlength = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypLen1) );
\r
90 batchmode_smesh.SetName(idlength, "Local_Length_100");
\r
92 print "-------------------------- NumberOfSegments"
\r
93 hyp2 = smesh.CreateHypothesis("NumberOfSegments", batchmode_geompy.myStudyId )
\r
94 hypNbSeg1=hyp2._narrow(SMESH.SMESH_NumberOfSegments)
\r
95 hypNbSeg1.SetNumberOfSegments(7)
\r
96 print hypNbSeg1.GetName()
\r
97 print hypNbSeg1.GetId()
\r
98 print hypNbSeg1.GetNumberOfSegments()
\r
100 idseg = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypNbSeg1) );
\r
101 batchmode_smesh.SetName(idseg, "NumberOfSegments_7");
\r
103 print "-------------------------- MaxElementArea"
\r
104 hyp3 = smesh.CreateHypothesis("MaxElementArea", batchmode_geompy.myStudyId)
\r
105 hypArea1=hyp3._narrow(SMESH.SMESH_MaxElementArea)
\r
106 hypArea1.SetMaxElementArea(2500)
\r
107 print hypArea1.GetName()
\r
108 print hypArea1.GetId()
\r
109 print hypArea1.GetMaxElementArea()
\r
111 idarea1 = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypArea1) );
\r
112 batchmode_smesh.SetName(idarea1, "MaxElementArea_2500");
\r
114 print "-------------------------- MaxElementArea"
\r
115 hyp3 = smesh.CreateHypothesis("MaxElementArea", batchmode_geompy.myStudyId)
\r
116 hypArea2 = hyp3._narrow(SMESH.SMESH_MaxElementArea)
\r
117 hypArea2.SetMaxElementArea(500)
\r
118 print hypArea2.GetName()
\r
119 print hypArea2.GetId()
\r
120 print hypArea2.GetMaxElementArea()
\r
122 idarea2 = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypArea2) );
\r
123 batchmode_smesh.SetName(idarea2, "MaxElementArea_500");
\r
125 print "-------------------------- Regular_1D"
\r
126 alg1 = smesh.CreateHypothesis("Regular_1D", batchmode_geompy.myStudyId)
\r
127 algo1 = alg1._narrow(SMESH.SMESH_Algo)
\r
128 listHyp = algo1.GetCompatibleHypothesis()
\r
129 for hyp in listHyp:
\r
130 print hyp
\r
131 algoReg=alg1._narrow(SMESH.SMESH_Regular_1D)
\r
132 print algoReg.GetName()
\r
133 print algoReg.GetId()
\r
135 idreg = batchmode_smesh.AddNewAlgorithms( orb.object_to_string(algoReg) );
\r
136 batchmode_smesh.SetName(idreg, "Regular_1D");
\r
138 print "-------------------------- MEFISTO_2D"
\r
139 alg2 = smesh.CreateHypothesis("MEFISTO_2D", batchmode_geompy.myStudyId)
\r
140 algo2 = alg2._narrow(SMESH.SMESH_Algo)
\r
141 listHyp=algo2.GetCompatibleHypothesis()
\r
142 for hyp in listHyp:
\r
143 print hyp
\r
144 algoMef=alg2._narrow(SMESH.SMESH_MEFISTO_2D)
\r
145 print algoMef.GetName()
\r
146 print algoMef.GetId()
\r
148 idmef = batchmode_smesh.AddNewAlgorithms( orb.object_to_string(algoMef) );
\r
149 batchmode_smesh.SetName(idmef, "MEFISTO_2D");
\r
152 # ---- add hypothesis to edge
\r
154 print "-------------------------- add hypothesis to edge"
\r
155 edge = batchmode_geompy.IDToObject(idedge)
\r
156 submesh = mesh.GetElementsOnShape(edge)
\r
157 ret = mesh.AddHypothesis(edge,algoReg)
\r
159 ret=mesh.AddHypothesis(edge,hypLen1)
\r
162 idsm1 = batchmode_smesh.AddSubMeshOnShape( idmesh,
\r
163 idedge,
\r
164 orb.object_to_string(submesh),
\r
165 ShapeType["Edge"] )
\r
166 batchmode_smesh.SetName(idsm1, "SubMeshEdge")
\r
167 batchmode_smesh.SetAlgorithms( idsm1, idreg );
\r
168 batchmode_smesh.SetHypothesis( idsm1, idlength );
\r
170 print "-------------------------- add hypothesis to face"
\r
171 face=batchmode_geompy.IDToObject(idface)
\r
172 submesh=mesh.GetElementsOnShape(face)
\r
173 ret=mesh.AddHypothesis(face,hypArea2)
\r
176 idsm2 = batchmode_smesh.AddSubMeshOnShape( idmesh,
\r
177 idface,
\r
178 orb.object_to_string(submesh),
\r
179 ShapeType["Face"] )
\r
180 batchmode_smesh.SetName(idsm2, "SubMeshFace")
\r
181 batchmode_smesh.SetHypothesis( idsm2, idarea2 );
\r
183 # ---- add hypothesis to box
\r
185 print "-------------------------- add hypothesis to box"
\r
186 box=batchmode_geompy.IDToObject(idbox)
\r
187 submesh=mesh.GetElementsOnShape(box)
\r
188 ret=mesh.AddHypothesis(box,algoReg)
\r
190 ret=mesh.AddHypothesis(box,hypNbSeg1)
\r
192 ret=mesh.AddHypothesis(box,algoMef)
\r
194 ret=mesh.AddHypothesis(box,hypArea1)
\r
197 batchmode_smesh.SetAlgorithms( idmesh, idreg );
\r
198 batchmode_smesh.SetHypothesis( idmesh, idseg );
\r
199 batchmode_smesh.SetAlgorithms( idmesh, idmef );
\r
200 batchmode_smesh.SetHypothesis( idmesh, idarea1 );
\r
204 print "-------------------------- compute box"
\r
205 ret=smesh.Compute(mesh,box)
\r
207 log=mesh.GetLog(0); # no erase trace
\r
208 for linelog in log:
\r
209 print linelog
\r
212 #=======================================================
\r
213 # add SuperVision component
\r
214 #=======================================================
\r
216 from batchmode_SuperV import *
\r
218 dir= os.getenv("SALOME_ROOT_DIR")
\r
220 raise RuntimeError, "SALOME_ROOT_DIR is not defined"
\r
221 xmlfile = dir +"/../SALOME_ROOT/SuperVisionTest/resources/GraphEssai.xml"
\r
222 print "--------------\n"+xmlfile+"\n--------------\n"
\r
224 myGraph = Graph ( xmlfile )
\r
226 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
\r
227 print myGraph.IsValid()
\r
230 myGraph.PrintNodes()
\r
231 Add,Sub,Mul,Div = myGraph.Nodes()
\r
234 Addx = Add.Input("x",3.)
\r
235 Addy = Add.Input("y",4.5)
\r
236 Subx = Sub.Input("x",1.5)
\r
239 Addz = Add.Port('z')
\r
240 Subz = Sub.Port('z')
\r
241 Mulz = Mul.Port('z')
\r
242 Divz = Div.Port('z')
\r
244 # This DataFlow is "executable" : all pending Ports are defined with Datas
\r
245 print myGraph.IsExecutable()
\r
247 # Starts only execution of that DataFlow and gets control immediatly
\r
248 print myGraph.Run()
\r
250 # That DataFlow is running ==> 0 (false)
\r
251 print myGraph.IsDone()
\r
253 # Events of execution :
\r
254 aStatus,aNode,anEvent,aState = myGraph.Event()
\r
256 print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
\r
257 aStatus,aNode,anEvent,aState = myGraph.Event()
\r
258 print myGraph.IsDone()
\r
260 # Wait for Completion (but it is already done after event loop ...)
\r
261 print "Done : ",myGraph.DoneW()
\r
264 print "Result : ",Divz.ToString()
\r
266 # Intermediate results :
\r
267 print "Intermediate Result Add\z : ",Addz.ToString()
\r
268 print "Intermediate Result Sub\z : ",Subz.ToString()
\r
269 print "Intermediate Result Mul\z : ",Mulz.ToString()
\r
271 print " "
\r
272 #print "Type : print myGraph.IsDone()"
\r
273 #print " If execution is finished ==> 1 (true)"
\r
274 res=myGraph.IsDone()
\r
276 raise RuntimeError, "myGraph.Run() is not done"
\r
278 print " "
\r
279 print "Type : print Divz.ToString()"
\r
280 print " You will get the result"
\r
283 print " "
\r
284 print "Type : myGraph.PrintPorts()"
\r
285 print " to see input and output values of the graph"
\r
286 myGraph.PrintPorts()
\r
288 print " "
\r
289 print "Type : Add.PrintPorts()"
\r
292 print "Type : Sub.PrintPorts()"
\r
295 print "Type : Mul.PrintPorts()"
\r
298 print "Type : Div.PrintPorts()"
\r
299 print " to see input and output values of nodes"
\r
302 # Export will create newsupervisionexample.xml and the corresponding .py file
\r
303 tmpdir=os.getenv("TmpDir")
\r
305 tmpdir="/tmp"
\r
306 file = tmpdir + "/newsupervisionexample"
\r
307 print "--------------\n"+file+"\n--------------\n"
\r
308 myGraph.Export(file)
\r
310 ior = batchmode_geompy.orb.object_to_string(myGraph.G)
\r
313 GraphName = myGraph.Name()
\r
314 print "Befor save ",
\r
315 nodes = myGraph.Nodes()
\r
316 length_bs = len(nodes)
\r
317 print "ListOfNodes length = ", length_bs
\r
320 names.append(node.Name())
\r
324 #=================================
\r
325 # save / restore study
\r
326 #=================================
\r
327 str= os.getenv("TmpDir")
\r
328 if str == None:
\r
329 str = "/tmp"
\r
330 file = str+"/test.hdf"
\r
333 #==================================================
\r
335 #==================================================
\r
336 print " ------- We will save to", file, "-----------"
\r
338 batchmode_geompy.myStudyManager.SaveAs(file, batchmode_geompy.myStudy)
\r
339 batchmode_geompy.myStudyManager.Close(batchmode_geompy.myStudy)
\r
341 #rename the file and try to reread it again
\r
343 os.mkdir(str + "/test_dir")
\r
345 #2.==================================================
\r
346 print " ------- We rename the file as " + str + "/test_dir/test_new.hdf"
\r
347 os.rename(file, str+"/test_dir/test_new.hdf")
\r
349 #==================================================
\r
351 #==================================================
\r
352 print " ------- We try to open " + str + "/test_dir/test_new.hdf"
\r
355 openedStudy=batchmode_geompy.myStudyManager.Open(str+"/test_dir/test_new.hdf")
\r
357 raise RuntimeError, "Can't open saved study!"
\r
359 if openedStudy == None:
\r
360 raise RuntimeError, "Can't open saved study!"
\r
362 father = openedStudy.FindComponent("GEOM")
\r
364 raise RuntimeError, "GEOM component is not found! Wrong study is opened."
\r
367 #=================================
\r
368 # find AttributeReal
\r
369 #=================================
\r
371 res,A=father.FindAttribute("AttributeReal")
\r
372 if res == 0 or A == None:
\r
373 raise RuntimeError, "Error: not found AttributeReal"
\r
375 A = A._narrow(SALOMEDS.AttributeReal)
\r
376 if A.Value() != 0.0001:
\r
377 raise RuntimeError, "Error : wrong value of AttributeReal"
\r
379 #==================================================
\r
380 # find box
\r
381 #==================================================
\r
382 box = openedStudy.FindObject("box")
\r
384 raise RuntimeError, "box was not found! Wrong study is opened."
\r
386 edge=openedStudy.FindObject("face_edge")
\r
388 raise RuntimeError, "face_edge was not found! Wrong study is opened."
\r
391 father = openedStudy.FindComponent("MESH")
\r
393 raise RuntimeError, "MESH component is not found! Wrong study is opened."
\r
395 hp=openedStudy.FindObject("Meshbox")
\r
397 raise RuntimeError, "Meshbox object was not found! Wrong study is opened."
\r
399 hp = openedStudy.FindObject("Algorithms Definition")
\r
401 raise RuntimeError, "Algorithms Definition object was not found! Wrong study is opened."
\r
403 hp = openedStudy.FindObject("MEFISTO_2D")
\r
405 raise RuntimeError, "MEFISTO_2D object was not found! Wrong study is opened."
\r
407 hp = openedStudy.FindObject("Hypothesis Definition")
\r
409 raise RuntimeError, "Algoriths Definition was not found! Wrong study is opened."
\r
411 #==================================================
\r
413 #==================================================
\r
414 father = openedStudy.FindComponent("SUPERV")
\r
415 if father is None:
\r
416 raise RuntimeError, "SUPERV component is not found! Wrong study is opened."
\r
418 SuperV = lcc.FindOrLoadComponent("SuperVisionContainer","Supervision")
\r
419 Builder = openedStudy.NewBuilder()
\r
420 Builder.LoadWith(father, SuperV)
\r
423 aChildIterator = openedStudy.NewChildIterator(father)
\r
425 #while aChildIterator.More():
\r
426 anSObject = aChildIterator.Value()
\r
427 #print "iterate: ", anSObject.GetID()
\r
428 res, anAttr=anSObject.FindAttribute("AttributeIOR")
\r
430 anAttr=anAttr._narrow(SALOMEDS.AttributeIOR)
\r
431 ior = anAttr.Value()
\r
432 Graph=SuperV.getGraph(ior)
\r
433 ListOfNodes=Graph.Nodes()
\r
434 length_as= len(ListOfNodes)
\r
435 print "ListOfNodes length = ", length_as
\r
436 if length_as != length_bs:
\r
437 raise RuntimeErrror, "defferent length of nodes after study open"
\r
438 #aChildIterator.Next()
\r
441 for node in ListOfNodes:
\r
442 Names.append(node.Name())
\r
445 if names != Names :
\r
446 raise RuntimeError, "List of dataflow nodes after save differs from one befor save operation"
\r
448 #==================================================
\r
450 #==================================================
\r
451 batchmode_geompy.myStudyManager.Save(openedStudy)
\r
452 batchmode_geompy.myStudyManager.Close(openedStudy)
\r
453 #==================================================
\r
455 #==================================================
\r
457 openedStudy = batchmode_geompy.myStudyManager.Open(str+"/test_dir/test_new.hdf")
\r
459 raise RuntimeError, "Can't open saved study!"
\r
463 if openedStudy == None:
\r
464 raise RuntimeError, "Can't open saved study!"
\r
466 father = openedStudy.FindComponent("GEOM")
\r
468 raise RuntimeError, "Geom component is not found! Wrong study is opened."
\r
471 #find AttributeReal
\r
472 res,A=father.FindAttribute("AttributeReal")
\r
473 if res == 0 or A == None:
\r
474 raise RuntimeError, "Error: not found AttributeReal"
\r
476 A = A._narrow(SALOMEDS.AttributeReal)
\r
477 if A.Value() != 0.0001:
\r
478 raise RuntimeError, "Error : wrong value of AttributeReal"
\r
479 #==================================================
\r
480 # find box
\r
481 #==================================================
\r
482 box = openedStudy.FindObject("box")
\r
484 raise RuntimeError, "box was not found! Wrong study is opened."
\r
486 edge=openedStudy.FindObject("face_edge")
\r
488 raise RuntimeError, "face_edge was not found! Wrong study is opened."
\r
491 father = openedStudy.FindComponent("MESH")
\r
493 raise RuntimeError, "MESH component is not found! Wrong study is opened."
\r
495 hp=openedStudy.FindObject("Meshbox")
\r
497 raise RuntimeError, "Meshbox object was not found! Wrong study is opened."
\r
499 hp = openedStudy.FindObject("Algorithms Definition")
\r
501 raise RuntimeError, "Algorithms Definition object was not found! Wrong study is opened."
\r
503 hp = openedStudy.FindObject("MEFISTO_2D")
\r
505 raise RuntimeError, "MEFISTO_2D object was not found! Wrong study is opened."
\r
507 hp = openedStudy.FindObject("Hypothesis Definition")
\r
509 raise RuntimeError, "Algoriths Definition was not found! Wrong study is opened."
\r
511 #==================================================
\r
513 #==================================================
\r
514 father = openedStudy.FindComponent("SUPERV")
\r
516 raise RuntimeError, "SUPERV component is not found! Wrong study is opened."
\r
518 SuperV = lcc.FindOrLoadComponent("SuperVisionContainer","Supervision")
\r
519 Builder = openedStudy.NewBuilder()
\r
520 Builder.LoadWith(father, SuperV)
\r
523 aChildIterator = openedStudy.NewChildIterator(father)
\r
525 #while aChildIterator.More():
\r
526 anSObject = aChildIterator.Value()
\r
528 res, anAttr=anSObject.FindAttribute("AttributeIOR")
\r
530 anAttr=anAttr._narrow(SALOMEDS.AttributeIOR)
\r
531 ior = anAttr.Value()
\r
532 Graph=SuperV.getGraph(ior)
\r
533 ListOfNodes=Graph.Nodes()
\r
534 length_as= len(ListOfNodes)
\r
535 print "ListOfNodes length = ", length_as
\r
536 if length_as != length_bs:
\r
537 raise RuntimeErrror, "defferent length of nodes after study open"
\r
538 #aChildIterator.Next()
\r
541 for node in ListOfNodes:
\r
542 Names.append(node.Name())
\r
545 if names != Names :
\r
546 raise RuntimeError, "List of dataflow nodes after save differs from one befor save operation"
\r