1 # Copyright (C) 2014-2023 EDF
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()
28 from salome.smesh import smeshBuilder
29 smesh = smeshBuilder.New()
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
56 aFilterManager = smesh.CreateFilterManager()
58 # Building geometric and mesh compounds and groups ##############################################
59 if Config.debug : print("Searching for geometric groups and publishing final compound")
64 for MacroObj in Config.ListObj :
65 TempGEOList += MacroObj.GeoChildren
66 TempMESHList += MacroObj.Mesh
68 FinalCompound = geompy.MakeCompound(TempGEOList)
69 geompy.addToStudy (FinalCompound,Config.StudyName)
70 MeshCompound = smesh.Concatenate(TempMESHList, 1, 1, 1e-5)
71 MeshCompound.SetName(Config.StudyName)
74 for group in Config.Groups :
76 # Geometric groups definition
79 for MacroObj in Config.ListObj :
80 if group in MacroObj.GroupNames :
81 Occurrences = IndexMultiOcc(MacroObj.GroupNames, group)
82 for Occ in Occurrences :
83 TempGEOList += MacroObj.GetBorder(Occ)
84 GroupGEO.append(geompy.MakeCompound(TempGEOList))
85 geompy.addToStudyInFather(FinalCompound,GroupGEO[-1],'GR_'+group)
87 # Mesh groups definition
88 Criterion = smesh.GetCriterion(SMESH.EDGE, SMESH.FT_BelongToGeom,'=',GroupGEO[-1],Tolerance=1e-06)
89 #Criterion = smesh.Filter.Criterion(18,39,0,'GR_'+group,'GR_'+group,39,39,1e-06,smesh.EDGE,7)
90 MeshCompound.MakeGroupByCriterion(group,Criterion)
92 StudyBuilder = Config.theStudy.NewBuilder()
93 for MeshObj in TempMESHList:
94 SO = Config.theStudy.FindObjectIOR(Config.theStudy.ConvertObjectToIOR(MeshObj))
95 if SO is not None: StudyBuilder.RemoveObjectWithChildren(SO)
100 def IndexMultiOcc (Array,Element) :
102 This function returns the occurrences indices of Element in Array.
103 As opposed to Array.index(Element) method, this allows determining
104 multiple entries rather than just the first one!
107 try : Array.index(Element)
108 except ValueError : print("No more occurrences")
109 else : Output.append(Array.index(Element))
111 if not(Output == [-1]) and len(Array) > 1 :
112 for index, ArrElem in enumerate(Array[Output[0]+1:]) :
113 if ArrElem is Element : Output.append(index+Output[0]+1)
117 def Publish (ObjToPublish):
118 for i,GeoObj in enumerate(ObjToPublish) : geompy.addToStudy(GeoObj,"Sub_"+str(i))
120 def RevolveMesh(MainMesh,**args):
122 This function permits to revolute and scale a 2D mesh while transforming the edge
123 groups into face groups. Moreover, the function automatically creates the face groups
124 corresponding to the symmetry lower and upper faces
125 Facultatif arguments are :
126 - Center [X,Y,Z], origin being the default
127 - Direction [VX,VY,VZ], x-axis being the default
128 - AngleDeg or AngleRad : ALPHA, 10 degrees being the default
129 - Scale : BETA, no scaling being default
131 ################################################################################
132 # Reading input arguments and proceeding to defaults if necessary
133 ################################################################################
134 if 'Center' in args : CenterCoor = [float(Coor) for Coor in args['Center']]
136 print("\nThe coordinates of the center of revolution were not given\nThe origin is used by default.")
137 CenterCoor = [0.,0.,0.]
139 if 'Direction' in args : Direction = [float(Dir) for Dir in args['Direction']]
141 print("\nThe axis vector of revolution was not given\nThe x-axis is used by default.")
142 Direction = [1.,0.,0.]
144 if 'AngleDeg' in args : Angle = float(args['AngleDeg'])*math.pi/180.
145 elif 'AngleRad' in args : Angle = float(args['AngleRad'])
147 print("\nThe revolution angle was not given\nAn angle of 10 degrees is used by default.")
148 Angle = 10.*math.pi/180.
150 if 'Scale' in args : Scale = float(args['Scale'])
154 # Creating the lower face group LOFAC
155 LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
156 LOFAC.AddFrom(MainMesh.GetMesh())
158 GR_Names = MainMesh.GetGroupNames()
159 GRs = MainMesh.GetGroups()
160 Rev3DMeshGroups = MainMesh.RotationSweepObject2D( MainMesh, SMESH.AxisStruct( CenterCoor[0], CenterCoor[1], CenterCoor[2], Direction[0], Direction[1], Direction[2] ), Angle, 1, 1e-05 ,True)
162 # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
164 CurrentName = GR.GetName()
165 if CurrentName in GR_Names and not(CurrentName=='LOFAC'): # Meaning that this is an old edge group
166 GR.SetName(CurrentName+'_EDGE')
168 # Removing the _rotated prefix from the rotated FACE groups
169 for GR in Rev3DMeshGroups:
170 CurrentName = GR.GetName()
171 if CurrentName.endswith( "_rotated"):
172 if CurrentName.startswith( 'LOFAC_' ):
175 GR.SetName(CurrentName[:-8])
176 elif CurrentName == 'LOFAC_top':
178 #Index = [ GR_Names[i] in CurrentName for i in range(0,len(GR_Names)) ].index(True)
179 #GR.SetName(GR_Names[Index])
181 # Creating the upper face group HIFAC
182 ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
183 ALLFAC.AddFrom(MainMesh.GetMesh())
185 #HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Rev3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
186 #HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Rev3DMeshGroups if ( not(MeshGroup.GetName()=='VOL') and MeshGroup.GetType() == SMESH.FACE )], 'HIFAC' )
188 # Scaling down the mesh to meter units
190 MeshEditor = MainMesh.GetMeshEditor()
191 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
194 def ExtrudeMesh(MainMesh,**args):
196 This function permits to extrude and scale a 2D mesh while transforming the edge
197 groups into face groups. Moreover, the function automatically creates the face groups
198 corresponding to the symmetry lower and upper faces
199 Facultatif arguments are :
200 - Direction [VX,VY,VZ], z-axis being default
201 - Distance : D, default is 1
202 - NSteps : the object will be extruded by NSteps*Distance, default is Nsteps = 1
203 - Scale : BETA, no scaling being default
205 ################################################################################
206 # Reading input arguments and proceeding to defaults if necessary
207 ################################################################################
208 if 'Distance' in args : Distance = float(args['Distance'])
210 print("\nThe extrusion distance was not given\nA default value of 1 is used.")
213 if 'Direction' in args : Direction = NormalizeVector([float(Dir) for Dir in args['Direction']],Distance)
215 print("\nThe extrusion vector of revolution was not given\nThe z-axis is used by default.")
216 Direction = NormalizeVector([0.,0.,1.],Distance)
218 if 'Scale' in args : Scale = float(args['Scale'])
221 if 'NSteps' in args : NSteps = int(args['NSteps'])
224 # Creating the lower face group LOFAC
225 LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
226 LOFAC.AddFrom(MainMesh.GetMesh())
228 GR_Names = MainMesh.GetGroupNames()
229 GRs = MainMesh.GetGroups()
230 Ext3DMeshGroups = MainMesh.ExtrusionSweepObject2D(MainMesh,SMESH.DirStruct(SMESH.PointStruct(Direction[0],Direction[1],Direction[2])), NSteps, True)
232 # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
234 CurrentName = GR.GetName()
235 if CurrentName in GR_Names and not(CurrentName=='LOFAC'): # Meaning that this is an old edge group
236 GR.SetName(CurrentName+'_EDGE')
238 # Removing the _extruded suffix from the extruded FACE groups
239 for GR in Ext3DMeshGroups:
240 CurrentName = GR.GetName()
241 if CurrentName.endswith( "_extruded"):
242 if CurrentName.startswith( 'LOFAC_' ):
245 GR.SetName(CurrentName[:-9])
246 elif CurrentName == 'LOFAC_top':
249 # Creating the upper face group HIFAC
250 ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
251 ALLFAC.AddFrom(MainMesh.GetMesh())
253 #HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Ext3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
255 # Scaling down the mesh to meter units
257 MeshEditor = MainMesh.GetMeshEditor()
258 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
261 def NormalizeVector (V,Norm):
263 This function returns a normalized vector (magnitude = Norm), parallel to the entered one
265 V = [float(Coor) for Coor in V]
267 MagV = math.sqrt(V[0]*V[0]+V[1]*V[1]+V[2]*V[2])
268 return [Coor*Norm/MagV for Coor in V]