Salome HOME
Rename HexaBlockEngine library to the HexaBlockPluginEngine to avoid problems on...
[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
110     shape = geompy.MakeBox(0, 0, 0,  1, 1, 1)
111     if (name == None) or (name == ""):
112         name = docname
113
114     geompy.addToStudy(shape, name)
115     comp_smesh = salome.lcc.FindOrLoadComponent(container, "SMESH")
116     comp_smesh.init_smesh(study, geompy.geom)
117     meshexa = comp_smesh.Mesh(shape)
118
119     so = "libHexaBlockPluginEngine.so"
120
121     algo = smesh.SMESH._objref_SMESH_Gen.CreateHypothesis(comp_smesh, "HEXABLOCK_3D", so)
122     meshexa.mesh.AddHypothesis(shape, algo)
123
124     hypo = smesh.SMESH._objref_SMESH_Gen.CreateHypothesis(comp_smesh, "HEXABLOCK_Parameters", so)
125     meshexa.mesh.AddHypothesis(shape, hypo)
126
127     ### hypo.SetDocument(doc.getXml())   ## Hexa6 TODO et a verifier
128     print " Maillage du document "
129     print " Maillage du document ", docname
130     hypo.SetDocument (docname);
131     hypo.SetDimension(dim)
132
133     meshexa.Compute()
134
135     return meshexa
136
137 # ======================================================== getFromStudy
138 def getFromStudy(entry):
139     study    = salome.myStudy
140     sobject  = study.FindObjectID(entry)
141     if sobject == None :
142        print " **** Entry ", entry, " is undefined"
143        return None
144
145     builder  = study.NewBuilder()
146     ok, attname = builder.FindAttribute(sobject, "AttributeName")
147     docname  = attname.Value()
148     doc = component.findDocument(docname)
149     if doc == None :
150        print " **** Entry ", entry, " doesn't correspond to an HexaBlock Document"
151
152     return doc
153
154 # ==================================================== findOrCreateComponent
155 # Find or create HexaBlock Study Component
156 def findOrCreateComponent( study, builder ):
157     father = study.FindComponent( moduleName() )
158     if father is None:
159        father = builder.NewComponent( moduleName() )
160        attr = builder.FindOrCreateAttribute( father, "AttributeName" )
161        attr.SetValue( "HexaBlock" )
162        attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
163        attr.SetPixMap( "ICO_MODULE_HEXABLOCK_SMALL" )
164
165     return father
166
167 # ==================================================== addToStudy
168 # Add a document in the current study
169 def addToStudy(doc):
170     if doc == None :
171        print " *** addToStudy : Bad Document Pointer"
172        return
173
174     study   = salome.myStudy
175     builder = study.NewBuilder()
176     father  = findOrCreateComponent( study, builder )
177     name    = doc.getName ()
178
179     present = study.FindObjectByName(name, moduleName())
180     if present != [] :
181        print " *** addToStudy : Document ", name, "is already in the study"
182        return
183
184     object  = builder.NewObject( father )
185     attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
186     attr.SetValue( name )
187     return object.GetID ()
188
189 # ==================================================== addShape
190 # Add a document in the current study
191 def addShape (doc, shape, name):
192     print "Call addShape"
193     forme = doc.addShape (shape.getShape(), name)
194     return forme