Skip to content

Commit

Permalink
Update geometry export - (Task #20)
Browse files Browse the repository at this point in the history
Add _export_to_stl function
Update test_full_export_shape_geometry test case

---
Task #20: Add testcases with special shapes to Im- and Exporter
  • Loading branch information
JAmmermann-DLR committed Jan 6, 2020
1 parent 8d782d6 commit a050c1f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
23 changes: 20 additions & 3 deletions VirtualSatelliteCAD/json_io/parts/json_part_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from json_io.parts.json_part import AJsonPart
from json_io.json_definitions import JSON_ELEMENT_STL_PATH
import Mesh # NOQA @UnresolvedImport
import Mesh, MeshPart # NOQA @UnresolvedImport
import Part # NOQA @UnusedImport
import os

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

# TODO: here?
self._export_to_stl(freecad_object)

# this part has no FreeCAD properties
def _set_freecad_properties(self, active_document):
pass

def _get_freecad_properties(self, geometry):
pass

def _get_geometry_name(self):
geometry_full_path = self.stl_path
geometry_base_name = os.path.basename(geometry_full_path)
Expand Down Expand Up @@ -94,5 +101,15 @@ def _create_freecad_object(self, active_document):
active_document.gui_active_document.getObject(object_geometry_name + "_form").Visibility = False
active_document.gui_active_document.getObject(object_geometry_name + "_cleaned").Visibility = False

def _get_freecad_properties(self, geometry):
pass
def _export_to_stl(self, freecad_object):

# object_name_and_type = self.get_shape_type()
# document_object = active_document.app_active_document.getObject(object_name_and_type)

if freecad_object is not None:
shape = freecad_object.Shape

meshed_object = freecad_object.Document.addObject("Mesh::Feature", "Mesh")
meshed_object.Mesh = MeshPart.meshFromShape(Shape=shape, MaxLength=520)

meshed_object.Mesh.write(self.stl_path)
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from module.environment import Environment
from json_io.json_definitions import JSON_ELEMENT_STL_PATH
from test.json_io.test_json_data import TEST_JSON_PART_GEOMETRY
from shutil import copyfile
import os

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

json_part = JsonPartGeometry()
json_part.parse_from_json(json_object)
Expand Down
39 changes: 36 additions & 3 deletions VirtualSatelliteCAD/test/json_io/test_json_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@
from test.test_setup import AWorkingDirectoryTest
from json_io.json_importer import JsonImporter
from json_io.json_exporter import JsonExporter
from test.json_io.test_json_data import TEST_JSON_FULL_VISCUBE
from test.json_io.test_json_data import TEST_JSON_FULL_VISCUBE, TEST_JSON_FULL_GEOMETRY
from json_io.json_definitions import JSON_PRODUCTS, JSON_ELEMNT_CHILDREN, JSON_ELEMENT_NAME, \
JSON_ELEMENT_ROT_X, JSON_ELEMENT_ROT_Y, JSON_ELEMENT_ROT_Z
JSON_ELEMENT_ROT_X, JSON_ELEMENT_ROT_Y, JSON_ELEMENT_ROT_Z, JSON_ELEMENT_STL_PATH, JSON_PARTS
import json
from module.environment import Environment
import os
from shutil import copyfile
import Mesh


class TestJsonExporter(AWorkingDirectoryTest):
Expand Down Expand Up @@ -75,4 +79,33 @@ def test_full_export_shape_none(self):
pass

def test_full_export_shape_geometry(self):
pass
json_importer = JsonImporter(self._WORKING_DIRECTORY)
json_object = json.loads(TEST_JSON_FULL_GEOMETRY)

# get the current module path and get the directory for the test resource
# place that path into the json object before executing the transformations
stl_test_resource_path = Environment.get_test_resource_path("Switch.stl")
stl_test_resource_path_cp = os.path.join(self._WORKING_DIRECTORY, "Switch_cp.stl")
copyfile(stl_test_resource_path, stl_test_resource_path_cp)
copy_time = os.path.getmtime(stl_test_resource_path_cp)
json_object[JSON_PARTS][0][JSON_ELEMENT_STL_PATH] = stl_test_resource_path_cp

_, _, active_document = json_importer.full_import(json_object)

json_exporter = JsonExporter(self._WORKING_DIRECTORY)
exported_json = json_exporter.full_export(active_document)

self.assertJsonObjectsAlmostEqual(exported_json, json_object, [], "JSON in and out equal each other", static_keys=[])

# check time stamps to make sure the file get's overridden
last_edit_time = os.path.getmtime(stl_test_resource_path_cp)
self.assertGreater(last_edit_time, copy_time, "File got edited after copying it")

# check STL file meshes almost equal
# points will differ, but we assume equality if area and volume are within a delta
mesh_before = Mesh.Mesh(stl_test_resource_path)
mesh_after = Mesh.Mesh(stl_test_resource_path_cp)
delta = 0.1

self.assertAlmostEqual(float(mesh_before.Area), float(mesh_after.Area), msg="Areas are almost equal", delta=delta)
self.assertAlmostEqual(float(mesh_before.Volume), float(mesh_after.Volume), msg="Volumes are almost equal", delta=delta)

0 comments on commit a050c1f

Please sign in to comment.