Skip to content

Commit

Permalink
Refactor command_import and resolve TODOs - (Task #5)
Browse files Browse the repository at this point in the history
---
Task #5: Implement Import Functionality
  • Loading branch information
JAmmermann-DLR committed Dec 3, 2019
1 parent 043b84c commit 2709748
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 92 deletions.
17 changes: 14 additions & 3 deletions VirtualSatelliteCAD/commands/command_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
from PySide2.QtWidgets import QFileDialog
from json_io.json_importer import JsonImporter
import os
import json

Log = FreeCAD.Console.PrintMessage


class CommandImport:
def Activated(self):
FreeCAD.Console.PrintMessage("Calling the importer\n")
Log("Calling the importer\n")

# call pyqt dialog: returns (filename, filter)
filename = QFileDialog.getOpenFileName(
Expand All @@ -45,10 +48,18 @@ def Activated(self):
"JSON(*.json)")[0] # filter

if filename != '':
FreeCAD.Console.PrintMessage(f"Selected file '{filename}'\n")
(f"Selected file '{filename}'\n")

with open(filename, 'r') as f:
try:
json_object = json.load(f)
except ValueError as error:
Log(f"ERROR: Invalid JSON found: '{error}'\n")
Log("Please provide a valid JSON\n")
return

json_importer = JsonImporter(Environment.get_appdata_module_path() + os.sep)
json_importer.full_import(filename)
json_importer.full_import(json_object)

def IsActive(self):
return True
Expand Down
15 changes: 2 additions & 13 deletions VirtualSatelliteCAD/json_io/json_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from json_io.products.json_product_assembly_tree_traverser import JsonProductAssemblyTreeTraverser
from json_io.json_definitions import get_part_name_uuid, JSON_PRODUCTS, JSON_PARTS, PART_IDENTIFIER

import json
from freecad import active_document

App = FreeCAD
Expand Down Expand Up @@ -74,21 +73,11 @@ def create_or_update_part(self, json_object):

return part_file_name

# TODO: give json
def full_import(self, filepath):
def full_import(self, json_object):
'''
Import a whole json file's products and parts into a FreeCAD document
'''
Log(f"Importing JSON file '{filepath}'\n")

# TODO: refactor in command
with open(filepath, 'r') as f:
try:
json_object = json.load(f)
except ValueError as error:
Log(f"ERROR: Invalid JSON found: '{error}'\n")
Log("Please provide a valid JSON\n")
return
Log(f"Calling the importer'\n")

json_parts = json_object[JSON_PARTS]

Expand Down
2 changes: 1 addition & 1 deletion VirtualSatelliteCAD/json_io/products/json_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _create_or_update_freecad_part(self, active_document):
import_part_ref = active_document.app_active_document.getObjectsByLabel(import_part_name_in_product)

# print(f"Called with '{import_part_name_in_product}'")
# TODO:
# TODO: CRUD
# If the part doesn't exists (the returned list is not empty) update (delete and recreate) it
if import_part_ref:
active_document.app_active_document.removeObject(import_part_ref[0].Name)
Expand Down
46 changes: 0 additions & 46 deletions VirtualSatelliteCAD/json_io/products/json_product_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,3 @@ def get_part_unique_name(self):
else:
return PART_IDENTIFIER + _get_combined_name_uuid(self.part_name, self.part_uuid)

# def parse_from_json(self, json_object):
#
# super().parse_from_json(json_object)
#
# if self.has_children:
# # Get all children from the json and try to parse them
# # into JsonProductChild objects
# json_object_children = list(json_object[JSON_ELEMNT_CHILDREN])
#
# self.children = []
# for json_object_child in json_object_children:
#
# # TODO: child to check it has children? -> differ
#
# json_product_child = JsonProductChild().parse_from_json(json_object_child)
# # json_product_child.propagate_pos_and_rot_from_parent(self)
# self.children.append(json_product_child)
#
# return self
#
# def write_to_freecad(self, active_document):
# # This assembly may refer to a part as well
# # hence if there is a partUuid and if there is a part name, than
# # it should be written to the FreeCAD document as well.
# if self.is_part_reference():
# super().write_to_freecad(active_document)
#
# # And now write the children, they decide on their own if they reference
# # part or a product
# if self.has_children:
# for child in self.children:
# child.write_to_freecad(active_document)

# def propagate_pos_and_rot_from_parent(self, parent):
# """
# This function propagates position and rotation parameters from the parent:
# Doing this the pos and rot of an object become absolute not relative, which could result
# in overhead when parsing back.
# """
# self.pos_x += parent.pos_x
# self.pos_y += parent.pos_y
# self.pos_z += parent.pos_z
#
# self.rot_x += parent.rot_x
# self.rot_y += parent.rot_y
# self.rot_z += parent.rot_z
1 change: 1 addition & 0 deletions VirtualSatelliteCAD/module/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def get_test_resource_path(cls, test_resource_name):
path = os.path.join(cls.get_tests_resource_path(), test_resource_name)
return path

# TODO: Update user file handling
@classmethod
def get_appdata_module_path(cls):
'''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def test_parse_with_children(self):
self.assertEqual(json_product.part_uuid, "3d3708fd_5c6c_4af9_b710_d68778466084", "Property is correctly set")

# Properties have to be 0 since an assembly itself has no position and orientation
# TODO: check
self.assertEqual(json_product.pos_x, 0, "Property is correctly set")
self.assertEqual(json_product.pos_y, 0, "Property is correctly set")
self.assertEqual(json_product.pos_z, 0, "Property is correctly set")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
#
from test.test_setup import AWorkingDirectoryTest
from test.json_io.test_json_data import TEST_JSON_PRODUCT_WITH_CHILDREN_WITH_CHILD, TEST_JSON_PRODUCT_WITHOUT_CHILDREN, \
TEST_JSON_FULL_VISCUBE, TEST_JSON_PRODUCT_WITH_CHILDREN_WITH_CHILD_SUBASSEMBLY_IS_NO_PART, \
TEST_JSON_PRODUCT_SUBASSEMBLY_WITH_SAME_PART
TEST_JSON_PRODUCT_WITH_CHILDREN_WITH_CHILD_SUBASSEMBLY_IS_NO_PART, TEST_JSON_PRODUCT_SUBASSEMBLY_WITH_SAME_PART
import json
from json_io.products.json_product_assembly_tree_traverser import JsonProductAssemblyTreeTraverser
from json_io.json_definitions import JSON_ELEMENT_NAME, PRODUCT_IDENTIFIER
from freecad.active_document import ActiveDocument
import unittest
import glob
import os

Expand Down Expand Up @@ -139,29 +137,6 @@ def test_traverse_and_parse_json_tree_subassembly_same_part(self):
PRODUCT_IDENTIFIER + "BasePlateBottom1_e8794f3d_86ec_44c5_9618_8b7170c45484")
self.assertEquals(len(active_document.app_active_document.RootObjects), 4, "Found correct amount of root objects 2 objects plus 2 sheets")

# TODO remove/adapt
@unittest.SkipTest
def test_traverse_and_parse_json_tree3(self):
json_data = TEST_JSON_FULL_VISCUBE
self.create_Test_Part()

json_object = json.loads(json_data)

traverser = JsonProductAssemblyTreeTraverser(self._WORKING_DIRECTORY)
traverser.traverse(json_object)
lst_of_depths = traverser._lst_of_depths

self.assertEqual(len(lst_of_depths), 2, "Found the right amount of 2 assemblies")

traverser.parse_from_json()

# this should have similar results to test_create_part_product_assembly_and_subassembly_with_root_part_manual in TestJsonProductAssembly
active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document("BeamStructure_2afb23c9_f458_4bdb_a4e7_fc863364644f")
self.assertEquals(len(active_document.app_active_document.RootObjects), 6, "Found correct amount of root objects 3 objects plus 3 sheets")

active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document("SpaceCube_a3533e02_125c_4066_bffe_d046d8d8342a")
self.assertEquals(len(active_document.app_active_document.RootObjects), 10, "Found correct amount of root objects 5 objects plus 5 sheets")

def test_traverse_and_parse_json_tree_rootassembly_without_children(self):
json_data = TEST_JSON_PRODUCT_WITHOUT_CHILDREN
self.create_Test_Part()
Expand Down
5 changes: 3 additions & 2 deletions VirtualSatelliteCAD/test/json_io/test_json_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from module.environment import Environment
from json_io.json_definitions import JSON_ELEMENT_STL_PATH, PART_IDENTIFIER, PRODUCT_IDENTIFIER
import unittest
from test.json_io.test_json_data import TEST_JSON_FULL_VISCUBE

App = FreeCAD
Gui = FreeCADGui
Expand Down Expand Up @@ -285,9 +286,9 @@ def test_full_import(self):
Full JSON import test
"""

json_test_resource_path = Environment.get_test_resource_path("VisCube2.json")
json_importer = JsonImporter(self._WORKING_DIRECTORY)
part_file_names, json_product, active_document = json_importer.full_import(json_test_resource_path)
json_object = json.loads(TEST_JSON_FULL_VISCUBE)
part_file_names, json_product, active_document = json_importer.full_import(json_object)

# =========================
# Check parts
Expand Down

0 comments on commit 2709748

Please sign in to comment.