DotNet

Temporary sample in C#

using System;
using LL = alisp_net.LispLinking;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices; 
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcEd = Autodesk.AutoCAD.EditorInput; 
using System.Runtime.InteropServices;
using System.Reflection;
using System.Collections.Specialized;
 
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(alisp_net.MyCommands))]
 
namespace alisp_net
{
    /// <summary>
    /// Class contining functions for calling DotNet assemblies and their objects
    /// </summary>
    public class MyCommands
    {
        /// <summary>
        /// List of loaded DotNet DLLs in AutoCAD
        /// </summary>
        /// <param name="args">Ignored, can contain anything including being omitted.</param>
        /// <returns>A list of strings of the DLLName s</returns>
        [LispFunction("net-GetAssemblies")]
        public ResultBuffer GetLoadedAssemblies(ResultBuffer args)
        {
            Assembly[] assems = AppDomain.CurrentDomain.GetAssemblies();
            if (assems.Length <= 0) return null;
            ResultBuffer res = new ResultBuffer();
            foreach (Assembly assem in assems)
            {
                res.Add(new TypedValue((int)LispDataType.Text, assem.ManifestModule.Name));
            }
            return res;
        }
 
        internal static Assembly GetAssembly(String Name)
        {
            Assembly[] assems = AppDomain.CurrentDomain.GetAssemblies();
            Assembly found = null;
            Name = Name.ToUpper();
            foreach (Assembly assem in assems)
            {
                if (Name == assem.ManifestModule.Name.ToUpper()) found = assem;
            }
            return found;
        }
 
        private static string[] GetCommands(Assembly asm)
        {
            StringCollection sc = new StringCollection();
            object[] objs = asm.GetCustomAttributes(typeof(CommandClassAttribute), true);
            Type[] tps;
            int numTypes = objs.Length;
            if (numTypes > 0)
            {
                tps = new Type[numTypes];
                for (int i = 0; i < numTypes; i++)
                {
                    CommandClassAttribute cca = objs[i] as CommandClassAttribute;
                    if (cca != null)
                    {
                        tps[i] = cca.Type;
                    }
                }
            }
            else tps = asm.GetExportedTypes();
 
            foreach (Type tp in tps)
            {
                MethodInfo[] meths = tp.GetMethods();
                foreach (MethodInfo meth in meths)
                {
                    objs = meth.GetCustomAttributes(typeof(CommandMethodAttribute), true);
                    foreach (object obj in objs)
                    {
                        CommandMethodAttribute attb = (CommandMethodAttribute)obj;
                        sc.Add(attb.GlobalName);
                    }
                }
            }
            string[] ret = new string[sc.Count];
            sc.CopyTo(ret, 0);
            return ret;
        }
 
        [LispFunction("net-GetAssembyCommands")]
        public ResultBuffer GetAssemblyCommands(ResultBuffer args)
        {
            if (args == null) return LL.GenerateError("Required argument not specified. No assembly specified.");
            Array argsArray = args.AsArray();
            if (argsArray.Length < 1) return LL.GenerateError("Required argument not specified. No assembly specified.");
            TypedValue argVal = (TypedValue)argsArray.GetValue(0);
            if (argVal.TypeCode != (int)LispDataType.Text) return LL.GenerateError("Required argument not text. No assembly specified.");
            String dllName = (String)argVal.Value;
            Assembly asm = GetAssembly(dllName);
            if (asm == null) return LL.GenerateError("Assembly " + dllName + " not found.");
            String[] cmds = GetCommands(asm);
            ResultBuffer rb = new ResultBuffer();
            foreach (String cmd in cmds) {
                rb.Add(new TypedValue((int)LispDataType.Text, cmd));
            }
            return rb;
        }
    }
}