using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.IO;
using System.Reflection;
namespace Application1
{
public class AppInitClass : IExtensionApplication
{
/// <summary>
/// Статический конструктор класса
/// </summary>
static AppInitClass()
{
// составляем путь к папке с библиотечными DLL
// в данном случае %ProgramData%\Autodesk\ApplicationPlugins\AppLibrary
string appsDirPath = Path.Combine
(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"Autodesk", "ApplicationPlugins", "AppLibrary");
// грузим библиотечные DLL
AssemblyLoad(Path.Combine(appsDirPath, "MyLibrary1.dll"));
AssemblyLoad(Path.Combine(appsDirPath, "MyLibrary2.dll"));
AssemblyLoad(Path.Combine(appsDirPath, "MyLibrary3.dll"));
}
/// <summary>
/// Безопасный метод загрузки DLL
/// </summary>
/// <param name="path">Путь к DLL</param>
/// <returns>true - загружена, false - не загружена</returns>
static bool AssemblyLoad(string path)
{
try
{
Assembly.LoadFrom(path);
return true;
}
catch (System.Exception ex)
{
Application.ShowAlertDialog($"Exception: {ex.Message}");
return false;
}
}
/// <summary>
/// Метод, выполняемый AutoCAD при загрузке сборки
/// </summary>
public void Initialize()
{
// Вызываем методы из библиотечных DLL, по одному из каждой
Document adoc = MyLibrary1.Support1.GetActiveDocument(Application.DocumentManager);
string appName = MyLibrary2.Support2.GetAppName
(Assembly.GetExecutingAssembly()) ?? "*no_app_name*";
string adocName = MyLibrary3.Support3.GetAdocName(adoc) ?? "*no_adoc_name*";
// Выводим сообщение
Application.ShowAlertDialog($"{appName} loaded into {adocName}!");
}
public void Terminate()
{
}
/// <summary>
/// Командный метод для проверки загруженности сборки
/// </summary>
[CommandMethod("App1Test")]
public void Run()
{
Document adoc = MyLibrary1.Support1.GetActiveDocument(Application.DocumentManager);
string appName = MyLibrary2.Support2.GetAppName
(Assembly.GetExecutingAssembly()) ?? "*no_app_name*";
string adocName = MyLibrary3.Support3.GetAdocName(adoc) ?? "*no_adoc_name*";
Application.ShowAlertDialog($"{appName} test command in {adocName}: OK");
Editor ed = adoc?.Editor;
ed?.WriteMessage("\n{0} test command in {1}: OK", appName, adocName);
}
}
}