Hi,
You can use this Editor.SelectByPolyline() extension method.
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
namespace Autodesk.AutoCAD.EditorInput
{
public enum PolygonSelectionMode { Crossing, Window }
public static class EditorExtension
{
public static PromptSelectionResult SelectByPolyline(this Editor ed, Polyline pline, PolygonSelectionMode mode, params TypedValue[] filter)
{
if (ed == null) throw new ArgumentNullException("ed");
if (pline == null) throw new ArgumentNullException("pline");
Matrix3d wcs = ed.CurrentUserCoordinateSystem.Inverse();
Point3dCollection polygon = new Point3dCollection();
for (int i = 0; i < pline.NumberOfVertices; i++)
{
polygon.Add(pline.GetPoint3dAt(i).TransformBy(wcs));
}
PromptSelectionResult result;
using (ViewTableRecord curView = ed.GetCurrentView())
{
ed.Zoom(pline.GeometricExtents);
if (mode == PolygonSelectionMode.Crossing)
result = ed.SelectCrossingPolygon(polygon, new SelectionFilter(filter));
else
result = ed.SelectWindowPolygon(polygon, new SelectionFilter(filter));
ed.SetCurrentView(curView);
}
return result;
}
public static void Zoom(this Editor ed, Extents3d extents)
{
if (ed == null) throw new ArgumentNullException("ed");
using (ViewTableRecord view = ed.GetCurrentView())
{
Matrix3d worldToEye =
(Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
Matrix3d.Displacement(view.Target - Point3d.Origin) *
Matrix3d.PlaneToWorld(view.ViewDirection))
.Inverse();
extents.TransformBy(worldToEye);
view.Width = extents.MaxPoint.X - extents.MinPoint.X;
view.Height = extents.MaxPoint.Y - extents.MinPoint.Y;
view.CenterPoint = new Point2d(
(extents.MaxPoint.X + extents.MinPoint.X) / 2.0,
(extents.MaxPoint.Y + extents.MinPoint.Y) / 2.0);
ed.SetCurrentView(view);
}
}
}
}
Testing example to select block references strctly inside the selected polyline:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace SelectInsidePolygonSample
{
public class Commands
{
[CommandMethod("TEST")]
public void Test()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
var options = new PromptEntityOptions("\nSelect polyline: ");
options.SetRejectMessage("\nMust be a polyline.");
options.AddAllowedClass(typeof(Polyline), true);
var result = ed.GetEntity(options);
if (result.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{
Polyline pline = (Polyline)tr.GetObject(result.ObjectId, OpenMode.ForRead);
PromptSelectionResult selection =
ed.SelectByPolyline(pline, PolygonSelectionMode.Window, new TypedValue(0, "INSERT"));
if (selection.Status == PromptStatus.OK)
ed.SetImpliedSelection(selection.Value);
tr.Commit();
}
}
}
}