Revit I 外部应用程序错误 - 似乎根本 运行

Revit IExternal Application Error- doesn't seems to run at all

我们是否应该使用外部应用程序来正确注册事件?

我还尝试将两个断点放在启动模块中,另一个放在导出模块中。 第一个回复等我继续下一个没回复(希望没运行线)

此外,我曾手动尝试将插件文件复制到插件位置以避免任何 post 构建事件错误,但似乎仍然不起作用。

你能告诉我我做错了什么吗?

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.DB.Events;
using System.IO;

namespace UserDataSheet
{
    [Transaction(TransactionMode.Manual)]

    [Regeneration(RegenerationOption.Manual)]

    public class UserDataSheetclass : IExternalApplication
    {
        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }

        public Result OnStartup(UIControlledApplication application)
        {
            try
            {
                // Register event. 
                application.ControlledApplication.DocumentOpened += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);
                return Result.Succeeded;
            }
            catch (Exception)
            {
                return Result.Failed;
            }
        }
        public void ExportLog(object sender, DocumentOpenedEventArgs args)
        {
            var doc = sender as Document;
            var isFamilyDoc = doc.IsFamilyDocument;

            // variables to use 
            string RevitUserName = "";
            DateTime OpenTime = new DateTime();
            string localUserName = "";
            string filename = "";
            string filepath = "";
            string content = "";

            if (isFamilyDoc == false)
            {

                var IsloggedIn = Autodesk.Revit.ApplicationServices.Application.IsLoggedIn;
                if (IsloggedIn == true )//&& doc.IsModelInCloud == true)
                {
                    // use variables
                    filepath = doc.PathName;
                    filename = doc.Title;
                    RevitUserName = doc.Application.Username;
                    OpenTime = DateTime.Now;
                    localUserName = System.Environment.MachineName;

                }
                content = string.Format("Date and time : {0} \n Revit UserName : {1} \n Local PC UserName : {2} \n FileName : {3} \n FilePath : {4} "
                    , OpenTime.ToString(), RevitUserName, localUserName, filename, filepath);
                TaskDialog.Show("Model Open Writer info", "user and file details : \n " + content);
            }
            var writefilepath = Path.GetTempPath();
            var Writefile = writefilepath + "//records.txt"; 
            FileStream fs = new FileStream(Writefile, FileMode.Append);
            StreamWriter writer = new StreamWriter(fs);
            writer.Write(content);
            writer.Close();
            File.OpenRead(Writefile);

        }  
    }
}

首先,您可以完全删除TransactionModeRegenerationOption。后者完全过时了,在任何地方都什么都不做。前者仅在声明外部命令时有用。它在外部应用程序的上下文中无用并被忽略。

其次,解决你的问题:你可以在OnStartup开头设置断点。如果未命中断点,则根本不会加载您的加载项。您的加载项注册可能有问题,例如,在加载项清单 *.addin 文件中。

回到第一步,即完成 getting started material and the developer guide instructions on registering and loading a Revit add-in

谢谢,杰里米它成功了

首先我很抱歉添加这个作为答案(我不知道如何在评论中添加代码)

当我删除我的 Addin 文件并重新创建它时它起作用了,它可能是我在其中犯了一些错误。

同时我从示例中复制了以下代码并使用了它,老实说我看不懂这行代码。

"public void ExportLog(object sender, DocumentOpenedEventArgs args)"

你能指出解释这部分的正确来源吗?我在这里有三个问题:

  1. sender 是什么对象类型,args 是应用程序类型吗?
  2. 如何向此方法添加第三个参数,比如我希望用户输入一个字符串来命名数据复制到的文件。
  3. 我可以这样做吗

           var newEvent  = new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);
    

而不是

application.ControlledApplication.DocumentOpened += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);

为什么每次打开 Revit 的新实例时,所有示例都使用 += 来注册事件?

感谢您的帮助。

如果遇到 OnStartup 中的断点,则表明您的加载项正在正确加载,这意味着加载项清单 *addin 文件没有问题。所以,你不必为此担心。顺便说一下,VisibilityMode 标签不用于外部应用程序。

  1. sender的class你可以通过在ExportLog开头设置断点并在调试器中查看来自己看到sender
  2. 不,您不能修改事件处理程序的签名。由Revit预先确定API.
  3. 是的。

在我看来,在继续处理此任务之前,您可以通过多了解一些 C# 和 .NET 编程的基本知识来为自己节省一些时间和精力。