Powershell to run macros in Revit

Powershell to run macros in Revit

jacsmithstrongtie
Contributor Contributor
467 Views
2 Replies
Message 1 of 3

Powershell to run macros in Revit

jacsmithstrongtie
Contributor
Contributor
1

I am experiencing some issues with trying to initiate a macro through power shell for Revit. In Excel it seems rather simple based on this

$excel = new-object -comobject excel.application
$excelFiles = Get-ChildItem -Path C:\fso -Include *.xls, *.xlsm -Recurse
Foreach($file in $excelFiles)
{
   $workbook = $excel.workbooks.open($file.fullname)
   $worksheet = $workbook.worksheets.item(1)
   $excel.Run("CreateChart")
   $workbook.save()
   $workbook.close()
}
$excel.quit()

However Revit.Application is not an object on my computer even though the executable is there. Due to Revit changing their API in 2024 we have a mirroring issue that I am trying to work around. If any of you could help me figure this out I would be forever in your debt.

here is a reference to a comment so you do not have to scroll further.

logicaldiagram I have the macro written. I just need Powershell to open the document run the macro and close the document. To give more of a highlight to what is happening Autodesk's Revit changed their api in 2024 which caused a major flaw in our feature. What is needed now is to utilize a post command that Revit has today (this is what the Macro does) however post commands can not run until all the code has completed. So the idea of putting this in a loop is out of the question

When trying this I am unable to get the script to work

0 Likes
468 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni

You cannot simply drive Revit from outside like Excel. The Revit API requires a valid Revit API context to execute. Such context is only provided within the event handlers of Revit API events. Such handlers can only be registered from a Revit add-in, which needs to be loaded into a running session of Revit.exe. This behaviour has not changed in recent years, and this description applies to all active versions of Revit, at least the last 10 or 15 or so. 

    

You can drive Revit from outside by implementing an external event:

  

  

Here are some articles on how that can be used:

  

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

jeremy_tammik
Alumni
Alumni

Discussed further in the StackOverflow thread on Powershell to run macros in Revit:

   

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes