Skip to content

Jig EntityJig

Most Basic type of Entity Jig

 public static class Commands
    {
        [CommandMethod("Test", CommandFlags.UsePickSet)]
        public static void Test()
        {
            try
            {
                using (Transaction transaction = ActiveUtil.Document.TransactionManager.StartTransaction())
                {
                    var text = new CadText(new Geometry.Entities.Point2D(), "Test")
                    {
                        TextHeight = 100,
                        TextStyle = "ARIAL"
                    };
                    text.Append();

                    //Code for jig
                    var textJig = new InsertTextJig(text.Entity);

                    // Loop as we run our jig, as we may have keywords
                    PromptStatus stat = PromptStatus.Keyword;

                    while (stat == PromptStatus.Keyword)
                    {
                        PromptResult jigResult = ActiveUtil.Editor.Drag(textJig);
                        stat = jigResult.Status;
                        if (stat != PromptStatus.OK && stat != PromptStatus.Keyword)
                        {
                            return;
                        }
                    }

                    transaction.Commit();
                }
            }
            catch (System.Exception ex)
            {
                Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");
            }
        }
    }

    public class InsertTextJig : EntityJig
    {
        private Point3d _position;

        public InsertTextJig(Entity entity) : base(entity)
        {
        }

        // Method for user interaction with entity
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigPointPrompt = new JigPromptPointOptions("\nPosition of text");
            jigPointPrompt.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted | UserInputControls.NoNegativeResponseAccepted | UserInputControls.GovernedByOrthoMode);

            PromptPointResult ppr = prompts.AcquirePoint(jigPointPrompt);
            if (ppr.Status == PromptStatus.OK)
            {
                // Check if it has changed or not (reduces flicker)
                if (_position.DistanceTo(ppr.Value) < Tolerance.Global.EqualPoint)
                {
                    return SamplerStatus.NoChange;
                }
                _position = ppr.Value;
                return SamplerStatus.OK;
            }
            return SamplerStatus.Cancel;
        }

        // Method to Update AutoCAD Entity depending on user interaction
        protected override bool Update()
        {
            DBText txt = (DBText)Entity;
            txt.Position = _position;
            return true;
        }
    }
public static class Commands
    {
        [CommandMethod("Test", CommandFlags.UsePickSet)]
        public static void Test()
        {
            try
            {
                using (Transaction transaction = ActiveUtil.Document.TransactionManager.StartTransaction())
                {
                    var circle = new CadCircle(new Geometry.Entities.Point2D(),50);
                    circle.Append();

                    //Code for jig
                    var textJig = new EntityPositionJig(circle.Entity);

                    // Loop as we run our jig, as we may have keywords
                    PromptStatus stat = PromptStatus.Keyword;

                    while (stat == PromptStatus.Keyword)
                    {
                        PromptResult jigResult = ActiveUtil.Editor.Drag(textJig);
                        stat = jigResult.Status;
                        if (stat != PromptStatus.OK && stat != PromptStatus.Keyword)
                        {
                            return;
                        }
                    }

                    transaction.Commit();
                }
            }
            catch (System.Exception ex)
            {
                Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");
            }
        }
    }

    public class EntityPositionJig : EntityJig
    {
        private Point3d _position;

        public EntityPositionJig(Entity entity) : base(entity)
        {
        }

        // Method for user interaction with entity
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigPointPrompt = new JigPromptPointOptions("\nPosition of entity");
            jigPointPrompt.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted | UserInputControls.NoNegativeResponseAccepted | UserInputControls.GovernedByOrthoMode);

            PromptPointResult ppr = prompts.AcquirePoint(jigPointPrompt);
            if (ppr.Status == PromptStatus.OK)
            {
                // Check if it has changed or not (reduces flicker)
                if (_position.DistanceTo(ppr.Value) < Tolerance.Global.EqualPoint)
                {
                    return SamplerStatus.NoChange;
                }
                _position = ppr.Value;
                return SamplerStatus.OK;
            }
            return SamplerStatus.Cancel;
        }

        // Method to Update AutoCAD Entity depending on user interaction
        protected override bool Update()
        {
            Circle entity = (Circle)Entity;
            entity.Center = _position;
            return true;
        }
    }

