亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Autocad .Net 集成 - 從 Excel 調用簡單函數時成功編譯 DLL 錯誤

Autocad .Net 集成 - 從 Excel 調用簡單函數時成功編譯 DLL 錯誤

C#
慕森卡 2023-08-13 15:59:34
我正在處理一個需要在 Excel 中集成 Autocad 的項目。我編譯了一個 DLL,并在 Excel 中成功引用,但調用一個簡單函數失敗。                                         COM接口沒有問題;該項目已勾選這些,我可以成功地從 Excel 調用一個簡單的“hello world”測試函數。我還擁有 C# 項目中所有正確的引用。多余的參考資料是后續工作所需要的。該函數在這一行失敗:var acDocMgr = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;如果無論 Autocad 應用程序是否打開都失敗。請幫忙。using System.Collections.Generic;using System.Runtime.InteropServices;using System.Linq;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Runtime;using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;using Autodesk.AutoCAD.ApplicationServices;[assembly: CommandClass(typeof(AutocadHandler.MyCommands))]namespace AutocadHandler{    [ClassInterface(ClassInterfaceType.AutoDual)]    public class MyCommands    {        public static void TestFunction()        {                                   string strFileName = "C:\\Users\\CORE I7\\Documents\\Drawing2XLS.dwg";            var acDocMgr = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;            acDocMgr.Open(strFileName, false);            acDocMgr.MdiActiveDocument.Editor.WriteMessage("Hello Excel");        }    }}Excel 返回的錯誤是:運行時錯誤'-2146233036 (80131534)':自動化錯誤
查看完整描述

1 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

您是否嘗試從 Excel 中運行代碼,并嘗試讓 Excel 打開 AutoCAD 來操作繪圖?我認為這行不通。您可以采用另一種方式,打開 AutoCAD,加載插件,然后通過 API 將 AutoCAD 中的信息提供給 Excel。AutoCAD API 需要運行 AutoCAD(或 ACCORECONSOLE,這是 AutoCAD 的命令行版本,但這需要一些額外的管道)才能對圖形文件執行任何操作。如果是 AutoCAD,而不是 ACCORECONSOLE,則通常需要至少打開一張圖形(..DocumentManager.MdiActiveDocument)。然后,您可以使用文檔管理器打開其他文檔(假設您有權限這樣做)。


    /// <summary>

    /// Look through the Application's Document manager for a Document object with the given name.  If found return it,

    /// else open the drawing/Document and return it.

    /// </summary>

    /// <param name="name">The name to look for in the collection</param>

    /// <returns>An AutoCAD Document object.</returns>

    public static ACADApp.Document GetDocumentByName(string name)

    {

        try

        {

            foreach (ACADApp.Document doc in ACADApp.Application.DocumentManager)

            {

                if (doc.Database.Filename.ToUpper() == name.ToUpper() || doc.Name.ToUpper() == name.ToUpper())

                {

                    return doc;

                }

            }

            return ACADApp.Application.DocumentManager.Open(name);


        }

        catch (System.Exception ex)

        {

            TBExceptionManager.HandleException(name, ex);

            return null;

        }

    }


查看完整回答
反對 回復 2023-08-13
  • 1 回答
  • 0 關注
  • 164 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號