Salome HOME
Merge from V6_main 01/04/2013
[modules/hexablock.git] / src / HEXABLOCK_I / hexablock.py
1 # -*- coding: latin-1 -*-
2
3 # Copyright (C) 2009-2013  CEA/DEN, EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 # Francis KLOSS - 2011-2013 - CEA-Saclay, DEN, DM2S, SFME, LGLS, F-91191 Gif-sur-Yvette, France
23 # =============================================================================================
24
25 import salome
26 import smesh
27
28 from HEXABLOCK_ORB import *
29 import HEXABLOCKPlugin
30
31 geompy = smesh.geompy
32
33 # Gives the component name
34 # -----------------------------------------------------------
35
36 def moduleName() :
37     return "HEXABLOCK"
38
39 # Load HEXABLOCK componant
40 # ------------------------
41
42 component = salome.lcc.FindOrLoadComponent("FactoryServer", moduleName())
43 component = component._narrow(HEXABLOCK_Gen)
44
45 for k in dir(component):
46     if k[0] == '_':
47         continue
48     globals()[k] = getattr(component, k)
49
50 del k
51
52 # Gives the component
53 # -----------------------------------------------------------
54
55 def getEngine() :
56     return component
57
58 # Add laws on propagations based on min or max segment length
59 # -----------------------------------------------------------
60
61 def addLaws(doc, lg, lgmax=True):
62     laws = {}
63     n = doc.countPropagation()
64     for i in xrange(n):
65         p = doc.getPropagation(i)
66         if lgmax:
67             m = 0
68         else:
69             m = (max)
70         for e in p.getEdges():
71             a = e.getAssociations()
72
73             if a == []:
74                 vam = e.getVertex(0)
75                 vag = vam.getAssociation()
76                 if vag == None:
77                     vax = vam.getX()
78                     vay = vam.getY()
79                     vaz = vam.getZ()
80                 else:
81                     vax, vay, vaz = geompy.PointCoordinates(vag)
82
83                 vbm = e.getVertex(1)
84                 vbg = vbm.getAssociation()
85                 if vbg == None:
86                     vbx = vbm.getX()
87                     vby = vbm.getY()
88                     vbz = vbm.getZ()
89                 else:
90                     vbx, vby, vbz = geompy.PointCoordinates(vbg)
91
92                 l = ( (vbx-vax)**2 + (vby-vay)**2 + (vbz-vaz)**2 )**0.5
93
94             else:
95                 l = 0.0
96                 for gdf in a:
97                     le, su, vo = geompy.BasicProperties(gdf.geomObj)
98                     l += le * (gdf.fin - gdf.debut)
99
100             if ( lgmax and l>m ) or ( (not lgmax) and l<m ):
101                 m = l
102
103         nn = m / lg
104         if lgmax and (int(nn) != nn):
105             nn = int(nn)
106         else:
107             nn = int(nn) - 1
108
109         try:
110             law = laws[nn]
111         except:
112             law = doc.addLaw("u_"+str(nn), nn)
113             laws[nn] = law
114
115         p.setLaw(law)
116
117 # Display informations about a document
118 # -------------------------------------
119
120 def dump(doc, mesh=None, full=False):
121     if full:
122         hn = doc.countUsedHexa()
123         print "Model dump: number of hexas: ", hn
124         for hi in xrange(hn):
125             hh = doc.getUsedHexa(hi)
126             print "  hexa: ", hi, "name: ", hh.getName()
127             for fi in xrange(6):
128                 ff = hh.getQuad(fi)
129                 fa = ff.getAssociations()
130                 print "    quadrangle: ", fi, "name: ", ff.getName(), " associated: ", fa!=[]
131                 for ei in xrange(4):
132                     ee = ff.getEdge(ei)
133                     ea = ee.getAssociations()
134                     print "      edge: ", ei, "name: ", ee.getName(), " associated: ", ea!=[]
135                     for vi in xrange(2):
136                         vv = ee.getVertex(vi)
137                         va = vv.getAssociation()
138                         print "        vertex: ", vi, "name: ", vv.getName(), " associated: ", va!=None
139                         print "          model: x= ", vv.getX(), " y= ", vv.getY(), " z= ", vv.getZ()
140                         if va!=None:
141                             x, y, z = geompy.PointCoordinates(va)
142                             print "          assoc: x= ", x, " y= ", y, " z= ", z
143
144     uv = doc.countUsedVertex()
145     ue = doc.countUsedEdge()
146     uq = doc.countUsedQuad()
147     uh = doc.countUsedHexa()
148
149     print "Model vertices    number: ", uv
150     print "Model edges       number: ", ue
151     print "Model quadrangles number: ", uq
152     print "Model blocks      number: ", uh
153
154     if mesh != None:
155         print "Mesh nodes       number: ", mesh.NbNodes()
156         print "Mesh segments    number: ", mesh.NbEdges()
157         print "Mesh quadrangles number: ", mesh.NbQuadrangles()
158         print "Mesh hexas       number: ", mesh.NbHexas()
159
160     return uv, ue, uq, uh
161
162 # Mesh a document
163 # ---------------
164
165 def mesh(doc, name=None, dim=3, container="FactoryServer"):
166     study = salome.myStudy
167
168     if type(doc) == type(""):
169         doc = component.findDocument (doc)
170
171     ####  shape = doc.getShape()
172     ####  if shape == None:
173         ####  shape = geompy.MakeBox(0, 0, 0,  1, 1, 1)
174
175     shape = geompy.MakeBox(0, 0, 0,  1, 1, 1)
176     if (name == None) or (name == ""):
177         name = doc.getName()
178
179     geompy.addToStudy(shape, name)
180     comp_smesh = salome.lcc.FindOrLoadComponent(container, "SMESH")
181     comp_smesh.init_smesh(study, geompy.geom)
182     meshexa = comp_smesh.Mesh(shape)
183
184     so = "libHexaBlockEngine.so"
185
186     algo = smesh.SMESH._objref_SMESH_Gen.CreateHypothesis(comp_smesh, "HEXABLOCK_3D", so)
187     meshexa.mesh.AddHypothesis(shape, algo)
188
189     hypo = smesh.SMESH._objref_SMESH_Gen.CreateHypothesis(comp_smesh, "HEXABLOCK_Parameters", so)
190     meshexa.mesh.AddHypothesis(shape, hypo)
191
192     hypo.SetDocument(doc)
193     hypo.SetDimension(dim)
194
195     meshexa.Compute()
196
197     return meshexa
198
199 # Get a document from the current study
200 # -------------------------------------
201
202 def getFromStudy(entry):
203     study    = salome.myStudy
204     sobject  = study.FindObjectID(entry)
205     if sobject == None :
206        print " **** Entry ", entry, " is undefined"
207        return None
208
209     builder  = study.NewBuilder()
210     ok, attname = builder.FindAttribute(sobject, "AttributeName")
211     docname  = attname.Value()
212     doc = component.findDocument(docname)
213     if doc == None :
214        print " **** Entry ", entry, " doesn't correspond to an HexaBlock Document"
215
216     return doc
217
218 # Find or create HexaBlock Study Component
219 # -------------------------------------
220
221 def findOrCreateComponent( study, builder ):
222     father = study.FindComponent( moduleName() )
223     if father is None:
224        father = builder.NewComponent( moduleName() )
225        attr = builder.FindOrCreateAttribute( father, "AttributeName" )
226        attr.SetValue( "HexaBlock" )
227        attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
228        ### attr.SetPixMap( "ICON_OBJBROWSER_HEXABLOCK" )
229        attr.SetPixMap( "ICO_MODULE_HEXABLOCK_SMALL" )
230        builder.DefineComponentInstance( father, getEngine() )
231
232     return father
233
234 # Add a document in the current study
235 # -------------------------------------
236
237 def addToStudy(doc):
238     if doc == None :
239        print " *** addToStudy : Bad Document Pointer"
240        return
241
242     study   = salome.myStudy
243     builder = study.NewBuilder()
244     father  = findOrCreateComponent( study, builder )
245     name    = doc.getName ()
246
247     present = study.FindObjectByName(name, moduleName())
248     if present != [] :
249        print " *** addToStudy : Document ", name, "is already in the study"
250        return
251
252     object  = builder.NewObject( father )
253     attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
254     attr.SetValue( name )
255     return object.GetID ()