Path: blob/devel/ElmerWorkflows/FreeCADBatchFEMTools/tests/singlecubemeshtest/singlecubemeshtest_freecadscript.py
3206 views
doc = App.newDocument('single cube geometry')1import sys2import os3import time4import Part5import FreeCAD67sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))8import FreeCADBatchFEMTools91011def print_line(line_str, start_time):12"""13Prints elapsed time and flushes stdout.1415:param line_str: A string.16:param start_time: A float.17"""18elapsed_time = round(time.time() - start_time, 1)19print('Elapsed time: {} seconds'.format(elapsed_time))20print(line_str)21sys.stdout.flush()22return elapsed_time2324def import_step_part(doc, path, name):25"""26Imports part from step file and returns it.2728:param doc: FreeCAD document.29:param path: Path to step file.30:param name: A string.3132:return: FreeCAD document object 'Part::Feature'.33"""34part = doc.addObject('Part::Feature', name + '_obj')35part.Shape = Part.read(path)36doc.recompute()37return part3839def get_cube_entity_dict(cube_name, mesh_size, directory):40"""41Import cube and create entity dictionary.4243:param cube_name: A string.44:param mesh_size: Mesh size of cube.45:param directory: Path to directory of this script.4647:return: A list containing entities dictionaries.48"""49cube = import_step_part(doc, os.path.join(directory, '..', 'stepfiles', 'cube200.step'), cube_name)50# create entities dict51face_picks = [('alpha0', 5), ('alpha1', 0), ('beta0', 4), ('beta1', 2), ('gamma0', 3), ('gamma1', 1)]52faces = FreeCADBatchFEMTools.pick_faces_from_geometry(cube, face_picks)53solids = []54FreeCADBatchFEMTools.add_entity_in_list(solids, cube_name, cube, {'mesh size': mesh_size})55return FreeCADBatchFEMTools.create_entities_dict(cube_name, faces, solids, cube)5657def create_geometry(directory):58"""59Imports one cube and exports unv file.6061:param directory: Path to directory of this script.6263:return: A list containing tuples (name, execution time).64"""65total_start_time = time.time()66entities_list = []67return_time_list = []68mesh_size = 2069mesh_size_max = mesh_size7071print_line('Creating cube geometry...', total_start_time)72entities_list.append(get_cube_entity_dict('cube200', mesh_size, directory))7374print_line("Merging entities dictionaries...", total_start_time)75entities_dict = FreeCADBatchFEMTools.merge_entities_dicts(entities_list, 'All', default_mesh_size=mesh_size_max,76add_prefixes={'solids': False, 'faces': True})77print_line("Getting solids from entities dictionaries...", total_start_time)78solid_objects = FreeCADBatchFEMTools.get_solids_from_entities_dict(entities_dict)79print_line("Creating mesh object and compound filter...", total_start_time)80mesh_object, compound_filter = FreeCADBatchFEMTools.create_mesh_object_and_compound_filter(solid_objects,81mesh_size_max, doc)8283print_line("Finding boundaries...", total_start_time)84start_time = time.time()85FreeCADBatchFEMTools.find_boundaries_with_entities_dict(mesh_object, compound_filter, entities_dict, doc)86return_time_list.append(('-Find boundaries:', time.time() - start_time))87print_line("Finding bodies...", total_start_time)88start_time = time.time()89body_mesh_groups = FreeCADBatchFEMTools.find_bodies_with_entities_dict(mesh_object, compound_filter,90entities_dict, doc)91return_time_list.append(('-Find solids:', time.time() - start_time))92print_line("Defining mesh sizes...", total_start_time)93start_time = time.time()94FreeCADBatchFEMTools.define_mesh_sizes_with_mesh_groups(mesh_object, body_mesh_groups, doc)95return_time_list.append(('-Define mesh sizes:', time.time() - start_time))96FreeCADBatchFEMTools.fit_view()97start_time = time.time()98print_line("Creating mesh...", total_start_time)99FreeCADBatchFEMTools.create_mesh(mesh_object)100return_time_list.append(('-Create mesh:', time.time() - start_time))101print_line("Exporting unv...", total_start_time)102FreeCADBatchFEMTools.export_unv(os.path.join(directory, 'singlecubemeshtest.unv'), mesh_object)103print_line("Geometry done", total_start_time)104return return_time_list + [('-Total time:', time.time() - total_start_time)]105106107script_directory = os.path.dirname(__file__)108109try:110elapsed_times = create_geometry(script_directory)111except Exception:112import traceback113print(str(traceback.format_exc()))114else:115print('\nexecution times with 1 cube:')116for time_tuple in elapsed_times:117print(time_tuple[0], round(time_tuple[1], 1))118sys.stdout.flush()119120if not FreeCAD.GuiUp:121exit()122123124