| 505 | } |
| 506 | |
| 507 | ModelObject ModelObject_Impl::clone(Model model) const { |
| 508 | // UniqueModelObject. |
| 509 | if (this->iddObject().properties().unique) { |
| 510 | Model m = this->model(); |
| 511 | if (model == m) { |
| 512 | // Return self |
| 513 | LOG(Info, "Cannot clone a UniqueModelObject into the same model, returning self, for " << briefDescription()); |
| 514 | return getObject<ModelObject>(); |
| 515 | } else { |
| 516 | // Remove any existing objects (there should really be only one) |
| 517 | for (auto& wo : model.getObjectsByType(this->iddObject())) { |
| 518 | LOG(Info, "Removing existing UniqueModelObject in the target model: " << wo.briefDescription()); |
| 519 | wo.remove(); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Business as usual... |
| 525 | |
| 526 | WorkspaceObjectVector result; |
| 527 | |
| 528 | // No children because ModelObject. |
| 529 | // Can have resources and costs. |
| 530 | WorkspaceObjectVector toAdd(1u, getObject<WorkspaceObject>()); |
| 531 | |
| 532 | // add costs |
| 533 | std::vector<LifeCycleCost> lifeCycleCosts = this->lifeCycleCosts(); |
| 534 | toAdd.insert(toAdd.end(), lifeCycleCosts.begin(), lifeCycleCosts.end()); |
| 535 | |
| 536 | // add additional properties |
| 537 | AdditionalPropertiesVector props = getObject<ModelObject>().getModelObjectSources<AdditionalProperties>(); |
| 538 | toAdd.insert(toAdd.end(), props.begin(), props.end()); |
| 539 | |
| 540 | std::string s = toString(this->handle()); |
| 541 | |
| 542 | // If same model, non-recursive insert of resources should be sufficient. |
| 543 | Model m = this->model(); |
| 544 | if (model == m) { |
| 545 | ResourceObjectVector resources = this->resources(); |
| 546 | WorkspaceObjectVector toInsert = castVector<WorkspaceObject>(resources); |
| 547 | result = m.addAndInsertObjects(toAdd, toInsert); |
| 548 | // adding this better have worked |
| 549 | OS_ASSERT(result.size() == 1u + lifeCycleCosts.size() + props.size() + resources.size()); |
| 550 | // inserting resources better have worked |
| 551 | unsigned i = 1 + lifeCycleCosts.size() + props.size(); |
| 552 | for (const ResourceObject& resource : resources) { |
| 553 | OS_ASSERT(result[i] == resource); |
| 554 | ++i; |
| 555 | } |
| 556 | return result[0].cast<ModelObject>(); |
| 557 | } |
| 558 | |
| 559 | // Not the same model. Resource handling is more complicated. |
| 560 | result = model.addAndInsertObjects(toAdd, castArray<WorkspaceObject>(getRecursiveResourceSubTrees(getObject<ModelObject>(), true))); |
| 561 | // Operation should work. |
| 562 | OS_ASSERT(!result.empty()); |
| 563 | return result[0].cast<ModelObject>(); |
| 564 | } |
no test coverage detected