找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1013|回复: 0

[分享] Striking an enclosed text area in AutoCAD using .NET

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-9 12:26:20 来自手机 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×
本帖最后由 csharp 于 2014-5-9 13:16 编辑

封闭区域点一点标注面积
http://www.tuicool.com/articles/VjQRjyZI’m still on pseudo-vacation. We really had a great week in the snow: sun during the day (everyday!) with the odd bit of fresh snow overnight on a couple of occasions. It’s snowing again now, but unfortunately this time it’s set to snow all the way through to the end of the day tomorrow, so it’s quite possible we’ll head home a little earlier than expected. Which, on the plus side, will allow me to do some prep-work for Monday’s Geneva Motor Show project installation.

Anyway, all this to say that I’ve written this post really quickly with plenty of shortcuts. Alex Fielder pinged me earlier with a requirement he has, and – given the other things going on – I thought I’d start with something quick and dirty.

Happy Friday! @keanw am wondering what approach to take WRT inserting crossing lines like this in Paperspace: pic.twitter.com/Ss2BDX8dPn
— Alex Fielder (@AlexFielder) February 28, 2014
Consider this a classic strawman implementation that’s designed to stimulate user feedback: it probably doesn’t come close to meeting Alex’s needs, but then it gives him some tyres to kick and say what’s working or not.

The various “issues” I have with the implementation are listed in the comments in this simple C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.Runtime;

namespace StrikeText

{

  public class Commands

  {

    [ CommandMethod ( "STXT" )]

    public static void StrikeTextArea()

    {

      var doc =

        Application .DocumentManager.MdiActiveDocument;

      var db = doc.Database;

      var ed = doc.Editor;

      // Ask for a point. Could also ask for an enclosed text entity

      // and use the picked point or the text's insertion point

      var ppo = new PromptPointOptions ( "\nSelect point" );

      var ppr = ed.GetPoint(ppo);

      if (ppr.Status != PromptStatus .OK)

        return ;

      using ( var tr = db.TransactionManager.StartTransaction())

      {

        // Use Editor.TraceBoundary() to check for a suitable

        // boundary. This may throw up a dialog, if selecting a

        // space inside a table, for instance (before you explode

        // it manually). Not ideal - it basically uses the same

        // code path as BHATCH or BOUNDARY, which includes a dialog

        var oc = ed.TraceBoundary(ppr.Value, false );

        // Only deal with the case where we get a single entity

        // returned

        if (oc.Count == 1)

        {

          var ent = oc[0] as Entity ;

          if (ent != null )

          {

            // Get the extents of the entity and determine points

            // at the top left and bottom right corners. This is

            // super-simplistic: will only work if we're dealing

            // with a WCS-aligned area. More work needed if dealing

            // with areas with other alignments

            var ext = ent.GeometricExtents;

            var pt1 = new Point3d (ext.MinPoint.X, ext.MaxPoint.Y, 0);

            var pt2 = new Point3d (ext.MaxPoint.X, ext.MinPoint.Y, 0);

            // Create a line between these points

            var ln = new Line (pt1, pt2);

            // Add it to the current space

            var btr =

              ( BlockTableRecord )tr.GetObject(

                db.CurrentSpaceId, OpenMode .ForWrite

              );

            btr.AppendEntity(ln);

            tr.AddNewlyCreatedDBObject(ln, true );

          }

        }

        // Commit even if we didn't add anything

        tr.Commit();

      }

    }

  }

}

When you run the STXT command and selecting a point in an enclosed area, it should strike it through from top-left to bottom-right. Providing the various assumptions mentioned in the code are true, of course.
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:31 , Processed in 0.170025 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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