def test_traverse_and_parse_json_tree_subassembly_same_part(self):
        self.clearWorkingDirectory()
        json_data = TEST_JSON_PRODUCT_SUBASSEMBLY_WITH_SAME_PART
        self.create_Test_Part()

        json_object = json.loads(json_data)

        traverser = JsonProductAssemblyTreeTraverser(self._WORKING_DIRECTORY)
        traverser.traverse(json_object)
        lst_of_depths = traverser._lst_of_depths

        self.assertEqual(len(lst_of_depths), 2,
                         "Found the right amount of 2 assemblies")

        traverser.parse_from_json()

        # in this test case the product assembly "BasePlate" refers a part "BasePlate" with the same name and uuid
        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PRODUCT_IDENTIFIER +
                "BasePlate_3d3708fd_5c6c_4af9_b710_d68778466084")
        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 4,
            "Found correct amount of root objects 2 objects plus 2 sheets")

        # the root assembly should only have a part and the product assembly "BasePlate"
        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PRODUCT_IDENTIFIER +
                "BasePlateBottom1_e8794f3d_86ec_44c5_9618_8b7170c45484")
        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 4,
            "Found correct amount of root objects 2 objects plus 2 sheets")
    def Activated(self):
        Log("Calling the exporter\n")

        # call pyqt dialog: returns (filename, filter)
        filename = QFileDialog.getSaveFileName(
            None,  # ui parent
            "Save JSON file",  # dialog caption
            Environment.get_appdata_module_path(),
            "JSON(*.json)")[0]  # filter

        if filename != '':
            json_exporter = JsonExporter(
                Environment.get_appdata_module_path() + os.sep)

            if (FreeCAD.ActiveDocument is not None):
                document_name = FreeCAD.ActiveDocument.Label
                active_document = ActiveDocument(
                    Environment.get_appdata_module_path(
                    )).open_set_and_get_document(document_name)
                json_dict = json_exporter.full_export(active_document)
                json_str = json.dumps(json_dict)

                # after export open the file again for the UI
                active_document = ActiveDocument(
                    Environment.get_appdata_module_path(
                    )).open_set_and_get_document(document_name)

                with open(filename, 'w') as file:
                    file.write(json_str)
            else:
                Log("Error: First open a document to export it\n")
    def read_from_freecad(self, active_document, working_output_directory, part_list, freecad_object=None, freecad_sheet=None):
        """
        Reads an ProductAssembly from FreeCAD
        Then calls read_from_freecad of his children (either another assembly or a ProductChild)
        """
        products_with_sheets = self.get_products_of_active_document(active_document)
        # read the assembly
        super().read_from_freecad(active_document, working_output_directory, part_list, freecad_object, freecad_sheet)

        self.children = []
        # read the children
        for product, sheet in products_with_sheets:
            name, label = product.Name, product.Label
            # use the source file of a2plus part
            # then get the file name (.split(os.path.sep)[-1]) and ignore the FreeCAD file ending ([:-6])
            child_file_name = product.sourceFile.split(os.path.sep)[-1][:-6]

            # open the document for this child
            child_document = ActiveDocument(working_output_directory).open_set_and_get_document(child_file_name)

            if(PRODUCT_IDENTIFIER in name):
                Log(f"Read ProductAssembly '{label}'\n")
                child = JsonProductAssembly()
            else:
                Log(f"Read Product '{label}'\n")
                child = AJsonProduct()

            child.read_from_freecad(child_document, working_output_directory, part_list, freecad_object=product, freecad_sheet=sheet)
            child_document.close_active_document(child_file_name)

            self.children.append(child)
