EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

萧闫子 发表于 2014-1-8 16:36:23

[即时预览] 动态创建引线



Creating a multileader in AutoCAD using a jig from .NET
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;
[*]          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;
[*]          opts.UseBasePoint = true;
[*]          opts.SetMessageAndKeywords(
[*]            "\nSpecify multileader vertex or : ",
[*]            "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 +
[*]            ((doglen + landgap) * dogvec);
[*]      }
[*]      }
[*]      public Entity GetEntity()
[*]      {
[*]      return Entity;
[*]      }
[*]    }
[*]   
[*]    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,
[*]                OpenMode.ForWrite,
[*]                false
[*]            );
[*]            btr.AppendEntity(jig.GetEntity());
[*]            tr.AddNewlyCreatedDBObject(
[*]            jig.GetEntity(),
[*]            true
[*]            );
[*]            tr.Commit();
[*]          }
[*]      }
[*]      }
[*]    }
[*]}
[*]}




老朽 发表于 2014-3-14 11:21:22

(*^__^*) 嘻嘻……

鹦鹉仔 发表于 2014-3-14 11:18:24

(*^__^*) 嘻嘻……

楚客 发表于 2014-3-10 13:23:30

(*^__^*) 嘻嘻……

AK47 发表于 2014-2-13 10:00:57

感谢楼主分享

龙龙..! 发表于 2014-2-18 12:07:59

(*^__^*) 嘻嘻……

龙龙..! 发表于 2014-3-11 11:06:35

楼主辛苦……

沧海冷月 发表于 2014-3-11 11:11:00

楼主辛苦……

大洪p1938 发表于 2014-3-11 11:16:38

谢谢BIM大神…

大奔KY 发表于 2014-3-11 11:22:17

顶!!!!!!!!!!

拉登 发表于 2014-3-14 11:31:14

顶起来…………

dgren 发表于 2014-3-14 11:32:02

路过!!!
不发表意见……

飞天舞 发表于 2014-5-13 10:17:37

顶!!!!!!!!!!

入樽 发表于 2014-5-13 10:22:56

顶......
楼下跟上.....

冰山 发表于 2014-5-13 10:30:07

路过!!!
帮顶……
页: [1] 2
查看完整版本: [即时预览] 动态创建引线