Salome HOME
Update copyrights
[modules/smesh.git] / src / SMESH_SWIG / YACS_geomesh0.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2018-2019  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 # test used in YACS, ForEachLoop
22
23 def geomesh(l0, r0, h0, d0, d1, my_container, direc):
24   print("Géometrie et maillage barre : (l0, r0, h0, d0, d1)=", (l0, r0, h0, d0, d1))
25
26   import sys
27   import salome
28   salome.salome_init()
29   import GEOM
30   from salome.geom import geomBuilder
31   import math
32   import os
33   import SALOMEDS
34   import  SMESH
35   from salome.smesh import smeshBuilder
36   
37   my_container.load_component_Library("GEOM")
38   #engineGeom = my_container.create_component_instance("GEOM")
39   engineGeom = my_container.load_impl("GEOM","")
40   geompy = geomBuilder.New(engineGeom)
41   my_container.load_component_Library("SMESH")
42   #engineSmesh = my_container.create_component_instance("SMESH")
43   engineSmesh = my_container.load_impl("SMESH","")
44   smesh = smeshBuilder.New(engineSmesh,engineGeom)
45   print("instances Names:", engineGeom.instanceName, engineSmesh.instanceName)
46   print("instances:", engineGeom, engineSmesh)
47   print("builders:", geompy, smesh)
48   
49   volume = (2.*l0*r0 + 0.75*math.pi*r0*r0)*h0
50   O = geompy.MakeVertex(0, 0, 0)
51   OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
52   OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
53   OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
54   Vertex_1 = geompy.MakeVertex(-l0, -r0, 0)
55   Vertex_2 = geompy.MakeVertex(-l0-r0, 0, 0)
56   Vertex_3 = geompy.MakeVertex(-l0, r0, 0)
57   Vertex_4 = geompy.MakeVertex(l0, r0, 0)
58   Vertex_5 = geompy.MakeVertex(l0+r0, 0, 0)
59   Vertex_6 = geompy.MakeVertex(l0, -r0, 0)
60   Arc_1 = geompy.MakeArc(Vertex_1, Vertex_2, Vertex_3)
61   Arc_2 = geompy.MakeArc(Vertex_4, Vertex_5, Vertex_6)
62   Line_1 = geompy.MakeLineTwoPnt(Vertex_3, Vertex_4)
63   Line_2 = geompy.MakeLineTwoPnt(Vertex_6, Vertex_1)
64   Face_1 = geompy.MakeFaceWires([Arc_1, Arc_2, Line_1, Line_2], 1)
65   barre0 = geompy.MakePrismVecH(Face_1, OZ, h0)
66   Vertex_1a = geompy.MakeVertex(-l0, -r0/2, 0)
67   Vertex_2a = geompy.MakeVertex(-l0-r0/2, 0, 0)
68   Vertex_3a = geompy.MakeVertex(-l0, r0/2, 0)
69   Vertex_4a = geompy.MakeVertex(l0, r0/2, 0)
70   Vertex_5a = geompy.MakeVertex(l0+r0/2, 0, 0)
71   Vertex_6a = geompy.MakeVertex(l0, -r0/2, 0)
72   Arc_1a = geompy.MakeArc(Vertex_1a, Vertex_2a, Vertex_3a)
73   Arc_2a = geompy.MakeArc(Vertex_4a, Vertex_5a, Vertex_6a)
74   Line_1a = geompy.MakeLineTwoPnt(Vertex_3a, Vertex_4a)
75   Line_2a = geompy.MakeLineTwoPnt(Vertex_6a, Vertex_1a)
76   Face_1a = geompy.MakeFaceWires([Arc_1a, Arc_2a, Line_1a, Line_2a], 1)
77   barrea = geompy.MakePrismVecH(Face_1a, OZ, h0)
78   barreb = geompy.MakeCut(barre0, barrea)
79   Plane_1 = geompy.MakePlane(Vertex_1, OX, 2000)
80   Plane_2 = geompy.MakePlane(Vertex_6, OX, 2000)
81   barre = geompy.MakePartition([barreb], [Plane_1, Plane_2], [], [], geompy.ShapeType["SOLID"], 0, [], 0)
82   v1 = geompy.MakeVertex(-l0-r0, 0, h0/2.)
83   v2 = geompy.MakeVertex(l0+r0, 0, h0/2.)
84   f1 = geompy.GetShapesNearPoint(barre, v1, geompy.ShapeType["FACE"])
85   f2 = geompy.GetShapesNearPoint(barre, v2, geompy.ShapeType["FACE"])
86   #f1 = geompy.CreateGroup(barre, geompy.ShapeType["FACE"])
87   #geompy.UnionIDs(f1, [3])
88   #f2 = geompy.CreateGroup(barre, geompy.ShapeType["FACE"])
89   #geompy.UnionIDs(f2, [20])
90
91   Auto_group_for_Sub_mesh_1 = geompy.CreateGroup(barre, geompy.ShapeType["FACE"])
92   geompy.UnionList(Auto_group_for_Sub_mesh_1, [f1, f2])
93   nom = r'barre_l_{:03d}__r_{:05.2f}__h_{:05.2f}__d0_{:05.2f}__d1_{:05.2f}'.format(int(l0), r0, h0, d0, d1)
94   nombrep = nom + ".brep"
95   geompy.ExportBREP(barre, direc + os.sep + nombrep )
96   props = geompy.BasicProperties(barre)
97   geomvol = props[2]
98
99   #geompy.addToStudy( barre, 'barre' )
100   #geompy.addToStudyInFather( barre, f1, 'f1' )
101   #geompy.addToStudyInFather( barre, f2, 'f2' )
102
103   smesh.SetEnablePublish( False )
104
105   isTetra = False
106   barre_1 = smesh.Mesh(barre)
107   # SO = salome.myStudy.FindObjectIOR(salome.myStudy.ConvertObjectToIOR(barre_1.GetMesh()))
108   # if SO:
109   #   print ("_______",SO.GetID(),SO.GetName())
110   # else:
111   #   print ("_______NO_SO!!!")
112   if (isTetra):
113     NETGEN_1D_2D_3D = barre_1.Tetrahedron(algo=smeshBuilder.NETGEN_1D2D3D)
114     NETGEN_3D_Parameters_1 = NETGEN_1D_2D_3D.Parameters()
115     NETGEN_3D_Parameters_1.SetMaxSize( d0 )
116     NETGEN_3D_Parameters_1.SetSecondOrder( 0 )
117     NETGEN_3D_Parameters_1.SetOptimize( 1 )
118     NETGEN_3D_Parameters_1.SetFineness( 3 )
119     NETGEN_3D_Parameters_1.SetChordalError( 0.1 )
120     NETGEN_3D_Parameters_1.SetChordalErrorEnabled( 0 )
121     NETGEN_3D_Parameters_1.SetMinSize( d0 )
122     NETGEN_3D_Parameters_1.SetUseSurfaceCurvature( 1 )
123     NETGEN_3D_Parameters_1.SetFuseEdges( 1 )
124     NETGEN_3D_Parameters_1.SetQuadAllowed( 0 )
125     NETGEN_1D_2D = barre_1.Triangle(algo=smeshBuilder.NETGEN_1D2D,geom=Auto_group_for_Sub_mesh_1)
126     NETGEN_2D_Parameters_1 = NETGEN_1D_2D.Parameters()
127     NETGEN_2D_Parameters_1.SetMaxSize( d1 )
128     NETGEN_2D_Parameters_1.SetSecondOrder( 0 )
129     NETGEN_2D_Parameters_1.SetOptimize( 1 )
130     NETGEN_2D_Parameters_1.SetFineness( 3 )
131     NETGEN_2D_Parameters_1.SetChordalError( 0.1 )
132     NETGEN_2D_Parameters_1.SetChordalErrorEnabled( 0 )
133     NETGEN_2D_Parameters_1.SetMinSize( d1 )
134     NETGEN_2D_Parameters_1.SetUseSurfaceCurvature( 1 )
135     NETGEN_2D_Parameters_1.SetFuseEdges( 1 )
136     NETGEN_2D_Parameters_1.SetQuadAllowed( 0 )
137   else:
138     Regular_1D = barre_1.Segment()
139     Number_of_Segments_1 = Regular_1D.NumberOfSegments(15)
140     Quadrangle_2D = barre_1.Quadrangle(algo=smeshBuilder.QUADRANGLE)
141     Hexa_3D = barre_1.Hexahedron(algo=smeshBuilder.Hexa)   
142   isDone = barre_1.Compute()
143   
144   f1_1 = barre_1.GroupOnGeom(f1,'f1',SMESH.FACE)
145   f2_1 = barre_1.GroupOnGeom(f2,'f2',SMESH.FACE)
146   smesh.SetName(barre_1, nom)
147   nommed = nom + ".med"
148   barre_1.ExportMED( direc + os.sep + nommed, auto_groups=0, minor=0, overwrite=1, meshPart=None, autoDimension=1 )
149   measure = smesh.CreateMeasurements()
150   meshvol = measure.Volume(barre_1.mesh)
151   print("Maillage publié : ", direc + os.sep + nommed)
152   clearMesh(barre_1, salome.myStudy, nom)
153   deltag = abs(geomvol - volume)/volume
154   deltam = abs(meshvol - geomvol)/geomvol
155   delta = abs(meshvol - volume)/volume
156   print("volumes:", volume, geomvol, meshvol, deltag, deltam)
157   assert(deltag < 1.e-5)
158   assert(deltam < 2.e-3)
159   #import time
160   #time.sleep(30)
161   return delta
162
163
164 def clearMesh(theMesh, theStudy, aName):
165   theMesh.Clear()
166   aMesh = theMesh.GetMesh()
167   aMesh.UnRegister()
168   # aStudyBuilder = theStudy.NewBuilder()
169   # SO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(aMesh))
170   # objects_to_unpublish = [SO]
171   # refs = theStudy.FindDependances(SO)
172   # objects_to_unpublish += refs
173   # for o in objects_to_unpublish:
174   #   if o is not None:
175   #     aStudyBuilder.RemoveObjectWithChildren(o)
176   print("clearMesh done:", aName)
177
178 def genere(r0, h0, my_container, direc):
179   l0 = 50.0
180   d0 = min(r0/2., h0/6.)
181   d1 = d0/2.
182   res = geomesh(l0, r0, h0, d0, d1, my_container, direc)
183   return res
184
185 def genere2(r0h0, my_container, direc):
186   l0 = 50.0
187   r0 = r0h0[0]
188   h0 = r0h0[1]
189   d0 = min(r0/2., h0/6.)
190   d1 = d0/2.
191   res = geomesh(l0, r0, h0, d0, d1, my_container, direc)
192   return res