The 'Union' method of the code in the referenced post returns an IEnumerable<Polyline>. The calling method is responsible to add these polylines to a Database or Dispose of them.
About the the command you are trying to write:
- Why do you set the CommandFags.Session?
- After each prompt for user input, you should check the PromptResult.Status value and stop the execution if it's different from PromptStatus.OK.
- You should use a SelectionFilter to ensure all entities in the selection set are closed polylines.
- You do not need to convert the List<Polyline> into an IEnumerable<Polyline> because List<T> implements IEnumerable<T>.
Try something like this:
[CommandMethod("ShrinkWrap")]
public static void ShrinkWrapLinework()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var pointResult = ed.GetPoint("\nSelect the first corner of Crossing Window: ");
if (pointResult.Status != PromptStatus.OK)
return;
var pt1 = pointResult.Value;
pointResult = ed.GetCorner("\nSelect the second corner of Crossing Window: ", pt1);
if (pointResult.Status != PromptStatus.OK)
return;
var pt2 = pointResult.Value;
var filter = new SelectionFilter(new[]
{
new TypedValue(0, "LWPOLYLINE"),
new TypedValue(-4, "&"),
new TypedValue(70, 1)
});
var selection = ed.SelectCrossingWindow(pt1, pt2, filter);
if (selection.Status != PromptStatus.OK)
return;
using (var tr = db.TransactionManager.StartOpenCloseTransaction())
{
var source = new List<Polyline>();
foreach (ObjectId id in selection.Value.GetObjectIds())
{
source.Add((Polyline)tr.GetObject(id, OpenMode.ForRead));
}
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
// Add the ShrinkWrap Polylines to the current space
foreach (var pline in ObjectExtensions.ShrinkWrapPolylines(source))
{
pline.Thickness = 2;
pline.ColorIndex = 1;
curSpace.AppendEntity(pline);
tr.AddNewlyCreatedDBObject(pline, true);
}
tr.Commit();
}
}