2 import geompy, smesh, SMESH
5 ##########################################################################################################
8 aFilterManager = smesh.CreateFilterManager()
10 # Building geometric and mesh compounds and groups ##############################################
11 if Config.debug : print "Searching for geometric groups and publishing final compound"
16 for MacroObj in Config.ListObj :
17 TempGEOList += MacroObj.GeoChildren
18 TempMESHList += MacroObj.Mesh
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)
26 for group in Config.Groups :
28 # Geometric groups definition
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)
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)
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)
51 def IndexMultiOcc (Array,Element) :
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!
58 try : Array.index(Element)
59 except ValueError : print "No more occurrences"
60 else : Output.append(Array.index(Element))
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)
68 def Publish (ObjToPublish):
69 for i,GeoObj in enumerate(ObjToPublish) : geompy.addToStudy(GeoObj,"Sub_"+str(i))
71 def RevolveMesh(MainMesh,**args):
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
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']]
87 print "\nThe coordinates of the center of revolution were not given\nThe origin is used by default."
88 CenterCoor = [0.,0.,0.]
90 if 'Direction' in args : Direction = [float(Dir) for Dir in args['Direction']]
92 print "\nThe axis vector of revolution was not given\nThe x-axis is used by default."
93 Direction = [1.,0.,0.]
95 if 'AngleDeg' in args : Angle = float(args['AngleDeg'])*math.pi/180.
96 elif 'AngleRad' in args : Angle = float(args['AngleRad'])
98 print "\nThe revolution angle was not given\nAn angle of 10 degrees is used by default."
99 Angle = 10.*math.pi/180.
101 if 'Scale' in args : Scale = float(args['Scale'])
105 # Creating the lower face group LOFAC
106 LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
107 LOFAC.AddFrom(MainMesh.GetMesh())
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)
113 # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
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')
119 # Removing the _rotated prefix from the rotated FACE groups
120 for GR in Rev3DMeshGroups:
121 CurrentName = GR.GetName()
122 if CurrentName=='LOFAC_rotated' :
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])
129 # Creating the upper face group HIFAC
130 ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
131 ALLFAC.AddFrom(MainMesh.GetMesh())
133 HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Rev3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
135 # Scaling down the mesh to meter units
137 MeshEditor = MainMesh.GetMeshEditor()
138 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
141 def ExtrudeMesh(MainMesh,**args):
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
152 ################################################################################
153 # Reading input arguments and proceeding to defaults if necessary
154 ################################################################################
155 if 'Distance' in args : Distance = float(args['Distance'])
157 print "\nThe extrusion distance was not given\nA default value of 1 is used."
160 if 'Direction' in args : Direction = NormalizeVector([float(Dir) for Dir in args['Direction']],Distance)
162 print "\nThe extrusion vector of revolution was not given\nThe z-axis is used by default."
163 Direction = NormalizeVector([0.,0.,1.],Distance)
165 if 'Scale' in args : Scale = float(args['Scale'])
168 if 'NSteps' in args : NSteps = int(args['NSteps'])
171 # Creating the lower face group LOFAC
172 LOFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'LOFAC' )
173 LOFAC.AddFrom(MainMesh.GetMesh())
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)
179 # Adding an EDGE suffix to the edge groups (to be deleted eventually by the user...)
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')
185 # Removing the _rotated prefix from the rotated FACE groups
186 for GR in Ext3DMeshGroups:
187 CurrentName = GR.GetName()
188 if CurrentName=='LOFAC_extruded' :
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])
195 # Creating the upper face group HIFAC
196 ALLFAC = MainMesh.CreateEmptyGroup( SMESH.FACE, 'ALLFAC' )
197 ALLFAC.AddFrom(MainMesh.GetMesh())
199 HIFAC = MainMesh.GetMesh().CutListOfGroups( [ ALLFAC ], [LOFAC] + [ MeshGroup for MeshGroup in Ext3DMeshGroups if not(MeshGroup.GetName()=='VOL') ], 'HIFAC' )
201 # Scaling down the mesh to meter units
203 MeshEditor = MainMesh.GetMeshEditor()
204 MeshEditor.Scale( MainMesh.GetMesh(), SMESH.PointStruct( 0, 0, 0 ) ,[ Scale, Scale, Scale ], 0 )
207 def NormalizeVector (V,Norm):
209 This function returns a normalized vector (magnitude = Norm), parallel to the entered one
211 V = [float(Coor) for Coor in V]
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]