Exemplo n.º 4
0
    def test_create_part_geometry(self):
        json_data = TEST_JSON_PART_GEOMETRY

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

        # get the current module path and get the directory for the test resource
        # place that path into the json object before executing the transformations
        stl_test_resource_path = Environment.get_test_resource_path(
            "Switch.stl")
        json_object[JSON_ELEMENT_STL_PATH] = stl_test_resource_path

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

        active_document.save_as("PartGeometry")

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

        # Check that the extra attribute for the STL files got written to the sheet
        sheet_stl_path = json_part.sheet.read_sheet_attribute(
            active_document, "stl_path")
        self.assertEqual(sheet_stl_path, stl_test_resource_path,
                         "The path is perfectly written to the spreadsheet")

        # Check that there is a box with the correct properties
        self.assertEquals(
            Gui.ActiveDocument.getObject("Geometry").ShapeColor,
            (0.003921568859368563, 0.007843137718737125, 0.9098039269447327,
             0.0), "Shape has correct color")
    def test_traverse_and_parse_json_tree(self):
        json_data = TEST_JSON_PRODUCT_WITH_CHILDREN_WITH_CHILD
        self.create_Test_Part()

        json_object = json.loads(json_data)

        traverser = JsonProductAssemblyTreeTraverser(self._WORKING_DIRECTORY)
        traverser.traverse(json_object)
        lst_of_depths = traverser._lst_of_depths

        self.assertEqual(len(lst_of_depths), 2,
                         "Found the right amount of 2 assemblies")

        traverser.parse_from_json()

        # this should have similar results to test_create_part_product_assembly_and_subassembly_with_root_part_manual in TestJsonProductAssembly
        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PRODUCT_IDENTIFIER +
                "BasePlateBottom2_e8794f3d_86ec_44c5_9618_8b7170c45484")
        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 4,
            "Found correct amount of root objects 2 objects plus 2 sheets")

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PRODUCT_IDENTIFIER +
                "BasePlateBottom1_e8794f3d_86ec_44c5_9618_8b7170c45484")
        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 6,
            "Found correct amount of root objects 3 objects plus 3 sheets")
Exemplo n.º 6
0
    def create_Test_Part(self):
        '''
        Method to create and store a test part, as it is needed in the assembly tests
        '''
        json_data = """{
            "color": 12632256,
            "shape": "BOX",
            "name": "BasePlate",
            "lengthX": 0.04,
            "lengthY": 0.02,
            "lengthZ": 0.002,
            "radius": 0.0,
            "uuid": "3d3708fd-5c6c-4af9-b710-d68778466084"
        }"""

        json_object = json.loads(json_data)
        json_part = JsonPartBox()
        json_part.parse_from_json(json_object)

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                json_part.get_unique_name())
        json_part.write_to_freecad(active_document)
        active_document.save_and_close_active_document(
            json_part.get_unique_name())
Exemplo n.º 7
0
    def test_create_part_box(self):
        json_data = TEST_JSON_PART_BOX

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

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

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

        # Check that there is a box with the correct properties
        self.assertEquals(
            App.ActiveDocument.getObject("Box").Length, 40,
            "Shape has correct size")
        self.assertEquals(
            App.ActiveDocument.getObject("Box").Width, 20,
            "Shape has correct size")
        self.assertEquals(
            App.ActiveDocument.getObject("Box").Height, 10,
            "Shape has correct size")

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

        active_document.save_and_close_active_document("PartBox")
Exemplo n.º 8
0
    def test_create_part_sphere(self):
        json_data = TEST_JSON_PART_SPHERE

        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")
    def test_create_part_product_subassembly_with_root_part(self):
        json_data = TEST_JSON_PRODUCT_WITH_CHILDREN_WITH_CHILD
        self.create_Test_Part()

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                "ProductSubassemblyRootPart")

        json_object = json.loads(json_data)

        subassembly = json_object[JSON_ELEMNT_CHILDREN][0]
        json_product = JsonProductAssembly().parse_from_json(subassembly)

        json_product.write_to_freecad(active_document)
        active_document.save_as("ProductSubassemblyRootPart")

        self.assertEquals(len(json_product.children), 1,
                          "correct amount of children")
        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 4,
            "Found correct amount of root objects 2 objects plus 2 sheets")

        product_part_name = json_product.get_unique_name()
        product_object = active_document.app_active_document.getObjectsByLabel(
            product_part_name)[0]
        self.assertIsNotNone(product_object,
                             "Found an object under the given part name")

        product_child1_part_name = json_product.children[0].get_unique_name()
        product_object = active_document.app_active_document.getObjectsByLabel(
            product_child1_part_name)[0]
        self.assertIsNotNone(product_object,
                             "Found an object under the given part name")
    def test_traverse_and_parse_json_tree_subassembly_no_part(self):
        self.clearWorkingDirectory()
        json_data = TEST_JSON_PRODUCT_WITH_CHILDREN_WITH_CHILD_SUBASSEMBLY_IS_NO_PART
        self.create_Test_Part()

        json_object = json.loads(json_data)

        traverser = JsonProductAssemblyTreeTraverser(self._WORKING_DIRECTORY)
        traverser.traverse(json_object)
        lst_of_depths = traverser._lst_of_depths

        self.assertEqual(len(lst_of_depths), 2,
                         "Found the right amount of 2 assemblies")

        traverser.parse_from_json()

        # in this test case the product assembly "BasePlateBottom2" only has a child and not a part reference
        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PRODUCT_IDENTIFIER +
                "BasePlateBottom2_e8794f3d_86ec_44c5_9618_8b7170c45484")
        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 2,
            "Found correct amount of root objects 1 objects plus 1 sheets")

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PRODUCT_IDENTIFIER +
                "BasePlateBottom1_e8794f3d_86ec_44c5_9618_8b7170c45484")
        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 6,
            "Found correct amount of root objects 3 objects plus 3 sheets")
