We demonstrated about some AutoCAD selection set building APIs such as the Editor.SelectFence and the Editor.SelectCrossingWindow and presented our PointCollector coder a few times before.
Now let’s see how to use AutoCAD .NET selection set APIs and the PointCollector coder together to make the selection work nicer and quicker. Specifically in this case, let us use the PointCollector to collect two window corners visually first and use the Editor.SelectWindow to build up a selection set from the two points.
[CommandMethod("WindowSelectionAndPointCollector", CommandFlags.Modal)]
public static void WindowSelectionAndPointCollector_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
using (PointCollector pc = new PointCollector(PointCollector.Shape.Window))
{
Point3dCollection points = pc.Collect();
PromptSelectionResult prSelRes = ed.SelectWindow(points[0], points[1]);
if (prSelRes.Status == PromptStatus.OK)
{
using (SelectionSet ss = prSelRes.Value)
{
if (ss != null)
ed.WriteMessage("\nThe SS is good and has {0} entities.", ss.Count);
else
ed.WriteMessage("\nThe SS is bad!");
}
}
else
ed.WriteMessage("\nSelection failed!");
}
}
catch (System.Exception ex)
{
ed.WriteMessage(Environment.NewLine + ex.ToString());
}
}
The above code is self explainable enough. In terms of the code of the PointCollector, we have demonstrated about it a few times before so haven’t appended it here for brevity. Please refer to earlier posts for details about it.
Here is how the command behaves in AutoCAD.
As can be seen, the fence is displayed nicely with the proper CONTRAST graphics drawing mode (specifically using the TransientDrawingMode.Contrast
enum in the TransientManager.CurrentTransientManager.AddTransient call) which is chosen automatically by the PointCollector. In addition, as also can be seen, the PointCollector honors UCS perfectly.
Readers may notice that our SelectWinow behavior is still different a bit from the Window selection option of some AutoCAD commands such as Select and Move in recent versions in that the AutoCAD will show some fill-out (hatch maybe?) with some background color (light blue maybe?) with some transparency (50% maybe?) inside the window area. We can make the same happen in the future if necessary, but for now things should be easy and good enough.
After pressing the ENTER key to finish the fence picking, we may get some output as follows in the command line window:
Command: WINDOWSELECTIONANDPOINTCOLLECTOR
The 1st corner:
The 2nd corner:
The SS is good and has 5 entities.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides project wizards in C#, VB.NET and CLI/Managed C++, and various item wizards such as Event Handlers, Command/LispFunction Definers, and Entity/Draw Jiggers in both C# and VB.NET, to help program AutoCAD addins.
Posted by: |