[CommandMethod("CTRLNS2")]
public void GetGeometryCmd()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
// Ask the user to select the first object (we're
// not actually limiting selection to only surfaces)
var peo =
new PromptEntityOptions(
"\nSelect first object for centerline extraction"
);
var per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
bool cont = true;
double total = 0.0;
var start = Point3d.Origin;
var end = Point3d.Origin;
bool first = true;
while (cont)
{
using (var tr = db.TransactionManager.StartTransaction())
{
// Get the modelspace for write
var bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
);
var ms =
(BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite
);
// Get the selected object, extract it's primitives,
// then try to link them into sections
var ent =
(Entity)tr.GetObject(per.ObjectId, OpenMode.ForRead);
var ents = ExtractPathPrimitives(ent);
var sections = LinkPrimitivesIntoSections(ents);
// Loop through the sections and attempt to stitch
// them with any that have been generated already
foreach (var section in sections)
{
bool connected = false;
var newStart = start;
var newEnd = end;
if (first)
{
newStart = section.StartPoint;
newEnd = section.EndPoint;
first = false;
connected = true;
}
else
{
if (SamePoint(start, section.StartPoint))
{
connected = true;
newStart = section.EndPoint;
}
else if (SamePoint(start, section.EndPoint))
{
connected = true;
newStart = section.StartPoint;
}
else if (SamePoint(end, section.StartPoint))
{
connected = true;
newEnd = section.EndPoint;
}
else if (SamePoint(end, section.EndPoint))
{
connected = true;
newEnd = section.StartPoint;
}
}
if (!connected)
{
ed.WriteMessage(
"\nSkipping non-contiguous section."
);
section.Clear();
}
else
{
start = newStart;
end = newEnd;
total += section.GetLength();
ed.WriteMessage("\nTotal pipe length: {0}", total);
section.WriteToBlock(tr, ms, 1);
}
}
tr.Commit();
}
// Change the prompt and ask again, looping if appropriate
peo.Message = "\nSelect next object";
per = ed.GetEntity(peo);
cont = per.Status == PromptStatus.OK;
}
}