| 242 | } |
| 243 | |
| 244 | bool UnitConverterRegistry::convertToUnit(DocumentPtr doc, const string& unitType, const string& targetUnit) |
| 245 | { |
| 246 | UnitTypeDefPtr unitTypeDef = doc->getUnitTypeDef(unitType); |
| 247 | UnitConverterPtr unitConverter = getUnitConverter(unitTypeDef); |
| 248 | |
| 249 | if (!unitTypeDef || !unitConverter) |
| 250 | { |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | bool convertedUnits = false; |
| 255 | for (ElementPtr elem : doc->traverseTree()) |
| 256 | { |
| 257 | NodePtr pNode = elem->asA<Node>(); |
| 258 | if (!pNode || !pNode->getInputCount()) |
| 259 | { |
| 260 | continue; |
| 261 | } |
| 262 | for (InputPtr input : pNode->getInputs()) |
| 263 | { |
| 264 | const std::string type = input->getType(); |
| 265 | const ValuePtr value = input->getValue(); |
| 266 | if (value && input->hasUnit() && (input->getUnitType() == unitType)) |
| 267 | { |
| 268 | if (type == getTypeString<float>()) |
| 269 | { |
| 270 | float originalval = value->asA<float>(); |
| 271 | float convertedValue = unitConverter->convert(originalval, input->getUnit(), targetUnit); |
| 272 | input->setValue<float>(convertedValue); |
| 273 | input->removeAttribute(ValueElement::UNIT_ATTRIBUTE); |
| 274 | input->removeAttribute(ValueElement::UNITTYPE_ATTRIBUTE); |
| 275 | convertedUnits = true; |
| 276 | } |
| 277 | else if (type == getTypeString<Vector2>()) |
| 278 | { |
| 279 | Vector2 originalval = value->asA<Vector2>(); |
| 280 | Vector2 convertedValue = unitConverter->convert(originalval, input->getUnit(), targetUnit); |
| 281 | input->setValue<Vector2>(convertedValue); |
| 282 | input->removeAttribute(ValueElement::UNIT_ATTRIBUTE); |
| 283 | input->removeAttribute(ValueElement::UNITTYPE_ATTRIBUTE); |
| 284 | convertedUnits = true; |
| 285 | } |
| 286 | else if (type == getTypeString<Vector3>()) |
| 287 | { |
| 288 | Vector3 originalval = value->asA<Vector3>(); |
| 289 | Vector3 convertedValue = unitConverter->convert(originalval, input->getUnit(), targetUnit); |
| 290 | input->setValue<Vector3>(convertedValue); |
| 291 | input->removeAttribute(ValueElement::UNIT_ATTRIBUTE); |
| 292 | input->removeAttribute(ValueElement::UNITTYPE_ATTRIBUTE); |
| 293 | convertedUnits = true; |
| 294 | } |
| 295 | else if (type == getTypeString<Vector4>()) |
| 296 | { |
| 297 | Vector4 originalval = value->asA<Vector4>(); |
| 298 | Vector4 convertedValue = unitConverter->convert(originalval, input->getUnit(), targetUnit); |
| 299 | input->setValue<Vector4>(convertedValue); |
| 300 | input->removeAttribute(ValueElement::UNIT_ATTRIBUTE); |
| 301 | input->removeAttribute(ValueElement::UNITTYPE_ATTRIBUTE); |
nothing calls this directly
no test coverage detected