Code to get user input with custom autocad graphics

    public class Main
    {
        [CommandMethod("TEST")]
        public void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect line: ");
            options.SetRejectMessage("\nSelected entity is not a line.");
            options.AddAllowedClass(typeof(Line), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var axis = (Line)tr.GetObject(result.ObjectId, OpenMode.ForRead);
                using (var perp = new Line(Point3d.Origin, axis.GetClosestPointTo(Point3d.Origin, true)))
                {
                    var jig = new PerpendicularJig(perp, axis, ed);
                    var pr = ed.Drag(jig);
                    if (pr.Status == PromptStatus.OK)
                    {
                        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        curSpace.AppendEntity(perp);
                        tr.AddNewlyCreatedDBObject(perp, true);
                    }
                }
                tr.Commit();
            }
        }
    }

    internal class PerpendicularJig : EntityJig
    {
        private Point3d dragPoint;
        Line axis, perp;
        private Vector3d axisDir;
        Editor ed;

        public PerpendicularJig(Line perp, Line axis, Editor ed) : base(perp)
        {
            this.axis = axis;
            this.perp = perp;
            axisDir = axis.StartPoint.GetVectorTo(axis.EndPoint);
            this.ed = ed;
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            var options = new JigPromptPointOptions("\nSpecify a point: ");
            options.UserInputControls = UserInputControls.Accept3dCoordinates;
            var result = prompts.AcquirePoint(options);
            if (result.Value.IsEqualTo(dragPoint))
                return SamplerStatus.NoChange;
            dragPoint = result.Value;
            return SamplerStatus.OK;
        }

        protected override bool Update()
        {
            using (var view = ed.GetCurrentView())
            {
                var viewDir = view.ViewDirection;
                perp.StartPoint = dragPoint;
                perp.EndPoint = axis.GetClosestPointTo(dragPoint, viewDir, true);
                var xform = Matrix3d.WorldToPlane(viewDir);
                var v1 = axisDir.ProjectTo(viewDir, viewDir);
                var v2 = perp.EndPoint.GetVectorTo(dragPoint).ProjectTo(viewDir, viewDir);
                perp.ColorIndex = v1.GetAngleTo(v2, viewDir) < Math.PI ? 1 : 3;
            }
            return true;
        }
    }

