Creating a multileader in AutoCAD using a jig from .NET

20 篇文章 0 订阅


I'm now back from a fantastic break in Italy and am trying hard to catch back up. Next week I'm off again to San Diego (work, this time), which may cause further interruptions in blog postings.

This question came through from Genésio from Brazil:

I wish jig a leader with an bubble in the and of the leader, at the same time. Can you help me. Perhaps post the solution in your blog (through the interface).

It took me a while - frustratingly long, in fact, and probably this is not exactly what Genésio is after - but here's what I managed to come up with. The "bubble" is framed MText, but it should be modifiable to use a classic bubble block, instead. I drew heavily on this previous post for the jig code.

The positioning of the text took some work, but I'm reasonably happy with the results. If anyone has tweaks to suggest, please post a comment.

Here's the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Geometry;


namespace DimensionLibrary

{

  public class DimensionCmds

  {

    class MLeaderJig : EntityJig

    {

      Point3dCollection m_pts;

      Point3d m_tempPoint;

      string m_contents;

      int m_leaderIndex;

      int m_leaderLineIndex;


      public MLeaderJig(string contents)

        : base(new MLeader())

      {

        // Store the string passed in


        m_contents = contents;


        // Create a point collection to store our vertices


        m_pts = new Point3dCollection();


        // Create mleader and set defaults


        MLeader ml = Entity as MLeader;

        ml.SetDatabaseDefaults();


        // Set up the MText contents


        ml.ContentType = ContentType.MTextContent;

        MText mt = new MText();

        mt.SetDatabaseDefaults();

        mt.Contents = m_contents;

        ml.MText = mt;

        ml.TextAlignmentType =

          TextAlignmentType.LeftAlignment;

        ml.TextAttachmentType =

          TextAttachmentType.AttachmentMiddle;


        // Set the frame and landing properties


        ml.EnableDogleg = true;

        ml.EnableFrameText = true;

        ml.EnableLanding = true;


        // Reduce the standard landing gap


        ml.LandingGap = 0.05;


        // Add a leader, but not a leader line (for now)


        m_leaderIndex = ml.AddLeader();

        m_leaderLineIndex = -1;

      }


      protected override SamplerStatus Sampler(

        JigPrompts prompts

      )

      {

        JigPromptPointOptions opts =

          new JigPromptPointOptions();


        // Not all options accept null response

        opts.UserInputControls =

          (UserInputControls.Accept3dCoordinates |

          UserInputControls.NoNegativeResponseAccepted

          );


        // Get the first point

        if (m_pts.Count == 0)

        {

          opts.UserInputControls |=

            UserInputControls.NullResponseAccepted;

          opts.Message =

            "/nStart point of multileader: ";

          opts.UseBasePoint = false;

        }

        // And the second

        else if (m_pts.Count == 1)

        {

          opts.BasePoint = m_pts[m_pts.Count - 1];

          opts.UseBasePoint = true;

          opts.Message =

            "/nSpecify multileader vertex: ";

        }

        // And subsequent points

        else if (m_pts.Count > 1)

        {

          opts.UserInputControls |=

            UserInputControls.NullResponseAccepted;

          opts.BasePoint = m_pts[m_pts.Count - 1];

          opts.UseBasePoint = true;

          opts.SetMessageAndKeywords(

            "/nSpecify multileader vertex or [End]: ",

            "End"

          );

        }

        else // Should never happen

          return SamplerStatus.Cancel;


        PromptPointResult res =

          prompts.AcquirePoint(opts);


        if (res.Status == PromptStatus.Keyword)

        {

          if (res.StringResult == "End")

          {

            return SamplerStatus.Cancel;

          }

        }


        if (m_tempPoint == res.Value)

        {

          return SamplerStatus.NoChange;

        }

        else if (res.Status == PromptStatus.OK)

        {

          m_tempPoint = res.Value;

          return SamplerStatus.OK;

        }

        return SamplerStatus.Cancel;

      }


      protected override bool Update()

