Salome HOME
Enable C++0x/C++11 support
[plugins/blsurfplugin.git] / tests / test_periodicity.py
1 # -*- coding: utf-8 -*-
2
3 import salome
4
5 import GEOM
6 from salome.geom import geomBuilder
7 geompy = geomBuilder.New(salome.myStudy)
8
9 import math
10
11 simple = False
12
13 r = 10
14 dist = 10
15
16 p1 = geompy.MakeVertex(0., 0., 0.)
17 p2 = geompy.MakeVertex(100., 100., 100.)
18 box = geompy.MakeBoxTwoPnt(p1, p2)
19 geompy.addToStudy(box, "box")
20
21 p3 = geompy.MakeVertex(25., 5., 25.)
22 sphere1 = geompy.MakeSpherePntR(p3, 15.)
23 geompy.addToStudy(sphere1, "sphere1")
24
25 sphere1_trans = geompy.MakeTranslation(sphere1, 0, 100, 0)
26 geompy.addToStudy(sphere1_trans, "sphere1_trans")
27
28 sphere1_trans = geompy.MakeTranslation(sphere1, 0, 100, 0)
29 geompy.addToStudy(sphere1_trans, "sphere1_trans")
30
31 p4 = geompy.MakeVertex(5, 50, 90)
32 sphere2 = geompy.MakeSpherePntR(p4, 20.)
33
34 sphere2_trans = geompy.MakeTranslation(sphere2, 100, 0, 0)
35 geompy.addToStudy(sphere2_trans, "sphere2_trans")
36
37 sphere2_trans2 = geompy.MakeTranslation(sphere2, 0, 0, -100)
38 geompy.addToStudy(sphere2_trans2, "sphere2_trans2")
39
40 sphere2_trans3 = geompy.MakeTranslation(sphere2, 100, 0, -100)
41 geompy.addToStudy(sphere2_trans3, "sphere2_trans3")
42
43 if simple:
44     part = box
45 else:
46     part = geompy.MakePartition([box], [sphere1, sphere1_trans, sphere2, sphere2_trans, sphere2_trans2, sphere2_trans3])
47 geompy.addToStudy(part, "part")
48
49 Vx = geompy.MakeVectorDXDYDZ(1, 0, 0)
50 Vy = geompy.MakeVectorDXDYDZ(0, 1, 0)
51 Vz = geompy.MakeVectorDXDYDZ(0, 0, 1)
52
53 left_faces = geompy.GetShapesOnPlane(part, geompy.ShapeType["FACE"], Vy, GEOM.ST_ON)
54 left = geompy.CreateGroup(part, geompy.ShapeType["FACE"])
55 geompy.UnionList(left, left_faces)
56 geompy.addToStudyInFather(part, left, "left")
57
58 right_faces = geompy.GetShapesOnPlaneWithLocation(part, geompy.ShapeType["FACE"], Vy, p2, GEOM.ST_ON)
59 right = geompy.CreateGroup(part, geompy.ShapeType["FACE"])
60 geompy.UnionList(right, right_faces)
61 geompy.addToStudyInFather(part, right, "right")
62
63 back_faces = geompy.GetShapesOnPlane(part, geompy.ShapeType["FACE"], Vx, GEOM.ST_ON)
64 back = geompy.CreateGroup(part, geompy.ShapeType["FACE"])
65 geompy.UnionList(back, back_faces)
66 geompy.addToStudyInFather(part, back, "back")
67
68 front_faces = geompy.GetShapesOnPlaneWithLocation(part, geompy.ShapeType["FACE"], Vx, p2, GEOM.ST_ON)
69 front = geompy.CreateGroup(part, geompy.ShapeType["FACE"])
70 geompy.UnionList(front, front_faces)
71 geompy.addToStudyInFather(part, front, "front")
72
73 bottom_faces = geompy.GetShapesOnPlane(part, geompy.ShapeType["FACE"], Vz, GEOM.ST_ON)
74 bottom = geompy.CreateGroup(part, geompy.ShapeType["FACE"])
75 geompy.UnionList(bottom, bottom_faces)
76 geompy.addToStudyInFather(part, bottom, "bottom")
77
78 top_faces = geompy.GetShapesOnPlaneWithLocation(part, geompy.ShapeType["FACE"], Vz, p2, GEOM.ST_ON)
79 top = geompy.CreateGroup(part, geompy.ShapeType["FACE"])
80 geompy.UnionList(top, top_faces)
81 geompy.addToStudyInFather(part, top, "top")
82
83 sources = geompy.CreateGroup(part, geompy.ShapeType["FACE"])
84 geompy.UnionList(sources, left_faces)
85 geompy.UnionList(sources, back_faces)
86 geompy.UnionList(sources, top_faces)
87 geompy.addToStudyInFather(part, sources, "sources")
88
89
90 # Mesh
91 # ====
92
93 import SMESH
94 from salome.smesh import smeshBuilder
95 smesh = smeshBuilder.New(salome.myStudy)
96
97 from salome.BLSURFPlugin import BLSURFPluginBuilder
98
99 Mesh = smesh.Mesh(part, "Mesh")
100
101 algo2d = Mesh.Triangle(algo=smeshBuilder.MG_CADSurf)
102 algo2d.SetGeometricMesh( 1 )
103 algo2d.SetAngleMesh( 4 )
104 algo2d.SetPhySize( 8 )
105 algo2d.SetOptionValue( 'periodic_tolerance', '1e-2' )
106 # Deactivate PreCAD
107 algo2d.SetTopology(BLSURFPluginBuilder.FromCAD)
108
109 def proj_x(shape1):
110     shape2 = geompy.MakeTranslation(shape1, 100, 0., 0)
111     return shape2
112     
113 def proj_y(shape1):
114     shape2 = geompy.MakeTranslation(shape1, 0, 100., 0)
115     return shape2
116
117 def proj_z(shape1):
118     shape2 = geompy.MakeTranslation(shape1, 0, 0, 100.)
119     return shape2
120
121 algo2d.AddAdvancedFacesPeriodicity(back, front, proj_x)
122 algo2d.AddAdvancedFacesPeriodicity(left, right, proj_y)
123 algo2d.AddAdvancedFacesPeriodicity(bottom, top, proj_z)
124
125 gr_left = Mesh.Group(left)
126 gr_right = Mesh.Group(right)
127 gr_front = Mesh.Group(front)
128 gr_back = Mesh.Group(back)
129 gr_bottom = Mesh.Group(bottom)
130 gr_top = Mesh.Group(top)
131
132 Mesh.Compute()
133
134 left_translated = Mesh.TranslateObjectMakeMesh( gr_left, SMESH.DirStruct( SMESH.PointStruct ( 0, 100, 0 )), 0, 'left_translated' )
135 front_translated = Mesh.TranslateObjectMakeMesh( gr_front, SMESH.DirStruct( SMESH.PointStruct ( -100, 0, 0 )), 0, 'front_translated' )
136 bottom_translated = Mesh.TranslateObjectMakeMesh( gr_bottom, SMESH.DirStruct( SMESH.PointStruct ( 0, 0, 100 )), 0, 'bottom_translated' )
137
138 def checkProjection(gr, mesh_translated, tol=1e-7):
139     name = gr.GetName() + "_" + mesh_translated.GetName().split("_")[0]
140     mesh_source = smesh.CopyMesh(gr, gr.GetName())
141     mesh_check = smesh.Concatenate([mesh_source.GetMesh(), mesh_translated.GetMesh()], 0, name=name)
142     ll_coincident_nodes = mesh_check.FindCoincidentNodes(tol)
143     coincident_nodes = [item for sublist in ll_coincident_nodes for item in sublist]
144     mesh_check.MakeGroupByIds("coincident_nodes", SMESH.NODE, coincident_nodes)
145     mesh_nodes = mesh_check.GetNodesId()
146     if len(ll_coincident_nodes) != mesh_translated.NbNodes():
147         non_coincident_nodes = list(set(mesh_nodes) - set(coincident_nodes))
148         mesh_check.MakeGroupByIds("non_coincident_nodes", SMESH.NODE, non_coincident_nodes)
149         raise Exception("Projection failed for %s"%name)
150         
151 checkProjection(gr_right, left_translated)
152 checkProjection(gr_back, front_translated)
153 checkProjection(gr_top, bottom_translated)
154
155 salome.sg.updateObjBrowser(0)
156