我们应该在外部应用程序中使用某些东西来正确注册事件吗?
我还尝试过将两个断点放在启动模块内,将另一个断点放在导出模块内。第一个响应并等待我继续,而第二个则没有响应(希望该行未运行)
而且,我已经手动尝试将加载项文件处理到加载项位置,以避免任何构建后事件错误,但似乎仍然无法正常工作。
你能告诉我我在做什么错吗。
这里是代码:
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);
}
}
}
首先,您可以完全删除TransactionMode
和RegenerationOption
。后者已完全过时,并且在任何地方都什么也不做。前者仅在声明外部命令时有用。它是无用的,在外部应用程序的上下文中将被忽略。
第二,解决您的问题:您可以在OnStartup
的开头设置一个断点。如果没有命中断点,则根本不会加载您的加载项。外接程序注册可能有问题,例如,在外接程序清单*.addin
文件中。
回到第一个平方,即遍历getting started material上的developer guide和registering and loading a Revit add-in指令。
首先,我很抱歉将此添加为答案(我不知道如何在注释中添加代码)如建议的那样,我在onStartup方法的第一行设置了断点,它在执行过程中命中,但是未执行导出日志(未击中其内部的断点)
这是我的插件代码:
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<AddIn Type="Application">
<Name>User Info</Name>
<FullClassName>UserDataSheet.UserDataSheetclass</FullClassName>
<Text>User Info</Text>
<Description>records user info on opening a document</Description>
<VisibilityMode>AlwaysVisible</VisibilityMode>
<Assembly>C:\Users\Hocon-TechIndia\source\repos\UserDataSheet\bin\Debug\UserDataSheet.dll</Assembly>
<AddInId>E52DC981-5FBE-42A2-8E9A-5AD4C7388CAA</AddInId>
<VendorId>Hocon-TechIndia</VendorId>
<VendorDescription>new vendor_me</VendorDescription>
</AddIn>
</RevitAddIns>
如果击中OnStartup
中的断点,则说明您的加载项正确加载,这意味着加载项清单*addin
文件是可以的。因此,您不必为此担心。顺便说一下,VisibilityMode
标签不用于外部应用程序。