1

.NETReflectorを使用してAutoCAD相互運用機能.dllから_DAcadApplicationEventsインターフェイスを再作成し、コードをAutoCAD 2010で機能させました。問題は、これがAutoCAD2006と2012年以降まで互換性がある必要があることです。 。.NET 4.0を実行しているため、Embed Interop Typeをtrueに設定して、アプリケーションオブジェクトがリフレクション呼び出しメソッドを必要とせずにランタイムに適切に解決されるようにしていますが、イベントインターフェイスを機能させる方法を見つけることができないようです。 GUIDでのハードコーディング(アセンブリ固有)。


これは、元のAutoDesk.AutoCAD.Interop.dllから作成したカスタムインターフェイスです。

    <ComImport(), Guid("1F893620-C96D-4361-BBAF-A61D4144B7F8"), TypeLibType(CShort(&H1010))>
    Public Interface _DAcadApplicationEventsClone
        <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime), DispId(1)>
        Sub SysVarChanged(<[In](), MarshalAs(UnmanagedType.BStr)> ByVal SysvarName As String, <[In](),
                          MarshalAs(UnmanagedType.Struct)> ByVal newVal As Object)

        <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime), DispId(8)>
        Sub BeginQuit(<[In](), Out()> ByRef Cancel As Boolean)
    End Interface

Applicationオブジェクトは、次の方法で作成されます。

Public Application As Object

Application = Marshal.GetActiveObject("AutoCAD.Application")

次に、IConnectionPointを使用して複製されたインターフェイスに接続しています。

Dim acGUID As New Guid(GetInterfaceGUID())

Dim acConnectionPoint As ComTypes.IConnectionPoint = Nothing
TryCast(Application, ComTypes.IConnectionPointContainer).FindConnectionPoint(acGUID, acConnectionPoint)

Dim acSinkCookie As Integer
acConnectionPoint.Advise(Me, acSinkCookie)

ご覧のとおり、システムにインストールされているAutoCADのバージョンに応じて、適切なGUIDを動的に検索するために「GetInterfaceGUID」という関数を実行しています。ただし、返されたGUIDがインターフェイスのハードコードされたGUIDと一致しない場合は、とにかく機能しません。System._ComObjectを操作するためにさまざまなリフレクションのメソッドを試しましたが、これが私が近づいた唯一のメソッドです。私はかなり成功に近づいているように感じます、そしてこの時点でどんな入力でもいただければ幸いです。前もって感謝します。

-ロック

4

1 に答える 1

0

ご覧のとおり、AutoCAD COM相互運用機能はバージョンに依存し、ビット数(32ビット/ 64ビット)にも依存します。次のように、AutoCAD .NETアプリケーションイベントを使用してこれらの問題を解決してみませんか?

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;

using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;

using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcadWindows = Autodesk.AutoCAD.Windows;

namespace AcadNetAddinByVS2010
{
    public class AppEvents1
    {
        bool mSuppressMsgOutput = false;

