1 # Copyright (C) 2014-2016 EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 from salome.geom import geomBuilder
26 geompy = geomBuilder.New( Config.theStudy )
28 from salome.smesh import smeshBuilder
29 smesh = smeshBuilder.New( Config.theStudy )
31 ##########################################################################################################
34 aFilterManager = smesh.CreateFilterManager()
36 # Building geometric and mesh compounds and groups ##############################################
37 if Config.debug : print "Searching for geometric groups and publishing final compound"
42 for MacroObj in Config.ListObj :
43 TempGEOList += MacroObj.GeoChildren
44 TempMESHList += MacroObj.Mesh
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)
52 for group in Config.Groups :
54 # Geometric groups definition
57 for MacroObj in Config.ListObj :
58 if group in MacroObj.GroupNames :
59 Occurrences = IndexMultiOcc(MacroObj.GroupNames, group)
60 for Occ in Occurrences :
61 TempGEOList += MacroObj.GetBorder(Occ)
62 GroupGEO.append(geompy.MakeCompound(TempGEOList))
63 geompy.addToStudyInFather(FinalCompound,GroupGEO[-1],'GR_'+group)
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)
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)
78 def IndexMultiOcc (Array,Element) :
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!
85 try : Array.index(Element)
86 except ValueError : print "No more occurrences"
87 else : Output.append(Array.index(Element))
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)
95 def Publish (ObjToPublish):
96 for i,GeoObj in enumerate(ObjToPublish) : geompy.addToStudy(GeoObj,"Sub_"+str(i))
98 def RevolveMesh(MainMesh,**args):
100 This function premits 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
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']]
114 print "\nThe coordinates of the center of revolution were not given\nThe origin is used by default."
115 CenterCoor = [0.,0.,0.]
117 if 'Direction' in args : Direction = [float(Dir) for Dir in args['Direction']]
119 print "\nThe axis vector of revolution was not given\nThe x-axis is used by default."
120 Direction = [1.,0.,0.]
122 if 'AngleDeg' in args : Angle = float(args['AngleDeg'])*math.pi/180.
123 elif 'AngleRad' in args : Angle = float(args['AngleRad'])
125 print "\nThe revolution angle was not given\nAn angle of 10 degrees is used by default."
126 Angle = 10.*math.pi/180.
128 if 'Scale' in args : Scale = float(args['Scale'])
132 # Creating the lower face group LOFAC
133 LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
134 LOFAC.AddFrom(MainMesh.GetMesh())
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)
140 # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
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')
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_' ):
153 GR.SetName(CurrentName[:-8])
154 elif CurrentName == 'LOFAC_top':
156 #Index = [ GR_Names[i] in CurrentName for i in range(0,len(GR_Names)) ].index(True)
157 #GR.SetName(GR_Names[Index])
159 # Creating the upper face group HIFAC
160 ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
161 ALLFAC.AddFrom(MainMesh.GetMesh())
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' )
166 # Scaling down the mesh to meter units
168 MeshEditor = MainMesh.GetMeshEditor()
169 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
172 def ExtrudeMesh(MainMesh,**args):
174 This function premits 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
183 ################################################################################
184 # Reading input arguments and proceeding to defaults if necessary
185 ################################################################################
186 if 'Distance' in args : Distance = float(args['Distance'])
188 print "\nThe extrusion distance was not given\nA default value of 1 is used."
191 if 'Direction' in args : Direction = NormalizeVector([float(Dir) for Dir in args['Direction']],Distance)
193 print "\nThe extrusion vector of revolution was not given\nThe z-axis is used by default."
194 Direction = NormalizeVector([0.,0.,1.],Distance)
196 if 'Scale' in args : Scale = float(args['Scale'])
199 if 'NSteps' in args : NSteps = int(args['NSteps'])
202 # Creating the lower face group LOFAC
203 LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
204 LOFAC.AddFrom(MainMesh.GetMesh())
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)
210 # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
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')
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_' ):
223 GR.SetName(CurrentName[:-9])
224 elif CurrentName == 'LOFAC_top':
227 # Creating the upper face group HIFAC
228 ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
229 ALLFAC.AddFrom(MainMesh.GetMesh())
231 #HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Ext3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
233 # Scaling down the mesh to meter units
235 MeshEditor = MainMesh.GetMeshEditor()
236 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
239 def NormalizeVector (V,Norm):
241 This function returns a normalized vector (magnitude = Norm), parallel to the entered one
243 V = [float(Coor) for Coor in V]
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]