Skip to content

Commit

Permalink
Implemented first version of box part object - (Task #5)
Browse files Browse the repository at this point in the history
also refactored some of the code such as Active Document to be more
handy.

---
Task #5: Implement Import Functionality
  • Loading branch information
PhilMFischer committed Aug 14, 2019
1 parent a4cf426 commit c6df50e
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ encoding//commands/command_definitions.py=utf-8
encoding//commands/command_export.py=utf-8
encoding//commands/command_import.py=utf-8
encoding//freecad/__init__.py=utf-8
encoding//freecad/active_document_helper.py=utf-8
encoding//freecad/active_document.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
Expand All @@ -21,7 +21,7 @@ encoding//json_io/parts/json_part_sphere.py=utf-8
encoding//module/__init__.py=utf-8
encoding//module/environment.py=utf-8
encoding//test/__init__.py=utf-8
encoding//test/test_actice_document_helper.py=utf-8
encoding//test/test_actice_document.py=utf-8
encoding//test/test_json_importer.py=utf-8
encoding//test/test_json_part.py=utf-8
encoding//test/test_json_part_factory.py=utf-8
Expand Down
2 changes: 1 addition & 1 deletion TestVirtualSatelliteApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
#from test.test_json_importer import TestJsonImporter # NOQA
from test.test_json_part import TestJsonPart # NOQA
from test.test_json_part_factory import TestJsonPartFactory # NOQA
from test.test_actice_document_helper import TestJsonPartSheet # NOQA
from test.test_actice_document import TestActiveDocument # NOQA
from test.test_json_part_sheet import TestJsonPartSheet # NOQA
14 changes: 12 additions & 2 deletions freecad/active_document_helper.py → freecad/active_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#
import FreeCADGui
import FreeCAD
import os

App = FreeCAD
Gui = FreeCADGui
Log = FreeCAD.Console.PrintLog
Msg = FreeCAD.Console.PrintMessage
Err = FreeCAD.Console.PrintError
Expand All @@ -35,7 +37,7 @@
FREECAD_FILE_EXTENSION = ".FCstd"


class ActiveDocumentHelper(object):
class ActiveDocument(object):

def __init__(self, working_directory):
self._working_directory = working_directory
Expand All @@ -59,10 +61,18 @@ def open_set_and_get_document(self, file_name_without_extension):
else:
Log('Open existing already open FreeCAD file for update...\n')

self.set_active_documents(file_name_without_extension)

return self

def set_active_documents(self, file_name_without_extension):
App.setActiveDocument(file_name_without_extension)

App.ActiveDocument = App.getDocument(file_name_without_extension)
Gui.ActiveDocument = Gui.getDocument(file_name_without_extension)

return App.ActiveDocument
self.app_active_document = App.ActiveDocument
self.gui_active_document = Gui.ActiveDocument

def save_as(self, file_name_without_extension):
file_full_path = self.get_file_full_path(file_name_without_extension)
Expand Down
32 changes: 1 addition & 31 deletions json_io/json_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,37 +83,7 @@ def create_or_update_part(self, json_object):
App.closeDocument(part_file_name)
Log('Saved part to file: ' + part_file_fullpath + "\n")

def create_or_update_sheet(self, json_part):
sheet = App.ActiveDocument.getObject("VirtualSatellite")
if sheet is None:
sheet = App.ActiveDocument.addObject("Spreadsheet::Sheet", "VirtualSatellite")

sheet.set("A1", "Virtual Satellite Part Data")
sheet.set("A2", "Name")
sheet.set("B2", "Value")
sheet.set("C2", "Unit")
sheet.setStyle("A1:C2", "bold")

sheet_line = 3
for json_part_attribute_name in list(json_part.attributes.keys()):

json_part_attribute_value = str(getattr(json_part, json_part_attribute_name))
json_part_attribute_unit = json_part.attributes[json_part_attribute_name]

sheet.set("A" + str(sheet_line), json_part_attribute_name)
sheet.set("B" + str(sheet_line), json_part_attribute_value)
sheet.set("C" + str(sheet_line), json_part_attribute_unit)

sheet_line += 1

def read_from_sheet(self, attribute_name):
sheet = App.ActiveDocument.getObject("VirtualSatellite")
column_a_cells = list(filter(lambda x: x.startswith('A'), sheet.PropertiesList))

for a_cell in column_a_cells:
if a_cell == attribute_name:
b_cell = a_cell.replace("A", "B")
return sheet.get(b_cell)


def create_or_update_box(self, json_part):
if App.ActiveDocument.getObject('Box') is None:
Expand Down
25 changes: 22 additions & 3 deletions json_io/parts/json_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
from json_io.json_definitions import JSON_ELEMENT_NAME, JSON_ELEMENT_SHAPE,\
JSON_ELEMENT_UUID, JSON_ELEMENT_LENGTH_X, JSON_ELEMENT_LENGTH_Y,\
JSON_ELEMENT_LENGTH_Z, JSON_ELEMENT_RADIUS, JSON_ELEMENT_COLOR
from abc import ABC


class AJsonPart(ABC):
class AJsonPart():
'''
This class translates a json object into a more specific
one which represents all relevant information of a part. On
Expand Down Expand Up @@ -67,9 +66,29 @@ def parse_from_json(self, json_object):

return self

def write_to_freecad(self, active_document):
def _create_freecad_object(self, active_document):
object_name_and_type = self.get_shape_type()
document_object = active_document.app_active_document.getObject(object_name_and_type)

if document_object is None:
object_type = "Part::" + object_name_and_type
object_name = object_name_and_type
active_document.app_active_document.addObject(object_type, object_name)

def _set_freecad_name_and_color(self, active_document):
object_name_and_type = self.get_shape_type()

active_document.app_active_document.getObject(object_name_and_type).Label = self.name
active_document.gui_active_document.getObject(object_name_and_type).ShapeColor = self.color

def _set_freecad_properties(self, active_document):
pass

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)

def get_shape_type(self):
shape_type = self.shape.lower().capitalize()
return shape_type
8 changes: 7 additions & 1 deletion json_io/parts/json_part_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@


class JsonPartBox(AJsonPart):
pass

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

active_document.app_active_document.getObject(object_name_and_type).Length = self.length_x + ' m'
active_document.app_active_document.getObject(object_name_and_type).Height = self.length_y + ' m'
active_document.app_active_document.getObject(object_name_and_type).Width = self.length_z + ' m'
8 changes: 4 additions & 4 deletions json_io/parts/json_part_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def write_to_freecad(self, active_document):
virtual satellite.
'''

sheet = active_document.getObject(FREECAD_PART_SHEET_NAME)
sheet = active_document.app_active_document.getObject(FREECAD_PART_SHEET_NAME)
if sheet is None:
sheet = active_document.addObject("Spreadsheet::Sheet", FREECAD_PART_SHEET_NAME)
sheet = active_document.app_active_document.addObject("Spreadsheet::Sheet", FREECAD_PART_SHEET_NAME)

sheet.set("A1", "Virtual Satellite Part Data")
sheet.set("A2", "Name")
Expand All @@ -68,15 +68,15 @@ def write_to_freecad(self, active_document):

# Recompute the sheet, so that all properties are correctly written
# if not recomputed accessing the properties will result in none objects
active_document.recompute()
active_document.app_active_document.recompute()

def read_sheet_attribute(self, active_document, attribute_name):
'''
This method can be used to read from the part sheet from a
given document. The method allows to individually access the
written properties.
'''
sheet = active_document.getObject(FREECAD_PART_SHEET_NAME)
sheet = active_document.app_active_document.getObject(FREECAD_PART_SHEET_NAME)
attribute_index = list(self.attributes).index(attribute_name)

if (attribute_index >= 0 and attribute_index < len(self.attributes)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@

import FreeCAD
import FreeCADGui
from freecad.active_document_helper import ActiveDocumentHelper
from freecad.active_document import ActiveDocument
import os
from test.test_setup import AWorkingDirectoryTest

App = FreeCAD
Gui = FreeCADGui


class TestJsonPartSheet(AWorkingDirectoryTest):
class TestActiveDocument(AWorkingDirectoryTest):

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

def tearDown(self):
Expand All @@ -48,35 +48,37 @@ def tearDown(self):
def test_get_file_full_path(self):
TEST_DOCUMENT_NAME = "box_263_456_789_111"

file_full_path = ActiveDocumentHelper(self._WORKING_DIRECTORY).get_file_full_path(TEST_DOCUMENT_NAME)
self.assertEqual(file_full_path, "/tmp/FreeCADtest/ActiveDocumentHelper/box_263_456_789_111.FCstd", "Got correct full path")
file_full_path = ActiveDocument(self._WORKING_DIRECTORY).get_file_full_path(TEST_DOCUMENT_NAME)
self.assertEqual(file_full_path, "/tmp/FreeCADtest/ActiveDocument/box_263_456_789_111.FCstd", "Got correct full path")

def test_create_document(self):
TEST_DOCUMENT_NAME = "box_263_456_789_222"

loaded_documents = list(App.listDocuments().keys())
self.assertNotIn(TEST_DOCUMENT_NAME, loaded_documents, "New Document is not yet loaded")

active_document = ActiveDocumentHelper(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)
active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)

loaded_documents = list(App.listDocuments().keys())
self.assertIn(TEST_DOCUMENT_NAME, loaded_documents, "Document is now loaded")

self.assertIsNotNone(active_document, "Created a document")
self.assertEquals(App.ActiveDocument, App.getDocument(TEST_DOCUMENT_NAME), "The Active Document got correctly set")
self.assertEquals(Gui.ActiveDocument, Gui.getDocument(TEST_DOCUMENT_NAME), "The Active Document got correctly set")

def test_save_and_close_active_document(self):
TEST_DOCUMENT_NAME = "box_263_456_789_333"

loaded_documents = list(App.listDocuments().keys())
self.assertNotIn(TEST_DOCUMENT_NAME, loaded_documents, "New Document is not yet loaded")

ActiveDocumentHelper(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)
ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)

