Skip to content

Commit a050c1f

Browse files
author
JAmmermann-DLR
committed
Update geometry export - (Task #20)
Add _export_to_stl function Update test_full_export_shape_geometry test case --- Task #20: Add testcases with special shapes to Im- and Exporter
1 parent 8d782d6 commit a050c1f

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

VirtualSatelliteCAD/json_io/parts/json_part_geometry.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from json_io.parts.json_part import AJsonPart
2929
from json_io.json_definitions import JSON_ELEMENT_STL_PATH
30-
import Mesh # NOQA @UnresolvedImport
30+
import Mesh, MeshPart # NOQA @UnresolvedImport
3131
import Part # NOQA @UnusedImport
3232
import os
3333

@@ -56,9 +56,16 @@ def read_from_freecad(self, freecad_object, freecad_sheet):
5656
# use already read in sheet
5757
self.stl_path = self.sheet.read_sheet_attribute_from_freecad(freecad_sheet, "stl_path")
5858

59+
# TODO: here?
60+
self._export_to_stl(freecad_object)
61+
62+
# this part has no FreeCAD properties
5963
def _set_freecad_properties(self, active_document):
6064
pass
6165

66+
def _get_freecad_properties(self, geometry):
67+
pass
68+
6269
def _get_geometry_name(self):
6370
geometry_full_path = self.stl_path
6471
geometry_base_name = os.path.basename(geometry_full_path)
@@ -94,5 +101,15 @@ def _create_freecad_object(self, active_document):
94101
active_document.gui_active_document.getObject(object_geometry_name + "_form").Visibility = False
95102
active_document.gui_active_document.getObject(object_geometry_name + "_cleaned").Visibility = False
96103

97-
def _get_freecad_properties(self, geometry):
98-
pass
104+
def _export_to_stl(self, freecad_object):
105+
106+
# object_name_and_type = self.get_shape_type()
107+
# document_object = active_document.app_active_document.getObject(object_name_and_type)
108+
109+
if freecad_object is not None:
110+
shape = freecad_object.Shape
111+
112+
meshed_object = freecad_object.Document.addObject("Mesh::Feature", "Mesh")
113+
meshed_object.Mesh = MeshPart.meshFromShape(Shape=shape, MaxLength=520)
114+
115+
meshed_object.Mesh.write(self.stl_path)

VirtualSatelliteCAD/test/json_io/parts/test_json_part_geometry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
from module.environment import Environment
3535
from json_io.json_definitions import JSON_ELEMENT_STL_PATH
3636
from test.json_io.test_json_data import TEST_JSON_PART_GEOMETRY
37+
from shutil import copyfile
38+
import os
3739

3840
App = FreeCAD
3941
Gui = FreeCADGui
@@ -86,7 +88,9 @@ def test_create_and_read_part_geometry(self):
8688
# get the current module path and get the directory for the test resource
8789
# place that path into the json object before executing the transformations
8890
stl_test_resource_path = Environment.get_test_resource_path("Switch.stl")
89-
json_object[JSON_ELEMENT_STL_PATH] = stl_test_resource_path
91+
stl_test_resource_path_cp = os.path.join(self._WORKING_DIRECTORY, "Switch_cp.stl")
92+
copyfile(stl_test_resource_path, stl_test_resource_path_cp)
93+
json_object[JSON_ELEMENT_STL_PATH] = stl_test_resource_path_cp
9094

9195
json_part = JsonPartGeometry()
9296
json_part.parse_from_json(json_object)

VirtualSatelliteCAD/test/json_io/test_json_exporter.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
from test.test_setup import AWorkingDirectoryTest
2828
from json_io.json_importer import JsonImporter
2929
from json_io.json_exporter import JsonExporter
30-
from test.json_io.test_json_data import TEST_JSON_FULL_VISCUBE
30+
from test.json_io.test_json_data import TEST_JSON_FULL_VISCUBE, TEST_JSON_FULL_GEOMETRY
3131
from json_io.json_definitions import JSON_PRODUCTS, JSON_ELEMNT_CHILDREN, JSON_ELEMENT_NAME, \
32-
JSON_ELEMENT_ROT_X, JSON_ELEMENT_ROT_Y, JSON_ELEMENT_ROT_Z
32+
JSON_ELEMENT_ROT_X, JSON_ELEMENT_ROT_Y, JSON_ELEMENT_ROT_Z, JSON_ELEMENT_STL_PATH, JSON_PARTS
3333
import json
34+
from module.environment import Environment
35+
import os
36+
from shutil import copyfile
37+
import Mesh
3438

3539

3640
class TestJsonExporter(AWorkingDirectoryTest):
@@ -75,4 +79,33 @@ def test_full_export_shape_none(self):
7579
pass
7680

7781
def test_full_export_shape_geometry(self):
78-
pass
82+
json_importer = JsonImporter(self._WORKING_DIRECTORY)
83+
json_object = json.loads(TEST_JSON_FULL_GEOMETRY)
84+
85+
# get the current module path and get the directory for the test resource
86+
# place that path into the json object before executing the transformations
87+
stl_test_resource_path = Environment.get_test_resource_path("Switch.stl")
88+
stl_test_resource_path_cp = os.path.join(self._WORKING_DIRECTORY, "Switch_cp.stl")
89+
copyfile(stl_test_resource_path, stl_test_resource_path_cp)
90+
copy_time = os.path.getmtime(stl_test_resource_path_cp)
91+
json_object[JSON_PARTS][0][JSON_ELEMENT_STL_PATH] = stl_test_resource_path_cp
92+
93+
_, _, active_document = json_importer.full_import(json_object)
94+
95+
json_exporter = JsonExporter(self._WORKING_DIRECTORY)
96+
exported_json = json_exporter.full_export(active_document)
97+
98+
self.assertJsonObjectsAlmostEqual(exported_json, json_object, [], "JSON in and out equal each other", static_keys=[])
99+
100+
# check time stamps to make sure the file get's overridden
101+
last_edit_time = os.path.getmtime(stl_test_resource_path_cp)
102+
self.assertGreater(last_edit_time, copy_time, "File got edited after copying it")
103+
104+
# check STL file meshes almost equal
105+
# points will differ, but we assume equality if area and volume are within a delta
106+
mesh_before = Mesh.Mesh(stl_test_resource_path)
107+
mesh_after = Mesh.Mesh(stl_test_resource_path_cp)
108+
delta = 0.1
109+
110+
self.assertAlmostEqual(float(mesh_before.Area), float(mesh_after.Area), msg="Areas are almost equal", delta=delta)
111+
self.assertAlmostEqual(float(mesh_before.Volume), float(mesh_after.Volume), msg="Volumes are almost equal", delta=delta)

0 commit comments

Comments
 (0)