AutoCAD .NET: The Simplest but Still Quite Functional Line Jig from DrawJig

嘎子 AutoCAD二次开发, 转载 2016-02-26 4,676 次浏览 , , , 没有评论

AutoCAD .NET API provides two concrete Jig classes for us to jig different graphics in different circumstances, EntityJig and DrawJig. EntityJig is generally used to jig a specific entity as its name indicates and the DrawJig is to jig anything that has graphics to draw, which can be a single entity, a group of entities, or something that is not available natively in AutoCAD.

We demonstrated many times before using both EntityJig and DrawJig to create jigs for kind of stuffs, including jigging graphics of either AutoCAD native entities or something brand new (e.g. Line, XLine, Multiple Line, Polyline, Spline, Circle, Text/DBText/MText/PowerText, Block/Insert, Attributes, Dynamic Dimension, Solid Wedge/Cone/Cylinder/Sphere/Cube/Torus/Tetrahedron/Pyramid,  Rectangle, Centric Circles, Nested Inscribed Squares and Circles, and Revision Cloud ), recreating various AutoCAD commands or inventing some totally new behavior  (e.g. Copy, Move, Rotate, Scale, Mirror, Circular Array, Rectangular Array, and Scribble), and interoperating with other user interaction technologies while jigging (e.g. keyword input, color index or true color specifying,  mouse wheel monitoring, keyboard stroke inspection, etc.).

To pursue perfectionism and professionalism, those jig code examples all try to cover as detail as possible and address every issue that could be thought of, e.g. considering various input options, input errors, and user escaping out; honoring UCS; reducing flicking; increasing performance; adding entities to database; committing or aborting transactions at different situations; disposing and clean-up.  All these are all good practices and deserve to invest time and effort into for sure, however, the code there may scare away some people like AutoCAD programming hobbyists with so many factors being cared about, though the code has already been made as succinct as possible.

In this post, let us create the simplest but still functional Line Jig from the DrawJig to meet such a need. Here it is.

    public class LineJig : Autodesk.AutoCAD.EditorInput.DrawJig
        public Autodesk.AutoCAD.Geometry.Point3d StartPoint { get; set; }
        public Autodesk.AutoCAD.Geometry.Point3d EndPoint { get; set; }
        public Autodesk.AutoCAD.EditorInput.Editor MgdEditor { get { return Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; } }
        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
            return draw.Geometry.WorldLine(StartPoint, EndPoint);
        protected override Autodesk.AutoCAD.EditorInput.SamplerStatus Sampler(Autodesk.AutoCAD.EditorInput.JigPrompts prompts)
            EndPoint = prompts.AcquirePoint(“nEnd point”).Value;
            return Autodesk.AutoCAD.EditorInput.SamplerStatus.OK;
        public void TestLineJig_Method()
            StartPoint = MgdEditor.GetPoint(“Start point”).Value.TransformBy(MgdEditor.CurrentUserCoordinateSystem);
            MgdEditor.WriteMessage(string.Format(“nLine with start point {0} and end point {1} has been jigged.”, StartPoint, EndPoint));

The LineJig sample has only about two dozens of lines of code (including empty and single bracket lines!) but works quite well in both WCS and UCS. So, the purpose of brevity here does not excuse losing fundamental features or being buggy. We simplify things as much as possible in this case, but the UCS support is not something we should sacrifice as it’s so basic and important. Here is what the screen looks like when the command is running.

The Hello Jig (if this sounds a nicer name) code and command functions well in normal circumstances. At least, it will not crash your AutoCAD. Of course, please feel free to add code little by little to the LineJig to address anything else of concern.

The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides Entity Jigger and Draw Jigger coders to help create jig code automatically, quickly and reliably.

原创文章,文章首发于:Riley Ge (@rileyge) — Steemit

原创文章,转载请注明: 转载自TsonTec:测量解决方案提供者

本文链接地址: AutoCAD .NET: The Simplest but Still Quite Functional Line Jig from DrawJig