Accessing the active space or layout in an AutoCAD drawing using .NET
2007-10-15 13:56
483 查看
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:
Use either BlockTableRecord.ModelSpace or BlockTableRecord.PaperSpace, if we know that we want to access either of these containers (the current approach).
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.
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
);
// ...
}
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:
Use either BlockTableRecord.ModelSpace or BlockTableRecord.PaperSpace, if we know that we want to access either of these containers (the current approach).
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.
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
);
// ...
}
相关文章推荐
- Purging registered application names in the current AutoCAD drawing using .NET
- Accessing DWG files not open in the AutoCAD editor using .NET
- Accessing the AutoCAD objects referred to by fields using .NET
- Updating a specific attribute inside an AutoCAD drawing using .NET
- How to write an application that supports the Fast User Switching feature by using Visual Basic .NET or Visual Basic
- failed to open the runspace pool. the server manager winrm plug-in might be corrupted or missing
- Writing an XML-RPC server or client in ASP.Net: Part 2
- > SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDRO
- Facebook面试题 find the peek or valley index in an array
- SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID
- Fw:Managing View State in ASP.NET 4 Using the New ViewStateMode Property
- Accessing List Data using the JavaScript Client OM and displaying in a Dialog
- Unable to determine the principal end of an association between the types '***. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
- An easy instance of using the template class in C++
- If running in the context of a test using the mocking API or bootstrap Grails correctly
- Discover the Design Patterns You're Already Using in the .NET Framework
- 【转载】Using the Web Service Callbacks in the .NET Application
- [翻译]Accessing and Updating Data in ASP.NET 2.0: Examining the Data Source Control's Events
- Previewing and plotting multiple sheets in AutoCAD using .NET
- The process was terminated due to an internal error in the .NET Runtime at IP 7915EE73 (79140000) wi