• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Valued Contributor
    Ertqwa
    Posts: 73
    Registered: ‎10-03-2011
    Accepted Solution

    AcadApplication - InvokeMember

    364 Views, 9 Replies
    11-11-2012 12:23 AM

    Hello Forum,

     

    I want to find out which members I can invoke on the class Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, like this:

     

    [CommandMethod("zoomExtentTest")]
    public static void zoomExtentTest()
    {
       Object acadObject = Application.AcadApplication;
    acadObject.GetType().InvokeMember("ZoomExtents",
       BindingFlags.InvokeMethod, null, acadObject, null);
    }

     However, AcadApplication returns type Object so I'm getting nowhere with "System.Reflection".

     

    Any one knows how to list all members that I can invoke, or if there is an overview somewhere?

     

    Thank you.

     

     

     

    Please use plain text.
    Distinguished Contributor
    khoa.ho
    Posts: 131
    Registered: ‎09-15-2011

    Re: AcadApplication - InvokeMember

    11-12-2012 08:58 AM in reply to: Ertqwa

    Hi,

     

    Using Reflection of AutoCAD COM Interop, we will get all methods to invoke. The following code will get all methods of a loaded AutoCAD application:

     

    public static Dictionary<string, string> GetAcadInteropMethods(object acadApp)
    {
        if (acadApp == null)
            return null;
        var methodDict = new Dictionary<string, string>();
        MethodInfo[] methods = acadApp.GetType().GetMethods();
        foreach (MethodInfo methodInfo in methods)
        {
            string methodName = methodInfo.Name;
            string methodReturnType = methodInfo.ReturnType.Name;
            methodDict.Add(methodName, methodReturnType);
        }
        return methodDict;
    }

     

    -Khoa

    Please use plain text.
    Valued Contributor
    Ertqwa
    Posts: 73
    Registered: ‎10-03-2011

    Re: AcadApplication - InvokeMember

    11-13-2012 10:26 AM in reply to: khoa.ho

    Hi, ty for your response.

     

    Your code returns the following:

    GetLifetimeService, Object
    InitializeLifetimeService, Object
    CreateObjRef, ObjRef
    ToString, String
    Equals, Boolean
    GetHashCode, Int32
    GetType, Type

     

    It does not, for example, returns "ZoomExtents", but I can invoke it. I want to find all membersI can invoke.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,332
    Registered: ‎10-08-2008

    Re: AcadApplication - InvokeMember

    11-13-2012 11:15 AM in reply to: Ertqwa

    Sorry you could try it on existing instance of application,

    then it will work like a sharm 

    Something like this:

       public static class AcadInteropApp
        {
           public  static Autodesk.AutoCAD.Interop.AcadApplication AcadApp =
                (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
    
            private static AcadDocument acdoc()
            {
                if (AcadApp != null)
                {
                    return AcadApp.ActiveDocument;
                }
                else
                {
                    return null;
                }
            }
        }
    
    
      
    
            [CommandMethod("mets")]
            public static void doit()
            {
                Autodesk.AutoCAD.Interop.AcadDocument doc = AcadInteropApp.AcadApp.ActiveDocument;
                Dictionary<string, string> dict = GetAcadInteropMethods(doc);
                AcadUtility acutil=doc.Utility;
                foreach (KeyValuePair<string,string> kvp in dict)
                {
                    acutil.Prompt("\nMethod name=" + kvp.Value.ToString());
                }
    
               // AcadInteropApp.AcadApp.Quit();
    
            }
    
            public static Dictionary<string, string> GetAcadInteropMethods(object acadApp)
            {
                if (acadApp == null)
                    return null;
                var methodDict = new Dictionary<string, string>();
                MethodInfo[] methods = acadApp.GetType().GetMethods();
                foreach (MethodInfo methodInfo in methods)
                {
                    string methodName = methodInfo.Name;
                    string methodReturnType = methodInfo.ReturnType.Name;
                    methodDict.Add(methodName, methodReturnType);
                }
                return methodDict;
            } //   Khoa     //

     

     

     

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Distinguished Contributor
    khoa.ho
    Posts: 131
    Registered: ‎09-15-2011

    Re: AcadApplication - InvokeMember

    11-13-2012 11:54 AM in reply to: Hallex

    Hallex is right to give the full code to run the given method. Thank you.

     

    -Khoa

    Please use plain text.
    Valued Mentor
    Posts: 299
    Registered: ‎05-06-2012

    Re: AcadApplication - InvokeMember

    11-13-2012 07:42 PM in reply to: Ertqwa

    The other replies you received all assume that you are using the COM interop assemblies, which is probably not the case, or you wouldn't need to invoke members via reflection.

     

    If you are using late binding (e.g., not importing the COM interop assemblies) the only way to reflect on a COM object is to access its type library using ITypeInfo or ITypeInfo2 COM interfaces, which is fairly complicated.

     

    A workaround would be to load the AutoCAD COM interop assemblies  into the reflection-only context (see  Assembly.ReflectionOnlyLoadFrom() in the docs), and then you can reflect on the the runtime-callable wrappers (e.g., AcadApplication, AcadDocument, etc.).

    Please use plain text.
    Valued Contributor
    Ertqwa
    Posts: 73
    Registered: ‎10-03-2011

    Re: AcadApplication - InvokeMember

    11-18-2012 12:10 AM in reply to: Hallex

    Hi, ty for you response.

     

    When I use your method, I get a lot of results, but not all. For Example, "ZoomExtents" is not in the return values:

     

    0: Void
    1: Void
    2: Void
    3: Void
    4: Void
    5: Void
    6: Void
    7: Void
    8: Void
    9: Void
    10: Void
    11: Void
    12: Void
    13: Void
    14: Void
    15: Void
    16: Void
    17: Void
    18: Void
    19: Void
    20: Void
    21: Void
    22: Void
    23: Void
    24: Void
    25: Void
    26: Void
    27: Void
    28: Void
    29: Void
    30: Void
    31: Void
    32: Void
    33: Void
    34: Void
    35: Void
    36: Void
    37: Void
    38: Void
    39: Int64
    40: String
    41: IAcadApplication
    42: AcadDatabase
    43: Void
    44: Void
    45: AcadMaterial
    46: Void
    47: Int32
    48: Void
    49: Void
    50: Void
    51: Void
    52: Void
    53: Void
    54: Void
    55: Void
    56: Void
    57: Void
    58: Void
    59: Void
    60: Void
    61: Void
    62: Void
    63: Void
    64: Void
    65: Void
    66: Boolean
    67: Void
    68: AcadUtility
    69: AcadDocument
    70: Void
    71: Object
    72: Void
    73: AcadDocument
    74: Void
    75: Void
    76: Void
    77: Void
    78: Object
    79: Void
    80: Void
    81: Void
    82: AcadSelectionSet
    83: Boolean
    84: Void
    85: Void
    86: Void
    87: AcWindowState
    88: Void
    89: Int32
    90: Void
    91: Int32
    92: Void
    93: AcadLayout
    94: AcadMaterials
    95: Object
    96: AcadPlot
    97: AcadLayer
    98: Void
    99: AcadLineType
    100: Void
    101: AcadDimStyle
    102: Void
    103: AcadTextStyle
    104: Void
    105: AcadUCS
    106: Void
    107: AcadViewport
    108: Void
    109: AcadPViewport
    110: Void
    111: AcActiveSpace
    112: Void
    113: AcadSelectionSets
    114: AcadSelectionSet
    115: String
    116: String
    117: String
    118: Boolean
    119: Void
    120: Boolean
    121: Boolean
    122: AcadModelSpace
    123: AcadPaperSpace
    124: AcadBlocks
    125: Object
    126: AcadGroups
    127: AcadDimStyles
    128: AcadLayers
    129: AcadLineTypes
    130: AcadDictionaries
    131: AcadRegisteredApplications
    132: AcadTextStyles
    133: AcadUCSs
    134: AcadViews
    135: AcadViewports
    136: Double
    137: Void
    138: Double
    139: Void
    140: Object
    141: Void
    142: Object
    143: Object
    144: AcadLayouts
    145: AcadPlotConfigurations
    146: AcadDatabasePreferences
    147: AcadFileDependencies
    148: AcadSummaryInfo
    149: AcadSectionManager
    150: Object
    151: Object
    152: ObjRef
    153: String
    154: Boolean
    155: Int32
    156: Type
    Found: 157

     

    Code:

     

                int i = 0;
    
                try
                {
                    Autodesk.AutoCAD.Interop.AcadApplication AcadApp =
                        (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
                    Autodesk.AutoCAD.Interop.AcadDocument doc = AcadApp.ActiveDocument;
                    Dictionary<string, string> dict = GetAcadInteropMethods(doc);
                    Autodesk.AutoCAD.Interop.AcadUtility acutil = doc.Utility;
                    foreach (KeyValuePair<string,string> kvp in dict)
                    {
                        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n{0}: {1}", i.ToString(), kvp.Value.ToString());
                        i++;
                    }
                    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nFound: {0}", dict.Count.ToString());
                }
                catch (System.Exception Ex)
                { MessageBox.Show("Error testing GetAcadInteropMethods on CADS.Application.AcadApplication: " + Ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }

     

    Thanks.

     

     

    Please use plain text.
    Valued Contributor
    Ertqwa
    Posts: 73
    Registered: ‎10-03-2011

    Re: AcadApplication - InvokeMember

    11-18-2012 12:13 AM in reply to: DiningPhilosopher

    Hi ty for your response.

     

    Yes you are right, all of this is to avoid using the COM interop assemblies. However, as you state yourself, I do not mind using the COM interop assemblies to get the information I need, as long as it wont end up in the final application.

     

    Using COM interop assemblies was working fine until the application had to run on both CAD2009 (32bit) and CAD2012 (64bit).

    Please use plain text.
    *Expert Elite*
    Posts: 1,639
    Registered: ‎04-29-2006

    Re: AcadApplication - InvokeMember

    11-18-2012 05:54 AM in reply to: Ertqwa

    Hi,

     

    What about using the COM/ActiveX documentation available here:

    http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112

    Gilles Chanteau
    Please use plain text.
    Valued Contributor
    Ertqwa
    Posts: 73
    Registered: ‎10-03-2011

    Re: AcadApplication - InvokeMember

    12-08-2012 12:05 AM in reply to: _gile

    Great thanks, found a list in acadauto.chm.

    Please use plain text.