Skip to content

Commit

Permalink
Implemented first generation of a box part - (Task #5)
Browse files Browse the repository at this point in the history
- Added code to import a box from a json object
- Added test code framework

---
Task #5: Implement Import Functionality
  • Loading branch information
PhilMFischer committed Aug 8, 2019
1 parent ec36320 commit 059f868
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ encoding//commands/__init__.py=utf-8
encoding//commands/command_definitions.py=utf-8
encoding//commands/command_export.py=utf-8
encoding//commands/command_import.py=utf-8
encoding//json_io/__init__.py=utf-8
encoding//json_io/json_definitions.py=utf-8
encoding//json_io/json_importer.py=utf-8
encoding//module/__init__.py=utf-8
encoding//module/environment.py=utf-8
encoding//test/__init__.py=utf-8
encoding//test/test_json_importer.py=utf-8
encoding/<project>=UTF-8
encoding/Init.py=utf-8
encoding/InitGui.py=utf-8
Expand Down
13 changes: 1 addition & 12 deletions TestVirtualSatelliteApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,4 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
#

import unittest


class Test(unittest.TestCase):

def testName(self):
self.assertTrue(True, "All Things are fine")


if __name__ == "__main__":
# import sys;sys.argv = ['', 'Test.testName']
unittest.main()
from test.test_json_importer import TestJsonImporter #NOQA
2 changes: 1 addition & 1 deletion commands/command_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

class CommandExport:
def Activated(self):
FreeCAD.Console.PrintMessage("Calling the exporter\n")
FreeCAD.Console.PrintMessage("Calling the json_io\n")

def IsActive(self):
return True
Expand Down
25 changes: 25 additions & 0 deletions json_io/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
#
# Virtual Satellite 4 - FreeCAD module
#
# Copyright (C) 2019 by
#
# DLR (German Aerospace Center),
# Software for Space Systems and interactive Visualization
# Braunschweig, Germany
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#
48 changes: 48 additions & 0 deletions json_io/json_definitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
#
# Virtual Satellite 4 - FreeCAD module
#
# Copyright (C) 2019 by
#
# DLR (German Aerospace Center),
# Software for Space Systems and interactive Visualization
# Braunschweig, Germany
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#


JSON_ELEMENT_COLOR = "color"
JSON_ELEMENT_SHAPE = "shape"

JSON_ELEMENT_SHAPE_BOX = "BOX"
JSON_ELEMENT_SHAPE_CONE = "CONE"
JSON_ELEMENT_SHAPE_CYLINDER = "CYLINDER"
JSON_ELEMENT_SHAPE_SPHERE = "SPHERE"
JSON_ELEMENT_SHAPE_GEOMETRY = "GEOMETRY"

JSON_ELEMENT_NAME = "name"

# The axis between Virtual Satellite and FreeCAD are not identical
# Therefore Y and Z gets swpapped here.
JSON_ELEMENT_LENGTH_X = "lengthX"
JSON_ELEMENT_LENGTH_Y = "lengthZ"
JSON_ELEMENT_LENGTH_Z = "lengthY"

JSON_ELEMENT_RADIUS = "radius"
JSON_ELEMENT_UUID = "uuid"

FREECAD_FILE_EXTENSION = ".FCstd"
99 changes: 99 additions & 0 deletions json_io/json_importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-
#
# Virtual Satellite 4 - FreeCAD module
#
# Copyright (C) 2019 by
#
# DLR (German Aerospace Center),
# Software for Space Systems and interactive Visualization
# Braunschweig, Germany
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#
from json_io.json_definitions import JSON_ELEMENT_NAME,\
JSON_ELEMENT_SHAPE,\
JSON_ELEMENT_UUID, FREECAD_FILE_EXTENSION, JSON_ELEMENT_LENGTH_Z,\
JSON_ELEMENT_LENGTH_Y, JSON_ELEMENT_LENGTH_X

import FreeCAD
import string

App = FreeCAD


class JsonImporter(object):
'''
classdocs
'''

working_output_directory: string

def __init__(self, working_ouput_directory):
self.working_output_directory = working_ouput_directory

def create_or_update_part(self, json_object):

# Retrieve name and shape information from json object
part_name = str(json_object[JSON_ELEMENT_NAME])
part_shape = str(json_object[JSON_ELEMENT_SHAPE])
part_uuid = str(json_object[JSON_ELEMENT_UUID]).replace("-", "_")

# Use the name to create the part document
# should be careful in case the name already exists.
# thus it is combined with the uuid. not really nice
# but definitely efficient
part_file_name = str(part_name + "_" + part_uuid)
App.newDocument(part_file_name)
App.setActiveDocument(part_file_name)
App.ActiveDocument = App.getDocument(part_file_name)

part_file_fullpath = self.working_output_directory + part_file_name + FREECAD_FILE_EXTENSION

# Dispatch to creation method depending on shape type
create_or_update_method_name = "create_or_update_" + part_shape.lower()
create_or_update_dispatch = getattr(self, create_or_update_method_name, lambda: "Invalid call to : " + create_or_update_method_name)
create_or_update_dispatch(json_object)

App.getDocument(part_file_name).saveAs(part_file_fullpath)
print("should have saved")

def create_or_update_box(self, json_object):
print("creating a box")
part_name = json_object[JSON_ELEMENT_NAME]

App.ActiveDocument.addObject("Part::Box", "Box")
App.ActiveDocument.ActiveObject.Label = part_name
App.ActiveDocument.recompute()

part_length_x = str(json_object[JSON_ELEMENT_LENGTH_X])
part_length_y = str(json_object[JSON_ELEMENT_LENGTH_Y])
part_length_z = str(json_object[JSON_ELEMENT_LENGTH_Z])

App.ActiveDocument.getObject("Box").Length = part_length_x + ' m'
App.ActiveDocument.getObject("Box").Height = part_length_y + ' m'
App.ActiveDocument.getObject("Box").Width = part_length_z + ' m'

def create_or_update_cone(self, json_object):
pass

def create_or_update_cylinder(self, json_object):
pass

def create_or_update_sphere(self, json_object):
pass

def create_or_update_geometry(self, json_object):
pass
25 changes: 25 additions & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
#
# Virtual Satellite 4 - FreeCAD module
#
# Copyright (C) 2019 by
#
# DLR (German Aerospace Center),
# Software for Space Systems and interactive Visualization
# Braunschweig, Germany
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#
66 changes: 66 additions & 0 deletions test/test_json_importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
#
# Virtual Satellite 4 - FreeCAD module
#
# Copyright (C) 2019 by
#
# DLR (German Aerospace Center),
# Software for Space Systems and interactive Visualization
# Braunschweig, Germany
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#

import unittest

import os
import FreeCAD
from json_io.json_importer import JsonImporter
import json

App = FreeCAD


# define the name of the directory to be created
TEST_WORKING_DIRECTORY = "/tmp/FreeCADtest/"


class TestJsonImporter(unittest.TestCase):

@classmethod
def setUpClass(cls):
try:
if not os.path.exists(TEST_WORKING_DIRECTORY):
os.makedirs(TEST_WORKING_DIRECTORY)
except OSError:
cls.fail(cls, "Cannot create test directory: %s failed" % TEST_WORKING_DIRECTORY)

def test_create_part(self):
json_data = """{
"color": 12632256,
"shape": "BOX",
"name": "Beam",
"lengthX": 0.04,
"lengthY": 0.01,
"lengthZ": 0.3,
"radius": 0.0,
"uuid": "6201a731-d703-43f8-ab37-6a0581dfe022"
}"""

json_object = json.loads(json_data)

json_importer = JsonImporter(TEST_WORKING_DIRECTORY)
json_importer.create_or_update_part(json_object)

0 comments on commit 059f868

Please sign in to comment.