file_full_path = ActiveDocumentHelper(self._WORKING_DIRECTORY).get_file_full_path(TEST_DOCUMENT_NAME)
file_full_path = ActiveDocument(self._WORKING_DIRECTORY).get_file_full_path(TEST_DOCUMENT_NAME)

self.assertFalse(os.path.isfile(file_full_path), "File not yet saved")

ActiveDocumentHelper(self._WORKING_DIRECTORY).save_and_close_active_document(TEST_DOCUMENT_NAME)
ActiveDocument(self._WORKING_DIRECTORY).save_and_close_active_document(TEST_DOCUMENT_NAME)

self.assertTrue(os.path.isfile(file_full_path), "File got saved")

Expand All @@ -90,7 +92,7 @@ def test_reopen_document(self):
self.assertNotIn(TEST_DOCUMENT_NAME, loaded_documents, "New Document is not yet loaded")
self.assertEquals(0, len(loaded_documents), "List of loaded documents is still empty")

ActiveDocumentHelper(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)
ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)

loaded_documents = list(App.listDocuments().keys())
self.assertIn(TEST_DOCUMENT_NAME, loaded_documents, "Document is now loaded")
Expand All @@ -99,12 +101,12 @@ def test_reopen_document(self):
# Now save and load the document
App.ActiveDocument.addObject("Part::Box", "BoxReopen")