Exemplo n.º 11
0
    def test_reopen_document(self):
        TEST_DOCUMENT_NAME = "box_263_456_789_444"

        loaded_documents = list(App.listDocuments().keys())
        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")

        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.assertEquals(1, len(loaded_documents),
                          "There is only one document loaded.")

        # Now save and load the document
        App.ActiveDocument.addObject("Part::Box", "BoxReopen")

        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")

        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")

        # Call the method a second time and see that it is the same
        # therefore add an object and make sure it still exists after reopening
        App.ActiveDocument.addObject("Part::Box", "BoxReopen2")
        freecad_object = App.ActiveDocument.getObject("BoxReopen")

        loaded_documents = list(App.listDocuments().keys())
        self.assertEquals(1, len(loaded_documents),
                          "Document is still loaded, just open it.")
        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.")

        freecad_object1 = App.ActiveDocument.getObject("BoxReopen")
        freecad_object2 = App.ActiveDocument.getObject("BoxReopen2")

        self.assertIsNotNone(freecad_object1, "Specific Object 1 exists")
        self.assertIsNotNone(
            freecad_object2,
            "Specific Object 2 did not disappear while reopening")
    def test_create_part_product_assembly_and_subassembly_with_root_part_manual(
            self):
        json_data = TEST_JSON_PRODUCT_WITH_CHILDREN_WITH_CHILD
        self.create_Test_Part()

        json_object = json.loads(json_data)

        subassembly = json_object[JSON_ELEMNT_CHILDREN][0]

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PRODUCT_IDENTIFIER +
                "BasePlateBottom2_e8794f3d_86ec_44c5_9618_8b7170c45484")

        json_product = JsonProductAssembly().parse_from_json(subassembly)
        json_product.write_to_freecad(active_document)
        active_document.save_as(
            PRODUCT_IDENTIFIER +
            "BasePlateBottom2_e8794f3d_86ec_44c5_9618_8b7170c45484")

        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 4,
            "Found correct amount of root objects 2 objects plus 2 sheets")

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                "ProductAssemblyAndSubassemblyRootPart")

        json_product = JsonProductAssembly().parse_from_json(json_object)
        json_product.write_to_freecad(active_document)
        active_document.save_as("ProductAssemblyAndSubassemblyRootPart")

        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 6,
            "Found correct amount of root objects 3 objects plus 3 sheets")
Exemplo n.º 13
0
    def test_create_and_read_part_geometry(self):
        json_data = TEST_JSON_PART_GEOMETRY

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

        # get the current module path and get the directory for the test resource
        # place that path into the json object before executing the transformations
        stl_test_resource_path = Environment.get_test_resource_path(
            "Switch.stl")
        stl_test_resource_path_cp = os.path.join(self._WORKING_DIRECTORY,
                                                 "Switch_cp.stl")
        copyfile(stl_test_resource_path, stl_test_resource_path_cp)
        json_object[JSON_ELEMENT_STL_PATH] = stl_test_resource_path_cp

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

        read_part = JsonPartGeometry()
        # first 3 objects belong to the stl
        freecad_object = active_document.app_active_document.Objects[3]
        freecad_sheet = active_document.app_active_document.Objects[4]

        read_part.read_from_freecad(freecad_object, freecad_sheet)
        read_json = read_part.parse_to_json()

        self.assertJsonObjectsEqual(json_object, read_json,
                                    "Equal JSON objects")
