Issue with Purging Family Documents using EtransmitForRevitDB

Issue with Purging Family Documents using EtransmitForRevitDB

Jesse.JamesN6MGB
Explorer Explorer
324 Views
2 Replies
Message 1 of 3

Issue with Purging Family Documents using EtransmitForRevitDB

Jesse.JamesN6MGB
Explorer
Explorer

I've created a Revit add-in that uses the EtransmitForRevitDB DLL to purge unused elements (based on Emiliano Capasso's approach: Why Revit Purge Command & Act of Love via API). The purge functionality works fine for project documents, but I'm encountering an issue when trying to purge family documents.

Is the EtransmitForRevitDB purge functionality compatible with family documents?

Has anyone successfully used this approach for purging family documents?

See below for my structure: 

Code Structure: I have a DocumentUtilities class that handles the purging:

csharp
public bool Purge()
{
    eTransmitUpgradeOMatic eTransmitUpgradeOMatic =         new eTransmitUpgradeOMatic(_uiApplication.Application);
    UpgradeFailureType result = eTransmitUpgradeOMatic.purgeUnused(_document);
    if (result == UpgradeFailureType.UpgradeSucceeded)         return true;
    else         return false;
}

And I'm calling this method in my family batch processor:

csharp
private static void PurgeFamily(Document familyDoc, string familyPath)
{
    // Purge unused elements
    DocumentUtilities docUtil = new DocumentUtilities(familyDoc);
    docUtil.Purge();
}

Error: When trying to purge a family document, I get the following error:

"Invalid call to Revit API! Revit is currently not within an API context"
0 Likes
Accepted solutions (1)
325 Views
2 Replies
Replies (2)
Message 2 of 3

Mohamed_Arshad
Advisor
Advisor
Accepted solution

Hi @Jesse.JamesN6MGB 

 

There is no issue with the eTransmitUpgradeOMatic.purgeUnused(Document doc) method itself. For example, you can open a single Revit Family document and execute the method — it will run without any errors.

The error you've reported is related to the Revit context and occurs due to batch processing. It's not an issue with the method, but rather with how batch processing is being handled.

I'm not sure how you're implementing batch processing in your setup. However, I've prepared a sample that demonstrates how to open family documents, purge them, and then close them. Please refer to the code below for further guidance.

 

Reference Code

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    UIDocument uiDoc = commandData.Application.ActiveUIDocument;
    Document doc = uiDoc.Document;

    ///Root Path
    string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Families");

    ///Get all the families from the location
    string[] families = Directory.GetFiles(path, "*.rfa", SearchOption.AllDirectories);

    foreach (string family in families)
    {
        UIDocument famUiDoc = uiDoc.Application.OpenAndActivateDocument(family);
        Purge(famUiDoc.Document.Application, famUiDoc.Document);

        ///Save the Document
        famUiDoc.Document.Save();
    }

    return Result.Succeeded;
}


public bool Purge(Application app, Document doc)
{
    eTransmitUpgradeOMatic eTransmitUpgradeOMatic = new eTransmitUpgradeOMatic(app);
    UpgradeFailureType result = eTransmitUpgradeOMatic.purgeUnused(doc);
    if (result == UpgradeFailureType.UpgradeSucceeded) return true;
    else return false;
}

Reference Video

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 3

Jesse.JamesN6MGB
Explorer
Explorer

Thank you so much for this very detailed and insightful response. its working now.

0 Likes