[CommandMethod("gaps")]
public void TestBreaks()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo
= new PromptEntityOptions
("\nSelect the first polyline to be break: "); peo.SetRejectMessage("Only a curve !");
peo
.AddAllowedClass(typeof(Curve
),
false); PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
ObjectId id1 = per.ObjectId;
peo.Message = "\nSelect the second curve: ";
per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
ObjectId id2 = per.ObjectId;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
doc.TransactionManager.EnableGraphicsFlush(true);
Curve curve1 = (Curve)tr.GetObject(id1, OpenMode.ForRead);
Curve curve2 = (Curve)tr.GetObject(id2, OpenMode.ForRead);
double gap = 10;
Point3dCollection ptColl
= new Point3dCollection
(); curve1.IntersectWith(curve2, Intersect.OnBothOperands, ptColl, 0, 0);
DBObjectCollection qcurves
= new DBObjectCollection
(); qcurves = curve1.GetSplitCurves(ptColl);
if (qcurves.Count > 0)
{
List
<DBObject
> objs
= new List
<DBObject
>(); foreach (DBObject o in qcurves)
objs.Add(o);
objs.OrderBy(o => curve1.GetParameterAtPoint((o as Curve).StartPoint));
ed.WriteMessage("\n" + ptColl.Count.ToString());
Point3dCollection offPts
= new Point3dCollection
(); List
<Point3d
> gapPts
= new List
<Point3d
>(); Entity en;
if (ptColl.Count > 0)
{
ed.WriteMessage("\n" + qcurves.Count.ToString());
int i = 0;
// int n = 0;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
DBObjectCollection newcurves
= new DBObjectCollection
(); for (i = 0; i < objs.Count; i++)
{
Curve curve = objs[i] as Curve;
en = (Entity)curve;
en.ColorIndex = 1;
btr.AppendEntity(en);
tr.AddNewlyCreatedDBObject(en, true);
tr.TransactionManager.QueueForGraphicsFlush();
newcurves.Add(en);
}
ed.WriteMessage("\n" + newcurves.Count.ToString());
int n = 0;
Polyline poly
= new Polyline
(); for (n = 0; n < newcurves.Count; n++)
{
en = (Entity)newcurves[n];
if (en.GetRXClass().DxfName == "LWPOLYLINE")
{
poly = en as Polyline;
}
if (poly == null) return;
if (!poly.IsWriteEnabled) poly.UpgradeOpen();
double leg = poly.GetDistanceAtParameter(poly.EndParam) - poly.GetDistanceAtParameter(poly.StartParam);
if (gap >= leg)
{
ed.WriteMessage("\nLength of curve exceed the gap, exiting...");
return;
}
double spar = poly.StartParam;
double epar = poly.EndParam;
double sgap = poly.GetParameterAtDistance(gap / 2);
Point3d sp = poly.GetPointAtDist(gap / 2).TransformBy(Matrix3d.Identity);
Point3d spprime = poly.GetPointAtDist(gap / 2);
ed.WriteMessage("\nPoint transformed by Identity: {0}\nPoint NOT transformed by Identity: {1}", sp, spprime);
Point3d ep = poly.GetPointAtDist(poly.GetDistanceAtParameter(poly.EndParam) - poly.GetDistanceAtParameter(poly.StartParam) - gap / 2).TransformBy(Matrix3d.Identity);
Point3d spt
= new Point3d
(); Point3d ept
= new Point3d
(); if (n == 0)
{
ept = poly.GetClosestPointTo(ep, false).TransformBy(Matrix3d.Identity);
poly
.SetPointAt(poly
.NumberOfVertices - 1,
new Point2d
(ept
.X, ept
.Y)); }
else if (n == newcurves.Count - 1)//<-------HERE!
{
spt = poly.GetClosestPointTo(sp, false).TransformBy(Matrix3d.Identity);
poly
.SetPointAt(0,
new Point2d
(spt
.X, spt
.Y)); }
else
{
spt = poly.GetClosestPointTo(sp, false).TransformBy(Matrix3d.Identity);
poly
.SetPointAt(0,
new Point2d
(spt
.X, spt
.Y));
ept = poly.GetClosestPointTo(ep, false).TransformBy(Matrix3d.Identity);
poly
.SetPointAt(poly
.NumberOfVertices - 1,
new Point2d
(ept
.X, ept
.Y)); }
tr.TransactionManager.QueueForGraphicsFlush();
ed.WriteMessage("Polyline n: {0}\n\tpoly.NumberOfVerticies: {5}\n\tsp: {1}\n\tspt: {2}\n\tep: {3}\n\tept: {4}", n, sp, spt, ep, ept,poly.NumberOfVertices);
}
curve1.UpgradeOpen();
curve1.Erase();
}
}
doc.TransactionManager.FlushGraphics();
tr.Commit();
}
}