Skip to content

Commit

Permalink
Implemented json part sphere - (Task #5)
Browse files Browse the repository at this point in the history
- added properties implementation for sphere objects
- added test case
- fixed issue with reocmputation

---
Task #5: Implement Import Functionality
  • Loading branch information
PhilMFischer committed Aug 14, 2019
1 parent b49d124 commit 51a3a61
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 12 deletions.
1 change: 1 addition & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ encoding//test/test_json_part_cone.py=utf-8
encoding//test/test_json_part_cylinder.py=utf-8
encoding//test/test_json_part_factory.py=utf-8
encoding//test/test_json_part_sheet.py=utf-8
encoding//test/test_json_part_sphere.py=utf-8
encoding//test/test_setup.py=utf-8
encoding/<project>=UTF-8
encoding/Init.py=utf-8
Expand Down
1 change: 1 addition & 0 deletions TestVirtualSatelliteApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from test.test_json_part_box import TestJsonPartBox # NOQA
from test.test_json_part_cone import TestJsonPartCone # NOQA
from test.test_json_part_cylinder import TestJsonPartCylinder # NOQA
from test.test_json_part_sphere import TestJsonPartSphere # NOQA
from test.test_json_part_factory import TestJsonPartFactory # NOQA
from test.test_actice_document import TestActiveDocument # NOQA
from test.test_json_part_sheet import TestJsonPartSheet # NOQA
7 changes: 0 additions & 7 deletions freecad/active_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ def set_active_documents(self, file_name_without_extension):
self.app = App
self.gui = Gui

# set the unit schema to mm/kg/s which is index 0
# apparently this setting just seems to affect the UI
# FreeCAD seems to be on standard units internally
self.app.Units.setSchema(0)
self.app_active_document.recompute()
self.gui_active_document.ActiveView.setAxisCross(True)

def save_as(self, file_name_without_extension):
file_full_path = self.get_file_full_path(file_name_without_extension)
App.getDocument(file_name_without_extension).saveAs(file_full_path)
Expand Down
2 changes: 2 additions & 0 deletions json_io/parts/json_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def write_to_freecad(self, active_document):
self._create_freecad_object(active_document)
self._set_freecad_name_and_color(active_document)
self._set_freecad_properties(active_document)
object_name_and_type = self.get_shape_type()
active_document.app_active_document.getObject(object_name_and_type).recompute()

def get_shape_type(self):
shape_type = self.shape.lower().capitalize()
Expand Down
25 changes: 22 additions & 3 deletions json_io/parts/json_part_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,32 @@


from json_io.parts.json_part import AJsonPart
from freecad.active_document import VECTOR_X


class JsonPartBox(AJsonPart):

def _set_freecad_properties(self, active_document):
object_name_and_type = self.get_shape_type()
box = active_document.app_active_document.getObject(object_name_and_type)

active_document.app_active_document.getObject(object_name_and_type).Length = self.length_x
active_document.app_active_document.getObject(object_name_and_type).Height = self.length_y
active_document.app_active_document.getObject(object_name_and_type).Width = self.length_z
box.Length = self.length_x
box.Height = self.length_y
box.Width = self.length_z

# now center the box as in virtual satellite

# Now virtual satellite axis correction
# 1. the cone is aligned on the y axis
# 2. the origin is in the center of it
# hence:
# 1. turn it by 90° on the x axis
# 2. move it forward by half its size on the y axis
vector_translation = active_document.app.Vector(-self.length_x/2, -self.length_z/2, -self.length_y/2)
vector_rotation = active_document.app.Rotation(VECTOR_X, 0)

placement = active_document.app.Placement(
vector_translation,
vector_rotation)

box.Placement = placement
7 changes: 6 additions & 1 deletion json_io/parts/json_part_sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@


class JsonPartSphere(AJsonPart):
pass

def _set_freecad_properties(self, active_document):
object_name_and_type = self.get_shape_type()
sphere = active_document.app_active_document.getObject(object_name_and_type)

sphere.Radius = self.radius
2 changes: 1 addition & 1 deletion test/test_json_part_cylinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def setUpClass(cls):
def tearDown(self):
super().tearDown()

def test_create_part_box(self):
def test_create_part_cylinder(self):
json_data = """{
"color": 12632256,
"shape": "CYLINDER",
Expand Down
77 changes: 77 additions & 0 deletions test/test_json_part_sphere.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- 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 json
from test.test_setup import AWorkingDirectoryTest
from freecad.active_document import ActiveDocument
import FreeCAD
import FreeCADGui
from json_io.parts.json_part_sphere import JsonPartSphere

App = FreeCAD
Gui = FreeCADGui


class TestJsonPartSphere(AWorkingDirectoryTest):

@classmethod
def setUpClass(cls):
cls.setUpDirectory("PartSphere/")
cls._WORKING_DIRECTORY = cls.getDirectoryFullPath()

def tearDown(self):
super().tearDown()

def test_create_part_sphere(self):
json_data = """{
"color": 12632256,
"shape": "SPHERE",
"name": "Tube",
"lengthX": 0.0,
"lengthY": 0.0,
"lengthZ": 0.0,
"radius": 0.003,
"uuid": "6201a731-d703-43f8-ab37-6a0581dfe022"
}"""

active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document("PartSphere")
json_object = json.loads(json_data)

json_part = JsonPartSphere()
json_part.parse_from_json(json_object)
json_part.write_to_freecad(active_document)

active_document.save_as("PartSphere")

self.assertIsNotNone(App.ActiveDocument.getObject("Sphere"), "The Sphere object got created")

# Check that there is a box with the correct properties
self.assertEquals(App.ActiveDocument.getObject("Sphere").Radius, 3, "Shape has correct size")

self.assertEquals(Gui.ActiveDocument.getObject("Sphere").ShapeColor,
(0.7529411911964417, 0.7529411911964417, 0.7529411911964417, 0.0),
"Shape has correct color")

0 comments on commit 51a3a61

Please sign in to comment.