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