Salome HOME
Cas tests bielle + tuyau
[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 = "libHexaBlockEngine.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     hypo.SetDocument (docname);
129     hypo.SetDimension(dim)
130
131     meshexa.Compute()
132
133     return meshexa
134
135 # ======================================================== getFromStudy
136 def getFromStudy(entry):
137     study    = salome.myStudy
138     sobject  = study.FindObjectID(entry)
139     if sobject == None :
140        print " **** Entry ", entry, " is undefined"
141        return None
142
143     builder  = study.NewBuilder()
144     ok, attname = builder.FindAttribute(sobject, "AttributeName")
145     docname  = attname.Value()
146     doc = component.findDocument(docname)
147     if doc == None :
148        print " **** Entry ", entry, " doesn't correspond to an HexaBlock Document"
149
150     return doc
151
152 # ==================================================== findOrCreateComponent
153 # Find or create HexaBlock Study Component
154 def findOrCreateComponent( study, builder ):
155     father = study.FindComponent( moduleName() )
156     if father is None:
157        father = builder.NewComponent( moduleName() )
158        attr = builder.FindOrCreateAttribute( father, "AttributeName" )
159        attr.SetValue( "HexaBlock" )
160        attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
161        attr.SetPixMap( "ICO_MODULE_HEXABLOCK_SMALL" )
162
163     return father
164
165 # ==================================================== addToStudy
166 # Add a document in the current study
167 def addToStudy(doc):
168     if doc == None :
169        print " *** addToStudy : Bad Document Pointer"
170        return
171
172     study   = salome.myStudy
173     builder = study.NewBuilder()
174     father  = findOrCreateComponent( study, builder )
175     name    = doc.getName ()
176
177     present = study.FindObjectByName(name, moduleName())
178     if present != [] :
179        print " *** addToStudy : Document ", name, "is already in the study"
180        return
181
182     object  = builder.NewObject( father )
183     attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
184     attr.SetValue( name )
185     return object.GetID ()
186
187 # ==================================================== addShape
188 # Add a document in the current study
189 def addShape (doc, shape, name):
190     forme = doc.addShape (shape.getShape(), name)
191     return forme