如何去除Revit中含WinForm的外部命令的加载项安全弹窗
Question
您好,我已创建多个External Command并将其停靠在自定义Revit选项卡中,但为其中一个命令添加WinForm后,启动Revit时出现了额外的Security addin安全弹窗(如GIF所示)。请问是否有办法消除该弹窗?以下是我的相关代码:
外部命令接口代码:
[Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class DeleteUnusedFilter : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; var Form = new DeleteFilter(commandData); try { var fltr = new FilterData(doc).GetUnusedFilter(); if (fltr.Count == 0) { MessageInfo.Display("No unused View Filters in Document"); Form.Close(); return Result.Cancelled; } Form.ShowDialog(); return Autodesk.Revit.UI.Result.Succeeded; } catch (Exception ex) { message = ex.Message; return Autodesk.Revit.UI.Result.Failed; } } }
WinForm代码:
public partial class DeleteFilter : System.Windows.Forms.Form { Document doc; List<Element> FiltersToDelete; public DeleteFilter(ExternalCommandData commandData) { InitializeComponent(); doc = commandData.Application.ActiveUIDocument.Document; } private void DeleteFilter_Load(object sender, EventArgs e) { callonload(); } }
插件注册代码:
//ribbon for delete unused filter string path3 = Assembly.GetExecutingAssembly().Location; var button3 = new PushButtonData("Button2", "Delete Unused Filter", path2, "RevitCode.DeleteUnusedFilter"); var panel3 = application.CreateRibbonPanel("Angelo Tools", "Delete"); Uri imagePath3 = new Uri(@"C:\ProgramData\Autodesk\Revit\Addins\2019\icon\DeleteFtr.png"); BitmapImage image3 = new BitmapImage(imagePath3); button3.ToolTip = "Delete Unused View Filters"; PushButton Push_button3 = panel3.AddItem(button3) as PushButton; Push_button3.LargeImage = image3; return Result.Succeeded;
Answer
Hey there! That security popup is definitely a nuisance when you're just trying to get your custom plugin working smoothly. Let's break down why this is happening and walk through the fixes to get rid of it for good:
Why This Happens
Revit has a built-in security system that scans add-ins for potential risks. When you added a WinForm (which relies on System.Windows.Forms, a non-Revit core library) to your external command, Revit might flag your unsigned assembly as untrusted. Also, loading the WinForm early in the Execute method can trigger security checks before Revit fully validates your plugin.
Solutions to Eliminate the Popup
1. Strong-Name Sign Your Assembly (Most Recommended)
This is the most reliable fix because Revit trusts signed assemblies far more. Here's how to do it in Visual Studio:
- Right-click your plugin project → Properties → Signing tab
- Check the box that says "Sign the assembly"
- Click New under "Choose a strong name key file"
- Name your key file, optionally set a password (for extra security), then click OK
- Recompile your plugin and replace the old assembly in your Revit Addins folder
2. Adjust WinForm Loading Logic
Your current code initializes the WinForm before checking if there are any unused filters, which can trigger unnecessary security checks. Let's refine that:
[Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class DeleteUnusedFilter : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; List<Element> unusedFilters; try { // First fetch the data, only load the form if needed unusedFilters = new FilterData(doc).GetUnusedFilter(); if (unusedFilters.Count == 0) { MessageInfo.Display("No unused View Filters in Document"); return Result.Cancelled; } // Use 'using' to ensure proper form disposal after use using (var deleteFilterForm = new DeleteFilter(commandData, unusedFilters)) { deleteFilterForm.ShowDialog(); } return Autodesk.Revit.UI.Result.Succeeded; } catch (Exception ex) { message = ex.Message; return Autodesk.Revit.UI.Result.Failed; } } }
Update your WinForm constructor to accept pre-fetched filters (avoids re-fetching data in the form):
public partial class DeleteFilter : System.Windows.Forms.Form { Document doc; List<Element> FiltersToDelete; // Updated constructor to receive pre-fetched unused filters public DeleteFilter(ExternalCommandData commandData, List<Element> unusedFilters) { InitializeComponent(); doc = commandData.Application.ActiveUIDocument.Document; FiltersToDelete = unusedFilters; } private void DeleteFilter_Load(object sender, EventArgs e) { callonload(); } }
3. Verify Plugin Location & Permissions
- Make sure your plugin is stored in the official Revit Addins folder (e.g.,
C:\ProgramData\Autodesk\Revit\Addins\2019\) - Ensure the folder has read/write permissions (Revit needs to access the assembly and resources like your icon)
- Avoid loading plugins from temporary folders, network drives, or untrusted locations—Revit flags these much more aggressively
4. Temporary: Adjust Revit Security Settings (Not for Production)
If you're just testing and need a quick fix, you can lower Revit's security level temporarily:
- Open Revit → Options → Add-Ins → Security
- Set "Load unsigned external applications" to Always load
- Note: This reduces overall security, so revert this setting when you're done testing or deploying your plugin.
Final Notes
- Keep WinForm interactions separate from Revit API operations as much as possible—only access the Revit document when you need to perform transactions (like deleting filters)
- Ensure all project references are up-to-date and compatible with your target Revit version
内容的提问来源于stack exchange,提问作者G-elyas

