Prohibition of Revit External Application Execution Outside API Context

For web applications, there are readily available APIs for spell checkers such as http://www.aspspellcheck.com/. In Dynamo for Revit 2020 or 2021 (2019 and 2022 are unaffected), when using the specified imports in a Python node, an error occurs. Although adding the full path to the DLL reference resolved the issue in Revit 2019, the same approach does not work for 2020 and 2021. What could be the reason for Dynamo not recognizing these imports?


Question:

I am attempting to initiate a transaction within my code, but as indicated in the title, it results in an error. I have gone through numerous discussions on External Application, but I still lack a proper understanding of its functioning.

To initiate my transaction, I utilize the following code within my form:
“””.

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();
    }

and that what I try to execute :

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";
    }
}

I’m really struggling with this. I have no idea about the functionality of “ExternalEventHandler” or “ExternalApplication”. Thank you for your assistance 🙂


Solution 1:

To ensure the external event is declared within the constructor of MyForm, use
external events
. I personally avoid using ShowDialog because it interrupts the flow of the
user access
with the rest of the UI.


Solution 2:


Please go through the introductory material for
Revit API
. It covers the essential basics of the Revit API and its structure, including the implementation and usage of external applications and commands.

Kindly be aware that to utilize the Revit API, it is necessary to operate within a valid Revit API context, which is exclusively established by the callbacks issued by Revit.exe during the execution and loading of a Revit add-in.

Hence, the statement mentioned in your description is both anticipated and preferred: an external application for Revit cannot function without a valid Revit API context, and as a result, it is indeed prohibited.

Before submitting your texts, particularly the title, it is important to check for any typos, like the one in your description, as it can make it more difficult to identify an issue.

Frequently Asked Questions

Posted in Uncategorized