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