Various Addin/API wizards, coders, widgets and examples are provided to help program AutoCAD .NET addins.

09/19/2012

AutoCAD .NET: Point Collector and Regular Polygon Pick

To make entity selections, we need to collect some points to make a fence, window, polygon, or so.

We can specify the UseBasePoint, BasePoint, and UseDashedLine properties of the PromptPointOptions for the Editor.GetPoint() method to connect the current cursor position with the previous point input, as we did to the Fence Selection previously, but it’s far from user friendly, especially for selection options like window and polygon. Even for the simple fence option, only the last two point picks can be connected through the hard coded dashed line by the GetPoint() call.

We created a nice little coder, Point Collector, to collect points visually to form a window, a fence, and a polygon before. Let us extend it to support regular polygon pick as well in this post.

Here is a screenshot to show how it behaves in AutoCAD.As can be seen, the command runs well in a UCS, indicating our Point Collector horrors UCS perfectly. As mentioned before, if some code/command works fine with UCS we can be sure that it works fine with WCS too as WCS is nothing but a special UCS, but vise is not versa!

So, to repeat one more time, it is a good practice to always test code and command in UCS instead of WCS because nothing reasonable enough should prevent users from doing the same thing in some UCS, which is supposed to provide them great flexibility and convenience instead of confusion or trouble.

By the way, the points returned by the CollectedPoints collection of the PointCollector are in UCS. We can convert them into WCS with a little additional effort for sure when necessary. We have demonstrated how to do so many times before.

Apart from this nice feature, a lot of other good stuffs are also in such a succinct but cool coder. Here are a few more.

• The PointCollector is disposable and when its Dispose() method is called things will be cleaned up properly in a single place.• We’d better use using to create the PointCollector to better utilize the above feature as we always do to the AutoCAD .NET Transaction creations.• Though the EntityJig or DrawJig can be used to jig the graphics, as we did a few times before, we use the Transient Graphics here to draw temporarily since it’s obvious that either the EntityJig or the DrawJig is over killed.• To draw some transient graphics onto the AutoCAD drawing area is pretty simple, through calling the TransientManager.CurrentTransientManager.AddTransient() method which needs a Drawable object and a few other parameters to specify the drawing conditions.• Please don’t forget to remove the Transient Graphics (through calling the method TransientManager.CurrentTransientManager.EraseTransient) in the next draw or when all are done. Otherwise AutoCAD may crash.• An Editor.PointMonitor event is planted into the system at right time to monitor the cursor positions so as to draw the temporary (transient) graphics.• The PointMonitorEventArgs.Context.ComputedPoint is used instead of the PointMonitorEventArgs.Context.RawPoint to honor object snapping, ortho-mode and so on.• Though the Editor.GetPoint() call returns a point in UCS, the PointMonitorEventArgs.Context.ComputedPoint is in WCS. That is why we have to do some transformations to make them consistent with each other so that the Transient Graphics can appear good in both WCS and UCS.• The APIs of removing and updating Polyline vertices are pretty tricky so we don’t use them at all here.• We only call the Polyline.AddVertexAt() method to both make our life a bit easier and avoid the database or the transient graphics system being messed up.• The PointCollector currently supports Window, Fence, Polygon, and Regular Polygon shapes.• Last but not least, the PointCollector can be easily extended to support other shapes, e.g. Circular one. We may demonstrate it in future. Please stay tuned.

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.