Salome HOME
Additional python3 porting to make python scripts work
[modules/hexablock.git] / src / HEXABLOCK_SWIG / hexablock.py
1 # -*- coding: latin-1 -*-
2 # Copyright (C) 2009-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 # HexaBlock : Module principal
22
23 import hexablock_swig
24
25 import HEXABLOCKPlugin
26
27 import salome
28 salome.salome_init()
29
30 from salome.geom import geomBuilder
31 geompy = geomBuilder.New()
32
33
34 import SMESH
35 from salome.smesh import smeshBuilder
36 smesh = smeshBuilder.New()
37
38 component = hexablock_swig.hex_instance ()
39
40 # ======================================================== moduleName
41 # ### Gives the component name
42 def moduleName () :
43     return "HEXABLOCK"
44
45 # ======================================================== getEngine
46 # ### Gives the component
47 def getEngine () :
48     return component
49
50 # ======================================================== what
51 def what () :
52    component.what ()
53
54 # ======================================================== countDocument
55 def countDocument () :
56    return component.countDocument ()
57
58 # ======================================================== getDocument
59 def getDocument (nro) :
60    return  component.getDocument (nro)
61
62 # ======================================================== removeDocument
63 def removeDocument (doc) :
64    return  component.removeDocument (doc)
65
66 # ======================================================== addDocument
67 def addDocument (nomdoc) :
68    return  component.addDocument (nomdoc)
69
70 # ======================================================== loadDocument
71 def loadDocument (filename) :
72    return  component.loadDocument (filename)
73
74 # ======================================================== findDocument
75 def findDocument (name) :
76    return  component.findDocument (name)
77
78 # ======================================================== dump
79 # ### Display informations about a document
80 def dump(doc, mesh=None, full=False):
81     if full:
82         hn = doc.countUsedHexa()
83         print("Model dump: number of hexas: ", hn)
84         for hi in range(hn):
85             hh = doc.getUsedHexa(hi)
86             print("  hexa: ", hi, "name: ", hh.getName())
87             for fi in range(6):
88                 ff = hh.getQuad(fi)
89                 fa = ff.getAssociations()
90                 print("    quadrangle: ", fi, "name: ", ff.getName(), " associated: ", fa!=[])
91                 for ei in range(4):
92                     ee = ff.getEdge(ei)
93                     ea = ee.getAssociations()
94                     print("      edge: ", ei, "name: ", ee.getName(), " associated: ", ea!=[])
95                     for vi in range(2):
96                         vv = ee.getVertex(vi)
97                         va = vv.getAssociation()
98                         print("        vertex: ", vi, "name: ", vv.getName(), " associated: ", va!=None)
99                         print("          model: x= ", vv.getX(), " y= ", vv.getY(), " z= ", vv.getZ())
100                         if va!=None:
101                             x, y, z = geompy.PointCoordinates(va)
102                             print("          assoc: x= ", x, " y= ", y, " z= ", z)
103
104     uv = doc.countUsedVertex()
105     ue = doc.countUsedEdge()
106     uq = doc.countUsedQuad()
107     uh = doc.countUsedHexa()
108
109     print("Model vertices    number: ", uv)
110     print("Model edges       number: ", ue)
111     print("Model quadrangles number: ", uq)
112     print("Model blocks      number: ", uh)
113
114     if mesh != None:
115         print()
116         print("Mesh nodes       number: ", mesh.NbNodes())
117         print("Mesh segments    number: ", mesh.NbEdges())
118         print("Mesh quadrangles number: ", mesh.NbQuadrangles())
119         print("Mesh hexas       number: ", mesh.NbHexas())
120
121     return uv, ue, uq, uh
122
123 # ======================================================== mesh
124 # ### Mesh a document
125 def mesh (doc, name=None, dim=3, container="FactoryServer"):
126     study = salome.myStudy
127
128     if isinstance(doc, type("")):
129         doc = component.findDocument (doc)
130     docname = doc.getName()
131
132     ####  if doc.countShape() == 0 :
133        ####  shape = geompy.MakeBox(0, 0, 0,  1, 1, 1)
134     ####  else :
135        ####  shape = doc.getShape (0)
136
137     ###   shape = doc.getFirstExplicitShape_else_box(1)
138     ###   the_stream = shape.getBREP() # sort une chaine de caractère
139     ###   geom_object = geompy.RestoreShape(the_stream)
140
141     stream = doc.getFirstExplicitShape ()
142     if stream != None :
143        shape = geompy.RestoreShape (stream.encode())
144     else :
145        shape = geompy.MakeBox(0, 0, 0,  1, 1, 1)
146
147     if (name == None) or (name == "") :
148         name = docname
149
150     geompy.addToStudy(shape, name)
151     comp_smesh = salome.lcc.FindOrLoadComponent(container, "SMESH")
152     comp_smesh.init_smesh(geomBuilder.geom)
153     meshexa = comp_smesh.Mesh(shape)
154
155     so = "libHexaBlockPluginEngine.so"
156
157     algo = SMESH._objref_SMESH_Gen.CreateHypothesis(comp_smesh, "HEXABLOCK_3D", so)
158     meshexa.mesh.AddHypothesis(shape, algo)
159
160     hypo = SMESH._objref_SMESH_Gen.CreateHypothesis(comp_smesh, "HEXABLOCK_Parameters", so)
161     meshexa.mesh.AddHypothesis(shape, hypo)
162
163     ### hypo.SetDocument(doc.getXml())   ## Hexa6 TODO et a verifier
164     print(" Maillage du document ", docname)
165     hypo.SetDocument (docname);
166     hypo.SetDimension(dim)
167
168     meshexa.Compute()
169
170     return meshexa
171
172 # ======================================================== getFromStudy
173 def getFromStudy(entry):
174     study    = salome.myStudy
175     sobject  = study.FindObjectID(entry)
176     if sobject == None :
177        print(" **** Entry ", entry, " is undefined")
178        return None
179
180     builder  = study.NewBuilder()
181     ok, attname = builder.FindAttribute(sobject, "AttributeName")
182     docname  = attname.Value()
183     doc = component.findDocument(docname)
184     if doc == None :
185        print(" **** Entry ", entry, " doesn't correspond to an HexaBlock Document")
186
187     return doc
188
189 # ==================================================== findOrCreateComponent
190 # Find or create HexaBlock Study Component
191 def findOrCreateComponent( builder ):
192     father = salome.myStudy.FindComponent( moduleName() )
193     if father is None:
194        father = builder.NewComponent( moduleName() )
195        attr = builder.FindOrCreateAttribute( father, "AttributeName" )
196        attr.SetValue( "HexaBlock" )
197        attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
198        attr.SetPixMap( "ICO_MODULE_HEXABLOCK_SMALL" )
199
200     return father
201
202 # ==================================================== addToStudy
203 # Add a document in the current study
204 def addToStudy(doc):
205     if doc == None :
206        print(" *** addToStudy : Bad Document Pointer")
207        return
208
209     study   = salome.myStudy
210     builder = study.NewBuilder()
211     father  = findOrCreateComponent( builder )
212     name    = doc.getName ()
213
214     present = study.FindObjectByName(name, moduleName())
215     if present != [] :
216        print(" *** addToStudy : Document ", name, "is already in the study")
217        return
218
219     object  = builder.NewObject( father )
220     attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
221     attr.SetValue( name )
222     return object.GetID ()
223
224 # ==================================================== addShape
225 # Add a document in the current study
226 def addShape (doc, shape, name):
227     print("Call addShape")
228     forme = doc.addShape (shape.getShape(), name)
229     return forme