【问题标题】:Revit External application running outside of API context is not allowed不允许在 API 上下文之外运行 Revit 外部应用程序
【发布时间】:2021-05-09 13:22:24
【问题描述】:

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

这是我用来启动交易的代码: 在我的表格中:

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”是如何工作的。 感谢您的帮助:)

【问题讨论】:

    标签: c# revit-api


    【解决方案1】:

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

    【讨论】:

    • 我知道这是我必须做的,但不知道如何:) 我尝试在互联网上搜索代码示例,但对我发现的内容不太满意。我还看了你的video 几次,看看你是如何工作的,但是,嗯,有我的帖子 :)
    • 从 github 分叉我的 19 个技术项目。它使用具有 18 个离散外部事件代码的无模式形式。 github.com/joshnewzealand/Revit-API-Playpen-CSharp 。只是第一次很难。
    • 我看了你的链接,我试试this。我让你看到问题。我想我不清楚我读到了什么:)
    • ExternalEvent.Create(lf) 必须进入 Interface_CPF() 构造方法(不是加载事件)。
    • 当然。我的 Loading_selected 方法位于 Interface_CPF() 类中。你可以看到here完整的Interface_CPF类
    【解决方案2】:

    请通过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 加载项时的回调问题提供。

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

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

    【讨论】:

    • 我已经查阅了您的网站。我在这里遇到的一个简单问题是为我的 Execute 方法提供正确的 UIApplication 参数,但不提供。
    • 当我在 Execute 方法中的 Command 类中使用 MyForm.Show() 时,当我尝试在我的命令类中但在其他方法中执行新事务时,我在 Revit API 之外。当我在同一个地方使用 MyForm.ShowDialog() 时,没关系,但是当我尝试在视图中放置一个家庭时,它不起作用,因为我无法关闭 MyForm。检查这个问题here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2019-01-15
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    相关资源
    最近更新 更多