///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2025 Equinor ASA // // ResInsight is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. // // See the GNU General Public License at // for more details. // ///////////////////////////////////////////////////////////////////////////////// #include "RimcWellPathCollection.h" #include "RiaApplication.h" #include "RiaKeyValueStoreUtil.h" #include "WellPathCommands/RicImportWellPaths.h" #include "RimEclipseCase.h" #include "RimModeledWellPath.h" #include "RimPointBasedWellPath.h" #include "RimProject.h" #include "RimWellPath.h" #include "RimWellPathCollection.h" #include "cafPdmFieldScriptingCapability.h" #include "cvfVector3.h" CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimWellPathCollection, RimcWellPathCollection_importWellPath, "ImportWellPath" ); //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimcWellPathCollection_importWellPath::RimcWellPathCollection_importWellPath( caf::PdmObjectHandle* self ) : caf::PdmObjectCreationMethod( self ) { CAF_PDM_InitObject( "Import Well Path" ); CAF_PDM_InitScriptableFieldNoDefault( &m_fileName, "FileName", "File Name" ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::expected RimcWellPathCollection_importWellPath::execute() { auto wellPathCollection = self(); if ( !wellPathCollection ) { return std::unexpected( QString( "Well path collection is null. Cannot add well path." ) ); } if ( m_fileName().isEmpty() ) { return std::unexpected( QString( "File name is empty. Cannot add well path." ) ); } QStringList errorMessages; std::vector importedWellPaths = wellPathCollection->addWellPaths( { m_fileName() }, &errorMessages ); if ( importedWellPaths.empty() ) { if ( !errorMessages.empty() ) { return std::unexpected( errorMessages.join( "\n" ) ); } else { return std::unexpected( QString( "No well paths were imported from file '%1'." ).arg( m_fileName() ) ); } } return importedWellPaths.front(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- QString RimcWellPathCollection_importWellPath::classKeywordReturnedType() const { return RimWellPath::classKeywordStatic(); } CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimWellPathCollection, RimcWellPathCollection_importWellPathFromPointsInternal, "ImportWellPathFromPointsInternal" ); //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimcWellPathCollection_importWellPathFromPointsInternal::RimcWellPathCollection_importWellPathFromPointsInternal( caf::PdmObjectHandle* self ) : caf::PdmObjectCreationMethod( self ) { CAF_PDM_InitObject( "Import Well Path From Points Internal" ); CAF_PDM_InitScriptableFieldNoDefault( &m_name, "Name", "Name" ); CAF_PDM_InitScriptableFieldNoDefault( &m_coordinateXKey, "CoordinateXKey", "Coordinate X Key" ); CAF_PDM_InitScriptableFieldNoDefault( &m_coordinateYKey, "CoordinateYKey", "Coordinate Y Key" ); CAF_PDM_InitScriptableFieldNoDefault( &m_coordinateZKey, "CoordinateZKey", "Coordinate Z Key" ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::expected RimcWellPathCollection_importWellPathFromPointsInternal::execute() { auto wellPathCollection = self(); if ( !wellPathCollection ) { return std::unexpected( QString( "Well path collection is null. Cannot add well path." ) ); } if ( m_name().isEmpty() ) { return std::unexpected( QString( "Name is empty. Cannot add well path." ) ); } if ( m_coordinateXKey().isEmpty() || m_coordinateYKey().isEmpty() || m_coordinateZKey().isEmpty() ) { return std::unexpected( QString( "Coordinate keys are empty. Cannot add well path." ) ); } // Retrieve coordinates from key-value store auto keyValueStore = RiaApplication::instance()->keyValueStore(); auto xData = keyValueStore->get( m_coordinateXKey().toStdString() ); auto yData = keyValueStore->get( m_coordinateYKey().toStdString() ); auto zData = keyValueStore->get( m_coordinateZKey().toStdString() ); if ( !xData || !yData || !zData ) { return std::unexpected( QString( "Failed to retrieve coordinate data from key-value store." ) ); } std::vector xCoords = RiaKeyValueStoreUtil::convertToFloatVector( xData ); std::vector yCoords = RiaKeyValueStoreUtil::convertToFloatVector( yData ); std::vector zCoords = RiaKeyValueStoreUtil::convertToFloatVector( zData ); if ( xCoords.empty() || yCoords.empty() || zCoords.empty() ) { return std::unexpected( QString( "Failed to convert coordinate data from key-value store." ) ); } if ( xCoords.size() != yCoords.size() || yCoords.size() != zCoords.size() ) { return std::unexpected( QString( "Coordinate arrays have different sizes: X=%1, Y=%2, Z=%3" ).arg( xCoords.size() ).arg( yCoords.size() ).arg( zCoords.size() ) ); } // Convert to cvf::Vec3d std::vector trajectoryPoints; for ( size_t i = 0; i < xCoords.size(); ++i ) { trajectoryPoints.push_back( cvf::Vec3d( static_cast( xCoords[i] ), static_cast( yCoords[i] ), static_cast( zCoords[i] ) ) ); } // Create the fixed trajectory well path auto fixedTrajectoryWellPath = std::make_unique(); fixedTrajectoryWellPath->setName( m_name() ); fixedTrajectoryWellPath->setTrajectoryPoints( trajectoryPoints ); auto* wellPath = fixedTrajectoryWellPath.release(); wellPathCollection->addWellPath( wellPath ); wellPathCollection->updateConnectedEditors(); wellPathCollection->scheduleRedrawAffectedViews(); return wellPath; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- QString RimcWellPathCollection_importWellPathFromPointsInternal::classKeywordReturnedType() const { return RimPointBasedWellPath::classKeywordStatic(); } CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimWellPathCollection, RimcWellPathCollection_setMswNameGrouping, "setMswNameGrouping" ); //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimcWellPathCollection_setMswNameGrouping::RimcWellPathCollection_setMswNameGrouping( caf::PdmObjectHandle* self ) : caf::PdmVoidObjectMethod( self ) { CAF_PDM_InitObject( "Set MSW Name Grouping" ); CAF_PDM_InitScriptableFieldNoDefault( &m_mswNameGrouping, "MswNameGrouping", "MSW Name Grouping" ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::expected RimcWellPathCollection_setMswNameGrouping::execute() { auto wellPathCollection = self(); if ( !wellPathCollection ) { return std::unexpected( QString( "Well path collection is null. Cannot add well path." ) ); } wellPathCollection->setMswWellPattern( m_mswNameGrouping() ); return nullptr; }