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

20 篇文章 0 订阅
 

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

      );

    // ...

  }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值