Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerWorkflows/FreeCADBatchFEMTools/tests/singlecubemeshtest/singlecubemeshtest_freecadscript.py
3206 views
1
doc = App.newDocument('single cube geometry')
2
import sys
3
import os
4
import time
5
import Part
6
import FreeCAD
7
8
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
9
import FreeCADBatchFEMTools
10
11
12
def print_line(line_str, start_time):
13
"""
14
Prints elapsed time and flushes stdout.
15
16
:param line_str: A string.
17
:param start_time: A float.
18
"""
19
elapsed_time = round(time.time() - start_time, 1)
20
print('Elapsed time: {} seconds'.format(elapsed_time))
21
print(line_str)
22
sys.stdout.flush()
23
return elapsed_time
24
25
def import_step_part(doc, path, name):
26
"""
27
Imports part from step file and returns it.
28
29
:param doc: FreeCAD document.
30
:param path: Path to step file.
31
:param name: A string.
32
33
:return: FreeCAD document object 'Part::Feature'.
34
"""
35
part = doc.addObject('Part::Feature', name + '_obj')
36
part.Shape = Part.read(path)
37
doc.recompute()
38
return part
39
40
def get_cube_entity_dict(cube_name, mesh_size, directory):
41
"""
42
Import cube and create entity dictionary.
43
44
:param cube_name: A string.
45
:param mesh_size: Mesh size of cube.
46
:param directory: Path to directory of this script.
47
48
:return: A list containing entities dictionaries.
49
"""
50
cube = import_step_part(doc, os.path.join(directory, '..', 'stepfiles', 'cube200.step'), cube_name)
51
# create entities dict
52
face_picks = [('alpha0', 5), ('alpha1', 0), ('beta0', 4), ('beta1', 2), ('gamma0', 3), ('gamma1', 1)]
53
faces = FreeCADBatchFEMTools.pick_faces_from_geometry(cube, face_picks)
54
solids = []
55
FreeCADBatchFEMTools.add_entity_in_list(solids, cube_name, cube, {'mesh size': mesh_size})
56
return FreeCADBatchFEMTools.create_entities_dict(cube_name, faces, solids, cube)
57
58
def create_geometry(directory):
59
"""
60
Imports one cube and exports unv file.
61
62
:param directory: Path to directory of this script.
63
64
:return: A list containing tuples (name, execution time).
65
"""
66
total_start_time = time.time()
67
entities_list = []
68
return_time_list = []
69
mesh_size = 20
70
mesh_size_max = mesh_size
71
72
print_line('Creating cube geometry...', total_start_time)
73
entities_list.append(get_cube_entity_dict('cube200', mesh_size, directory))
74
75
print_line("Merging entities dictionaries...", total_start_time)
76
entities_dict = FreeCADBatchFEMTools.merge_entities_dicts(entities_list, 'All', default_mesh_size=mesh_size_max,
77
add_prefixes={'solids': False, 'faces': True})
78
print_line("Getting solids from entities dictionaries...", total_start_time)
79
solid_objects = FreeCADBatchFEMTools.get_solids_from_entities_dict(entities_dict)
80
print_line("Creating mesh object and compound filter...", total_start_time)
81
mesh_object, compound_filter = FreeCADBatchFEMTools.create_mesh_object_and_compound_filter(solid_objects,
82
mesh_size_max, doc)
83
84
print_line("Finding boundaries...", total_start_time)
85
start_time = time.time()
86
FreeCADBatchFEMTools.find_boundaries_with_entities_dict(mesh_object, compound_filter, entities_dict, doc)
87
return_time_list.append(('-Find boundaries:', time.time() - start_time))
88
print_line("Finding bodies...", total_start_time)
89
start_time = time.time()
90
body_mesh_groups = FreeCADBatchFEMTools.find_bodies_with_entities_dict(mesh_object, compound_filter,
91
entities_dict, doc)
92
return_time_list.append(('-Find solids:', time.time() - start_time))
93
print_line("Defining mesh sizes...", total_start_time)
94
start_time = time.time()
95
FreeCADBatchFEMTools.define_mesh_sizes_with_mesh_groups(mesh_object, body_mesh_groups, doc)
96
return_time_list.append(('-Define mesh sizes:', time.time() - start_time))
97
FreeCADBatchFEMTools.fit_view()
98
start_time = time.time()
99
print_line("Creating mesh...", total_start_time)
100
FreeCADBatchFEMTools.create_mesh(mesh_object)
101
return_time_list.append(('-Create mesh:', time.time() - start_time))
102
print_line("Exporting unv...", total_start_time)
103
FreeCADBatchFEMTools.export_unv(os.path.join(directory, 'singlecubemeshtest.unv'), mesh_object)
104
print_line("Geometry done", total_start_time)
105
return return_time_list + [('-Total time:', time.time() - total_start_time)]
106
107
108
script_directory = os.path.dirname(__file__)
109
110
try:
111
elapsed_times = create_geometry(script_directory)
112
except Exception:
113
import traceback
114
print(str(traceback.format_exc()))
115
else:
116
print('\nexecution times with 1 cube:')
117
for time_tuple in elapsed_times:
118
print(time_tuple[0], round(time_tuple[1], 1))
119
sys.stdout.flush()
120
121
if not FreeCAD.GuiUp:
122
exit()
123
124