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