Question

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.

Example: 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?

Was it helpful?

Solution

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);
            }
        }
    } 

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top