Entity Jig to insert new Text

 public static class Commands
    {
        [CommandMethod("QT")]
        static public void QuickText()
        {
            PromptStringOptions pso = new PromptStringOptions("\nEnter text string");
            pso.AllowSpaces = true;

            PromptResult pr = ActiveUtil.Editor.GetString(pso);
            if (pr.Status != PromptStatus.OK)
            {
                return;
            }

            using (Transaction transaction = ActiveUtil.TransactionManager.StartTransaction())
            {
                // Create the text object, set its normal and contents
                DBText txt = new DBText
                {
                    Normal = ActiveUtil.Editor.CurrentUserCoordinateSystem.CoordinateSystem3d.Zaxis,
                    TextString = pr.StringResult
                };

                // We'll add the text to the database before jigging
                // it - this allows alignment adjustments to be
                // reflected

                ActiveUtil.Database.GetCurrentSpace(OpenMode.ForWrite).AppendEntity(txt);
                transaction.AddNewlyCreatedDBObject(txt, true);

                // Create our jig
                TextPlacementJig pj = new TextPlacementJig(txt);

                // Loop as we run our jig, as we may have keywords
                PromptStatus stat = PromptStatus.Keyword;

                while (stat == PromptStatus.Keyword)
                {
                    PromptResult res = ActiveUtil.Editor.Drag(pj);
                    stat = res.Status;
                    if (stat != PromptStatus.OK && stat != PromptStatus.Keyword)
                    {
                        return;
                    }
                }
                transaction.Commit();
            }
        }
    }

    internal class TextPlacementJig : EntityJig
    {
        // Declare some internal state
        private Point3d _position;

        private double _angle, _txtSize;

        // Constructor
        public TextPlacementJig(Entity ent) : base(ent)
        {
            _angle = 0;
            _txtSize = 1;
        }

        protected override SamplerStatus Sampler(JigPrompts jp)
        {
            // We acquire a point but with keywords
            JigPromptPointOptions po = new JigPromptPointOptions("\nPosition of text");

            po.UserInputControls =
              (UserInputControls.Accept3dCoordinates |
                UserInputControls.NullResponseAccepted |
                UserInputControls.NoNegativeResponseAccepted |
                UserInputControls.GovernedByOrthoMode);

            po.SetMessageAndKeywords("\nSpecify position of text or [Bold/Italic/Larger/Smaller/Rotate90]: ", "Bold Italic Larger Smaller Rotate90");

            PromptPointResult ppr = jp.AcquirePoint(po);
            if (ppr.Status == PromptStatus.Keyword)
            {
                switch (ppr.StringResult)
                {
                    case "Bold":
                        {
                            // TODO
                            break;
                        }

                    case "Italic":
                        {
                            // TODO
                            break;
                        }

                    case "Larger":
                        {
                            // Multiple the text size by two
                            _txtSize *= 2;
                            break;
                        }

                    case "Smaller":
                        {
                            // Divide the text size by two
                            _txtSize /= 2;
                            break;
                        }

                    case "Rotate90":
                        {
                            // To rotate clockwise we subtract 90 degrees and
                            // then normalise the angle between 0 and 360
                            _angle -= Math.PI / 2;
                            while (_angle < Math.PI * 2)
                            {
                                _angle += Math.PI * 2;
                            }
                            break;
                        }
                }

                return SamplerStatus.OK;
            }
            else if (ppr.Status == PromptStatus.OK)
            {
                // Check if it has changed or not (reduces flicker)
                if (_position.DistanceTo(ppr.Value) < Tolerance.Global.EqualPoint)
                {
                    return SamplerStatus.NoChange;
                }

                _position = ppr.Value;

                return SamplerStatus.OK;
            }

            return SamplerStatus.Cancel;
        }

        protected override bool Update()
        {
            // Set properties on our text object
            DBText txt = (DBText)Entity;
            txt.Position = _position;
            txt.Height = _txtSize;
            txt.Rotation = _angle;
            return true;
        }
    }

Sample Code to Draw Line using Entity Jig

public static class Commands
{
    [CommandMethod("Test")]
    public static void TestEntityJigger12_Method()
    {
        if (LineJigger.Jig())
        {
            ActiveUtil.Editor.WriteMessage("\nA line segment has been successfully jigged and added to the database.\n");
        }
        else
        {
            ActiveUtil.Editor.WriteMessage("\nIt failed to jig and add a line segment to the database.\n");
        }
    }
}

  public class LineJigger : EntityJig
  {
      public Point3d mEndPoint = new Point3d();

      public LineJigger(Line ent) : base(ent)
      {
      }

      protected override bool Update()
      {
          (Entity as Line).EndPoint = mEndPoint;

          return true;
      }

      protected override SamplerStatus Sampler(JigPrompts prompts)
      {
          JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nNext point:");
          prOptions1.BasePoint = (Entity as Line).StartPoint;
          prOptions1.UseBasePoint = true;
          prOptions1.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.AnyBlankTerminatesInput
              | UserInputControls.GovernedByOrthoMode | UserInputControls.GovernedByUCSDetect | UserInputControls.UseBasePointElevation
              | UserInputControls.InitialBlankTerminatesInput | UserInputControls.NullResponseAccepted;
          PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
          if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;

          if (prResult1.Value.Equals(mEndPoint))
          {
              return SamplerStatus.NoChange;
          }
          else
          {
              mEndPoint = prResult1.Value;
              return SamplerStatus.OK;
          }
      }

      public static bool Jig()
      {
          try
          {
              Database db = HostApplicationServices.WorkingDatabase;

              PromptPointResult ppr = ActiveUtil.Editor.GetPoint("\nStart point");
              if (ppr.Status != PromptStatus.OK) return false;

              Point3d pt = ppr.Value;
              Line ent = new Line(pt, pt);
              ent.TransformBy(ActiveUtil.Editor.CurrentUserCoordinateSystem);
              LineJigger jigger = new LineJigger(ent);
              PromptResult pr = ActiveUtil.Editor.Drag(jigger);

              if (pr.Status == PromptStatus.OK)
              {
                  using (Transaction tr = db.TransactionManager.StartTransaction())
                  {
                      BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                      BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                      btr.AppendEntity(jigger.Entity);
                      tr.AddNewlyCreatedDBObject(jigger.Entity, true);
                      tr.Commit();
                  }
              }
              else
              {
                  ent.Dispose();
                  return false;
              }

              return true;
          }
          catch
          {
              return false;
          }
      }
  }