Exemplo n.º 14
0
    def test_create_change_and_read_part_sphere(self):
        json_data = TEST_JSON_PART_SPHERE

        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)

        read_part = JsonPartSphere()
        freecad_object = active_document.app_active_document.Objects[0]
        freecad_sheet = active_document.app_active_document.Objects[1]

        # Change sphere properties
        freecad_object.Radius = 10

        read_part.read_from_freecad(freecad_object, freecad_sheet)
        read_json = read_part.parse_to_json()

        # check that only the expected values got changed
        self.assertJsonObjectsAlmostEqual(
            read_json, json_object, [JSON_ELEMENT_RADIUS],
            "JSON objects at most differ in radius")

        # check that the values got changed correct
        self.assertEqual(read_json[JSON_ELEMENT_RADIUS], 0.01,
                         "Radius set correct")
    def test_write_to_freecad(self):
        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                "PartSheetTest_Write")
        json_part = AJsonPart().parse_from_json(self._json_test_object)
        json_spread_sheet = JsonSpreadSheet(json_part)

        json_spread_sheet_name = json_spread_sheet.create_sheet_name()

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

        json_spread_sheet.write_to_freecad(active_document)

        json_part_sheet_object = active_document.app_active_document.getObject(
            json_spread_sheet_name)
        self.assertIsNotNone(json_part_sheet_object,
                             "The object does exist now")
        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")
Exemplo n.º 16
0
    def test_get_file_full_path(self):
        TEST_DOCUMENT_NAME = "box_263_456_789_111"

        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 parse_from_json(self):
        """
        Iterate through the list created by traversing the tree in reverse and parse the found product assemblies
        """
        json_product, active_document = None, None

        # parse in reverse order
        for depth in reversed(self._lst_of_depths):
            for assembly in depth:
                Log(f"Parsing '{assembly[JSON_ELEMENT_NAME]}'\n")

                json_product = JsonProductAssembly().parse_from_json(assembly)
                active_document = ActiveDocument(
                    self.working_output_directory).open_set_and_get_document(
                        json_product.get_product_unique_name())

                json_product.write_to_freecad(active_document)
                active_document.save_and_close_active_document(
                    json_product.get_product_unique_name())

        # the last json_product is the root of the assembly, open it again for the UI
        if (json_product is not None):
            active_document = ActiveDocument(
                self.working_output_directory).open_set_and_get_document(
                    json_product.get_product_unique_name())

        return json_product, active_document
    def test_read_sheet_attribute(self):
        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                "PartSheetTest_Read")
        json_part = AJsonPart().parse_from_json(self._json_test_object)
        json_spread_sheet = JsonSpreadSheet(json_part)

        json_spread_sheet.write_to_freecad(active_document)

        attribute = json_spread_sheet.read_sheet_attribute(
            active_document, "height")

        self.assertEquals(attribute, 300, "Got correct value")
    def test_create_part_product_assembly_with_root_part(self):
        self.create_Test_Part()

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                "ProductAssemblyRootPart")
        json_object = json.loads(self.json_data)
        json_product = JsonProductAssembly().parse_from_json(json_object)

        json_product.write_to_freecad(active_document)

        active_document.save_as("ProductAssemblyRootPart")

        # find the object by its label there should be three objects identifiable in the current export
        # the part in the product and two times the referenced parts in the children.
        self.assertEquals(len(json_product.children), 2,
                          "correct amount of children")
        self.assertEquals(
            len(active_document.app_active_document.RootObjects), 6,
            "Found correct amount of root objects 3 objects plus 3 sheets")

        product_part_name = json_product.get_unique_name()
        product_object = active_document.app_active_document.getObjectsByLabel(
            product_part_name)[0]
        self.assertIsNotNone(product_object,
                             "Found an object under the given part name")

        product_child1_part_name = json_product.children[0].get_unique_name()
        product_object = active_document.app_active_document.getObjectsByLabel(
            product_child1_part_name)[0]
        self.assertIsNotNone(product_object,
                             "Found an object under the given part name")

        product_child2_part_name = json_product.children[1].get_unique_name()
        product_object = active_document.app_active_document.getObjectsByLabel(
            product_child2_part_name)[0]
        self.assertIsNotNone(product_object,
                             "Found an object under the given part name")
