Mesh Recording
This code creates a list of all selected named selections and utilize mesh recording to mesh the corresponding geometry in all possible sequencies (N! number of meshing sequencies, where N is the number of named selections). Then, the final mesh will be based on the sequence with the highest average element quality.
So, select the Named Selections you want to use (in the Tree) and run the script!
Named Selections Selected = 1 –> no re-meshing
Named Selections Selected = 2 –> 2! = 2 * 1 = 2 times to re-mesh
Named Selections Selected = 3 –> 3! = 3 * 2 * 1 = 6 times to re-mesh
Named Selections Selected = N –> N! = N * N-1 * N-2 * N-3 * … * N-(N-1) times to re-mesh
import itertools
#Model.Geometry.UnsuppressAllBodies()
named_sels = DataModel.GetObjectsByType(DataModelObjectCategory.NamedSelection)
#sequence_shuffled = [i.Name for i in named_sels]
sequence_shuffled = [i.Name for i in Tree.ActiveObjects]
all_sequencies = list(itertools.permutations(sequence_shuffled))
mesh = Model.Mesh
mesh.MeshMetric = MeshMetricType.ElementQuality
average_mesh_quality = {}
activeBodies = []
for index,sequence in enumerate(all_sequencies):
sequence_dict = {}
for child in sequence:
current_ns = [i for i in named_sels if i.Name == child][0]
currentIds = current_ns.Location
for id_ in currentIds:
geoBody = DataModel.GeoData.GeoEntityById(id_)
treeBody = Model.Geometry.GetBody(geoBody)
if treeBody not in activeBodies: activeBodies.append(treeBody)
if current_ns.Name not in sequence_dict.keys():
sequence_dict.Add(current_ns.Name,[])
sequence_dict[current_ns.Name].append(treeBody)
else: sequence_dict[current_ns.Name].append(treeBody)
treeBody.Suppressed = True
for key in sequence:
for body in sequence_dict[key]:
body.Suppressed = False
body.GenerateMesh()
sequence_success = True
for meshed_body in activeBodies:
if meshed_body.Nodes < 1:
sequence_success = False
break
if sequence_success:
average_mesh_quality.Add(index,[])
average_mesh_quality[index].append(sequence)
average_mesh_quality[index].append(mesh.Average.Value)
if 'values' not in average_mesh_quality.keys(): average_mesh_quality.Add('values',[])
average_mesh_quality['values'].append(mesh.Average.Value)
mesh.ClearGeneratedData()
try:
max_quality = max(average_mesh_quality['values'])
for i in range(average_mesh_quality.Count-1):
if average_mesh_quality[i][1] == max_quality:
best_sequence = average_mesh_quality[i][0]
except: print ("No Sequence successed!")
for child in best_sequence:
current_ns = [i for i in named_sels if i.Name == child][0]
currentIds = current_ns.Location
for id_ in currentIds:
geoBody = DataModel.GeoData.GeoEntityById(id_)
treeBody = Model.Geometry.GetBody(geoBody)
treeBody.Suppressed = False
treeBody.GenerateMesh()