WinForm带有用户选择;往哪走?

时间:2019-01-03 17:18:26

标签: c# winforms revit-api

我正在使用Jeremy Tammik的模板编写Revit的Addin,这为我创建了一个带按钮的功能区。 我正在读取一个Excel文件,选择行和起点。

My Ribbon My WinForm

用户完成输入后,他应该单击“完成”按钮(在我的情况下是缺少的^^),它应该在项目中生成一些FamilyInstances,而不关闭WinForm。

我的问题:

  • 我不能使用ShowDialog()对吗?因为这将为我创建一个模态表单,然后由于Revit窗口显示为灰色,所以我无法获得任何用户输入
  • 我不能使用Show()对吗?由于我的代码将继续

我的问题:

有人可以帮助我找到解决我问题的正确方法。我不太确定我的方法是否最终会奏效

到目前为止我尝试过的事情:

我试图在WinForm类中进行事务处理,但是我知道这行不通。
我正在考虑一个单独的类,该类将保存用户输入,第二个ExternalCommand类将执行该事务以创建FamilyInstances。
我查看了SDK,发现了ModelessForm_ExternalEvent,但我很难理解它...
我也找到了这些帖子,并试图理解它们:
https://forums.autodesk.com/t5/revit-api-forum/winform-focus-after-user-selection/td-p/7344224
Revit Pick element from WinForm

我的代码:

我有一个创建功能区和按钮的应用程序类:

class App : IExternalApplication
{
    public Result OnStartup(UIControlledApplication a)
    {
        // Ingenieurbau Reiter erstellen
        string engineerTabName = "Ingenieurbau Addin";
        a.CreateRibbonTab(engineerTabName);
        // Gruppen erstellen
        RibbonPanel lswPanel = a.CreateRibbonPanel(engineerTabName, "Lärmschutzwand");
        // Buttons hinzufügen
        AddLswPushButtons(lswPanel);
        AddTestPushButton(lswPanel);

        return Result.Succeeded;
    }

    private void AddLswPushButtons(RibbonPanel panel)
    {
        // Strings erzeugen
        string lswName1 = "LSW Daten einlesen";
        string lswAssemblyName1 = Assembly.GetExecutingAssembly().Location;
        string lswClassName1 = "IngenieurbauAddin1.Lsw.DataInput";

        // ButtonData erzeugen
        PushButtonData lswPushButtonData1 = new PushButtonData(lswName1, lswName1, lswAssemblyName1, lswClassName1);

        // ButtonData dem Panel hinzufügen und in einen PushButton umwandeln
        PushButton lswPushButton1 = panel.AddItem(lswPushButtonData1) as PushButton;

        // ButtonBild einfügen
        lswPushButton1.LargeImage = PngImageSource("IngenieurbauAddin1.Resources.Excel.png");
    }
}

我有一个显示形式的DataInput类:

[Transaction(TransactionMode.Manual)]
class DataInput : 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;

        // Form instanziieren
        DataInputForm dataInputForm = new DataInputForm(uidoc);
        // Form starten
        dataInputForm.Show();

        return Result.Succeeded;
    }
}

这是我的DataInputForm类(我知道PlaceFamily不起作用):

public partial class DataInputForm : System.Windows.Forms.Form
{
    private UIDocument UIDocument { get; set; }
    private Document Document { get; set; }

    public DataInputForm(UIDocument uiDocument)
    {
        UIDocument = uiDocument;
        Document = uiDocument.Document;
        InitializeComponent();
    }

    private void btnSelectPoint_Click(object sender, EventArgs e)
    {
        Hide();
        SelectionHelper selectionHelper = new SelectionHelper(Document);
        XYZ point = selectionHelper.SelectLineEndPoint(UIDocument);

        labelPointLocalXValue.Text = Math.Round(point.X, 3).ToString();
        labelPointLocalYValue.Text = Math.Round(point.Y, 3).ToString();
        labelPointLocalZValue.Text = Math.Round(point.Z, 3).ToString();
        Show();

        PlaceFamily(point);
    }

    private void PlaceFamily(XYZ point)
    {
        FamilySymbolHelper familySymbolHelper = new FamilySymbolHelper(Document);
        FamilyInstanceHelper familyInstanceHelper = new FamilyInstanceHelper(Document);
        FamilySymbol famSym = familySymbolHelper.FamilySymbolByName("Master-Bauteil");
        try
        {
            using (Transaction t = new Transaction(Document, "Familie platzieren"))
            {
                t.Start();
                FamilyInstance famInst = familyInstanceHelper.FamilyInstanceByPoint(famSym, point);
                t.Commit();
            }
        }
        catch (Exception e)
        {
            TaskDialog.Show("Error", e.Message);
        }
    }
}

我只想输入输入(Excelfile,线,点),然后它应该为我生成FamilyInstances。但是我现在有点卡住了^^。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您在ExternalCommand中使用了Form.Show()方法,因此它是无模式窗口。 在此窗口中,有一个按钮,用户单击它,然后选择一个终点。 好吧,问题是:您无法在Revit API上下文之外启动事务! 我建议您研究Revit API示例,其中有一个名为“无模式对话框”的目录。它清楚地演示了如何通过外部事件使用无模式对话框。 https://thebuildingcoder.typepad.com/blog/2015/12/external-event-and-10-year-forum-anniversary.html

答案 1 :(得分:0)

调用dataInputForm.Show();

时,只需将Revit窗口句柄传递给。

这是修改后的代码:

[Transaction(TransactionMode.Manual)]
internal class DataInput : IExternalCommand
{
    private static WindowHandle _hWndRevit = null;

    private void SetHandle()
    {
        if (null == _hWndRevit)
        {
            Process process
              = Process.GetCurrentProcess();

            IntPtr h = process.MainWindowHandle;
            _hWndRevit = new WindowHandle(h);
        }
    }

    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements)
    {

        SetHandle();

        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Application app = uiapp.Application;
        Document doc = uidoc.Document;

        // Form instanziieren
        var dataInputForm = new DataInputForm(uidoc);
        // Form starten
        if (_hWndRevit != null)
        {
            dataInputForm.Show(_hWndRevit);
        }
        else
        {
            return Result.Failed;
        }

        return Result.Succeeded;
    }
}

以下是Jeremy Tammik博客中有关此问题的主题: https://thebuildingcoder.typepad.com/blog/2009/02/revit-window-handle-and-modeless-dialogues.html