Exemplo n.º 20
0
    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")

        ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document(
            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")

        ActiveDocument(self._WORKING_DIRECTORY).save_and_close_active_document(
            TEST_DOCUMENT_NAME)

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

        loaded_documents = list(App.listDocuments().keys())
        self.assertNotIn(TEST_DOCUMENT_NAME, loaded_documents,
                         "Document got closed")
    def test_is_sheet_attached(self):
        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                "PartSheetTest_Attached")
        json_part = AJsonPart().parse_from_json(self._json_test_object)
        json_spread_sheet = JsonSpreadSheet(json_part)

        self.assertFalse(json_spread_sheet.is_sheet_attached(active_document),
                         "There is no sheet yet")

        json_spread_sheet.write_to_freecad(active_document)

        self.assertTrue(json_spread_sheet.is_sheet_attached(active_document),
                        "Sheet got attached and can be read")
    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 = ""

        if json_part is not None:
            # 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 = PART_IDENTIFIER + get_part_name_uuid(json_object)

            active_document = ActiveDocument(self.working_output_directory).open_set_and_get_document(part_file_name)

            json_part.write_to_freecad(active_document)

            active_document.save_and_close_active_document(part_file_name)
            Log("Saved part to file: " + part_file_name + "\n")
        else:
            Log("Visualization shape is most likely NONE, therefore no file is created\n")

        return part_file_name
    def test_create_cylinder(self):
        json_data = TEST_JSON_PART_CYLINDER

        json_object = json.loads(json_data)
        json_part = JsonPartFactory().create_from_json(json_object)

        self.assertIsInstance(json_part, JsonPartCylinder, "Created correct object")

        active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document("PartCylinder")
        json_part.write_to_freecad(active_document)
        freecad_object = active_document.app_active_document.Objects[0]
        freecad_sheet = active_document.app_active_document.Objects[1]

        json_part_freecad = JsonPartFactory().create_from_freecad(freecad_object, freecad_sheet)
        self.assertIsInstance(json_part_freecad, JsonPartCylinder, "Created correct object")
