diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 538082d..f9c68bb 100755 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -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 @@ -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 diff --git a/TestVirtualSatelliteApp.py b/TestVirtualSatelliteApp.py index 9335581..6bff9f1 100755 --- a/TestVirtualSatelliteApp.py +++ b/TestVirtualSatelliteApp.py @@ -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 \ No newline at end of file diff --git a/freecad/active_document_helper.py b/freecad/active_document.py similarity index 84% rename from freecad/active_document_helper.py rename to freecad/active_document.py index ad3461a..11554b7 100644 --- a/freecad/active_document_helper.py +++ b/freecad/active_document.py @@ -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 @@ -35,7 +37,7 @@ FREECAD_FILE_EXTENSION = ".FCstd" -class ActiveDocumentHelper(object): +class ActiveDocument(object): def __init__(self, working_directory): self._working_directory = working_directory @@ -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) diff --git a/json_io/json_importer.py b/json_io/json_importer.py index b027d2a..5ee9554 100644 --- a/json_io/json_importer.py +++ b/json_io/json_importer.py @@ -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: diff --git a/json_io/parts/json_part.py b/json_io/parts/json_part.py index 18a2882..f0d4884 100644 --- a/json_io/parts/json_part.py +++ b/json_io/parts/json_part.py @@ -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 @@ -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 diff --git a/json_io/parts/json_part_box.py b/json_io/parts/json_part_box.py index 7747495..42143a8 100644 --- a/json_io/parts/json_part_box.py +++ b/json_io/parts/json_part_box.py @@ -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' diff --git a/json_io/parts/json_part_sheet.py b/json_io/parts/json_part_sheet.py index b063163..5c0c580 100644 --- a/json_io/parts/json_part_sheet.py +++ b/json_io/parts/json_part_sheet.py @@ -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") @@ -68,7 +68,7 @@ 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): ''' @@ -76,7 +76,7 @@ def read_sheet_attribute(self, active_document, attribute_name): 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)): diff --git a/test/test_actice_document_helper.py b/test/test_actice_document.py similarity index 74% rename from test/test_actice_document_helper.py rename to test/test_actice_document.py index 9e8def9..45981de 100644 --- a/test/test_actice_document_helper.py +++ b/test/test_actice_document.py @@ -27,7 +27,7 @@ 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 @@ -35,11 +35,11 @@ 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): @@ -48,8 +48,8 @@ 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" @@ -57,12 +57,14 @@ def test_create_document(self): 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" @@ -70,13 +72,13 @@ def test_save_and_close_active_document(self): 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") @@ -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") @@ -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") @@ -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.") diff --git a/test/test_json_part_sheet.py b/test/test_json_part_sheet.py index 711f8b7..f138427 100644 --- a/test/test_json_part_sheet.py +++ b/test/test_json_part_sheet.py @@ -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 @@ -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") diff --git a/test/test_setup.py b/test/test_setup.py index 6249ca1..46aad8f 100644 --- a/test/test_setup.py +++ b/test/test_setup.py @@ -30,7 +30,6 @@ import FreeCAD import FreeCADGui -from abc import ABC App = FreeCAD Gui = FreeCADGui @@ -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):