diff --git a/cura/Settings/GlobalStack.py b/cura/Settings/GlobalStack.py index 9dfb8523b3..964551f263 100755 --- a/cura/Settings/GlobalStack.py +++ b/cura/Settings/GlobalStack.py @@ -107,6 +107,21 @@ class GlobalStack(CuraContainerStack): def setNextStack(self, next_stack: ContainerStack) -> None: raise Exceptions.InvalidOperationError("Global stack cannot have a next stack!") + ## Gets the approximate filament diameter that the machine requires. + # + # The approximate material diameter is the material diameter rounded to + # the nearest millimetre. + # + # If the machine has no requirement for the diameter, -1 is returned. + # + # \return The approximate filament diameter for the printer, as a string. + @pyqtProperty(str) + def approximateMaterialDiameter(self) -> str: + material_diameter = self.definition.getMetaDataEntry("material_diameter") + if material_diameter is None: + return "-1" + return str(round(float(self.definition.getMetaDataEntry("material_diameter")))) #Round, then convert back to string. + # protected: # Determine whether or not we should try to get the "resolve" property instead of the diff --git a/tests/Settings/TestGlobalStack.py b/tests/Settings/TestGlobalStack.py index 6dab02b220..3cac17fa3a 100755 --- a/tests/Settings/TestGlobalStack.py +++ b/tests/Settings/TestGlobalStack.py @@ -86,6 +86,28 @@ def test_addExtruder(global_stack): # global_stack.addExtruder(unittest.mock.MagicMock()) assert len(global_stack.extruders) == 2 #Didn't add the faulty extruder. +## Tests getting the approximate material diameter. +@pytest.mark.parametrize("diameter, approximate_diameter", [ + #Some real-life cases that are common in printers. + (2.85, 3), + (1.75, 2), + (3.0, 3), + (2.0, 2), + #Exceptional cases. + (0, 0), + (-10.1, -10), + (-1, -1) +]) +def test_approximateMaterialDiameter(diameter, approximate_diameter, global_stack): + global_stack.definition = DefinitionContainer(container_id = "TestDefinition") + global_stack.definition._metadata["material_diameter"] = str(diameter) + assert float(global_stack.approximateMaterialDiameter) == approximate_diameter + +## Tests getting the material diameter when there is no material diameter. +def test_approximateMaterialDiameterNoDiameter(global_stack): + global_stack.definition = DefinitionContainer(container_id = "TestDefinition") + assert global_stack.approximateMaterialDiameter == "-1" + #Tests setting user changes profiles to invalid containers. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "wrong container type"),