首页 > 解决方案 > 初始化 Win。来自 Revit 按钮和执行命令的表单

问题描述

任何人都可以告诉我如何(如果可能的话)实现一个我已经编写的类,按下 Revit 插件功能区中的按钮时显示的表单中的按钮?附件是我到目前为止所取得的图像的链接。

我在 Visual Studio 中使用 C# 语言实现 ExternalApplication 类。

谢谢

            namespace PEASA_TOOLS_2021
            {
                public class PEASA_TOOLS_2021 : IExternalApplication
                {
                    static void AddRibbonPanel(UIControlledApplication application)
                        {
                          //Create panel
                          RibbonPanel ribbonPannel4 = application.CreateRibbonPanel(tabName, "Pruebas");
                          //Button for execute Form.
                          PushButtonData pbFormdata = new 
                          PushButtonData("cmdFormData","Extracción de Data", thisAssemblyPath, "PEASA_TOOLS_2021.DATA_EXTRACTION.DataExtractionButton");
                          PushButton pbForm = ribbonPannel4.AddItem(pbFormdata) as PushButton;
                          BitmapImage pruebasImage = new BitmapImage(new Uri("pack://application:,,,/PEASA_TOOLS_2021;component/RECURSOS/PRUEBAS.png"));
                          pbForm.LargeImage = pruebasImage;
                         }
                 }
             }

1

标签: c#visual-studiorevit-api

解决方案


我去过那里,我知道你的挣扎。这是我到目前为止所做的,它对我有用,它可能不是最好的解决方案,但它是我唯一的解决方案。

首先,您需要将这些变量添加到 Form 的部分类中

[Transaction(TransactionMode.Manual)] 
[Regeneration(RegenerationOption.Manual)]
public partial class Form1 : System.Windows.Forms.Form
{
    private UIApplication uiapp;
    private UIDocument uidoc;
    private Document doc;
    private ExternalCommandData commandData;
    private string message;
    private ElementSet elements;

然后,您必须将表单构造函数编辑为如下所示:

public Form1(ExternalCommandData commandData,ref string message, ElementSet elements)
    {
        InitializeComponent();
        this.commandData = commandData;
        this.elements = elements;
        uiapp = commandData.Application;
        uidoc = uiapp.ActiveUIDocument;
        doc = uidoc.Document;
    }

然后,您将在 Form 部分类下创建一个函数,该函数就是您在其中编写命令的函数:

它会是这样的:

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
     write your code here
     return Result.Succeeded;
     }

那么您只需调用 Button1_Click 函数下的执行函数

protected internal  void Button1_Click(object sender, EventArgs e)
    {
        Execute(commandData, ref message, elements);       
    }

现在对于在按下 PushButton 后调用 Form 的类:

Class DataExtractionButton : IExternalCommand
{
public Result Execute (ExternalCommandData commandData, ref string message, ElementSet elements)
{
        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Application app = uiapp.Application;
        Document doc = uidoc.Document;
        ExternalCommandData revit = commandData;
var window = new Form1(commandData, ref message, elements);
window.ShowDialog();
return Result.Succeeded;
    }
}

这应该是这样做的方法。

我希望这可以帮助你,它适用于我的情况。


推荐阅读