不允许 API 上下文之外的 Revit 外部应用程序 运行

Revit External application running outside of API context is not allowed

我正尝试在我的代码中启动一个事务,但正如您在标题中看到的那样,它生成了一个错误。我已经阅读了很多关于外部应用程序的帖子,但不正确地知道它是如何工作的。

这是我用来开始交易的代码: 在我的表单中:

private void Loading_FA_only()//Lance l'import en boucle suivant les items dans la file d'attente
    {
        foreach(FileInfo fi in selected_rfas_donnees)
        {
            Loading_Family.famille = fi;
            Loading_Family lf = new Loading_Family();
            lf.Execute(Command.uiapplication);
            Listing_succes(Loading_Family.succes, fi);
        }
        selected_rfas.Clear();
        selected_rfas_donnees.Clear();
        Update_Compteur();
    }

这就是我尝试执行的内容:

public class Loading_Family : IExternalEventHandler
{
    public static FileInfo famille;
    public static bool succes;
    public void Execute(UIApplication uiapp)
    {
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Application app = uiapp.Application;
        Document doc = uidoc.Document;

        // Access current selection

        Selection sel = uidoc.Selection;

        // Retrieve elements from database

        FilteredElementCollector col
          = new FilteredElementCollector(doc)
            .WhereElementIsNotElementType()
            .OfCategory(BuiltInCategory.INVALID)
            .OfClass(typeof(Wall));

        // Filtered element collector is iterable

        foreach (Element e in col)
        {
            Debug.Print(e.Name);
        }

        // Modify document within a transaction

        succes = false;
        string nom_famille = famille.Name.Remove(famille.Name.Length - 4, 4);
        FilteredElementCollector familles_doc = new FilteredElementCollector(doc).OfClass(typeof(Family));
        foreach (Element elmt in familles_doc)
        {
            if (elmt.Name == nom_famille)
            {
                var result = TaskDialog.Show("CPF - Importation", "Il semblerait que " + nom_famille + " soit déjà présent dans votre projet.\nVoullez-vous le remplacer ?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
                if (result == TaskDialogResult.Yes)
                {
                    using (Transaction tr = new Transaction(doc, "Importer la famille"))
                    {
                        tr.Start();
                        doc.LoadFamily(famille.FullName);
                        tr.Commit();
                        tr.Dispose();
                    }
                }
            }
            else
            {
                using (Transaction tr = new Transaction(doc, "Importer la famille"))
                {
                    tr.Start();
                    doc.LoadFamily(famille.FullName);
                    tr.Commit();
                    tr.Dispose();
                }
            }
        }
        familles_doc = new FilteredElementCollector(doc);
        foreach (Element elmt in familles_doc)
        {
            if (elmt.Name == nom_famille) { succes = true; }
            else { succes = false; }
        }

        using (Transaction tx = new Transaction(doc))
        {
            tx.Start("Transaction Name");
            tx.Commit();
        }

    }
    public string GetName()
    {
        return "my event";
    }
}

我对此很绝望。我绝对不知道他们的“ExternalEventHandler”或“ExternalApplication”是如何工作的。 感谢您的帮助:)

请完成 Revit API getting started material。这解释了 Revit API 及其体系结构所需的所有基础知识,包括如何实施和使用外部应用程序和命令。

请注意 the Revit API cannot be used at all outside of a valid Revit API context,这样的上下文仅由 Revit.exe 在 运行 加载 Revit add-in 时的回调问题提供。

因此,您在描述中所做的声明是预期和需要的:Revit 外部应用程序永远不能 运行 在有效的 Revit API 上下文之外,因此确实是不允许的。

请在提交前检查您的文本,尤其是标题,因为拼写错误(例如描述中的拼写错误)会导致更难找到问题。

很简单。使用外部事件并确保在 MyForm 的构造函数中声明了外部事件。我个人从未使用过 ShowDialog,因为它会阻止用户访问 UI.

的其余部分