Skip to content

Commit

Permalink
Refactor importer and add fixture - (Task #5)
Browse files Browse the repository at this point in the history
- Add full import function in JsonImporter
- Use that function in CommandImport
- Add test function for full import
- Add test fixture json file

---
Task #5: Implement Import Functionality
  • Loading branch information
JAmmermann-DLR committed Nov 14, 2019
1 parent 4e00299 commit b2ef16c
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 12 deletions.
158 changes: 158 additions & 0 deletions VirtualSatelliteCAD/Resources/Tests/VisCube2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"Products": {
"children": [{
"posX": 0.0,
"posY": 0.0,
"posZ": 1.0,
"rotX": 0.0,
"children": [],
"rotZ": 0.0,
"rotY": 0.0,
"name": "Top",
"uuid": "cc14e2c7-9d7e-4cf2-8d6d-9b8cf5e96d56",
"partUuid": "cc14e2c7-9d7e-4cf2-8d6d-9b8cf5e96d56",
"partName": "Top"
}, {
"posX": 0.0,
"posY": 0.0,
"posZ": 0.0,
"rotX": 0.0,
"children": [],
"rotZ": 0.0,
"rotY": 0.0,
"name": "Bottom",
"uuid": "61db0622-6fef-4f12-932d-a00fdb9d0848",
"partUuid": "00f430a6-6311-4a33-961b-41ded4cf57d5",
"partName": "Plate"
}, {
"posX": 0.5,
"posY": 0.0,
"posZ": 0.5,
"rotX": 0.0,
"children": [],
"rotZ": 0.0,
"rotY": 1.5707963267948966,
"name": "Front",
"uuid": "e6af9d3f-8ad6-4488-b3d0-d35549be9a1e",
"partUuid": "e6af9d3f-8ad6-4488-b3d0-d35549be9a1e",
"partName": "Front"
}, {
"posX": -0.5,
"posY": 0.0,
"posZ": 0.5,
"rotX": 0.0,
"children": [],
"rotZ": 0.0,
"rotY": 1.5707963267948966,
"name": "Back",
"uuid": "a3c9c547-8fd3-40d5-97a1-a3f9a3a9c337",
"partUuid": "a3c9c547-8fd3-40d5-97a1-a3f9a3a9c337",
"partName": "Back"
}, {
"posX": 0.0,
"posY": 0.0,
"posZ": 0.5,
"rotX": 0.0,
"children": [{
"posX": 0.0,
"posY": 0.5,
"posZ": 0.0,
"rotX": 1.5707963267948966,
"children": [],
"rotZ": 0.0,
"rotY": 0.0,
"name": "Left",
"uuid": "615985c0-73fd-48db-8f8b-e11b7cbb2ee8",
"partUuid": "615985c0-73fd-48db-8f8b-e11b7cbb2ee8",
"partName": "Left"
}, {
"posX": 0.0,
"posY": -0.5,
"posZ": 0.0,
"rotX": 1.5707963267948966,
"children": [],
"rotZ": 0.0,
"rotY": 0.0,
"name": "Right",
"uuid": "882a0b35-7da8-4555-903d-fd6b5cbec392",
"partUuid": "882a0b35-7da8-4555-903d-fd6b5cbec392",
"partName": "Right"
}
],
"rotZ": 0.0,
"rotY": 0.0,
"name": "BeamStructure",
"uuid": "2afb23c9-f458-4bdb-a4e7-fc863364644f",
"partUuid": "2afb23c9-f458-4bdb-a4e7-fc863364644f",
"partName": "BeamStructure"
}
],
"name": "SpaceCube",
"uuid": "a3533e02-125c-4066-bffe-d046d8d8342a"
},
"Parts": [{
"color": 16744448,
"shape": "CYLINDER",
"name": "BeamStructure",
"lengthY": 1.0,
"lengthX": 1.0,
"radius": 0.05,
"uuid": "2afb23c9-f458-4bdb-a4e7-fc863364644f",
"lengthZ": 1.0
}, {
"color": 8388608,
"shape": "BOX",
"name": "Right",
"lengthY": 1.0,
"lengthX": 1.0,
"radius": 0.05,
"uuid": "882a0b35-7da8-4555-903d-fd6b5cbec392",
"lengthZ": 0.02
}, {
"color": 32832,
"shape": "BOX",
"name": "Front",
"lengthY": 1.0,
"lengthX": 1.0,
"radius": 0.05,
"uuid": "e6af9d3f-8ad6-4488-b3d0-d35549be9a1e",
"lengthZ": 0.02
}, {
"color": 16711680,
"shape": "BOX",
"name": "Left",
"lengthY": 1.0,
"lengthX": 1.0,
"radius": 0.05,
"uuid": "615985c0-73fd-48db-8f8b-e11b7cbb2ee8",
"lengthZ": 0.02
}, {
"color": 65280,
"shape": "BOX",
"name": "Plate",
"lengthY": 1.0,
"lengthX": 1.0,
"radius": 0.05,
"uuid": "00f430a6-6311-4a33-961b-41ded4cf57d5",
"lengthZ": 0.02
}, {
"color": 16776960,
"shape": "BOX",
"name": "Back",
"lengthY": 1.0,
"lengthX": 1.0,
"radius": 0.05,
"uuid": "a3c9c547-8fd3-40d5-97a1-a3f9a3a9c337",
"lengthZ": 0.02
}, {
"color": 32768,
"shape": "BOX",
"name": "Top",
"lengthY": 1.0,
"lengthX": 1.0,
"radius": 0.05,
"uuid": "cc14e2c7-9d7e-4cf2-8d6d-9b8cf5e96d56",
"lengthZ": 0.02
}
]
}
13 changes: 1 addition & 12 deletions VirtualSatelliteCAD/commands/command_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@
from module.environment import Environment, ICON_IMPORT
from commands.command_definitions import COMMAND_ID_IMPORT_2_FREECAD
from PySide2.QtWidgets import QFileDialog
import json
from json_io.json_importer import JsonImporter

