开发者

How to get the .NET object from a given COM object in AutoCAD

I have a AutoCAD 2008 plugin written in VB.NET. This plugin uses mostly the COM interface for accessing ACAD objects.

I'm currently switching from the COM interface to the managed interface and have the following question: How can I get the matching managed ACAD object from a given COM ACAD object.

Exam开发者_StackOverflow中文版ple: I have a AcadBlockReference object and I wan't to have the BlockReference object which points to the same object in the drawing. How do I get this object or maybe its ObjectID?


I use the .NET library quite often to grab block references, however, I've never grabbed one by using a COM object. The following method is one that I've had that would take in a block name and location, and return the block reference. I've modified it here to take in a AcadBlockReference and I use it's Name property to find it in the BlockTable. I didn't test this as I don't have the time to, but just thought I'd throw it out here and hope that it helps you move forward.

Sorry that my example is in C#, but it should be easy enough to re-write in VB.NET.

public BlockReference GetBlockReferenceFromCOM(AcadBlockReference comReference, Point3d location)
    {
        using (Database database = Application.DocumentManager.MdiActiveDocument.Database)
        {
            using (Transaction transaction = database.TransactionManager.StartTransaction())
            {
                BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord acBlkTblRecNewDoc = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                BlockReference newBlockReference = new BlockReference(location, blockTable[comReference.Name]);

                transaction.Commit();

                return newBlockReference;
            }
        }
    }

EDIT Do you have the object id for these items? If so, you could do something like this:

public DBObject GetBlockReferenceFromCOM(ObjectId id)
    {
        using (Database database = Application.DocumentManager.MdiActiveDocument.Database)
        {
            using (Transaction transaction = database.TransactionManager.StartTransaction())
            {
                return transaction.GetObject(id, OpenMode.ForRead);
            }
        }
    } 


You might want to investigate the Handle or ObjectID properties of the COM object and see if you can match them up with the ObjectId of the .NET object or the Handle property of that ObjectId.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