diff --git a/ApplicationCode/ProjectDataModel/RimWellPathCollection.cpp b/ApplicationCode/ProjectDataModel/RimWellPathCollection.cpp index 95d6917672..0d2cfd74b5 100644 --- a/ApplicationCode/ProjectDataModel/RimWellPathCollection.cpp +++ b/ApplicationCode/ProjectDataModel/RimWellPathCollection.cpp @@ -163,13 +163,15 @@ std::vector RimWellPathCollection::detachWellPaths( const std::vec std::vector detachedWellPaths; for ( auto wellPath : regularWellPathsToDetach ) { - removeWellPath( wellPath ); - detachedWellPaths.push_back( wellPath ); + if ( detachWellPath( wellPath ) ) + { + detachedWellPaths.push_back( wellPath ); + } } for ( auto group : wellPathGroupsToRemove ) { - removeWellPath( group ); + detachWellPath( group ); } for ( auto group : wellPathGroupsToRemove ) @@ -179,6 +181,29 @@ std::vector RimWellPathCollection::detachWellPaths( const std::vec return detachedWellPaths; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RimWellPathCollection::detachWellPath( gsl::not_null wellPath ) +{ + if ( m_wellPaths.count( wellPath ) != 0u ) + { + m_wellPaths.removeChildObject( wellPath ); + return true; + } + else + { + RimWellPathGroup* group = nullptr; + wellPath->firstAncestorOfType( group ); + if ( group ) + { + group->removeChildWellPath( wellPath ); + return true; + } + } + return false; +} + //-------------------------------------------------------------------------------------------------- /// Read files containing well path data, or create geometry based on the targets //-------------------------------------------------------------------------------------------------- @@ -324,7 +349,7 @@ void RimWellPathCollection::addWellPath( gsl::not_null wellPath, b RimWellPathGroup* parentWellPathGroup = nullptr; if ( importGrouped ) { - parentWellPathGroup = findOrCreateWellPathGroup( wellPath ); + parentWellPathGroup = findOrCreateWellPathGroup( wellPath, allWellPaths() ); } if ( !parentWellPathGroup ) @@ -628,10 +653,13 @@ void RimWellPathCollection::deleteAllWellPaths() //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::groupWellPaths( const std::vector& wellPaths ) { - auto detachedWellPaths = detachWellPaths( wellPaths ); + auto detachedWellPaths = detachWellPaths( wellPaths ); + std::vector allWellPathsToGroupWith = detachedWellPaths; + for ( auto wellPath : detachedWellPaths ) { - addWellPath( wellPath, true ); + auto parentWellPathGroup = findOrCreateWellPathGroup( wellPath, allWellPathsToGroupWith ); + allWellPathsToGroupWith.push_back( parentWellPathGroup ); } this->sortWellsByName(); this->updateAllRequiredEditors(); @@ -645,7 +673,7 @@ void RimWellPathCollection::ungroupWellPaths( const std::vector& w auto detachedWellPaths = detachWellPaths( wellPaths ); for ( auto wellPath : detachedWellPaths ) { - addWellPath( wellPath, false ); + m_wellPaths.push_back( wellPath ); } this->sortWellsByName(); @@ -705,33 +733,7 @@ void RimWellPathCollection::reloadAllWellPathFormations() //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::removeWellPath( gsl::not_null wellPath ) { - if ( auto group = dynamic_cast( wellPath.get() ); group ) - { - group->removeAllChildWellPaths(); - } - - bool removed = false; - if ( m_wellPaths.count( wellPath ) != 0u ) - { - m_wellPaths.removeChildObject( wellPath ); - removed = true; - } - else - { - for ( auto possibleParentWellPath : allWellPaths() ) - { - if ( auto wellPathGroup = dynamic_cast( possibleParentWellPath ); wellPathGroup ) - { - if ( wellPathGroup->hasChildWellPath( wellPath ) ) - { - wellPathGroup->removeChildWellPath( wellPath ); - removed = true; - break; - } - } - } - } - if ( !removed ) return; + bool removed = detachWellPath( wellPath ); RimFileWellPath* fileWellPath = dynamic_cast( wellPath.get() ); if ( fileWellPath ) @@ -797,7 +799,8 @@ std::vector RimWellPathCollection::topLevelGroups() const //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -RimWellPathGroup* RimWellPathCollection::findOrCreateWellPathGroup( gsl::not_null wellPath ) +RimWellPathGroup* RimWellPathCollection::findOrCreateWellPathGroup( gsl::not_null wellPath, + const std::vector& wellPathsToGroupWith ) { auto wellPathGeometry = wellPath->wellPathGeometry(); if ( !wellPathGeometry ) return nullptr; @@ -805,7 +808,7 @@ RimWellPathGroup* RimWellPathCollection::findOrCreateWellPathGroup( gsl::not_nul const double eps = 1.0e-4; std::map wellPathsWithCommonGeometry; - for ( auto existingWellPath : allWellPaths() ) + for ( auto existingWellPath : wellPathsToGroupWith ) { double identicalTubeLength = existingWellPath->wellPathGeometry()->identicalTubeLength( *wellPathGeometry ); if ( identicalTubeLength > eps ) @@ -829,6 +832,7 @@ RimWellPathGroup* RimWellPathCollection::findOrCreateWellPathGroup( gsl::not_nul if ( mostSimilarWellPathGroup ) { + detachWellPath( wellPath ); mostSimilarWellPathGroup->addChildWellPath( wellPath.get() ); } else if ( !wellPathsWithCommonGeometry.empty() ) @@ -838,10 +842,9 @@ RimWellPathGroup* RimWellPathCollection::findOrCreateWellPathGroup( gsl::not_nul for ( auto [existingWellPath, identicalTubeLength] : wellPathsWithCommonGeometry ) { - removeWellPath( existingWellPath ); + detachWellPath( existingWellPath ); group->addChildWellPath( existingWellPath ); } - group->addChildWellPath( wellPath ); mostSimilarWellPathGroup = group; } diff --git a/ApplicationCode/ProjectDataModel/RimWellPathCollection.h b/ApplicationCode/ProjectDataModel/RimWellPathCollection.h index cda4399547..536d59d895 100644 --- a/ApplicationCode/ProjectDataModel/RimWellPathCollection.h +++ b/ApplicationCode/ProjectDataModel/RimWellPathCollection.h @@ -131,6 +131,7 @@ protected: void fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) override; std::vector detachWellPaths( const std::vector wellPathsToDetach ); + bool detachWellPath( gsl::not_null wellPath ); private: void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override; @@ -142,7 +143,8 @@ private: void sortWellsByName(); std::vector topLevelGroups() const; - RimWellPathGroup* findOrCreateWellPathGroup( gsl::not_null wellPath ); + RimWellPathGroup* findOrCreateWellPathGroup( gsl::not_null wellPath, + const std::vector& wellPathsToGroupWith ); RiaEclipseUnitTools::UnitSystemType findUnitSystemForWellPath( const RimWellPath* wellPath ); diff --git a/ApplicationCode/ProjectDataModel/RimWellPathGroup.cpp b/ApplicationCode/ProjectDataModel/RimWellPathGroup.cpp index b9ba340e33..f1e610d684 100644 --- a/ApplicationCode/ProjectDataModel/RimWellPathGroup.cpp +++ b/ApplicationCode/ProjectDataModel/RimWellPathGroup.cpp @@ -99,6 +99,10 @@ bool RimWellPathGroup::hasChildWellPath( RimWellPath* wellPath ) void RimWellPathGroup::removeChildWellPath( RimWellPath* wellPath ) { m_childWellPaths.removeChildObject( wellPath ); + if ( auto geometry = wellPath->wellPathGeometry(); geometry ) + { + geometry->setUniqueStartIndex( 0u ); + } createWellPathGeometry(); updateWellPathName(); } @@ -108,7 +112,11 @@ void RimWellPathGroup::removeChildWellPath( RimWellPath* wellPath ) //-------------------------------------------------------------------------------------------------- void RimWellPathGroup::removeAllChildWellPaths() { - m_childWellPaths.clear(); + auto childWellPaths = m_childWellPaths.childObjects(); + for ( auto wellPath : childWellPaths ) + { + removeChildWellPath( wellPath ); + } setWellPathGeometry( cvf::ref( new RigWellPath ).p() ); updateWellPathName(); }