Salome HOME
updated copyright message
[plugins/blsurfplugin.git] / tests / periodicity_2D_prepro.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2013-2023  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import sys
22 import salome
23
24 ###
25 ### GEOM component
26 ###
27
28 import math
29
30 import GEOM
31 from salome.geom import geomBuilder
32 geompy = geomBuilder.New()
33
34 O = geompy.MakeVertex(0, 0, 0)
35 OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
36 OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
37 OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
38 Face_1 = geompy.MakeFaceHW(10, 10, 1)
39 geomObj_1 = geompy.MakeMarker(0, 0, 0, 1, 0, 0, 0, 1, 0)
40 Sketch_1 = geompy.MakeSketcherOnPlane("Sketcher:F -2.5209155082703 -1.4416453838348:TT -1.1105282306671 -2.9872753620148:TT 0.76354801654816 -2.3303825855255:TT 1.9614112377167 -3.0838770866394:TT 3.8354876041412 -1.2677619457245:TT 4.2218952178955 0.644955098629:TT 3.2751967906952 2.5576722621918:TT 0.58966463804245 3.5430111885071:TT -3.7380990982056 3.2338852882385:TT -4.433632850647 0.85747921466827:WW", Face_1 )
41 vertices = geompy.ExtractShapes(Sketch_1, geompy.ShapeType["VERTEX"], True)
42 Curve_1 = geompy.MakeInterpol(vertices, True, True)
43
44 part = geompy.MakePartition([Face_1], [Curve_1], Limit=geompy.ShapeType["FACE"])
45 geompy.addToStudy(part, "part")
46
47 Vx = geompy.MakeVectorDXDYDZ(1, 0, 0)
48 Vy = geompy.MakeVectorDXDYDZ(0, 1, 0)
49 Vz = geompy.MakeVectorDXDYDZ(0, 0, 1)
50
51 p1 = geompy.MakeVertex(-5, -5, 0)
52 p2 = geompy.MakeVertex(5, 5, 0)
53 left_edges = geompy.GetShapesOnPlaneWithLocation(part, geompy.ShapeType["EDGE"], Vx, p1, GEOM.ST_ON)
54 left = geompy.CreateGroup(part, geompy.ShapeType["EDGE"])
55 geompy.UnionList(left, left_edges)
56 geompy.addToStudyInFather(part, left, "left")
57
58 right_edges = geompy.GetShapesOnPlaneWithLocation(part, geompy.ShapeType["EDGE"], Vx, p2, GEOM.ST_ON)
59 right = geompy.CreateGroup(part, geompy.ShapeType["EDGE"])
60 geompy.UnionList(right, right_edges)
61 geompy.addToStudyInFather(part, right, "right")
62
63 bottom_edges = geompy.GetShapesOnPlaneWithLocation(part, geompy.ShapeType["EDGE"], Vy, p1, GEOM.ST_ON)
64 bottom = geompy.CreateGroup(part, geompy.ShapeType["EDGE"])
65 geompy.UnionList(bottom, bottom_edges)
66 geompy.addToStudyInFather(part, bottom, "bottom")
67
68 top_edges = geompy.GetShapesOnPlaneWithLocation(part, geompy.ShapeType["EDGE"], Vy, p2, GEOM.ST_ON)
69 top = geompy.CreateGroup(part, geompy.ShapeType["EDGE"])
70 geompy.UnionList(top, top_edges)
71 geompy.addToStudyInFather(part, top, "top")
72
73 source_face = geompy.GetFaceNearPoint(part, p1)
74 geompy.addToStudyInFather(part, source_face, "source_face")
75
76
77 # Mesh
78 # ====
79
80 import SMESH
81 from salome.smesh import smeshBuilder
82 smesh = smeshBuilder.New()
83
84 Mesh = smesh.Mesh(part, "Mesh")
85
86 algo2d = Mesh.Triangle(algo=smeshBuilder.MG_CADSurf)
87 algo2d.SetGeometricMesh( 1 )
88 algo2d.SetAngleMesh( 4 )
89 algo2d.SetPhySize( 8 )
90 #algo2d.SetGradation(1.05)
91
92 # Periodicity
93 #algo2d.SetVerbosity(10)
94 algo2d.SetPreCADOptionValue("periodic_tolerance", "1e-2")
95 algo2d.AddPreCadEdgesPeriodicity(left, right)
96 algo2d.AddPreCadEdgesPeriodicity(bottom, top)
97
98
99 Mesh.Compute()
100
101 gr_left = Mesh.Group(left)
102 gr_right = Mesh.Group(right)
103 gr_bottom = Mesh.Group(bottom)
104 gr_top = Mesh.Group(top)
105
106 Mesh.Compute()
107
108 left_translated = Mesh.TranslateObjectMakeMesh( gr_left, SMESH.DirStruct( SMESH.PointStruct ( 10, 0, 0 )), 0, 'left_translated' )
109 bottom_translated = Mesh.TranslateObjectMakeMesh( gr_bottom, SMESH.DirStruct( SMESH.PointStruct ( 0, 10, 0 )), 0, 'bottom_translated' )
110
111 def checkProjection(gr, mesh_translated, tol=1e-7):
112     name = gr.GetName() + "_" + mesh_translated.GetName().split("_")[0]
113     mesh_source = smesh.CopyMesh(gr, gr.GetName())
114     mesh_check = smesh.Concatenate([mesh_source.GetMesh(), mesh_translated.GetMesh()], 0, name=name)
115     ll_coincident_nodes = mesh_check.FindCoincidentNodes(tol)
116     coincident_nodes = [item for sublist in ll_coincident_nodes for item in sublist]
117     mesh_check.MakeGroupByIds("coincident_nodes", SMESH.NODE, coincident_nodes)
118     mesh_nodes = mesh_check.GetNodesId()
119     if len(ll_coincident_nodes) != mesh_translated.NbNodes():
120         non_coincident_nodes = list(set(mesh_nodes) - set(coincident_nodes))
121         mesh_check.MakeGroupByIds("non_coincident_nodes", SMESH.NODE, non_coincident_nodes)
122         raise Exception("Projection failed for %s"%name)
123         
124 checkProjection(gr_right, left_translated)
125 checkProjection(gr_top, bottom_translated)
126
127 if salome.sg.hasDesktop():
128   salome.sg.updateObjBrowser()