Materials: Default units (#20909)

Improves handling and assignment of default units

When creating a material without specifying units, no units are
currently assigned. This commit will assign the default units when none are
given, or throw an error when incompatible units are given. In the
latter case, the units are set to the property defaults.

This commit also incidentally fixed an issue when saving the material that
resulted in accessing an uninitialized pointer.
This commit is contained in:
David Carter
2025-05-05 16:31:25 +00:00
committed by GitHub
parent fe7d3e9071
commit 8abb9f0128
6 changed files with 90 additions and 12 deletions

View File

@@ -246,8 +246,24 @@ void MaterialYamlEntry::addToTree(
propertyValue = propertyValue.remove(
QRegularExpression(QStringLiteral("[\r\n]")));
}
finalModel->setPhysicalValue(QString::fromStdString(propertyName),
propertyValue);
try {
finalModel->setPhysicalValue(QString::fromStdString(propertyName),
propertyValue);
}
catch (const Base::ValueError& e) {
// Units mismatch
Base::Console().Log("Units mismatch in material '%s':'%s' = '%s', "
"setting to default property units '%s'\n",
name.toStdString().c_str(),
propertyName,
propertyValue.toStdString().c_str(),
prop->getUnits().toStdString().c_str());
auto quantity = Base::Quantity::parse(propertyValue.toStdString());
finalModel->setPhysicalValue(
QString::fromStdString(propertyName),
Base::Quantity(quantity.getValue(),
prop->getUnits().toStdString()));
}
}
}
catch (const YAML::BadConversion& e) {