明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖

[Kean专集] Kean专题(1)—Jigs

   关闭 [复制链接]
 楼主| 发表于 2009-5-25 16:10:00 | 显示全部楼层
七、动态创建引线
September 21, 2007
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:
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.AutoCAD.Geometry;
  6. namespace DimensionLibrary
  7. {
  8.   public class DimensionCmds
  9.   {
  10.     class MLeaderJig : EntityJig
  11.     {
  12.       Point3dCollection m_pts;
  13.       Point3d m_tempPoint;
  14.       string m_contents;
  15.       int m_leaderIndex;
  16.       int m_leaderLineIndex;
  17.       public MLeaderJig(string contents)
  18.         : base(new MLeader())
  19.       {
  20.         // Store the string passed in
  21.         m_contents = contents;
  22.         // Create a point collection to store our vertices
  23.         m_pts = new Point3dCollection();
  24.         // Create mleader and set defaults
  25.         MLeader ml = Entity as MLeader;
  26.         ml.SetDatabaseDefaults();
  27.         // Set up the MText contents
  28.         ml.ContentType = ContentType.MTextContent;
  29.         MText mt = new MText();
  30.         mt.SetDatabaseDefaults();
  31.         mt.Contents = m_contents;
  32.         ml.MText = mt;
  33.         ml.TextAlignmentType =
  34.           TextAlignmentType.LeftAlignment;
  35.         ml.TextAttachmentType =
  36.           TextAttachmentType.AttachmentMiddle;
  37.         // Set the frame and landing properties
  38.         ml.EnableDogleg = true;
  39.         ml.EnableFrameText = true;
  40.         ml.EnableLanding = true;
  41.         // Reduce the standard landing gap
  42.         ml.LandingGap = 0.05;
  43.         // Add a leader, but not a leader line (for now)
  44.         m_leaderIndex = ml.AddLeader();
  45.         m_leaderLineIndex = -1;
  46.       }
  47.       protected override SamplerStatus Sampler(
  48.         JigPrompts prompts
  49.       )
  50.       {
  51.         JigPromptPointOptions opts =
  52.           new JigPromptPointOptions();
  53.         // Not all options accept null response
  54.         opts.UserInputControls =
  55.           (UserInputControls.Accept3dCoordinates |
  56.           UserInputControls.NoNegativeResponseAccepted
  57.           );
  58.         // Get the first point
  59.         if (m_pts.Count == 0)
  60.         {
  61.           opts.UserInputControls |=
  62.             UserInputControls.NullResponseAccepted;
  63.           opts.Message =
  64.             "\nStart point of multileader: ";
  65.           opts.UseBasePoint = false;
  66.         }
  67.         // And the second
  68.         else if (m_pts.Count == 1)
  69.         {
  70.           opts.BasePoint = m_pts[m_pts.Count - 1];
  71.           opts.UseBasePoint = true;
  72.           opts.Message =
  73.             "\nSpecify multileader vertex: ";
  74.         }
  75.         // And subsequent points
  76.         else if (m_pts.Count > 1)
  77.         {
  78.           opts.UserInputControls |=
  79.             UserInputControls.NullResponseAccepted;
  80.           opts.BasePoint = m_pts[m_pts.Count - 1];
  81.           opts.UseBasePoint = true;
  82.           opts.SetMessageAndKeywords(
  83.             "\nSpecify multileader vertex or [End]: ",
  84.             "End"
  85.           );
  86.         }
  87.         else // Should never happen
  88.           return SamplerStatus.Cancel;
  89.         PromptPointResult res =
  90.           prompts.AcquirePoint(opts);
  91.         if (res.Status == PromptStatus.Keyword)
  92.         {
  93.           if (res.StringResult == "End")
  94.           {
  95.             return SamplerStatus.Cancel;
  96.           }
  97.         }
  98.         if (m_tempPoint == res.Value)
  99.         {
  100.           return SamplerStatus.NoChange;
  101.         }
  102.         else if (res.Status == PromptStatus.OK)
  103.         {
  104.           m_tempPoint = res.Value;
  105.           return SamplerStatus.OK;
  106.         }
  107.         return SamplerStatus.Cancel;
  108.       }
  109.       protected override bool Update()
  110.       {
  111.         try
  112.         {
  113.           if (m_pts.Count > 0)
  114.           {
  115.             // Set the last vertex to the new value
  116.             MLeader ml = Entity as MLeader;
  117.             ml.SetLastVertex(
  118.               m_leaderLineIndex,
  119.               m_tempPoint
  120.             );
  121.             // Adjust the text location
  122.             Vector3d dogvec =
  123.               ml.GetDogleg(m_leaderIndex);
  124.             double doglen =
  125.               ml.DoglegLength;
  126.             double landgap =
  127.               ml.LandingGap;
  128.             ml.TextLocation =
  129.               m_tempPoint +
  130.               ((doglen + landgap) * dogvec);
  131.           }
  132.         }
  133.         catch (System.Exception ex)
  134.         {
  135.           Document doc =
  136.             Application.DocumentManager.MdiActiveDocument;
  137.           doc.Editor.WriteMessage(
  138.             "\nException: " + ex.Message
  139.           );
  140.           return false;
  141.         }
  142.         return true;
  143.       }
  144.       public void AddVertex()
  145.       {
  146.         MLeader ml = Entity as MLeader;
  147.         // For the first point...
  148.         if (m_pts.Count == 0)
  149.         {
  150.           // Add a leader line
  151.           m_leaderLineIndex =
  152.             ml.AddLeaderLine(m_leaderIndex);
  153.           // And a start vertex
  154.           ml.AddFirstVertex(
  155.             m_leaderLineIndex,
  156.             m_tempPoint
  157.           );
  158.           // Add a second vertex that will be set
  159.           // within the jig
  160.           ml.AddLastVertex(
  161.             m_leaderLineIndex,
  162.             new Point3d(0, 0, 0)
  163.           );
  164.         }
  165.         else
  166.         {
  167.           // For subsequent points,
  168.           // just add a vertex
  169.           ml.AddLastVertex(
  170.             m_leaderLineIndex,
  171.             m_tempPoint
  172.           );
  173.         }
  174.         // Reset the attachment point, otherwise
  175.         // it seems to get forgotten
  176.         ml.TextAttachmentType =
  177.           TextAttachmentType.AttachmentMiddle;
  178.         // Add the latest point to our history
  179.         m_pts.Add(m_tempPoint);
  180.       }
  181.       public void RemoveLastVertex()
  182.       {
  183.         // We don't need to actually remove
  184.         // the vertex, just reset it
  185.         MLeader ml = Entity as MLeader;
  186.         if (m_pts.Count >= 1)
  187.         {
  188.           Vector3d dogvec =
  189.             ml.GetDogleg(m_leaderIndex);
  190.           double doglen =
  191.             ml.DoglegLength;
  192.           double landgap =
  193.             ml.LandingGap;
  194.           ml.TextLocation =
  195.             m_pts[m_pts.Count - 1] +
  196.             ((doglen + landgap) * dogvec);
  197.         }
  198.       }
  199.       public Entity GetEntity()
  200.       {
  201.         return Entity;
  202.       }
  203.     }
  204.     [CommandMethod("MYML")]
  205.     public void MyMLeaderJig()
  206.     {
  207.       Document doc =
  208.         Application.DocumentManager.MdiActiveDocument;
  209.       Editor ed = doc.Editor;
  210.       Database db = doc.Database;
  211.       // Get the text outside of the jig
  212.       PromptStringOptions pso =
  213.         new PromptStringOptions(
  214.           "\nEnter text: "
  215.         );
  216.       pso.AllowSpaces = true;
  217.       PromptResult pr =
  218.         ed.GetString(pso);
  219.       if (pr.Status == PromptStatus.OK)
  220.       {
  221.         // Create MleaderJig
  222.         MLeaderJig jig =
  223.           new MLeaderJig(pr.StringResult);
  224.         // Loop to set vertices
  225.         bool bSuccess = true, bComplete = false;
  226.         while (bSuccess && !bComplete)
  227.         {
  228.           PromptResult dragres = ed.Drag(jig);
  229.           bSuccess =
  230.             (dragres.Status == PromptStatus.OK);
  231.           if (bSuccess)
  232.             jig.AddVertex();
  233.           bComplete =
  234.             (dragres.Status == PromptStatus.None);
  235.           if (bComplete)
  236.             jig.RemoveLastVertex();
  237.         }
  238.         if (bComplete)
  239.         {
  240.           // Append entity
  241.           Transaction tr =
  242.             db.TransactionManager.StartTransaction();
  243.           using (tr)
  244.           {
  245.             BlockTable bt =
  246.               (BlockTable)tr.GetObject(
  247.                 db.BlockTableId,
  248.                 OpenMode.ForRead,
  249.                 false
  250.               );
  251.             BlockTableRecord btr =
  252.               (BlockTableRecord)tr.GetObject(
  253.                 bt[BlockTableRecord.ModelSpace],
  254.                 OpenMode.ForWrite,
  255.                 false
  256.               );
  257.             btr.AppendEntity(jig.GetEntity());
  258.             tr.AddNewlyCreatedDBObject(
  259.               jig.GetEntity(),
  260.               true
  261.             );
  262.             tr.Commit();
  263.           }
  264.         }
  265.       }
  266.     }
  267.   }
  268. }
