# Use COM Interfaces with COM Interop for AutoCAD OEM

**URL:** <https://forum.techsoft3d.com/t/use-com-interfaces-with-com-interop-for-autocad-oem/4220>\
**Category:** AutoCAD OEM\
**Tags:** knowledge-base\
**Created:** [November 16, 2024, 11:26am UTC](https://forum.techsoft3d.com/t/use-com-interfaces-with-com-interop-for-autocad-oem/4220 "2024-11-16T11:26:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jm1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/jm1/32/317_2.png) [@jm1](https://forum.techsoft3d.com/u/jm1)\
**Post date:** [November 16, 2024, 11:26am UTC](https://forum.techsoft3d.com/t/use-com-interfaces-with-com-interop-for-autocad-oem/4220/1 "2024-11-16T11:26:34Z")

</div>

There are some usefull COM Interfaces predating the introduction of the .NET software development platform for the customization of the AutoCAD ([COM Interoperability (.NET)](https://help.autodesk.com/view/OARX/2025/ENU/?guid=GUID-BFFF308E-CC10-4C56-A81E-C15FB300EB70)).

When developing for AutoCAD OEM, you have to be aware that the approach to work with the COM Interfaces is different than when working with a vanilla AutoCAD ([see here for more details](https://forums.autodesk.com/t5/net/how-to-get-acadapplication-from-a-c-assembly-running-in-autocad/m-p/5074520/highlight/true#M40864))

Here is some sample code where an existing drawing is openend and all layouts are cycled to zoom the paperspace to the maximum extents.

```auto
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;

using AcApp2 = Autodesk.AutoCAD.ApplicationServices.Application;
using AcApp = Autodesk.AutoCAD.ApplicationServices;
using AcRt = Autodesk.AutoCAD.Runtime;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcIop = Autodesk.AutoCAD.Interop;

```

```auto
[assembly: AcRt.ExtensionApplication(null)]
[assembly: AcRt.CommandClass(typeof(layout.ZoomLayoutsToExtents))]

```

```auto
namespace Zoom
{
    public class Layouts
    {
        [AcRt.CommandMethod("ZoomLayoutsToExtents", AcRt.CommandFlags.Session)]
        public void ZoomLayoutsToExtents()
        {
            try
            {
                /* get path to executing assembly */
                string dwgPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                /* Full file path to a sample drawing that is located in the same directory as the executing assembly */
                dwgPath += "test.dwg";

                /* close document without saving */
                bool bSaveDocumentBeforeClosing = false;

                /* open document from filepath */
                AcApp.DocumentCollection docCol = AcApp2.DocumentManager;
                AcApp.Document doc = AcApp.DocumentCollectionExtension.Open(docCol, dwgPath, false);
                docCol.MdiActiveDocument = doc;
                AcDb.Database db = doc.Database;

                /* ObjectId List to store the objectIds for all layouts */
                List<AcDb.ObjectId> oIdList = new List<AcDb.ObjectId>();

                /* Here we get the objectId's of all layouts */
                using (AcApp.DocumentLock docLoc = AcApp2.DocumentManager.MdiActiveDocument.LockDocument())
                {                   
                    using (AcDb.Transaction trans = db.TransactionManager.StartTransaction())
                    {
                        /* switch to modelspace */
                        AcDb.LayoutManager.Current.CurrentLayout = @"Model";
                        /* ALTERNATIVE >> */
                        /* AcApp.Application.SetSystemVariable("TILEMODE", 1); */

                        /* get layout dictionary */
                        AcDb.DBDictionary layoutDict = trans.GetObject(db.LayoutDictionaryId, AcDb.OpenMode.ForRead) as AcDb.DBDictionary;

                        /* get objectId's from dictionary */
                        foreach (AcDb.DBDictionaryEntry entry in layoutDict)
                            oIdList.Add(entry.Value);
                       
                        trans.Commit();

                    } // using trans

                } // using trans

                
                /* this works with vanilla AutoCAD */
                #if !OEM
                dynamic thisApp = AcApp.Application.AcadApplication;

                /* dynamic does NOT work for AutoCAD OEM */
                /* Use AcIop.AcadApplication instead */
                #elif OEM
                AcIop.AcadApplication thisApp = AcApp.Application.AcadApplication as AcIop.AcadApplication;
                #endif

                /* lock the document for editing */
                using (AcApp.DocumentLock docLoc = doc.LockDocument())
                {
                    /* cycle objectId's */
                    foreach (AcDb.ObjectId oId in oIdList)
                    {
                        AcDb.LayoutManager.Current.SetCurrentLayoutId(oId);

                        if (AcDb.LayoutManager.Current.CurrentLayout != "Model")
                        {
                            using (var tr = doc.Database.TransactionManager.StartTransaction())
                            {
                                /* https://forums.autodesk.com/t5/net/how-to-activate-viewport-in-paperspace/td-p/12740814 */
                                doc.Editor.SwitchToPaperSpace();
                                tr.Commit();
                            }
                        }

                        /* zoom paperspace to extents */
                        thisApp.GetType().InvokeMember("ZoomExtents", BindingFlags.InvokeMethod, null, thisApp, null);                                
                    }
                }

                /* The command MUST be run in AcRt.CommandFlags.Session */
                /* otherwise, there is a System.Runtime.InteropServices.COMException */
                if (bSaveDocumentBeforeClosing)
                    AcApp.DocumentExtension.CloseAndSave(doc, dwgPath);
                else
                    AcApp.DocumentExtension.CloseAndDiscard(doc);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(
                    ex.Message.ToString(), 
                    @"Error Message", 
                    System.Windows.Forms.MessageBoxButtons.OK, 
                    System.Windows.Forms.MessageBoxIcon.Error
                    );    
            }

        } // END

    } // class

} // namespace

```

The zoom is done with this COM Interop:

```auto
thisApp.GetType().InvokeMember("ZoomExtents", BindingFlags.InvokeMethod, null, thisApp, null);

```

For **vanilla AutoCAD** you can use

```auto
dynamic thisApp = AcApp.Application.AcadApplication;

```

to get a variable for the AutoCAD application.

This will **not work for AutoCAD OEM**.  
For AutoCAD OEM you have to add the using to the interop:

```auto
using AcIop = Autodesk.AutoCAD.Interop;

```

and an explicit variable assignment with

```auto
AcIop.AcadApplication thisApp = AcApp.Application.AcadApplication as AcIop.AcadApplication;

```

You can do this to target vanilla AutoCAD and AutoCAD OEM:

```auto
/* this works with vanilla AutoCAD */
#if !OEM
dynamic thisApp = AcApp.Application.AcadApplication;

/* dynamic does NOT work for AutoCAD OEM */
/* Use AcIop.AcadApplication instead */
#elif OEM
AcIop.AcadApplication thisApp = AcApp.Application.AcadApplication as AcIop.AcadApplication;
#endif

```

When you want to use the variable as a parameter for a subroutine, you have to avoid some implicit casting. This will not work with AutoCAD OEM:

```auto
        private void SomeSubRoutine(
            dynamic thisApp);

```

Instead, you have to do this with explicit variable parameters:

```auto
        private void SomeSubRoutine(
            #if !OEM
            dynamic thisApp
            #elif OEM
            AcIop.AcadApplication thisApp
            #endif
            )

```

---

<div class="post-metadata">

**Author:** ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)\
**Post date:** [November 20, 2024, 5:53pm UTC](https://forum.techsoft3d.com/t/use-com-interfaces-with-com-interop-for-autocad-oem/4220/2 "2024-11-20T17:53:39Z")

</div>

Thanks for this @jm1
