Files
ResInsight/ApplicationLibCode/ProjectDataModelCommands/RimcPolygonCollection.cpp
T

95 lines
3.8 KiB
C++
Raw Normal View History

2025-04-07 09:14:41 +02:00
/////////////////////////////////////////////////////////////////////////////////
//
// 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 <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimcPolygonCollection.h"
#include "RiaNameUniquenessTools.h"
2025-04-07 09:14:41 +02:00
#include "Polygons/RimPolygon.h"
#include "Polygons/RimPolygonCollection.h"
#include "cafPdmAbstractFieldScriptingCapability.h"
#include "cafPdmFieldScriptingCapability.h"
#include "cafPdmFieldScriptingCapabilityCvfVec3d.h"
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimPolygonCollection, RimcPolygonCollection_createPolygon, "CreatePolygon" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcPolygonCollection_createPolygon::RimcPolygonCollection_createPolygon( caf::PdmObjectHandle* self )
: caf::PdmObjectCreationMethod( self )
2025-04-07 09:14:41 +02:00
{
CAF_PDM_InitObject( "Create and Add New Polygon", "", "", "Create and Add New Polygon" );
2025-04-07 09:14:41 +02:00
CAF_PDM_InitScriptableFieldNoDefault( &m_name, "Name", "Name" );
CAF_PDM_InitScriptableFieldNoDefault( &m_coordinates, "Coordinates", "Coordinates" );
CAF_PDM_InitScriptableField( &m_onNameConflict,
"OnNameConflict",
caf::AppEnum<RiaDefines::NameConflictPolicy>( RiaDefines::NameConflictPolicy::FAIL ),
"",
"",
"",
"How to handle a polygon name already used in this folder" );
2025-04-07 09:14:41 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2025-05-07 11:34:59 +02:00
std::expected<caf::PdmObjectHandle*, QString> RimcPolygonCollection_createPolygon::execute()
2025-04-07 09:14:41 +02:00
{
auto polygonCollection = self<RimPolygonCollection>();
QString name = m_name();
std::vector<cvf::Vec3d> coords = m_coordinates();
if ( !name.isEmpty() )
{
auto resolution = RiaNameUniquenessTools::applyConflictPolicy( &polygonCollection->itemsField(), name, m_onNameConflict().value() );
if ( !resolution.errorMessage.isEmpty() ) return std::unexpected( resolution.errorMessage );
// The new polygon is added below, and that refreshes the views for both objects
if ( auto* existingPolygon = dynamic_cast<RimPolygon*>( resolution.objectToReplace ) )
{
polygonCollection->deleteItem( existingPolygon );
}
name = resolution.nameToUse;
}
2025-05-05 13:59:29 +02:00
RimPolygon* polygon = new RimPolygon();
2025-04-07 09:14:41 +02:00
if ( !name.isEmpty() )
{
polygon->setName( name );
}
polygon->setPointsInDomainCoords( coords );
2025-05-05 13:59:29 +02:00
polygonCollection->addUserDefinedPolygon( polygon );
2025-04-07 09:14:41 +02:00
polygonCollection->updateConnectedEditors();
return polygon;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimcPolygonCollection_createPolygon::classKeywordReturnedType() const
2025-04-07 09:14:41 +02:00
{
return RimPolygon::classKeywordStatic();
2025-04-07 09:14:41 +02:00
}