ActiveDocumentHelper(self._WORKING_DIRECTORY).save_and_close_active_document(TEST_DOCUMENT_NAME)
ActiveDocument(self._WORKING_DIRECTORY).save_and_close_active_document(TEST_DOCUMENT_NAME)

loaded_documents = list(App.listDocuments().keys())
self.assertNotIn(TEST_DOCUMENT_NAME, loaded_documents, "Document got closed")

ActiveDocumentHelper(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)
ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)
freecad_object = App.ActiveDocument.getObject("BoxReopen")

self.assertIsNotNone(freecad_object, "Was able to read file and get specific object for this test")
Expand All @@ -116,7 +118,7 @@ def test_reopen_document(self):

loaded_documents = list(App.listDocuments().keys())
self.assertEquals(1, len(loaded_documents), "Document is still loaded, just open it.")
ActiveDocumentHelper(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)
ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document(TEST_DOCUMENT_NAME)
loaded_documents = list(App.listDocuments().keys())
self.assertEquals(1, len(loaded_documents), "Document is still the same. No other document got loaded.")

Expand Down
18 changes: 9 additions & 9 deletions test/test_json_part_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import FreeCAD
import FreeCADGui
from freecad.active_document_helper import ActiveDocumentHelper
from freecad.active_document import ActiveDocument
from test.test_setup import AWorkingDirectoryTest
from json_io.parts.json_part_sheet import JsonPartSheet, FREECAD_PART_SHEET_NAME
import json
Expand Down Expand Up @@ -61,25 +61,25 @@ def tearDown(self):
super().tearDown()