from json_io.json_definitions import get_part_name_uuid
from freecad.active_document import FREECAD_FILE_EXTENSION


class CommandImport:
def Activated(self):
Expand All @@ -52,17 +48,10 @@ def Activated(self):
if filename != '':
FreeCAD.Console.PrintMessage(f"Successful read file '{filename}'\n")

with open(filename, 'r') as f:
json_object = json.load(f)

# TODO: where do we save the created FCstd files? AppData?
# maybe create an subdir in AppData for VirSat stds?
json_importer = JsonImporter(path)
json_importer.create_or_update_part(json_object)

# TODO: return the path (or at least the name) of the std in create_or_update_part?
test_file_name = path + get_part_name_uuid(json_object) + FREECAD_FILE_EXTENSION
FreeCAD.open(test_file_name)
json_importer.fullImport(filename)

def IsActive(self):
return True
Expand Down
25 changes: 25 additions & 0 deletions VirtualSatelliteCAD/json_io/json_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from json_io.parts.json_part_factory import JsonPartFactory
from json_io.json_definitions import get_part_name_uuid

import json
from freecad.active_document import FREECAD_FILE_EXTENSION

App = FreeCAD
Gui = FreeCADGui
Log = FreeCAD.Console.PrintLog
Expand All @@ -50,6 +53,9 @@ def create_or_update_part(self, json_object):
Log("Creating or Updating a part...\n")
json_part = JsonPartFactory().create_from_json(json_object)

part_file_name = ""

# for: create each part
if json_part is not None:
# Use the name to create the part document
# should be careful in case the name already exists.
Expand All @@ -65,3 +71,22 @@ def create_or_update_part(self, json_object):
Log("Saved part to file: " + part_file_name + "\n")
else:
Log("Visualization shape is most likely NONE, therefore no file is created\n")

# json assembly with json product object
# goal: instead of a part open the product assembly

return part_file_name

def full_import(self, filepath):
'''
'''

with open(filepath, 'r') as f:
json_object = json.load(f)

json_parts = json_object['Parts']
part_file_name = self.create_or_update_part(json_parts[0])

# TODO: return the path (or at least the name) of the std in create_or_update_part?
test_file_name = self.working_output_directory + part_file_name + FREECAD_FILE_EXTENSION
FreeCAD.open(test_file_name)
10 changes: 10 additions & 0 deletions VirtualSatelliteCAD/test/json_io/test_json_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,13 @@ def test_create_part_change_shape(self):
self.assertIsNone(App.ActiveDocument.getObject("Geometry"), "Removed previous object")
self.assertIsNotNone(App.ActiveDocument.getObject("Cone"), "Got correct object")
App.closeDocument("Beam_6201a731_d703_43f8_ab37_6a7171dfe022")

def test_full_import(self):

json_test_resource_path = Environment.get_test_resource_path("VisCube2.json")
json_importer = JsonImporter(self._WORKING_DIRECTORY)
json_importer.full_import(json_test_resource_path)

# Check the file got created
# test_file_name = self._WORKING_DIRECTORY + "Beam_6201a731_d703_43f8_ab37_6a0581dfe022" + FREECAD_FILE_EXTENSION
# self.assertTrue(os.path.isfile(test_file_name), "File exists on drive")

0 comments on commit b2ef16c

Please sign in to comment.