Sample Code to Draw entire circle using entity jig

public static class Commands
  {
      [CommandMethod("Test", CommandFlags.UsePickSet)]
      public static void Test()
      {
          if (CircleJig.Jig())
          {
              ActiveUtil.Editor.WriteLine("A circle has been successfully jigged and added to the database.\n");
          }
          else
          {
              ActiveUtil.Editor.WriteLine("\nIt failed to jig and add a circle to the database.\n");
          }
      }
  }

public class CircleJig : EntityJig
{
    private Circle _circle;
    private Point3d _centerPoint = new Point3d();
    private double _radius = 0.001;
    public int _mCurJigFactorNumber = 1;

    public CircleJig(Entity ent) : base(ent)
    {
        _circle = (Circle)ent;
        _circle.Radius = _radius;
        _circle.Center = _centerPoint;
    }

    internal static bool Jig()
    {
        try
        {
            Circle ent = new Circle();
            CircleJig jigger = new CircleJig(ent);

            PromptResult pr;
            do
            {
                pr = ActiveUtil.Editor.Drag(jigger);
                jigger._mCurJigFactorNumber++;
            } while (pr.Status != PromptStatus.Cancel && jigger._mCurJigFactorNumber <= 2);

            if (pr.Status != PromptStatus.Cancel)
            {
                using (Transaction tr = ActiveUtil.TransactionManager.StartTransaction())
                {
                    ActiveUtil.Database.GetModelSpace(OpenMode.ForWrite).AppendEntity(jigger.Entity);
                    tr.AddNewlyCreatedDBObject(jigger.Entity, true);
                    tr.Commit();
                }
            }
        }
        catch
        {
            return false;
        }
        return true;
    }

    protected override SamplerStatus Sampler(JigPrompts prompts)
    {
        switch (_mCurJigFactorNumber)
        {
            case 1:
                JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nCircle center:");
                PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
                if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;

                if (prResult1.Value.Equals(_centerPoint))
                {
                    return SamplerStatus.NoChange;
                }
                else
                {
                    _centerPoint = prResult1.Value;
                    return SamplerStatus.OK;
                }
            case 2:
                JigPromptDistanceOptions prOptions2 = new JigPromptDistanceOptions("\nCircle radius:");
                prOptions2.BasePoint = _centerPoint;
                PromptDoubleResult prResult2 = prompts.AcquireDistance(prOptions2);
                if (prResult2.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;

                if (prResult2.Value.Equals(_radius))
                {
                    return SamplerStatus.NoChange;
                }
                else
                {
                    if (prResult2.Value < 0.0001)  // To avoid the degeneration problem!
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        _radius = prResult2.Value;
                        return SamplerStatus.OK;
                    }
                }
            default:
                break;
        }
        return SamplerStatus.OK;
    }

    protected override bool Update()
    {
        switch (_mCurJigFactorNumber)
        {
            case 1:
                (Entity as Circle).Center = _centerPoint;
                break;

            case 2:
                (Entity as Circle).Radius = _radius;
                break;

            default:
                return false;
        }
        return true;
    }
}