Using JIG with Civil 3D Labels

By Augusto Goncalves

This is based on Kean’s original post for AutoCAD entities, but adjusted to Move (instead Rotate) and using the Label specific property: LabelLocation.

Some background: JIG is an AutoCAD API feature to manipulate objects on screen, but with the ability to actually drag them around (rotate, move, scale, adjust) while seeing the object.

For labels, the final behavior is interesting as we can see the pointer adjusting while the location is also dragged with the cursor.

Enough said, here is the sample code.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;

using Autodesk.Civil.DatabaseServices;

namespace SampleTesting
{
  public class MovelLabelJig : EntityJig
  {
    Point3d _location;

    public MovelLabelJig(Label lbl)
      : base(lbl.Clone() as Label)
    {
      _location = lbl.LabelLocation;
    }

    protected override bool Update()
    {
      // adjust the entity on screen
      ((Label)Entity).LabelLocation = _location;
      return true;
    }

    protected override SamplerStatus Sampler(
      JigPrompts prompts)
    {
      // get the new location (on cursor move)
      JigPromptPointOptions jppo =
        new JigPromptPointOptions("Move label: ");
      PromptPointResult pdr = prompts.AcquirePoint(jppo);

      // check if is OK
      if (pdr.Status != PromptStatus.OK)
        return SamplerStatus.Cancel;
      if (pdr.Value.DistanceTo(_location)
        < Tolerance.Global.EqualPoint)
        return SamplerStatus.NoChange;

      // and store the new valuye
      _location = pdr.Value;
      return SamplerStatus.OK;
    }

    /// <summary>
    /// Return the new location, during/after JIG
    /// </summary>
    public Point3d Location { get { return _location; } }

    [CommandMethod("moveLabel")]
    public static void CmdMoveLabel()
    {
      Editor ed = Application.DocumentManager
        .MdiActiveDocument.Editor;

      // select the label
      PromptEntityOptions peo =
        new PromptEntityOptions("\nSelect a label to move: ");
      peo.SetRejectMessage("\nLabels only");
      peo.AddAllowedClass(typeof(Label), false);
      PromptEntityResult per = ed.GetEntity(peo);
      if (per.Status != PromptStatus.OK) return;

      // start a transaction
      Database db = Application.DocumentManager
        .MdiActiveDocument.Database;
      using (Transaction trans = db.
        TransactionManager.StartTransaction())
      {
        // open the label object
        Label lbl = trans.GetObject(per.ObjectId,
          OpenMode.ForRead) as Label;

        // start the JIG drag
        MovelLabelJig jig = new MovelLabelJig(lbl);
        PromptResult pr = ed.Drag(jig);

        // if not OK, clean return
        if (pr.Status != PromptStatus.OK) return;

        // everything ok, update the location
        lbl.UpgradeOpen();
        lbl.LabelLocation = jig.Location;

        trans.Commit();
      }
    }
  }
}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading