代码之家  ›  专栏  ›  技术社区  ›  Cflux Matt

Revit API/WinForms-在Windows窗体关闭后,试图传递值时被卡在蓝色Spinner中

  •  0
  • Cflux Matt  · 技术社区  · 6 年前

    我试图从我试图设置的WinForms中传递一个值,但是一旦窗体关闭,Revit在单击继续按钮后会卡在蓝色微调器中,并且不会返回运行原始程序。

    我四处查看了一下,但没有找到任何可以让它返回程序的东西。WinForms上的大多数(如果不是全部)教程与处理Revit无关。我发现/观察到的大多数方法都是将值从一种形式传递到另一种形式,尽管 I found this one 尽管它有点不同。

    这是我的代码:

    表格1.cs

    public partial class Form1 : System.Windows.Forms.Form
        {
            private UIApplication uiapp;
            private UIDocument uidoc;
            private Autodesk.Revit.ApplicationServices.Application app;
            private Document doc;
    
            private string myVal;
    
            public string MyVal
            {
                get { return myVal; }
                set { myVal = value; }
            }
    
            public Form1(ExternalCommandData commandData)
            {
                InitializeComponent();
    
                uiapp = commandData.Application;
                uidoc = uiapp.ActiveUIDocument;
                app = uiapp.Application;
                doc = uidoc.Document;
        }
    
        public delegate void delPassData(System.Windows.Forms.ComboBox text);
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //Create a filter to get all the title block types.
                FilteredElementCollector colTitleBlocks = new FilteredElementCollector(doc);
                colTitleBlocks.OfCategory(BuiltInCategory.OST_TitleBlocks);
                colTitleBlocks.WhereElementIsElementType();
    
                foreach(Element x in colTitleBlocks)
                {
                    comboBox1TitleBlockList.Items.Add(x.Name);
                }
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
    
            private void button1Continue_Click(object sender, EventArgs e)
            {
    
                MyVal = comboBox1TitleBlockList.Text;  // is returning with Titleblock.Name
    
                button1Continue.DialogResult = DialogResult.OK;
                Debug.WriteLine("OK button was clicked.");
                Close();
                //Dispose();
                return;
            }
    
            private void button2Cancel_Click(object sender, EventArgs e)
            {
                button2Cancel.DialogResult = DialogResult.Cancel;
                Debug.WriteLine("Cancel button was clicked");
                //Dispose();
    
                Close();
                return;
            }
    
            private void comboBox1TitleBlockList_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
        }
    

    命令cs

     Form1 form1 = new Form1(commandData);
    
     System.Windows.Forms.Application.Run(new Form1(commandData)); // not sure if this line is required.
    
     form1.ShowDialog();
    
     String elementString = form1.MyVal; //<---- this is returning null. need to figure out how to pass value from form1 back to Command.cs
    
    if (elementString != null)
       {
           elementString = form1.MyVal.ToString();
           Element eFromString = doc.GetElement(elementString);
           titleBlockId = eFromString.Id;                                
       }
       else
       {
            titleBlockId = collector.FirstElementId();
       }
    
    

    感谢任何帮助/指导。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Jeremy Tammik    6 年前

    这永远行不通 not sure if this line is required 试图。。。我甚至无法描述它试图实现什么。

    Revit API完全由事件驱动,您的Revit插件只能从有效的Revit API上下文中调用Revit API。此类上下文仅在与Revit事件关联的事件处理程序中提供。

    您的代码不订阅任何此类事件。因此,它不是Revit附加模块。

    请通过以下方式工作 Revit API getting started material 并按照Revit外接程序建筑要求构建外接程序。