Here's what you get when you run the MYML command:
 楼主| 发表于 2010-2-3 21:59:00 | 显示全部楼层
八、在Jig中检测shift和control的按键状态
Detecting the use of the shift and control keys during an AutoCAD jig using .NET
Another interesting little problem, that of how to detect the use of  modifier keys during a jig operation (to indicate different jig behaviour). In this case the specific task was to detect the use of the Control and Shift keys, which – if held down during the jig – should cause our object to display differently.
I started with the code from this previous post which uses a DrawJig to place text in the plane of the screen during the jig operation. I initially thought I’d have to use a message filter (as shown in this previous post), but I eventually realised it wasn’t needed: it’s simply a matter of querying the state of System.Windows.Forms.Control.ModifierKeys at the appropriate moment during our WorldDraw().
Here’s the C# code I used:
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.GraphicsInterface;
  6. namespace JigTextPlanarToScreen
  7. {
  8.   public class TextJig : DrawJig
  9.   {
  10.     private Point3d _position;
  11.     public Point3d Position
  12.     {
  13.       get { return _position; }
  14.     }
  15.     // We'll keep our styles alive rather than recreating them
  16.     private TextStyle _normal;
  17.     private TextStyle _highlight;
  18.     public TextJig()
  19.     {
  20.       _normal = new TextStyle();
  21.       _normal.Font =
  22.         new FontDescriptor("Calibri", false, true, 0, 0);
  23.       _normal.TextSize = 10;
  24.       _highlight = new TextStyle();
  25.       _highlight.Font =
  26.         new FontDescriptor("Calibri", true, false, 0, 0);
  27.       _highlight.TextSize = 14;
  28.     }
  29.     protected override SamplerStatus Sampler(JigPrompts prompts)
  30.     {
  31.       JigPromptPointOptions opts = new JigPromptPointOptions();
  32.       opts.UserInputControls =
  33.         UserInputControls.Accept3dCoordinates |
  34.         UserInputControls.AcceptMouseUpAsPoint |
  35.         UserInputControls.GovernedByUCSDetect;
  36.       opts.Message = "\nSelect point: ";
  37.       PromptPointResult res = prompts.AcquirePoint(opts);
  38.       Document doc =
  39.         Application.DocumentManager.MdiActiveDocument;
  40.       Editor ed = doc.Editor;
  41.       if (res.Status == PromptStatus.OK)
  42.       {
  43.         if (_position == res.Value)
  44.         {
  45.           return SamplerStatus.NoChange;
  46.         }
  47.         else
  48.         {
  49.           _position = res.Value;
  50.           return SamplerStatus.OK;
  51.         }
  52.       }
  53.       return SamplerStatus.Cancel;
  54.     }
  55.     protected override bool WorldDraw(WorldDraw draw)
  56.     {
  57.       // We make use of another interface to push our transforms
  58.       WorldGeometry wg = draw.Geometry as WorldGeometry;
  59.       if (wg != null)
  60.       {
  61.         // Push our transforms onto the stack
  62.         wg.PushOrientationTransform(
  63.           OrientationBehavior.Screen
  64.         );
  65.         wg.PushPositionTransform(
  66.           PositionBehavior.Screen,
  67.           new Point2d(30, 30)
  68.         );
  69.         System.Windows.Forms.Keys mods =
  70.           System.Windows.Forms.Control.ModifierKeys;
  71.         // Check whether the shift key is being pressed
  72.         bool shift =
  73.           (mods & System.Windows.Forms.Keys.Shift) > 0;
  74.         // Check whether the control key is being pressed
  75.         bool control =
  76.           (mods & System.Windows.Forms.Keys.Control) > 0;
  77.         // Draw our screen-fixed text
  78.         wg.Text(
  79.           new Point3d(0, 0, 0),  // Position
  80.           new Vector3d(0, 0, 1), // Normal
  81.           new Vector3d(1, 0, 0), // Direction
  82.           _position.ToString(),  // Text
  83.           true,                  // Rawness
  84.           (shift &&              // Textstyle
  85.           control ?
  86.           _highlight :          // Highlight if Ctrl-Shift
  87.           _normal)              // Normal, otherwise
  88.         );
  89.         // Remember to pop our transforms off the stack
  90.         wg.PopModelTransform();
  91.         wg.PopModelTransform();
  92.       }
  93.       return true;
  94.     }
  95.     [CommandMethod("SELPT")]
  96.     static public void SelectPointWithJig()
  97.     {
  98.       Document doc =
  99.         Application.DocumentManager.MdiActiveDocument;
  100.       Editor ed = doc.Editor;
  101.       TextJig jig = new TextJig();
  102.       PromptResult res = ed.Drag(jig);
  103.       if (res.Status == PromptStatus.OK)
  104.       {
  105.         ed.WriteMessage(
  106.           "\nPoint selected: {0}",
  107.           jig.Position
  108.         );
  109.       }
  110.     }
  111.   }
  112. }
