Kean Walmsley

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Twitter Updates

    follow me on Twitter



    « AU Unplugged session on AutoCAD development | Main | Heading for the hills »

    September 10, 2007

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

    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

          );

        // ...

      }

    TrackBack

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e200e54eef74ae8834

    Listed below are links to weblogs that reference Accessing the active space or layout in an AutoCAD drawing using .NET:

    Comments

    You used the islayout qualifier to cal the proper block you are looking for in theis sequence. Is there a way to change the props of a block that is not a layout but rather just an ordinary modelspace designed block. I am having trouble as I seem to need to end the transaction of insert before I can access the dynamic props. But now how do I reselect the block without actually clicking it again to make it active? Any help would be appreciated

    It's not clear exactly what you need... you'll have to post some code, ideally to the AutoCAD .NET discussion group. You can then submit a link to the post as a comment, of course.

    Regards,

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search