      {

        try

        {

          if (m_pts.Count > 0)

          {

            // Set the last vertex to the new value


            MLeader ml = Entity as MLeader;

            ml.SetLastVertex(

              m_leaderLineIndex,

              m_tempPoint

            );


            // Adjust the text location


            Vector3d dogvec =

              ml.GetDogleg(m_leaderIndex);

            double doglen =

              ml.DoglegLength;

            double landgap =

              ml.LandingGap;

            ml.TextLocation =

              m_tempPoint +

              ((doglen + landgap) * dogvec);

          }

        }

        catch (System.Exception ex)

        {

          Document doc =

            Application.DocumentManager.MdiActiveDocument;

          doc.Editor.WriteMessage(

            "/nException: " + ex.Message

          );

          return false;

        }

        return true;

      }


      public void AddVertex()

      {

        MLeader ml = Entity as MLeader;


        // For the first point...


        if (m_pts.Count == 0)

        {

          // Add a leader line


          m_leaderLineIndex =

            ml.AddLeaderLine(m_leaderIndex);


          // And a start vertex


          ml.AddFirstVertex(

            m_leaderLineIndex,

            m_tempPoint

          );


          // Add a second vertex that will be set

          // within the jig


          ml.AddLastVertex(

            m_leaderLineIndex,

            new Point3d(0, 0, 0)

          );

        }

        else

        {

          // For subsequent points,

          // just add a vertex


          ml.AddLastVertex(

            m_leaderLineIndex,

            m_tempPoint

          );

        }


        // Reset the attachment point, otherwise

        // it seems to get forgotten


        ml.TextAttachmentType =

          TextAttachmentType.AttachmentMiddle;


        // Add the latest point to our history


        m_pts.Add(m_tempPoint);

      }


      public void RemoveLastVertex()

      {

        // We don't need to actually remove

        // the vertex, just reset it


        MLeader ml = Entity as MLeader;

        if (m_pts.Count >= 1)

        {

          Vector3d dogvec =

            ml.GetDogleg(m_leaderIndex);

          double doglen =

            ml.DoglegLength;

          double landgap =

            ml.LandingGap;

          ml.TextLocation =

            m_pts[m_pts.Count - 1] +

            ((doglen + landgap) * dogvec);

        }

      }


      public Entity GetEntity()

      {

        return Entity;

      }

    }


    [CommandMethod("MYML")]

    public void MyMLeaderJig()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

      Database db = doc.Database;


      // Get the text outside of the jig


      PromptStringOptions pso =

        new PromptStringOptions(

          "/nEnter text: "

        );

      pso.AllowSpaces = true;

      PromptResult pr =

        ed.GetString(pso);

      if (pr.Status == PromptStatus.OK)

      {

        // Create MleaderJig


        MLeaderJig jig =

          new MLeaderJig(pr.StringResult);


        // Loop to set vertices


        bool bSuccess = true, bComplete = false;

        while (bSuccess && !bComplete)

        {

          PromptResult dragres = ed.Drag(jig);

          bSuccess =

            (dragres.Status == PromptStatus.OK);

          if (bSuccess)

            jig.AddVertex();

          bComplete =

            (dragres.Status == PromptStatus.None);

          if (bComplete)

            jig.RemoveLastVertex();

        }


        if (bComplete)

        {

          // Append entity


          Transaction tr =

            db.TransactionManager.StartTransaction();

          using (tr)

          {

            BlockTable bt =

              (BlockTable)tr.GetObject(

                db.BlockTableId,

                OpenMode.ForRead,

                false

              );

            BlockTableRecord btr =

              (BlockTableRecord)tr.GetObject(

                bt[BlockTableRecord.ModelSpace],

                OpenMode.ForWrite,

                false

              );

            btr.AppendEntity(jig.GetEntity());

            tr.AddNewlyCreatedDBObject(

              jig.GetEntity(),

              true

            );

            tr.Commit();

          }

        }

      }

    }

  }

}

Here's what you get when you run the MYML command:

Jigged_mleader

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值