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

问题描述

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

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

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加载项时独家提供的回调。

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

在提交文本之前,请先对它们进行拼写检查,尤其是标题,因为描述中的错别字使查找问题变得更加困难。

,

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...