def test_write_to_freecad(self):
self._active_document = ActiveDocumentHelper(self._WORKING_DIRECTORY).open_set_and_get_document("PartSheetTest_Write")
active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document("PartSheetTest_Write")
json_part_sheet = JsonPartSheet().parse_from_json(self._json_test_object)

json_part_sheet_object = self._active_document.getObject(FREECAD_PART_SHEET_NAME)
json_part_sheet_object = active_document.app_active_document.getObject(FREECAD_PART_SHEET_NAME)
self.assertIsNone(json_part_sheet_object, "The object does not yet exist")

json_part_sheet.write_to_freecad(self._active_document)
json_part_sheet.write_to_freecad(active_document)

json_part_sheet_object = self._active_document.getObject(FREECAD_PART_SHEET_NAME)
json_part_sheet_object = active_document.app_active_document.getObject(FREECAD_PART_SHEET_NAME)
self.assertIsNotNone(json_part_sheet_object, "The object does exist now")
self.assertEquals(len(self._active_document.RootObjects), 1, "Correct amount of objects in document")
self.assertEquals(len(active_document.app_active_document.RootObjects), 1, "Correct amount of objects in document")
self.assertEquals(len(json_part_sheet_object.PropertiesList), 36, "Computed correct amount of properties in the sheet")

def test_read_sheet_attribute(self):
self._active_document = ActiveDocumentHelper(self._WORKING_DIRECTORY).open_set_and_get_document("PartSheetTest_Read")
active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document("PartSheetTest_Read")
json_part_sheet = JsonPartSheet().parse_from_json(self._json_test_object)

json_part_sheet.write_to_freecad(self._active_document)
json_part_sheet.write_to_freecad(active_document)

attribute = json_part_sheet.read_sheet_attribute(self._active_document, "length_z")
attribute = json_part_sheet.read_sheet_attribute(active_document, "length_z")

self.assertEquals(attribute, 0.01, "Got correct value")
3 changes: 1 addition & 2 deletions test/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import FreeCAD
import FreeCADGui
from abc import ABC

App = FreeCAD
Gui = FreeCADGui
Expand All @@ -40,7 +39,7 @@
TEST_WORKING_BASE_DIRECTORY = "/tmp/FreeCADtest/"


class AWorkingDirectoryTest(unittest.TestCase, ABC):
class AWorkingDirectoryTest(unittest.TestCase):

@classmethod
def setUpDirectory(cls, working_directory):
Expand Down

0 comments on commit c6df50e

Please sign in to comment.