// (C) CodeHimBelonga: kdub 2023/03/07
//
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
//using Kdub.Common;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly
: CommandClass
(typeof(TouchingPolylines
.TestCommands_02))]
namespace TouchingPolylines
{
/// <summary>
/// Refer Commands_02_Helper.cs
/// </summary>
public partial class TestCommands_02
{
[CommandMethod("StretchPline")]
public static void StretchPline()
{
var peo
= new PromptEntityOptions
("\nSelect a Polyline Segment to Stretch: "); peo.SetRejectMessage("\nMust be a Polyline...");
peo
.AddAllowedClass(typeof(Polyline
),
true); var perActor = _ed.GetEntity(peo);
if (perActor.Status != PromptStatus.OK) return;
peo
= new PromptEntityOptions
("\nSelect a Target Polyline Segment: "); peo.SetRejectMessage("\nMust be a Polyline...");
peo
.AddAllowedClass(typeof(Polyline
),
true); var perTarget = _ed.GetEntity(peo);
if (perTarget.Status != PromptStatus.OK) { return; }
using (Transaction tr = _doc.TransactionManager.StartTransaction())
{
(bool success, double XAxis) = GetTargetSegmentXAxis(perTarget);
// TODO : refactor out debug statements
if (success) { _ed.WriteMessage($"\nGetTargetSegmentXAxis: {XAxis}\n"); }
else
{
_ed.WriteMessage($"GetTargetSegmentXAxis: Failed\n");
return;
}
// . . . . continue and change Actor VertexPoints
SetActorVertexPoints(perActor, XAxis);
tr.Commit();
}
}
private static void SetActorVertexPoints(PromptEntityResult perActor, double xAxis)
{
Transaction tr = _db.TransactionManager.TopTransaction;
var pline = (Polyline)tr.GetObject(perActor.ObjectId, OpenMode.ForWrite);
var wcsPickedPoint = perActor.PickedPoint.TransformBy(_ed.CurrentUserCoordinateSystem);
var wcspointOnPline = pline.GetClosestPointTo(wcsPickedPoint, false);
var segmentIndex = (int)pline.GetParameterAtPoint(wcspointOnPline);
if (pline.GetSegmentType(segmentIndex) == SegmentType.Line)
{
Point2d p1 = pline.GetPoint2dAt(segmentIndex);
int vertexCount = pline.NumberOfVertices;
int p2Index = segmentIndex + 1 < vertexCount ? segmentIndex + 1 : 0;
Point2d p2 = pline.GetPoint2dAt(p2Index);
// TODO : refactor out debug statements
_ed.WriteMessage($"p1: {p1}\n");
_ed.WriteMessage($"p2: {p2}\n");
p1
= new Point2d
(xAxis, p1
.Y); pline.SetPointAt(segmentIndex, p1);
p2
= new Point2d
(xAxis, p2
.Y); pline.SetPointAt(p2Index, p2);
_ed.WriteMessage($"NEW p1: {p1}\n");
_ed.WriteMessage($"NEW p2: {p2}\n");
}
}
private static Tuple<bool, double> GetTargetSegmentXAxis(PromptEntityResult per)
{
bool success = default;
double xAxis = default;
Transaction tr = _db.TransactionManager.TopTransaction;
var pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
var wcsPickedPoint = per.PickedPoint.TransformBy(_ed.CurrentUserCoordinateSystem);
var wcspointOnPline = pline.GetClosestPointTo(wcsPickedPoint, false);
var segmentIndex = (int)pline.GetParameterAtPoint(wcspointOnPline);
if (pline.GetSegmentType(segmentIndex) == SegmentType.Line)
{
LineSegment2d segment = pline.GetLineSegment2dAt(segmentIndex);
xAxis = segment.StartPoint.X;
success = true;
}
return Tuple.Create(success, xAxis);
}
}
}