2 #======================================================================
3 #1. Create Study and add some components to it
4 #======================================================================
6 #=================================
8 #=================================
9 A = batchmode_geompy.myBuilder.FindOrCreateAttribute(batchmode_geompy.father, "AttributeReal")
11 raise RuntimeError, "Can't create AttributeReal attribute"
12 A = A._narrow(SALOMEDS.AttributeReal)
14 if A.Value() != 0.0001:
15 raise RuntimeError, "Error : wrong value of AttributeReal"
17 # === Geometry ==================================================
19 import batchmode_geompy
21 #===================================
23 #===================================
25 box = batchmode_geompy.MakeBox(0., 0., 0., 100., 200., 300.)
26 idbox = batchmode_geompy.addToStudy(box,"box")
29 geom = batchmode_geompy.geom
31 import batchmode_smesh
32 ShapeType = batchmode_smesh.ShapeType
34 # ---- add first face of box in study
36 subShapeList = batchmode_geompy.SubShapeAll(box,ShapeType["Face"])
40 idface= batchmode_geompy.addToStudyInFather(box,face,name)
42 # ---- add shell from box in study
44 subShellList= batchmode_geompy.SubShapeAll(box,ShapeType["Shell"])
45 shell = subShellList[0]
48 idshell= batchmode_geompy.addToStudyInFather(box,shell,name)
50 # ---- add first edge of face in study
52 edgeList = batchmode_geompy.SubShapeAll(face,ShapeType["Edge"])
56 idedge= batchmode_geompy.addToStudyInFather(face,edge,name)
58 # ---- launch SMESH, init a Mesh with the box
60 smesh = batchmode_smesh.smesh
62 shape = batchmode_geompy.IDToObject(idbox)
63 mesh=smesh.Init(geom, shape)
65 orb = batchmode_geompy.orb
67 idmesh = batchmode_smesh.AddNewMesh( orb.object_to_string(mesh) )
68 batchmode_smesh.SetName(idmesh, "Meshbox");
69 batchmode_smesh.SetShape(idbox, idmesh);
71 # ---- create Hypothesis
73 print("-------------------------- create Hypothesis ----------------------")
74 print("-------------------------- LocalLength")
75 hyp1 = smesh.CreateHypothesis("LocalLength")
76 hypLen1 = hyp1._narrow(SMESH.SMESH_LocalLength)
77 hypLen1.SetLength(100)
78 print(hypLen1.GetName())
79 print(hypLen1.GetId())
80 print(hypLen1.GetLength())
82 idlength = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypLen1) );
83 batchmode_smesh.SetName(idlength, "Local_Length_100");
85 print("-------------------------- NumberOfSegments")
86 hyp2 = smesh.CreateHypothesis("NumberOfSegments")
87 hypNbSeg1=hyp2._narrow(SMESH.SMESH_NumberOfSegments)
88 hypNbSeg1.SetNumberOfSegments(7)
89 print(hypNbSeg1.GetName())
90 print(hypNbSeg1.GetId())
91 print(hypNbSeg1.GetNumberOfSegments())
93 idseg = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypNbSeg1) );
94 batchmode_smesh.SetName(idseg, "NumberOfSegments_7");
96 print("-------------------------- MaxElementArea")
97 hyp3 = smesh.CreateHypothesis("MaxElementArea")
98 hypArea1=hyp3._narrow(SMESH.SMESH_MaxElementArea)
99 hypArea1.SetMaxElementArea(2500)
100 print(hypArea1.GetName())
101 print(hypArea1.GetId())
102 print(hypArea1.GetMaxElementArea())
104 idarea1 = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypArea1) );
105 batchmode_smesh.SetName(idarea1, "MaxElementArea_2500");
107 print("-------------------------- MaxElementArea")
108 hyp3 = smesh.CreateHypothesis("MaxElementArea")
109 hypArea2 = hyp3._narrow(SMESH.SMESH_MaxElementArea)
110 hypArea2.SetMaxElementArea(500)
111 print(hypArea2.GetName())
112 print(hypArea2.GetId())
113 print(hypArea2.GetMaxElementArea())
115 idarea2 = batchmode_smesh.AddNewHypothesis( orb.object_to_string(hypArea2) );
116 batchmode_smesh.SetName(idarea2, "MaxElementArea_500");
118 print("-------------------------- Regular_1D")
119 alg1 = smesh.CreateHypothesis("Regular_1D")
120 algo1 = alg1._narrow(SMESH.SMESH_Algo)
121 listHyp = algo1.GetCompatibleHypothesis()
124 algoReg=alg1._narrow(SMESH.SMESH_Regular_1D)
125 print(algoReg.GetName())
126 print(algoReg.GetId())
128 idreg = batchmode_smesh.AddNewAlgorithms( orb.object_to_string(algoReg) );
129 batchmode_smesh.SetName(idreg, "Regular_1D");
131 print("-------------------------- MEFISTO_2D")
132 alg2 = smesh.CreateHypothesis("MEFISTO_2D")
133 algo2 = alg2._narrow(SMESH.SMESH_Algo)
134 listHyp=algo2.GetCompatibleHypothesis()
137 algoMef=alg2._narrow(SMESH.SMESH_MEFISTO_2D)
138 print(algoMef.GetName())
139 print(algoMef.GetId())
141 idmef = batchmode_smesh.AddNewAlgorithms( orb.object_to_string(algoMef) );
142 batchmode_smesh.SetName(idmef, "MEFISTO_2D");
145 # ---- add hypothesis to edge
147 print("-------------------------- add hypothesis to edge")
148 edge = batchmode_geompy.IDToObject(idedge)
149 submesh = mesh.GetElementsOnShape(edge)
150 ret = mesh.AddHypothesis(edge,algoReg)
152 ret=mesh.AddHypothesis(edge,hypLen1)
155 idsm1 = batchmode_smesh.AddSubMeshOnShape( idmesh,
157 orb.object_to_string(submesh),
159 batchmode_smesh.SetName(idsm1, "SubMeshEdge")
160 batchmode_smesh.SetAlgorithms( idsm1, idreg );
161 batchmode_smesh.SetHypothesis( idsm1, idlength );
163 print("-------------------------- add hypothesis to face")
164 face=batchmode_geompy.IDToObject(idface)
165 submesh=mesh.GetElementsOnShape(face)
166 ret=mesh.AddHypothesis(face,hypArea2)
169 idsm2 = batchmode_smesh.AddSubMeshOnShape( idmesh,
171 orb.object_to_string(submesh),
173 batchmode_smesh.SetName(idsm2, "SubMeshFace")
174 batchmode_smesh.SetHypothesis( idsm2, idarea2 );
176 # ---- add hypothesis to box
178 print("-------------------------- add hypothesis to box")
179 box=batchmode_geompy.IDToObject(idbox)
180 submesh=mesh.GetElementsOnShape(box)
181 ret=mesh.AddHypothesis(box,algoReg)
183 ret=mesh.AddHypothesis(box,hypNbSeg1)
185 ret=mesh.AddHypothesis(box,algoMef)
187 ret=mesh.AddHypothesis(box,hypArea1)
190 batchmode_smesh.SetAlgorithms( idmesh, idreg );
191 batchmode_smesh.SetHypothesis( idmesh, idseg );
192 batchmode_smesh.SetAlgorithms( idmesh, idmef );
193 batchmode_smesh.SetHypothesis( idmesh, idarea1 );
197 print("-------------------------- compute box")
198 ret=smesh.Compute(mesh,box)
200 log=mesh.GetLog(0); # no erase trace
205 #=======================================================
206 # add SuperVision component
207 #=======================================================
209 from batchmode_SuperV import *
211 dir= os.getenv("SUPERV_ROOT_DIR")
213 raise RuntimeError, "SUPERV_ROOT_DIR is not defined"
214 xmlfile = dir +"/examples/GraphEssai.xml"
215 print("--------------\n"+xmlfile+"\n--------------\n")
217 myGraph = Graph ( xmlfile )
219 # This DataFlow is "valid" : no loop, correct links between Nodes etc...
220 print(myGraph.IsValid())
224 Add,Sub,Mul,Div = myGraph.Nodes()
227 Addx = Add.Input("x",3.)
228 Addy = Add.Input("y",4.5)
229 Subx = Sub.Input("x",1.5)
237 # This DataFlow is "executable" : all pending Ports are defined with Datas
238 print(myGraph.IsExecutable())
240 # Starts only execution of that DataFlow and gets control immediately
243 # That DataFlow is running ==> 0 (false)
244 print(myGraph.IsDone())
246 # Events of execution :
247 aStatus,aNode,anEvent,aState = myGraph.Event()
249 print(aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState)
250 aStatus,aNode,anEvent,aState = myGraph.Event()
251 print(myGraph.IsDone())
253 # Wait for Completion (but it is already done after event loop ...)
254 print("Done : ",myGraph.DoneW())
257 print("Result : ",Divz.ToString())
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())
265 #print("Type : print(myGraph.IsDone()")
266 #print(" If execution is finished ==> 1 (true)")
269 raise RuntimeError, "myGraph.Run() is not done"
272 print("Type : print(Divz.ToString()")
273 print(" You will get the result")
277 print("Type : myGraph.PrintPorts()")
278 print(" to see input and output values of the graph")
282 print("Type : Add.PrintPorts()")
285 print("Type : Sub.PrintPorts()")
288 print("Type : Mul.PrintPorts()")
291 print("Type : Div.PrintPorts()")
292 print(" to see input and output values of nodes")
295 # Export will create newsupervisionexample.xml and the corresponding .py file
296 tmpdir=os.getenv("TmpDir")
299 file = tmpdir + "/newsupervisionexample"
300 print("--------------\n"+file+"\n--------------\n")
303 ior = batchmode_geompy.orb.object_to_string(myGraph.G)
306 GraphName = myGraph.Name()
307 print("Before save ",)
308 nodes = myGraph.Nodes()
309 length_bs = len(nodes)
310 print("ListOfNodes length = ", length_bs)
313 names.append(node.Name())
317 #=================================
318 # save / restore study
319 #=================================
320 str= os.getenv("TmpDir")
323 file = str+"/test.hdf"
326 #==================================================
328 #==================================================
329 print(" ------- We will save to", file, "-----------")
331 batchmode_geompy.myStudy.SaveAs(file)
332 batchmode_geompy.myStudy.Clear()
334 #rename the file and try to reread it again
336 os.mkdir(str + "/test_dir")
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")
342 #==================================================
344 #==================================================
345 print(" ------- We try to open " + str + "/test_dir/test_new.hdf")
348 openedStudy=batchmode_geompy.myStudy.Open(str+"/test_dir/test_new.hdf")
350 raise RuntimeError, "Can't open saved study!"
352 if openedStudy == None:
353 raise RuntimeError, "Can't open saved study!"
355 father = openedStudy.FindComponent("GEOM")
357 raise RuntimeError, "GEOM component is not found! Wrong study is opened."
360 #=================================
362 #=================================
364 res,A=father.FindAttribute("AttributeReal")
365 if res == 0 or A == None:
366 raise RuntimeError, "Error: not found AttributeReal"
368 A = A._narrow(SALOMEDS.AttributeReal)
369 if A.Value() != 0.0001:
370 raise RuntimeError, "Error : wrong value of AttributeReal"
372 #==================================================
374 #==================================================
375 box = openedStudy.FindObject("box")
377 raise RuntimeError, "box was not found! Wrong study is opened."
379 edge=openedStudy.FindObject("face_edge")
381 raise RuntimeError, "face_edge was not found! Wrong study is opened."
384 father = openedStudy.FindComponent("MESH")
386 raise RuntimeError, "MESH component is not found! Wrong study is opened."
388 hp=openedStudy.FindObject("Meshbox")
390 raise RuntimeError, "Meshbox object was not found! Wrong study is opened."
392 hp = openedStudy.FindObject("Algorithms Definition")
394 raise RuntimeError, "Algorithms Definition object was not found! Wrong study is opened."
396 hp = openedStudy.FindObject("MEFISTO_2D")
398 raise RuntimeError, "MEFISTO_2D object was not found! Wrong study is opened."
400 hp = openedStudy.FindObject("Hypothesis Definition")
402 raise RuntimeError, "Algoriths Definition was not found! Wrong study is opened."
404 #==================================================
406 #==================================================
407 father = openedStudy.FindComponent("SUPERV")
409 raise RuntimeError, "SUPERV component is not found! Wrong study is opened."
411 SuperV = lcc.FindOrLoadComponent("SuperVisionContainer","Supervision")
412 Builder = openedStudy.NewBuilder()
413 Builder.LoadWith(father, SuperV)
416 aChildIterator = openedStudy.NewChildIterator(father)
418 #while aChildIterator.More():
419 anSObject = aChildIterator.Value()
420 #print("iterate: ", anSObject.GetID())
421 res, anAttr=anSObject.FindAttribute("AttributeIOR")
423 anAttr=anAttr._narrow(SALOMEDS.AttributeIOR)
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, "different length of nodes after study open"
431 #aChildIterator.Next()
434 for node in ListOfNodes:
435 Names.append(node.Name())
439 raise RuntimeError, "List of dataflow nodes after save differs from one before save operation"
441 #==================================================
443 #==================================================
444 batchmode_geompy.myStudy.Save()
445 batchmode_geompy.myStudy.Clear()
446 #==================================================
448 #==================================================
450 openedStudy = batchmode_geompy.myStudy.Open(str+"/test_dir/test_new.hdf")
452 raise RuntimeError, "Can't open saved study!"
456 if openedStudy == None:
457 raise RuntimeError, "Can't open saved study!"
459 father = openedStudy.FindComponent("GEOM")
461 raise RuntimeError, "Geom component is not found! Wrong study is opened."
465 res,A=father.FindAttribute("AttributeReal")
466 if res == 0 or A == None:
467 raise RuntimeError, "Error: not found AttributeReal"
469 A = A._narrow(SALOMEDS.AttributeReal)
470 if A.Value() != 0.0001:
471 raise RuntimeError, "Error : wrong value of AttributeReal"
472 #==================================================
474 #==================================================
475 box = openedStudy.FindObject("box")
477 raise RuntimeError, "box was not found! Wrong study is opened."
479 edge=openedStudy.FindObject("face_edge")
481 raise RuntimeError, "face_edge was not found! Wrong study is opened."
484 father = openedStudy.FindComponent("MESH")
486 raise RuntimeError, "MESH component is not found! Wrong study is opened."
488 hp=openedStudy.FindObject("Meshbox")
490 raise RuntimeError, "Meshbox object was not found! Wrong study is opened."
492 hp = openedStudy.FindObject("Algorithms Definition")
494 raise RuntimeError, "Algorithms Definition object was not found! Wrong study is opened."
496 hp = openedStudy.FindObject("MEFISTO_2D")
498 raise RuntimeError, "MEFISTO_2D object was not found! Wrong study is opened."
500 hp = openedStudy.FindObject("Hypothesis Definition")
502 raise RuntimeError, "Algoriths Definition was not found! Wrong study is opened."
504 #==================================================
506 #==================================================
507 father = openedStudy.FindComponent("SUPERV")
509 raise RuntimeError, "SUPERV component is not found! Wrong study is opened."
511 SuperV = lcc.FindOrLoadComponent("SuperVisionContainer","Supervision")
512 Builder = openedStudy.NewBuilder()
513 Builder.LoadWith(father, SuperV)
516 aChildIterator = openedStudy.NewChildIterator(father)
518 #while aChildIterator.More():
519 anSObject = aChildIterator.Value()
521 res, anAttr=anSObject.FindAttribute("AttributeIOR")
523 anAttr=anAttr._narrow(SALOMEDS.AttributeIOR)
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, "different length of nodes after study open"
531 #aChildIterator.Next()
534 for node in ListOfNodes:
535 Names.append(node.Name())
539 raise RuntimeError, "List of dataflow nodes after save differs from one before save operation"