AutoCAD c# dll中包装其他的dl文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IFoxCAD.Cad;
using IFoxCAD.Collections;
using IFoxCAD.Linq;

using Autodesk.AutoCAD.Runtime;
using System.Reflection;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices.Core;

namespace 嵌入IfoxcadDll测试
{
    public class MianClass : IExtensionApplication
    {
        public void Initialize()
        {
            var ass = Assembly.GetExecutingAssembly();
            Type type = MethodBase.GetCurrentMethod().DeclaringType;
            var NameSpaceName = type.Namespace;
            var resourceNames = new List<string>() { "IFoxCAD.Basal.dll", "IFoxCAD.Cad.dll", "System.ValueTuple.dll" };
            AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
            {
                try
                {
                    var assName = e.Name.Split(',')[0];
                    if (resourceNames.Any(c => c == assName + ".dll"))
                    {
                        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{NameSpaceName}.Resources.{assName}.dll"))
                        {
                            var bytes = new byte[stream.Length];
                            stream.Read(bytes, 0, (int)stream.Length);
                            return Assembly.Load(bytes);//加载资源文件中的dll,代替加载失败的程序集
                        }
                    }
                    else
                    {
                        Application.ShowAlertDialog($"缺少dll{assName}");
                        return null;
                    }

                    //throw new DllNotFoundException(assName);
                }
                catch (System.Exception ex)
                {
                    Application.ShowAlertDialog(ex.StackTrace);
                    return null;
                }
            };
        }

        public void Terminate()
        {
            throw new NotImplementedException();
        }
    }

    public class TestClass
    {

        [CommandMethod("mytt")]
        public void TestClassMethods()
        {
            DBTrans dbtr = new DBTrans();
            Line l = new Line(Point3d.Origin, new Point3d(200, 200, 0));
            dbtr.CurrentSpace.AddEntity(l);
        }

    }
}

 

posted @ 2022-05-21 20:55  南胜NanSheng  阅读(463)  评论(0编辑  收藏  举报