Salome HOME
0023360: [CEA 1963] Use salome.sg.updateObjBrowser(True) instead of salome.sg.updateO...
[modules/gui.git] / src / SalomeApp / pluginsdemo / tubebuilder.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2016  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, 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(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     from salome.smesh import smeshBuilder
70     smesh = smeshBuilder.New(study)
71
72     mesh = smesh.Mesh(shape)
73     Regular_1D = mesh.Segment()
74     Nb_Segments = Regular_1D.NumberOfSegments(10)
75     Nb_Segments.SetDistrType( 0 )
76     Quadrangle_2D = mesh.Quadrangle()
77     Hexa_3D = mesh.Hexahedron()
78
79     isDone = mesh.Compute()
80
81     if salome.sg.hasDesktop():
82         smesh.SetName(mesh.GetMesh(), 'TubeMesh')
83         smesh.SetName(Regular_1D.GetAlgorithm(), 'Regular_1D')
84         smesh.SetName(Nb_Segments, 'Nb. Segments_1')
85         smesh.SetName(Quadrangle_2D.GetAlgorithm(), 'Quadrangle_2D')
86         smesh.SetName(Hexa_3D.GetAlgorithm(), 'Hexa_3D')
87         salome.sg.updateObjBrowser(False)
88
89     return mesh
90
91
92 def createModel(study, radius=DEFAULT_RADIUS, length=DEFAULT_LENGTH,width=DEFAULT_WIDTH):
93     '''
94     This function create the geomtrical shape AND the associated mesh.
95     '''
96     # We first create a shape with a partition so that the hexaedric
97     # algorithm could be used.
98     shape = createGeometryWithPartition(study,radius,length,width)
99
100     # Then the mesh can be defined and computed
101     mesh = createMesh(study,shape)
102     
103 def exportModel(mesh, filename):
104     '''
105     This exports the mesh to the specified filename in the med format
106     '''
107     print "TUBE: exporting mesh to file %s ..."%filename
108     import SMESH
109     mesh.ExportMED(filename, 0, SMESH.MED_V2_2, 1 )
110
111
112 #
113 # ===================================================================
114 # Use cases and test functions
115 # ===================================================================
116 #
117 def TEST_createGeometry():
118     salome.salome_init()
119     theStudy=salome.myStudy
120     createGeometry(theStudy)
121
122 def TEST_createMesh():
123     salome.salome_init()
124     theStudy=salome.myStudy
125     shape = createGeometryWithPartition(theStudy)
126     mesh  = createMesh(theStudy, shape)
127
128 def TEST_createModel():
129     salome.salome_init()
130     theStudy=salome.myStudy
131     createModel(theStudy)
132
133 def TEST_exportModel():
134     salome.salome_init()
135     theStudy=salome.myStudy
136     shape = createGeometryWithPartition(theStudy)
137     mesh  = createMesh(theStudy, shape)
138     exportModel(mesh,"tubemesh.med")
139     
140 if __name__ == "__main__":
141     #TEST_createGeometry()
142     #TEST_createMesh()
143     TEST_createModel()
144     #TEST_exportModel()