Salome HOME
dc8dcd899974a36829bc615200baf578435813be
[modules/smesh.git] / src / Tools / MacMesh / MacMesh / PublishGroups.py
1
2 import geompy, smesh, SMESH
3 import math
4 import Config
5 ##########################################################################################################
6
7 def PublishGroups ():
8         aFilterManager = smesh.CreateFilterManager()
9
10         # Building geometric and mesh compounds and  groups ##############################################
11         if Config.debug : print "Searching for geometric groups and publishing final compound"
12         
13         TempGEOList = []
14         TempMESHList = []
15         
16         for MacroObj in Config.ListObj : 
17                 TempGEOList += MacroObj.GeoChildren
18                 TempMESHList += MacroObj.Mesh
19                 
20         FinalCompound = geompy.MakeCompound(TempGEOList)
21         geompy.addToStudy (FinalCompound,Config.StudyName)
22         MeshCompound = smesh.Concatenate(TempMESHList, 1, 1, 1e-5)
23         MeshCompound.SetName(Config.StudyName)
24         
25         GroupGEO = []
26         for group in Config.Groups :
27         
28                 # Geometric groups definition
29                 TempGEOList = []
30                 TempNames = []
31                 for MacroObj in Config.ListObj :
32                         if group in MacroObj.GroupNames :
33                                 Occurences = IndexMultiOcc(MacroObj.GroupNames, group)
34                                 for Occ in Occurences :
35                                         TempGEOList += MacroObj.GetBorder(Occ)
36                 GroupGEO.append(geompy.MakeCompound(TempGEOList))
37                 geompy.addToStudyInFather(FinalCompound,GroupGEO[-1],'GR_'+group)
38                 
39                 # Mesh groups definition
40                 Criterion = smesh.Filter.Criterion(18,39,0,'GR_'+group,'GR_'+group,39,39,1e-06,smesh.EDGE,7)
41                 MeshCompound.MakeGroupByCriterion(group,Criterion)
42         
43         StudyBuilder = Config.theStudy.NewBuilder()
44         for MeshObj in TempMESHList:
45                 SO = Config.theStudy.FindObjectIOR(Config.theStudy.ConvertObjectToIOR(MeshObj))
46                 if SO is not None: StudyBuilder.RemoveObjectWithChildren(SO)
47         
48         return MeshCompound        
49                 
50
51 def IndexMultiOcc (Array,Element) :
52         """
53         This function returns the occurrences indices of Element in Array.
54         As opposed to Array.index(Element) method, this allows determining      
55         multiple entries rather than just the first one!
56         """
57         Output = []
58         try : Array.index(Element)
59         except ValueError : print "No more occurrences"
60         else : Output.append(Array.index(Element))
61                 
62         if not(Output == [-1]) and len(Array) > 1 :
63                 for index, ArrElem in enumerate(Array[Output[0]+1:]) :
64                         if ArrElem is Element : Output.append(index+Output[0]+1)
65                  
66         return Output
67             
68 def Publish (ObjToPublish):
69         for i,GeoObj in enumerate(ObjToPublish) : geompy.addToStudy(GeoObj,"Sub_"+str(i))
70         
71 def RevolveMesh(MainMesh,**args):
72         """
73         This function premits to revolute and scale a 2D mesh while transforming the edge
74         groups into face groups. Moreover, the function automatically creates the face groups 
75         corresponding to the symmetry lower and upper faces
76         Facultatif arguments are : 
77                 - Center [X,Y,Z], origin being the default
78                 - Direction [VX,VY,VZ], x-axis being the default
79                 - AngleDeg or AngleRad : ALPHA, 10 degrees being the default
80                 - Scale : BETA, no scaling being default
81         """
82         ################################################################################
83         # Reading input arguments and proceeding to defaults if necessary
84         ################################################################################       
85         if 'Center' in args : CenterCoor = [float(Coor) for Coor in args['Center']]
86         else : 
87                 print "\nThe coordinates of the center of revolution were not given\nThe origin is used by default."
88                 CenterCoor = [0.,0.,0.]
89         
90         if 'Direction' in args : Direction = [float(Dir) for Dir in args['Direction']]
91         else :
92                 print "\nThe axis vector of revolution was not given\nThe x-axis is used by default."
93                 Direction = [1.,0.,0.]
94         
95         if 'AngleDeg' in args : Angle = float(args['AngleDeg'])*math.pi/180.
96         elif 'AngleRad' in args : Angle = float(args['AngleRad'])
97         else :
98                 print "\nThe revolution angle was not given\nAn angle of 10 degrees is used by default."
99                 Angle = 10.*math.pi/180.
100                 
101         if 'Scale' in args : Scale = float(args['Scale'])
102         else : Scale = 1.
103         
104
105         # Creating the lower face group LOFAC
106         LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
107         LOFAC.AddFrom(MainMesh.GetMesh())
108
109         GR_Names = MainMesh.GetGroupNames()
110         GRs = MainMesh.GetGroups()
111         Rev3DMeshGroups = MainMesh.RotationSweepObject2D( MainMesh, SMESH.AxisStruct( CenterCoor[0], CenterCoor[1], CenterCoor[2], Direction[0], Direction[1], Direction[2] ), Angle, 1, 1e-05 ,True)
112
113         # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
114         for GR in GRs:
115                 CurrentName = GR.GetName()
116                 if CurrentName in GR_Names and not(CurrentName=='LOFAC'):  # Meaning that this is an old edge group
117                         GR.SetName(CurrentName+'_EDGE')
118
119         # Removing the _rotated prefix from the rotated FACE groups
120         for GR in Rev3DMeshGroups:
121                 CurrentName = GR.GetName()
122                 if CurrentName=='LOFAC_rotated' :
123                         GR.SetName('VOL')
124                 else :
125                         #Index = [ GR_Names[i] in CurrentName for i in range(0,len(GR_Names)) ].index(True)
126                         #GR.SetName(GR_Names[Index])
127                         GR.SetName(CurrentName[:-8])
128
129         # Creating the upper face group HIFAC
130         ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
131         ALLFAC.AddFrom(MainMesh.GetMesh())
132
133         HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Rev3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
134
135         # Scaling down the mesh to meter units
136         if not(Scale==1.):
137                 MeshEditor = MainMesh.GetMeshEditor()
138                 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
139      
140                 
141 def ExtrudeMesh(MainMesh,**args):
142         """
143         This function premits to extrude and scale a 2D mesh while transforming the edge
144         groups into face groups. Moreover, the function automatically creates the face groups 
145         corresponding to the symmetry lower and upper faces
146         Facultatif arguments are : 
147                 - Direction [VX,VY,VZ], z-axis being default
148                 - Distance : D, default is 1
149                 - NSteps : the object will be extruded by NSteps*Distance, default is Nsteps = 1
150                 - Scale : BETA, no scaling being default
151         """
152         ################################################################################
153         # Reading input arguments and proceeding to defaults if necessary
154         ################################################################################              
155         if 'Distance' in args : Distance = float(args['Distance'])
156         else :
157                 print "\nThe extrusion distance was not given\nA default value of 1 is used."
158                 Distance = 1.
159                 
160         if 'Direction' in args : Direction = NormalizeVector([float(Dir) for Dir in args['Direction']],Distance)
161         else :
162                 print "\nThe extrusion vector of revolution was not given\nThe z-axis is used by default."
163                 Direction = NormalizeVector([0.,0.,1.],Distance)
164                                 
165         if 'Scale' in args : Scale = float(args['Scale'])
166         else : Scale = 1.
167         
168         if 'NSteps' in args : NSteps = int(args['NSteps'])
169         else : NSteps = 1
170         
171         # Creating the lower face group LOFAC
172         LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
173         LOFAC.AddFrom(MainMesh.GetMesh())
174
175         GR_Names = MainMesh.GetGroupNames()
176         GRs = MainMesh.GetGroups()
177         Ext3DMeshGroups = MainMesh.ExtrusionSweepObject2D(MainMesh,SMESH.DirStruct(SMESH.PointStruct(Direction[0],Direction[1],Direction[2])), NSteps, True)
178
179         # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
180         for GR in GRs:
181                 CurrentName = GR.GetName()
182                 if CurrentName in GR_Names and not(CurrentName=='LOFAC'):  # Meaning that this is an old edge group
183                         GR.SetName(CurrentName+'_EDGE')
184
185         # Removing the _rotated prefix from the rotated FACE groups
186         for GR in Ext3DMeshGroups:
187                 CurrentName = GR.GetName()
188                 if CurrentName=='LOFAC_extruded' :
189                         GR.SetName('VOL')
190                 else :
191                         #Index = [ GR_Names[i] in CurrentName for i in range(0,len(GR_Names)) ].index(True)
192                         #GR.SetName(GR_Names[Index])
193                         GR.SetName(CurrentName[:-9])
194
195         # Creating the upper face group HIFAC
196         ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
197         ALLFAC.AddFrom(MainMesh.GetMesh())
198
199         HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Ext3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
200
201         # Scaling down the mesh to meter units
202         if not(Scale==1.):
203                 MeshEditor = MainMesh.GetMeshEditor()
204                 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
205      
206                
207 def NormalizeVector (V,Norm):
208         """
209         This function returns a normalized vector (magnitude = Norm), parallel to the entered one
210         """
211         V = [float(Coor) for Coor in V]
212         Norm = float(Norm)
213         MagV = math.sqrt(V[0]*V[0]+V[1]*V[1]+V[2]*V[2])
214         return [Coor*Norm/MagV for Coor in V]
215