How to get a managed object from a COM database resident object?

By Philippe Leefsma

The following sample illustrates how to retrieve the .Net managed object once we have access to its COM wrapper. The best way to achieve that is to use the old objectId property as below:

[CommandMethod("Com2DotNet")]

public static void Com2DotNet()

{

    AcadApplication acadApp = Application.AcadApplication

        as AcadApplication;

 

    Object obj = new Object();

    Object pt = new Object();

            

    acadApp.ActiveDocument.Utility.GetEntity(

        out obj,

        out pt,

        "\nPick an entity: ");

 

    Autodesk.AutoCAD.Interop.Common.AcadEntity acadEntity = obj

        as Autodesk.AutoCAD.Interop.Common.AcadEntity;

&#160
;

 

 

    Document doc = Application.DocumentManager.MdiActiveDocument;

    Database db = doc.Database;

    Editor ed = doc.Editor;

 

    using(Transaction Tx = db.TransactionManager.StartTransaction())

    {

        ObjectId id = new ObjectId((IntPtr)acadEntity.ObjectID);

 

        Entity entity = Tx.GetObject(id, OpenMode.ForWrite)

            as Entity;

 

        entity.ColorIndex = 1;

 

        Tx.Commit();

    }

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”>}


Comments

3 responses to “How to get a managed object from a COM database resident object?”

  1. Anonymoose Avatar
    Anonymoose

    It appears that you’ve overlooked the following static member of DBObject:
    public static unsafe ObjectId FromAcadObject(object acadObj)

  2. Hi Philippe.

    How to get a managed object from a COM
    Why you wrote such many code rows? Maybe such:
    id.ObjectClass.Create().GetType()
    Regards

  3. Oh, I seem confused managed object with controlled type.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading