Accessing the active space or layout in an AutoCAD drawing using .NET

转载 2007年10月15日 13:56:00
 

This question was asked as comment to a previous post by har!s:

Thanks a lot for the code. I have yet to see 2008 and MultiLeader. But I presume that it works on both Model and paper spaces. In that case, what is the best method to make the operation space independent? i.e., it should work on active space irrespective of whether it's model or paper. I think this will be generally applicable to almost all the entity creations.

The question is very valid and does indeed apply to a lot of entity creation - and other - activities. Most of the time I simply show how to open the modelspace in my code, for example:

  Document doc =

    Application.DocumentManager.MdiActiveDocument;

  Editor ed = doc.Editor;

  Database db = doc.Database;

  Transaction tr =

    db.TransactionManager.StartTransaction();

  using (tr)

  {

    BlockTable bt =

      (BlockTable)tr.GetObject(

        db.BlockTableId,

        OpenMode.ForRead

      );

    BlockTableRecord btr =

      (BlockTableRecord)tr.GetObject(

        bt[BlockTableRecord.ModelSpace],

        OpenMode.ForWrite

      );

    // ...

  }

The key statement here is at the end, where we use GetObject() to open the BlockTableRecord to which we want to (for example) append an entity. The form we use is:

  bt[BlockTableRecord.ModelSpace]

Breaking this down: we're actually looking up the ObjectId of the BlockTableRecord with the name of "*MODEL_SPACE", which is the string stored in the static ModelSpace property of the BlockTableRecord class.

Here are a few different options for what we might do here:

  1. Use either BlockTableRecord.ModelSpace or BlockTableRecord.PaperSpace, if we know that we want to access either of these containers (the current approach).
  2. Use foreach() on the BlockTable to iterate through the various BlockTableRecords: you can open each one using GetObject() and check the IsLayout property to find those that are either modelspace or paperspace layouts.
  3. Use db.CurrentSpaceId to open the currently active space in that particular database.

Option 3 is really the answer to this question, which makes the code like this:

  Document doc =

    Application.DocumentManager.MdiActiveDocument;

  Editor ed = doc.Editor;

  Database db = doc.Database;

  Transaction tr =

    db.TransactionManager.StartTransaction();

  using (tr)

  {

    BlockTable bt =

      (BlockTable)tr.GetObject(

        db.BlockTableId,

        OpenMode.ForRead

      );

    BlockTableRecord btr =

      (BlockTableRecord)tr.GetObject(

        db.CurrentSpaceId,

        OpenMode.ForWrite

      );

    // ...

  }

AutoCAD .NET API基础(二) 访问AutoCAD 对象层次

Access the bject Hierarchy访问 AutoCAD 对象层次 While the Application is the root object in the AutoCAD .N...
  • sunchengbo2007
  • sunchengbo2007
  • 2011-06-22 16:58:00
  • 2200

Accessing the AutoCAD objects referred to by fields using .NET

Thanks to Wolfgang Ruthensteiner for suggesting this excellent topic a comment to this previous po...
  • sdphg
  • sdphg
  • 2007-07-20 16:20:00
  • 2123

Using a modeless .NET dialog to display properties of multiple AutoCAD objects

After a few comments on this previous post I decided that, rather than rushing on to show palettes,...
  • sdphg
  • sdphg
  • 2007-07-13 11:54:00
  • 1247

Using a palette from .NET to display properties of multiple AutoCAD objects

After a brief interlude were back on the series of posts showing how to implement basic user-interf...
  • sdphg
  • sdphg
  • 2007-07-20 16:22:00
  • 1644

Windows Forms Programming with C#

  • 2008年12月24日 16:02
  • 9.82MB
  • 下载

using System.Drawing;是否缺少程序集引用?的解决办法

在用vs2010写文本框控件代码时,
  • u012997454
  • u012997454
  • 2014-05-19 22:22:51
  • 3685

AutoCAD .Net 创建Ribbon界面(一)

从 AutoCAD 2010 版开始,引入了 Ribbon 界面。 那怎样用代码创建 Ribbon 界面呢? 本篇文章讲解如何创建如下图所示的 Ribbon Tab 页:测试Ribbon页。 ...
  • wangyao1052
  • wangyao1052
  • 2017-12-13 21:28:52
  • 289

System.Drawing.Imaging 命名空间

System.Drawing.Imaging 命名空间 .NET Framework 3.5 System.Drawing.Imaging 命名空间提供高级 GDI+ 图像处理功能。基...
  • panting8
  • panting8
  • 2012-02-08 15:59:22
  • 2933

Android tips tool 发现的性能问题

1.ObsoleteLayoutParam不起作用的标签 Invalid layout param in a LinearLayout: layout_centerVertical Iss...
  • time_hunter
  • time_hunter
  • 2013-03-12 18:19:01
  • 9483

DrawTools(画图工具) From CodeProject

http://www.codeproject.com/csharp/DrawTools.asp(源代码请参见原文)IntroductionDrawTools sample shows how to c...
  • vsweaver
  • vsweaver
  • 2006-07-07 11:30:00
  • 3873
收藏助手
不良信息举报
您举报文章:Accessing the active space or layout in an AutoCAD drawing using .NET
举报原因:
原因补充:

(最多只允许输入30个字)