        void OptionalMessageOutput(EventArgs obj, string eventName)
        {
            if (!mSuppressMsgOutput)
            {
                string msg = string.Format("Info about the {0} event:{1}", eventName, Environment.NewLine);

                PropertyInfo[] piArr = obj.GetType().GetProperties();
                foreach (PropertyInfo pi in piArr)
                {
                    string attValue = pi.GetValue(obj, null).ToString();
                    msg += string.Format("\t{0}: {1}{2}", pi.Name, attValue, Environment.NewLine);
                }

                MessageBox.Show(msg, "Event: " + eventName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                System.Diagnostics.Debug.Write(msg);
            }
        }

        public void Register()
        {
            AcadApplication.SystemVariableChanging += AppEvent_SystemVariableChanging_Handler;
            AcadApplication.SystemVariableChanged += AppEvent_SystemVariableChanged_Handler;
            AcadApplication.QuitWillStart += AppEvent_QuitWillStart_Handler;
            AcadApplication.QuitAborted += AppEvent_QuitAborted_Handler;
            AcadApplication.PreTranslateMessage += AppEvent_PreTranslateMessage_Handler;
            AcadApplication.LeaveModal += AppEvent_LeaveModal_Handler;
            AcadApplication.Idle += AppEvent_Idle_Handler;
            AcadApplication.EnterModal += AppEvent_EnterModal_Handler;
            AcadApplication.DisplayingOptionDialog += AppEvent_DisplayingOptionDialog_Handler;
            AcadApplication.DisplayingDraftingSettingsDialog += AppEvent_DisplayingDraftingSettingsDialog_Handler;
            AcadApplication.DisplayingCustomizeDialog += AppEvent_DisplayingCustomizeDialog_Handler;
            AcadApplication.BeginQuit += AppEvent_BeginQuit_Handler;

        }

        public void UnRegister()
        {
            AcadApplication.SystemVariableChanging -= AppEvent_SystemVariableChanging_Handler;
            AcadApplication.SystemVariableChanged -= AppEvent_SystemVariableChanged_Handler;
            AcadApplication.QuitWillStart -= AppEvent_QuitWillStart_Handler;
            AcadApplication.QuitAborted -= AppEvent_QuitAborted_Handler;
            AcadApplication.PreTranslateMessage -= AppEvent_PreTranslateMessage_Handler;
            AcadApplication.LeaveModal -= AppEvent_LeaveModal_Handler;
            AcadApplication.Idle -= AppEvent_Idle_Handler;
            AcadApplication.EnterModal -= AppEvent_EnterModal_Handler;
            AcadApplication.DisplayingOptionDialog -= AppEvent_DisplayingOptionDialog_Handler;
            AcadApplication.DisplayingDraftingSettingsDialog -= AppEvent_DisplayingDraftingSettingsDialog_Handler;
            AcadApplication.DisplayingCustomizeDialog -= AppEvent_DisplayingCustomizeDialog_Handler;
            AcadApplication.BeginQuit -= AppEvent_BeginQuit_Handler;

        }

        public void AppEvent_BeginQuit_Handler(object sender, EventArgs e)
        {
            OptionalMessageOutput(e, "BeginQuit");

        }

        public void AppEvent_DisplayingCustomizeDialog_Handler(object sender, TabbedDialogEventArgs e)
        {
            OptionalMessageOutput(e, "DisplayingCustomizeDialog");

        }

        public void AppEvent_DisplayingDraftingSettingsDialog_Handler(object sender, TabbedDialogEventArgs e)
        {
            OptionalMessageOutput(e, "DisplayingDraftingSettingsDialog");

        }

        public void AppEvent_DisplayingOptionDialog_Handler(object sender, TabbedDialogEventArgs e)
        {
            OptionalMessageOutput(e, "DisplayingOptionDialog");

        }

        public void AppEvent_EnterModal_Handler(object sender, EventArgs e)
        {
            OptionalMessageOutput(e, "EnterModal");

        }

        public void AppEvent_Idle_Handler(object sender, EventArgs e)
        {
            OptionalMessageOutput(e, "Idle");

        }

        public void AppEvent_LeaveModal_Handler(object sender, EventArgs e)
        {
            OptionalMessageOutput(e, "LeaveModal");

        }

        public void AppEvent_PreTranslateMessage_Handler(object sender, PreTranslateMessageEventArgs e)
        {
            OptionalMessageOutput(e, "PreTranslateMessage");

        }

        public void AppEvent_QuitAborted_Handler(object sender, EventArgs e)
        {
            OptionalMessageOutput(e, "QuitAborted");

        }

        public void AppEvent_QuitWillStart_Handler(object sender, EventArgs e)
        {
            OptionalMessageOutput(e, "QuitWillStart");

        }

        public void AppEvent_SystemVariableChanged_Handler(object sender, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
        {
            OptionalMessageOutput(e, "SystemVariableChanged");

        }

        public void AppEvent_SystemVariableChanging_Handler(object sender, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangingEventArgs e)
        {
            OptionalMessageOutput(e, "SystemVariableChanging");

        }

    }
}

ちなみに、選択したIExtensionApplication実装のイベント登録コードは次のとおりです。

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;

using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcadWindows = Autodesk.AutoCAD.Windows;

#endregion

namespace AcadNetAddinByVS2010
{
    public class ExtApp : IExtensionApplication
    {
        #region IExtensionApplication Members

        public void Initialize()
        {
            //TODO: add code to run when the ExtApp initializes. Here are a few examples:
            //          Checking some host information like build #, a patch or a particular Arx/Dbx/Dll;
            //          Creating/Opening some files to use in the whole life of the assembly, e.g. logs;
            //          Adding some ribbon tabs, panels, and/or buttons, when necessary;
            //          Loading some dependents explicitly which are not taken care of automatically;
            //          Subscribing to some events which are important for the whole session;
            //          Etc.

            mAppEvents1.Register();
        }

        public void Terminate()
        {
            //TODO: add code to clean up things when the ExtApp terminates. For example:
            //          Closing the log files;
            //          Deleting the custom ribbon tabs/panels/buttons;
            //          Unloading those dependents;
            //          Un-subscribing to those events;
            //          Etc.

            mAppEvents1.UnRegister();
        }

        #endregion

        AppEvents1 mAppEvents1 = new AppEvents1();
    }
}
于 2012-06-10T08:13:23.383 に答える