You may have noticed the jig’s point acquisition has slightly different UserInputControls flags defined, mainly because I wanted to test out the jig’s ability to automatically switch the UCS when hovering over the faces of solids. It doesn’t have direct relevance to the rest of this post, but I’ve left it in as the application is actually quite a useful testbed for the 3D point acquisition capabilities of a jig. Plus it was easier to leave the code in than to change it. :-)
Here’s what happens when we run the SELPT command and just move the cursor around:

And here’s what happens when we do the same while pressing Ctrl-Shift – we see the text shown in a larger, bold, non-italic font:


I haven’t attempted to filter any key-down/key-up messages (we would have to go back an implement an IMessageFilter to do so), so use of the Shift key during our point input temporarily overrides the ORTHOMODE system variable (hence the glyph to the upper-right of the cursor). It’s possible to remove this temporary override via the CUI command, if needed.
 楼主| 发表于 2010-12-19 18:12:28 | 显示全部楼层
本帖最后由 lzh741206 于 2010-12-19 18:17 编辑

九、动态绘制一个带有弧段的多段线
December 06, 2010
Jigging an AutoCAD polyline with arc segments using .NET
I was just easing back into post-AU work – dealing with my email backlog and thinking about possible blog posts for the week – when I received a very welcome email from Philippe Leefsma, a member of the DevTech team based in Prague. Philippe had a bit of time to spare during our annual DevDays tour and decided to polish up a sample he’d been working on for posting. It extends a post of mine from four years ago (I can’t believe it’s been that long, but anyway), which shows how to jig a polyline with keywords. Philippe adjusted the code to modify the bulge factor of the current arc segment dynamically based on the cursor position – very much as the standard PLINE command does, in fact.

