Salome HOME
Updated copyright comment
[modules/gui.git] / src / SalomeApp / pluginsdemo / tubebuilder.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2024  CEA, EDF, OPEN CASCADE
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 # Author : Guillaume Boulant (EDF)
21
22 import salome
23
24 DEFAULT_RADIUS = 100
25 DEFAULT_LENGTH = 300
26 DEFAULT_WIDTH  = 20
27
28 from salome.geom import geomtools
29
30 def createGeometry(radius=DEFAULT_RADIUS, length=DEFAULT_LENGTH, width=DEFAULT_WIDTH):
31     '''
32     This function creates the geometry on the specified study and with
33     given parameters.
34     '''
35     print("TUBE: creating the geometry ...")
36     geompy = geomtools.getGeompy()
37
38     radius_ext = radius
39     radius_int = radius_ext - width
40
41     CylinderExt = geompy.MakeCylinderRH(radius_ext, length)
42     CylinderInt = geompy.MakeCylinderRH(radius_int, length)
43     Tube = geompy.MakeCut(CylinderExt, CylinderInt)
44     return Tube
45     
46 def createGeometryWithPartition(radius=DEFAULT_RADIUS, length=DEFAULT_LENGTH, width=DEFAULT_WIDTH):
47     '''
48     This function create the geometrical shape with a partition so
49     that the hexaedric algorithm could be used for meshing.
50     '''
51     shape = createGeometry(radius,length,width)
52
53     # We have to create a partition so that we can use an hexaedric
54     # meshing algorithm.
55     geompy = geomtools.getGeompy()
56
57     print("TUBE: creating a partition ...")
58     toolPlane = geompy.MakeFaceHW(2.1*length,2.1*radius,3)
59     partition = geompy.MakePartition([shape], [toolPlane], [], [], geompy.ShapeType["SOLID"], 0, [], 0)
60     entry = geompy.addToStudy( partition, "TubeWithPartition" )
61     return partition
62     
63 def createMesh(shape):
64     '''This function creates the mesh of the specified shape on the current study'''
65     print("TUBE: creating the mesh ...")
66     import SMESH
67     from salome.smesh import smeshBuilder
68     smesh = smeshBuilder.New()
69
70     mesh = smesh.Mesh(shape)
71     Regular_1D = mesh.Segment()
72     Nb_Segments = Regular_1D.NumberOfSegments(10)
73     Nb_Segments.SetDistrType( 0 )
74     Quadrangle_2D = mesh.Quadrangle()
75     Hexa_3D = mesh.Hexahedron()
76
77     isDone = mesh.Compute()
78
79     if salome.sg.hasDesktop():
80         smesh.SetName(mesh.GetMesh(), 'TubeMesh')
81         smesh.SetName(Regular_1D.GetAlgorithm(), 'Regular_1D')
82         smesh.SetName(Nb_Segments, 'Nb. Segments_1')
83         smesh.SetName(Quadrangle_2D.GetAlgorithm(), 'Quadrangle_2D')
84         smesh.SetName(Hexa_3D.GetAlgorithm(), 'Hexa_3D')
85         salome.sg.updateObjBrowser()
86
87     return mesh
88
89
90 def createModel(radius=DEFAULT_RADIUS, length=DEFAULT_LENGTH,width=DEFAULT_WIDTH):
91     '''
92     This function create the geomtrical shape AND the associated mesh.
93     '''
94     # We first create a shape with a partition so that the hexaedric
95     # algorithm could be used.
96     shape = createGeometryWithPartition(radius,length,width)
97
98     # Then the mesh can be defined and computed
99     mesh = createMesh(shape)
100     
101 def exportModel(mesh, filename):
102     '''
103     This exports the mesh to the specified filename in the med format
104     '''
105     print("TUBE: exporting mesh to file %s ..."%filename)
106     import SMESH
107     mesh.ExportMED(filename)
108
109
110 #
111 # ===================================================================
112 # Use cases and test functions
113 # ===================================================================
114 #
115 def TEST_createGeometry():
116     salome.salome_init()
117     createGeometry()
118
119 def TEST_createMesh():
120     salome.salome_init()
121     shape = createGeometryWithPartition()
122     mesh  = createMesh(shape)
123
124 def TEST_createModel():
125     salome.salome_init()
126     createModel()
127
128 def TEST_exportModel():
129     salome.salome_init()
130     shape = createGeometryWithPartition()
131     mesh  = createMesh(shape)
132     exportModel(mesh,"tubemesh.med")
133     
134 if __name__ == "__main__":
135     #TEST_createGeometry()
136     #TEST_createMesh()
137     TEST_createModel()
138     #TEST_exportModel()