Skip to content

Commit

Permalink
[cad] Fix snapping to line extensions for multi geometry as well as p…
Browse files Browse the repository at this point in the history
…olygons
  • Loading branch information
nirvn committed May 28, 2024
1 parent 895193f commit e77428e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
53 changes: 27 additions & 26 deletions src/core/qgscadutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "qgslogger.h"
#include "qgssnappingutils.h"
#include "qgsgeometryutils.h"
#include "qgsgeometrycollection.h"
#include "qgscurvepolygon.h"

// tolerances for soft constraints (last values, and common angles)
// for angles, both tolerance in pixels and degrees are used for better performance
Expand Down Expand Up @@ -302,6 +304,8 @@ QgsCadUtils::AlignMapPointOutput QgsCadUtils::alignMapPoint( const QgsPointXY &o
// *****************************
// ---- Line Extension Constraint

QgsPointXY lineExtensionPt1;
QgsPointXY lineExtensionPt2;
if ( numberOfHardLock < 2 && ctx.lineExtensionConstraint.locked && ctx.lockedSnapVertices().length() != 0 )
{
const QgsPointLocator::Match snap = ctx.lockedSnapVertices().last();
Expand Down Expand Up @@ -380,18 +384,32 @@ QgsCadUtils::AlignMapPointOutput QgsCadUtils::alignMapPoint( const QgsPointXY &o
};

const QgsFeature feature = snap.layer()->getFeature( snap.featureId() );
const QgsGeometry geom = feature.geometry();
const QgsGeometry geometry = feature.geometry();
const QgsAbstractGeometry *geom = geometry.constGet();

bool checked = checkLineExtension( geom.vertexAt( snap.vertexIndex() - 1 ) );
if ( checked )
QgsVertexId vertexId;
geometry.vertexIdFromVertexNr( snap.vertexIndex(), vertexId );
if ( vertexId.isValid() )
{
res.softLockLineExtension = Qgis::LineExtensionSide::BeforeVertex;
}
QgsVertexId previousVertexId;
QgsVertexId nextVertexId;
geom->adjacentVertices( vertexId, previousVertexId, nextVertexId );

checked = checkLineExtension( geom.vertexAt( snap.vertexIndex() + 1 ) );
if ( checked )
{
res.softLockLineExtension = Qgis::LineExtensionSide::AfterVertex;
bool checked = checkLineExtension( geom->vertexAt( previousVertexId ) );
if ( checked )
{
res.softLockLineExtension = Qgis::LineExtensionSide::BeforeVertex;
lineExtensionPt1 = snap.point();
lineExtensionPt2 = QgsPointXY( geom->vertexAt( previousVertexId ) );
}

checked = checkLineExtension( geom->vertexAt( nextVertexId ) );
if ( checked )
{
res.softLockLineExtension = Qgis::LineExtensionSide::AfterVertex;
lineExtensionPt1 = snap.point();
lineExtensionPt2 = QgsPointXY( geom->vertexAt( nextVertexId ) );
}
}
}
}
Expand Down Expand Up @@ -440,23 +458,6 @@ QgsCadUtils::AlignMapPointOutput QgsCadUtils::alignMapPoint( const QgsPointXY &o
}
else if ( res.softLockLineExtension != Qgis::LineExtensionSide::NoVertex )
{
const QgsPointLocator::Match snap = ctx.lockedSnapVertices().last();
const QgsFeature feature = snap.layer()->getFeature( snap.featureId() );
const QgsGeometry geom = feature.geometry();


const QgsPointXY lineExtensionPt1 = snap.point();

QgsPointXY lineExtensionPt2;
if ( res.softLockLineExtension == Qgis::LineExtensionSide::AfterVertex )
{
lineExtensionPt2 = QgsPointXY( geom.vertexAt( snap.vertexIndex() + 1 ) );
}
else
{
lineExtensionPt2 = QgsPointXY( geom.vertexAt( snap.vertexIndex() - 1 ) );
}

const bool intersect = QgsGeometryUtils::lineCircleIntersection( previousPt, ctx.distanceConstraint.value, lineExtensionPt1, lineExtensionPt2, point );
if ( !intersect )
{
Expand Down
24 changes: 17 additions & 7 deletions src/gui/qgsadvanceddigitizingcanvasitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,26 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
const QPointF snappedPoint = toCanvasCoordinates( snap.point() );

const QgsFeature feature = snap.layer()->getFeature( snap.featureId() );
const QgsGeometry geom = feature.geometry();
const QgsGeometry geometry = feature.geometry();
const QgsAbstractGeometry *geom = geometry.constGet();

QgsPoint vertex;
if ( lineExtensionSide == Qgis::LineExtensionSide::BeforeVertex )
QgsVertexId vertexId;
geometry.vertexIdFromVertexNr( snap.vertexIndex(), vertexId );
if ( vertexId.isValid() )
{
vertex = geom.vertexAt( snap.vertexIndex() - 1 );
}
else
{
vertex = geom.vertexAt( snap.vertexIndex() + 1 );
QgsVertexId previousVertexId;
QgsVertexId nextVertexId;
geom->adjacentVertices( vertexId, previousVertexId, nextVertexId );

if ( lineExtensionSide == Qgis::LineExtensionSide::BeforeVertex )
{
vertex = geom->vertexAt( previousVertexId );
}
else
{
vertex = geom->vertexAt( nextVertexId );
}
}

if ( !vertex.isEmpty() )
Expand Down
1 change: 0 additions & 1 deletion src/gui/qgsadvanceddigitizingdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ void QgsAdvancedDigitizingDockWidget::releaseLocks( bool releaseRepeatingLocks )
mMConstraint->setValue( mCadPointList.constLast().m(), true );
}
}

}

#if 0
Expand Down

0 comments on commit e77428e

Please sign in to comment.