Here’s the C# code, with a few minor – mostly formatting – modifications from my side:


  1. using Autodesk.AutoCAD.ApplicationServices;

  2. using Autodesk.AutoCAD.DatabaseServices;

  3. using Autodesk.AutoCAD.EditorInput;

  4. using Autodesk.AutoCAD.Geometry;

  5. using Autodesk.AutoCAD.Runtime;

  6. using System;



  7. namespace PolylineJig

  8. {

  9.   class JigUtils

  10.   {

  11.     // Custom ArcTangent method, as the Math.Atan

  12.     // doesn't handle specific cases



  13.     public static double Atan(double y, double x)

  14.     {

  15.       if (x > 0)

  16.         return Math.Atan(y / x);

  17.       else if (x < 0)

  18.         return Math.Atan(y / x) - Math.PI;

  19.       else  // x == 0

  20.       {

  21.         if (y > 0)

  22.           return Math.PI;

  23.         else if (y < 0)

  24.           return -Math.PI;

  25.         else // if (y == 0) theta is undefined

  26.           return 0.0;

  27.       }

  28.     }



  29.     // Computes Angle between current direction

  30.     // (vector from last vertex to current vertex)

  31.     // and the last pline segment



  32.     public static double ComputeAngle(

  33.       Point3d startPoint, Point3d endPoint,

  34.       Vector3d xdir, Matrix3d ucs

  35.     )

  36.     {

  37.       Vector3d v =

  38.         new Vector3d(

  39.           (endPoint.X - startPoint.X) / 2,

  40.           (endPoint.Y - startPoint.Y) / 2,

  41.           (endPoint.Z - startPoint.Z) / 2

  42.         );



  43.       double cos = v.DotProduct(xdir);

  44.       double sin =

  45.         v.DotProduct(

  46.           Vector3d.ZAxis.TransformBy(ucs).CrossProduct(xdir)

  47.         );



  48.       return Atan(sin, cos);

  49.     }

  50.   }



  51.   public class BulgePolyJig : EntityJig

  52.   {

  53.     Point3d _tempPoint;

  54.     Plane _plane;

  55.     bool _isArcSeg = false;

  56.     bool _isUndoing = false;

  57.     Matrix3d _ucs;



  58.     public BulgePolyJig(Matrix3d ucs) : base(new Polyline())

  59.     {

  60.       _ucs = ucs;

  61.       Vector3d normal = Vector3d.ZAxis.TransformBy(ucs);

  62.       _plane = new Plane(Point3d.Origin, normal);

  63.       Polyline pline = Entity as Polyline;

  64.       pline.SetDatabaseDefaults();

  65.       pline.Normal = normal;

  66.       AddDummyVertex();

  67.     }



  68.     protected override SamplerStatus Sampler(JigPrompts prompts)

  69.     {

  70.       JigPromptPointOptions jigOpts = new JigPromptPointOptions();



  71.       jigOpts.UserInputControls =

  72.         (UserInputControls.Accept3dCoordinates |

  73.         UserInputControls.NullResponseAccepted |

  74.         UserInputControls.NoNegativeResponseAccepted);



  75.       _isUndoing = false;



  76.       Polyline pline = Entity as Polyline;



  77.       if (pline.NumberOfVertices == 1)

  78.       {

  79.         // For the first vertex, just ask for the point



  80.         jigOpts.Message = "\nSpecify start point: ";

  81.       }

  82.       else if (pline.NumberOfVertices > 1)

  83.       {

  84.         string msgAndKwds =

  85.           (_isArcSeg ?

  86.             "\nSpecify endpoint of arc or [Line/Undo]: " :

  87.             "\nSpecify next point or [Arc/Undo]: "

  88.           );



  89.         string kwds = (_isArcSeg ? "Line Undo" : "Arc Undo");



  90.         jigOpts.SetMessageAndKeywords(msgAndKwds, kwds);

  91.       }

  92.       else

  93.         return SamplerStatus.Cancel; // Should never happen



  94.       // Get the point itself



  95.       PromptPointResult res = prompts.AcquirePoint(jigOpts);



  96.       if (res.Status == PromptStatus.Keyword)

  97.       {

  98.         if (res.StringResult.ToUpper() == "ARC")

  99.           _isArcSeg = true;

  100.         else if (res.StringResult.ToUpper() == "LINE")

  101.           _isArcSeg = false;

  102.         else if (res.StringResult.ToUpper() == "UNDO")

  103.           _isUndoing = true;



  104.         return SamplerStatus.OK;

  105.       }

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

  107.       {

  108.         // Check if it has changed or not (reduces flicker)



  109.         if (_tempPoint == res.Value)

  110.           return SamplerStatus.NoChange;

  111.         else

  112.         {

  113.           _tempPoint = res.Value;

  114.           return SamplerStatus.OK;

  115.         }

  116.       }



  117.       return SamplerStatus.Cancel;

  118.     }



  119.     protected override bool Update()

  120.     {

  121.       // Update the dummy vertex to be our 3D point

  122.       // projected onto our plane



  123.       Polyline pl = Entity as Polyline;



  124.       if (_isArcSeg)

  125.       {

  126.         Point3d lastVertex =

  127.           pl.GetPoint3dAt(pl.NumberOfVertices - 2);



  128.         Vector3d refDir;



  129.         if (pl.NumberOfVertices < 3)

  130.           refDir = new Vector3d(1.0, 1.0, 0.0);

  131.         else

  132.         {

  133.           // Check bulge to see if last segment was an arc or a line



  134.           if (pl.GetBulgeAt(pl.NumberOfVertices - 3) != 0)

  135.           {

  136.             CircularArc3d arcSegment =

  137.               pl.GetArcSegmentAt(pl.NumberOfVertices - 3);



  138.             Line3d tangent = arcSegment.GetTangent(lastVertex);



  139.             // Reference direction is the invert of the arc tangent

  140.             // at last vertex



  141.             refDir = tangent.Direction.MultiplyBy(-1.0);

  142.           }

  143.           else

  144.           {

  145.             Point3d pt =

  146.               pl.GetPoint3dAt(pl.NumberOfVertices - 3);



  147.             refDir =

  148.               new Vector3d(

  149.                 lastVertex.X - pt.X,

  150.                 lastVertex.Y - pt.Y,

  151.                 lastVertex.Z - pt.Z

  152.               );

  153.           }

  154.         }



  155.         double angle =

  156.           JigUtils.ComputeAngle(

  157.             lastVertex, _tempPoint, refDir, _ucs

  158.           );



  159.         // Bulge is defined as tan of one fourth of included angle

  160.         // Need to double the angle since it represents the included

  161.         // angle of the arc

  162.         // So formula is: bulge = Tan(angle * 2 * 0.25)



  163.         double bulge = Math.Tan(angle * 0.5);



  164.         pl.SetBulgeAt(pl.NumberOfVertices - 2, bulge);

  165.       }

  166.       else

  167.       {

  168.         // Line mode. Need to remove last bulge if there was one



  169.         if (pl.NumberOfVertices > 1)

  170.           pl.SetBulgeAt(pl.NumberOfVertices - 2, 0);

  171.       }



  172.       pl.SetPointAt(

  173.         pl.NumberOfVertices - 1, _tempPoint.Convert2d(_plane)

  174.       );



  175.       return true;

  176.     }



  177.     public bool IsUndoing

  178.     {

  179.       get

  180.       {

  181.         return _isUndoing;

  182.       }

  183.     }



  184.     public void AddDummyVertex()

  185.     {

  186.       // Create a new dummy vertex... can have any initial value



  187.       Polyline pline = Entity as Polyline;

  188.       pline.AddVertexAt(

  189.         pline.NumberOfVertices, new Point2d(0, 0), 0, 0, 0

  190.       );

  191.     }



  192.     public void RemoveLastVertex()

  193.     {

  194.       Polyline pline = Entity as Polyline;



  195.       // Let's first remove our dummy vertex   



  196.       if (pline.NumberOfVertices > 0)

  197.         pline.RemoveVertexAt(pline.NumberOfVertices - 1);



  198.       // And then check the type of the last segment



  199.       if (pline.NumberOfVertices >= 2)

  200.       {

  201.         double blg = pline.GetBulgeAt(pline.NumberOfVertices - 2);

  202.         _isArcSeg = (blg != 0);

  203.       }

  204.     }



  205.     public void Append()

  206.     {

  207.       Database db = HostApplicationServices.WorkingDatabase;



  208.       Transaction tr =

  209.         db.TransactionManager.StartTransaction();

  210.       using (tr)

  211.       {

  212.         BlockTable bt =

  213.           tr.GetObject(

  214.             db.BlockTableId, OpenMode.ForRead

  215.           ) as BlockTable;

  216.         BlockTableRecord btr =

  217.           tr.GetObject(

  218.             bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite

  219.           ) as BlockTableRecord;



  220.         btr.AppendEntity(this.Entity);

  221.         tr.AddNewlyCreatedDBObject(this.Entity, true);

  222.         tr.Commit();

  223.       }

  224.     }



  225.     [CommandMethod("BPJIG")]

  226.     public static void RunBulgePolyJig()

  227.     {

  228.       Document doc = Application.DocumentManager.MdiActiveDocument;

  229.       Database db = doc.Database;

  230.       Editor ed = doc.Editor;



  231.       BulgePolyJig jig =

  232.         new BulgePolyJig(ed.CurrentUserCoordinateSystem);



  233.       while (true)

  234.       {

  235.         PromptResult res = ed.Drag(jig);



  236.         switch (res.Status)

  237.         {

  238.           // New point was added, keep going



  239.           case PromptStatus.OK:

  240.             jig.AddDummyVertex();

  241.             break;



  242.           // Keyword was entered



  243.           case PromptStatus.Keyword:

  244.             if (jig.IsUndoing)

  245.               jig.RemoveLastVertex();

  246.             break;



  247.           // If the jig completed successfully, add the polyline



  248.           case PromptStatus.None:

  249.             jig.RemoveLastVertex();

  250.             jig.Append();

  251.             return;



  252.           // User cancelled the command, get out of here

  253.           // and don't forget to dispose the jigged entity



  254.           default:

  255.             jig.Entity.Dispose();

  256.             return;

  257.         }

  258.       }

  259.     }

  260.   }

  261. }


Here’s the BPJIG command in action, jigging a polyline with arc segments:



Now it must be noted that this implementation is some way from re-implementing the full PLINE command – for those of you who like to re-invent the wheel ;-) - but it should provide some idea of how to allow users to input bulge factors for polyline segments that are being jigged by your custom commands.

Thanks, Philippe! :-)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-5 12:40 , Processed in 0.165202 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表