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