| 86 | } |
| 87 | |
| 88 | std::map<UUID, UUID> ModelMerger::suggestHandleMapping(const Model& currentModel, const Model& newModel) const { |
| 89 | std::map<UUID, UUID> result; |
| 90 | |
| 91 | using HandleSet = std::set<UUID>; |
| 92 | using StringHandleMap = std::map<std::string, UUID>; |
| 93 | using ObjectLookup = std::tuple<HandleSet, StringHandleMap, StringHandleMap>; // 0 - handle, 1 - CADObjectId, 2 - Name |
| 94 | using IddToObjectLookupMap = std::map<IddObjectType, ObjectLookup>; |
| 95 | |
| 96 | IddToObjectLookupMap currentIddToObjectLookupMap; |
| 97 | for (const auto& iddObjectType : m_iddObjectTypesToMerge) { |
| 98 | ObjectLookup currentLookup; |
| 99 | for (const auto& object : currentModel.getObjectsByType(iddObjectType)) { |
| 100 | Handle handle = object.handle(); |
| 101 | std::get<0>(currentLookup).insert(handle); |
| 102 | |
| 103 | auto modelObject = object.cast<ModelObject>(); |
| 104 | if (modelObject.hasAdditionalProperties()) { |
| 105 | model::AdditionalProperties additionalProperties = modelObject.additionalProperties(); |
| 106 | if (additionalProperties.hasFeature("CADObjectId")) { |
| 107 | boost::optional<std::string> cadObjectId = additionalProperties.getFeatureAsString("CADObjectId"); |
| 108 | if (cadObjectId) { |
| 109 | std::get<1>(currentLookup).insert(std::make_pair(*cadObjectId, handle)); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | std::get<2>(currentLookup).insert(std::make_pair(object.nameString(), handle)); |
| 115 | } |
| 116 | currentIddToObjectLookupMap.insert(std::make_pair(iddObjectType, currentLookup)); |
| 117 | } |
| 118 | |
| 119 | for (const auto& iddObjectType : m_iddObjectTypesToMerge) { |
| 120 | ObjectLookup currentLookup = currentIddToObjectLookupMap[iddObjectType]; |
| 121 | for (const auto& object : newModel.getObjectsByType(iddObjectType)) { |
| 122 | Handle handle = object.handle(); |
| 123 | if (std::get<0>(currentLookup).count(handle) > 0) { |
| 124 | // handle is in both models |
| 125 | result[handle] = handle; |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if ((iddObjectType == IddObjectType::OS_Site) || (iddObjectType == IddObjectType::OS_Facility) |
| 130 | || (iddObjectType == IddObjectType::OS_Building)) { |
| 131 | // this is a unique object |
| 132 | if (std::get<0>(currentLookup).size() == 1) { |
| 133 | Handle currentHandle = *(std::get<0>(currentLookup).begin()); |
| 134 | result[currentHandle] = handle; |
| 135 | continue; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | auto modelObject = object.cast<ModelObject>(); |
| 140 | if (modelObject.hasAdditionalProperties()) { |
| 141 | model::AdditionalProperties additionalProperties = modelObject.additionalProperties(); |
| 142 | if (additionalProperties.hasFeature("CADObjectId")) { |
| 143 | boost::optional<std::string> cadObjectId = additionalProperties.getFeatureAsString("CADObjectId"); |
| 144 | if (cadObjectId) { |
| 145 | if (std::get<1>(currentLookup).count(*cadObjectId) > 0) { |