Salome HOME
23517: [CEA 2203] : Mesh group of polyhedron not exported in med and not saved in hdf
[modules/smesh.git] / src / Tools / MacMesh / MacMesh / PublishGroups.py
1 # Copyright (C) 2014-2016  EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20
21 import SMESH
22 import math
23 import Config
24
25 from salome.geom import geomBuilder
26 geompy = geomBuilder.New( Config.theStudy )
27
28 from salome.smesh import smeshBuilder
29 smesh = smeshBuilder.New( Config.theStudy )
30
31 ##########################################################################################################
32
33 def PublishGroups ():
34         aFilterManager = smesh.CreateFilterManager()
35
36         # Building geometric and mesh compounds and  groups ##############################################
37         if Config.debug : print "Searching for geometric groups and publishing final compound"
38         
39         TempGEOList = []
40         TempMESHList = []
41         
42         for MacroObj in Config.ListObj : 
43                 TempGEOList += MacroObj.GeoChildren
44                 TempMESHList += MacroObj.Mesh
45                 
46         FinalCompound = geompy.MakeCompound(TempGEOList)
47         geompy.addToStudy (FinalCompound,Config.StudyName)
48         MeshCompound = smesh.Concatenate(TempMESHList, 1, 1, 1e-5)
49         MeshCompound.SetName(Config.StudyName)
50         
51         GroupGEO = []
52         for group in Config.Groups :
53         
54                 # Geometric groups definition
55                 TempGEOList = []
56                 TempNames = []
57                 for MacroObj in Config.ListObj :
58                         if group in MacroObj.GroupNames :
59                                 Occurences = IndexMultiOcc(MacroObj.GroupNames, group)
60                                 for Occ in Occurences :
61                                         TempGEOList += MacroObj.GetBorder(Occ)
62                 GroupGEO.append(geompy.MakeCompound(TempGEOList))
63                 geompy.addToStudyInFather(FinalCompound,GroupGEO[-1],'GR_'+group)
64                 
65                 # Mesh groups definition
66                 Criterion = smesh.GetCriterion(SMESH.EDGE, SMESH.FT_BelongToGeom,'=',GroupGEO[-1],Tolerance=1e-06)
67                 #Criterion = smesh.Filter.Criterion(18,39,0,'GR_'+group,'GR_'+group,39,39,1e-06,smesh.EDGE,7)
68                 MeshCompound.MakeGroupByCriterion(group,Criterion)
69         
70         StudyBuilder = Config.theStudy.NewBuilder()
71         for MeshObj in TempMESHList:
72                 SO = Config.theStudy.FindObjectIOR(Config.theStudy.ConvertObjectToIOR(MeshObj))
73                 if SO is not None: StudyBuilder.RemoveObjectWithChildren(SO)
74         
75         return MeshCompound        
76                 
77
78 def IndexMultiOcc (Array,Element) :
79         """
80         This function returns the occurrences indices of Element in Array.
81         As opposed to Array.index(Element) method, this allows determining      
82         multiple entries rather than just the first one!
83         """
84         Output = []
85         try : Array.index(Element)
86         except ValueError : print "No more occurrences"
87         else : Output.append(Array.index(Element))
88                 
89         if not(Output == [-1]) and len(Array) > 1 :
90                 for index, ArrElem in enumerate(Array[Output[0]+1:]) :
91                         if ArrElem is Element : Output.append(index+Output[0]+1)
92                  
93         return Output
94             
95 def Publish (ObjToPublish):
96         for i,GeoObj in enumerate(ObjToPublish) : geompy.addToStudy(GeoObj,"Sub_"+str(i))
97         
98 def RevolveMesh(MainMesh,**args):
99         """
100         This function permits to revolute and scale a 2D mesh while transforming the edge
101         groups into face groups. Moreover, the function automatically creates the face groups 
102         corresponding to the symmetry lower and upper faces
103         Facultatif arguments are : 
104                 - Center [X,Y,Z], origin being the default
105                 - Direction [VX,VY,VZ], x-axis being the default
106                 - AngleDeg or AngleRad : ALPHA, 10 degrees being the default
107                 - Scale : BETA, no scaling being default
108         """
109         ################################################################################
110         # Reading input arguments and proceeding to defaults if necessary
111         ################################################################################       
112         if 'Center' in args : CenterCoor = [float(Coor) for Coor in args['Center']]
113         else : 
114                 print "\nThe coordinates of the center of revolution were not given\nThe origin is used by default."
115                 CenterCoor = [0.,0.,0.]
116         
117         if 'Direction' in args : Direction = [float(Dir) for Dir in args['Direction']]
118         else :
119                 print "\nThe axis vector of revolution was not given\nThe x-axis is used by default."
120                 Direction = [1.,0.,0.]
121         
122         if 'AngleDeg' in args : Angle = float(args['AngleDeg'])*math.pi/180.
123         elif 'AngleRad' in args : Angle = float(args['AngleRad'])
124         else :
125                 print "\nThe revolution angle was not given\nAn angle of 10 degrees is used by default."
126                 Angle = 10.*math.pi/180.
127                 
128         if 'Scale' in args : Scale = float(args['Scale'])
129         else : Scale = 1.
130         
131
132         # Creating the lower face group LOFAC
133         LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
134         LOFAC.AddFrom(MainMesh.GetMesh())
135
136         GR_Names = MainMesh.GetGroupNames()
137         GRs = MainMesh.GetGroups()
138         Rev3DMeshGroups = MainMesh.RotationSweepObject2D( MainMesh, SMESH.AxisStruct( CenterCoor[0], CenterCoor[1], CenterCoor[2], Direction[0], Direction[1], Direction[2] ), Angle, 1, 1e-05 ,True)
139
140         # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
141         for GR in GRs:
142                 CurrentName = GR.GetName()
143                 if CurrentName in GR_Names and not(CurrentName=='LOFAC'):  # Meaning that this is an old edge group
144                         GR.SetName(CurrentName+'_EDGE')
145
146         # Removing the _rotated prefix from the rotated FACE groups
147         for GR in Rev3DMeshGroups:
148                 CurrentName = GR.GetName()
149                 if CurrentName.endswith( "_rotated"):
150                         if CurrentName.startswith( 'LOFAC_' ):
151                                 GR.SetName('VOL')
152                         else:
153                                 GR.SetName(CurrentName[:-8])
154                 elif CurrentName == 'LOFAC_top':
155                         GR.SetName('HIFAC')
156                         #Index = [ GR_Names[i] in CurrentName for i in range(0,len(GR_Names)) ].index(True)
157                         #GR.SetName(GR_Names[Index])
158
159         # Creating the upper face group HIFAC
160         ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
161         ALLFAC.AddFrom(MainMesh.GetMesh())
162
163         #HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Rev3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
164         #HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Rev3DMeshGroups if ( not(MeshGroup.GetName()=='VOL') and MeshGroup.GetType() == SMESH.FACE )], 'HIFAC' )
165
166         # Scaling down the mesh to meter units
167         if not(Scale==1.):
168                 MeshEditor = MainMesh.GetMeshEditor()
169                 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
170      
171                 
172 def ExtrudeMesh(MainMesh,**args):
173         """
174         This function permits to extrude and scale a 2D mesh while transforming the edge
175         groups into face groups. Moreover, the function automatically creates the face groups 
176         corresponding to the symmetry lower and upper faces
177         Facultatif arguments are : 
178                 - Direction [VX,VY,VZ], z-axis being default
179                 - Distance : D, default is 1
180                 - NSteps : the object will be extruded by NSteps*Distance, default is Nsteps = 1
181                 - Scale : BETA, no scaling being default
182         """
183         ################################################################################
184         # Reading input arguments and proceeding to defaults if necessary
185         ################################################################################              
186         if 'Distance' in args : Distance = float(args['Distance'])
187         else :
188                 print "\nThe extrusion distance was not given\nA default value of 1 is used."
189                 Distance = 1.
190                 
191         if 'Direction' in args : Direction = NormalizeVector([float(Dir) for Dir in args['Direction']],Distance)
192         else :
193                 print "\nThe extrusion vector of revolution was not given\nThe z-axis is used by default."
194                 Direction = NormalizeVector([0.,0.,1.],Distance)
195                                 
196         if 'Scale' in args : Scale = float(args['Scale'])
197         else : Scale = 1.
198         
199         if 'NSteps' in args : NSteps = int(args['NSteps'])
200         else : NSteps = 1
201         
202         # Creating the lower face group LOFAC
203         LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
204         LOFAC.AddFrom(MainMesh.GetMesh())
205
206         GR_Names = MainMesh.GetGroupNames()
207         GRs = MainMesh.GetGroups()
208         Ext3DMeshGroups = MainMesh.ExtrusionSweepObject2D(MainMesh,SMESH.DirStruct(SMESH.PointStruct(Direction[0],Direction[1],Direction[2])), NSteps, True)
209
210         # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
211         for GR in GRs:
212                 CurrentName = GR.GetName()
213                 if CurrentName in GR_Names and not(CurrentName=='LOFAC'):  # Meaning that this is an old edge group
214                         GR.SetName(CurrentName+'_EDGE')
215
216         # Removing the _extruded suffix from the extruded FACE groups
217         for GR in Ext3DMeshGroups:
218                 CurrentName = GR.GetName()
219                 if CurrentName.endswith( "_extruded"):
220                         if CurrentName.startswith( 'LOFAC_' ):
221                                 GR.SetName('VOL')
222                         else:
223                                 GR.SetName(CurrentName[:-9])
224                 elif CurrentName == 'LOFAC_top':
225                         GR.SetName('HIFAC')
226
227         # Creating the upper face group HIFAC
228         ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
229         ALLFAC.AddFrom(MainMesh.GetMesh())
230
231         #HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Ext3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
232
233         # Scaling down the mesh to meter units
234         if not(Scale==1.):
235                 MeshEditor = MainMesh.GetMeshEditor()
236                 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
237      
238                
239 def NormalizeVector (V,Norm):
240         """
241         This function returns a normalized vector (magnitude = Norm), parallel to the entered one
242         """
243         V = [float(Coor) for Coor in V]
244         Norm = float(Norm)
245         MagV = math.sqrt(V[0]*V[0]+V[1]*V[1]+V[2]*V[2])
246         return [Coor*Norm/MagV for Coor in V]
247