Purge Unused using Python

 import clr
    clr.AddReference('ProtoGeometry')
    from Autodesk.DesignScript.Geometry import *
    # Import ToDSType(bool) extension method
    clr.AddReference("RevitNodes")
    import Revit
    clr.ImportExtensions(Revit.Elements)
    # Import geometry conversion extension methods
    clr.ImportExtensions(Revit.GeometryConversion)
    # Import DocumentManager and TransactionManager
    clr.AddReference("RevitServices")
    import RevitServices
    from RevitServices.Persistence import DocumentManager
    from RevitServices.Transactions import TransactionManager
    from System.Collections.Generic import *
    # Import RevitAPI
    clr.AddReference("RevitAPI")
    import Autodesk
    from Autodesk.Revit.DB import *
    clr.AddReference('RevitAPIUI')
    from Autodesk.Revit.UI import *
    from Autodesk.Revit import Creation

    doc = DocumentManager.Instance.CurrentDBDocument
    uiapp = DocumentManager.Instance.CurrentUIApplication
    app = uiapp.Application

    # Start Transaction
    TransactionManager.Instance.EnsureInTransaction(doc)

    rvtComId = RevitCommandId.LookupPostableCommandId(PostableCommand.PurgeUnused)
    post = uiapp.CanPostCommand(rvtComId)

    # End Transaction
    TransactionManager.Instance.TransactionTaskDone()


`OUT=post`

I tried using postable command but it would’nt work,but it would not give a error it would just return true

As I’m aware Purge is not accessable in the API :slight_smile:

1 Like

Would i be able to at least bring up the purge Window and manually select purge all?

Wouldn’t it be easier just to bind a keyshortcut to Purge then? :slight_smile:

1 Like

This is part of a bigger workflow where i delete sheets,view,schedules…and unpinned elements so purging would be the final step before i save the files

Ive done everything else in Dynamo using python …so i wanted to do this too :sweat_smile:

If theres no other way then il do it manually:expressionless:

I might eat my words.
Someone found a solution, read here: https://gitlab.com/MattTaylor/RevitPurgeUnused.
It is for Revit 2017+.

Try translate it to python :-).

And as you were doing in the start, they also use the Postable Command

2 Likes

i saw this in the version history of the Dynamo Automation github site by @Andreas_Dieckmann
It also says it has not been tested with 2.x

1.3.3

New nodes:

  • Journal.PurgeModel
  • Journal.PurgeModels
  • Journal.RevitStartupArguments
  • Process.KillCurrentProcess
1 Like

Thanks @Marcel_Rijsmus - interesting! Any idea when @Andreas_Dieckmann will test it for 2.x?

Hi @ashwinash112,

The Postable command works like this :

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
import Autodesk
from Autodesk.Revit.UI import RevitCommandId
from Autodesk.Revit.UI import UIApplication
from Autodesk.Revit.UI import ExternalCommandData
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication

RunIt = IN[0] 

if RunIt ==  True:
	CmndID = RevitCommandId.LookupCommandId('ID_PURGE_UNUSED')
	CmId = CmndID.Id
	uiapp.PostCommand(CmndID)
	errorReport = 'Success'
else:
	errorReport = 'Set IN[0] to true'

#Assign your output to the OUT variable
OUT = errorReport

11 Likes

Thnx

very useful. Thanks a lot!

This is great - but purging just once doesn’t do the job properly - does anyone have an idea how to get this command to execute 3x within a script. Even if i put the python node in 3 times in the script it only executes once. Has this been solved elsewhere?

Thanks Alban,

Very interesting :slight_smile:

Do all of the (postable) command IDs have the same string syntax? I couldn’t see a list anywhere in the revit api docs, just a reference to the journal file, which is a bit of a random process to find them :slight_smile:

Cheers,

Mark

https://thebuildingcoder.typepad.com/files/commandids.xlsx

4 Likes

Awesome thanks, 1287 commands will keep me going a while!

Thanks Ashwin.

yet another node in the Orchid package

2 Likes

Awesome nodes, Luke, thank you very much.
Guys, you can grab his dyn files on GuitHub.