Exemplo n.º 24
0
    def test_create_and_read_part_cone(self):
        json_data = TEST_JSON_PART_CONE

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

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

        read_part = JsonPartCone()
        freecad_object = active_document.app_active_document.Objects[0]
        freecad_sheet = active_document.app_active_document.Objects[1]

        read_part.read_from_freecad(freecad_object, freecad_sheet)
        read_json = read_part.parse_to_json()

        self.assertJsonObjectsEqual(json_object, read_json, "Equal JSON objects")
    def test_create_part(self):
        json_data = TEST_JSON_PART_BOX

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

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

        self.assertIsNotNone(App.ActiveDocument.getObject("Box"),
                             "The Box object got created")
        self.assertEquals(
            Gui.ActiveDocument.getObject("Box").ShapeColor,
            (0.7529411911964417, 0.7529411911964417, 0.7529411911964417, 0.0),
            "Shape has correct color")
    def test_full_import_shape_geometry(self):
        json_importer = JsonImporter(self._WORKING_DIRECTORY)
        json_object = json.loads(TEST_JSON_FULL_GEOMETRY)

        # get the current module path and get the directory for the test resource
        # place that path into the json object before executing the transformations
        stl_test_resource_path = Environment.get_test_resource_path(
            "Switch.stl")
        stl_test_resource_path_cp = os.path.join(self._WORKING_DIRECTORY,
                                                 "Switch_cp.stl")
        copyfile(stl_test_resource_path, stl_test_resource_path_cp)
        json_object[JSON_PARTS][0][
            JSON_ELEMENT_STL_PATH] = stl_test_resource_path_cp

        _, json_product, active_document = json_importer.full_import(
            json_object)

        self.assertEqual(len(json_product.children), 1,
                         "Correct amount of children")
        self.assertEqual(len(active_document.app_active_document.RootObjects),
                         2, "Found correct amount of 1 object and 1 sheet")
        name = "Geometry_cc14e2c7_9d7e_4cf2_8d6d_9b8cf5e96d56"
        self.assertEqual(
            active_document.app_active_document.RootObjects[0].Label, name,
            "Found the right object")
        self.assertEqual(
            active_document.app_active_document.RootObjects[1].Label,
            FREECAD_PART_SHEET_NAME + "_" + name, "Found the right object")

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PART_IDENTIFIER +
                "Geometry_38eae3a5_8338_4a51_b1df_5583058f9e77")
        self.assertEqual(len(active_document.app_active_document.RootObjects),
                         5, "Found correct amount of 4 object and 1 sheet")

        freecad_sheet = active_document.app_active_document.Objects[4]
        sheet = JsonSpreadSheet(JsonPartGeometry())
        stl_path = sheet.read_sheet_attribute_from_freecad(
            freecad_sheet, "stl_path")

        # Check that the extra attribute for the STL files got written to the sheet
        self.assertEqual(stl_path, stl_test_resource_path_cp,
                         "The path is written to the spreadsheet")
    def test_rotation(self):
        self.create_Test_Part()

        json_data = TEST_JSON_PRODUCT_WITHOUT_CHILDREN

        json_object = json.loads(json_data)
        json_product = AJsonProduct().parse_from_json(json_object)
        rot_old = [json_product.rot_x, json_product.rot_y, json_product.rot_z]

        active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document("Rotation_test")
        json_product.write_to_freecad(active_document)
        freecad_object = active_document.app_active_document.Objects[0]

        json_product._get_freecad_rotation(freecad_object)

        read_json = json_product.parse_to_json()

        self.assertAlmostEqualVector(rot_old, [json_product.rot_x, json_product.rot_y, json_product.rot_z], msg="Product rotations are equal")
        self.assertAlmostEqualVector([json_object[JSON_ELEMENT_ROT_X], json_object[JSON_ELEMENT_ROT_Y], json_object[JSON_ELEMENT_ROT_Z]],
                                     [read_json[JSON_ELEMENT_ROT_X], read_json[JSON_ELEMENT_ROT_Y], read_json[JSON_ELEMENT_ROT_Z]],
                                     msg="Rotations in JSON files are equal")
    def test_create_geometry(self):
        json_data = TEST_JSON_PART_GEOMETRY

        json_object = json.loads(json_data)
        # get the current module path and get the directory for the test resource
        # place that path into the json object before executing the transformations
        stl_test_resource_path = Environment.get_test_resource_path("Switch.stl")
        json_object[JSON_ELEMENT_STL_PATH] = stl_test_resource_path

        json_part = JsonPartFactory().create_from_json(json_object)

        self.assertIsInstance(json_part, JsonPartGeometry, "Created correct object")

        active_document = ActiveDocument(self._WORKING_DIRECTORY).open_set_and_get_document("PartGeometry")
        json_part.write_to_freecad(active_document)
        # first 3 objects belong to the stl
        freecad_object = active_document.app_active_document.Objects[3]
        freecad_sheet = active_document.app_active_document.Objects[4]

        json_part_freecad = JsonPartFactory().create_from_freecad(freecad_object, freecad_sheet)
        self.assertIsInstance(json_part_freecad, JsonPartGeometry, "Created correct object")
    def test_full_import_shape_none_assembly(self):
        json_importer = JsonImporter(self._WORKING_DIRECTORY)
        json_object = json.loads(TEST_JSON_FULL_NONE_SHAPE_ASSEMBLY)
        part_file_names, json_product, active_document = json_importer.full_import(
            json_object)

        self.assertEqual(part_file_names,
                         ['part_None_cc14e2c7_9d7e_4cf2_8d6d_9b8cf5e96d56'],
                         "Found dummy part")

        self.assertEqual(len(json_product.children), 1,
                         "Correct amount of children")
        self.assertEqual(len(active_document.app_active_document.RootObjects),
                         2, "Found correct amount of 1 sheet and 1 dummy")

        active_document = ActiveDocument(
            self._WORKING_DIRECTORY).open_set_and_get_document(
                PRODUCT_IDENTIFIER +
                "NoneAssembly_2afb23c9_f458_4bdb_a4e7_fc863364644f")
        self.assertEqual(
            len(active_document.app_active_document.RootObjects), 4,
            "Found correct amount of 1 assembly and 2 sheet and 1 dummy")
Exemplo n.º 30
0
    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 = 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")