For a single polyline entity of multiple segments, I'm looking to create a gap at each intersection. Ideally need to be able to specify the gap distance.
simple example:
more complex example:
Basic code outline:
1. Get polyline entity from editor
2. Explode polyline into individual polylines
3. test each polyline against each other for intersections (intersections must be 'genuine', eg. can't be between 2 consecutive segments)
4. create temporary circle at each intersection of specific radius
5. for each polyline, "trim" the contents inside the circle *(having difficulty at this point onwards)*, thereby removing the intersection
6. delete the temporary circles
7. for each existing polyline, where possible if they can be joined back up, do so
Biggest problem I'm facing is trimming each of the polylines at the circle intersections - I'm just not sure how to put this in code
Eventually I would like to be able to incorporate arcs into the polylines.
My code so far (up to point 4):
[CommandMethod("SelfIntersectPline")]
public static void SelfIntersectPline()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions(
"\nSelect Curve: ");
peo.SetRejectMessage("\nMust be a Curve...");
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord ms = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
//get curve and explode into entities, store curves in entities dbobjectcollection
Curve curve = per.ObjectId.GetObject(OpenMode.ForRead) as Curve;
DBObjectCollection origDBObjects = new DBObjectCollection();
curve.Explode(origDBObjects);
//erase original curve
var originalCurve = (Entity)tr.GetObject(curve.ObjectId, OpenMode.ForWrite);
originalCurve.Erase();
//create temp dbcollection for circle entities
DBObjectCollection circleDBObjects = new DBObjectCollection();
//convert exploded curve entities to polylines
for (int i = origDBObjects.Count - 1; i >= 0; i--)
{
Curve curveObj = origDBObjects[i] as Curve;
origDBObjects.Add(ConvertToPolyline(curveObj));
origDBObjects.RemoveAt(i);
}
//iterate through each polyline within entities collection
for (int i = 0; i < origDBObjects.Count; ++i)
{
for (int j = i + 1; j < origDBObjects.Count; ++j)
{
Polyline pl1 = origDBObjects[i] as Polyline;
Polyline pl2 = origDBObjects[j] as Polyline;
Point3dCollection points = new Point3dCollection();
//test for intersection(s)
pl1.IntersectWith(pl2, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
//validation for intersections
foreach (Point3d point in points)
{
// Skip the start/end points since they are connected vertices
if (point == pl1.StartPoint ||
point == pl1.EndPoint)
{
if (point == pl2.StartPoint ||
point == pl2.EndPoint)
{
// If two consecutive segments, then skip
if (j == i + 1)
{
continue;
}
}
}
//if genuine intersection detected, create temporary circle of specified radius at intersection
foreach (Point3d pt3d in points)
{
Circle c = new Circle();
c.Center = pt3d;
c.Radius = 0.5;
circleDBObjects.Add(c);
}
}
}
}