Tips for PowerShell and Parameter Sync

If you are attempting to use the Revit API in the PowerShell scripting functionality there are a few tips to keep in mind.

#1. Write it first as a macro to debug the code

This will also allow you to receive Intellisense as well. Once in working order, then convert it to PowerShell Syntax.


var e = doc.GetElement(eid);
var pntId = ((FamilyInstance)e).GetSubComponentIds().FirstOrDefault(id => doc.GetElement(id).Name.Contains("Point"));
var pnt = doc.GetElement(pntId);
Parameter oHosParameter = e.LookupParameter("eM_Service Name");
Parameter oNestedParameter = pnt.LookupParameter("eM_Service Name");
if (string.IsNullOrEmpty(oNestedParameter.AsString()))
Parameter pntdesc = e.LookupParameter("eM_Point Description");
if (string.IsNullOrEmpty(pntdesc.AsString()))
pntdesc.Set(e.LookupParameter("Family").AsValueString() + e.LookupParameter("Size").AsString());
$doc = $eVolveElement.Document

$Max = $eVolveElement.get_BoundingBox($doc.ActiveView).Max

$Min = $eVolveElement.get_BoundingBox($doc.ActiveView).Min

$eloc = $Max + $Min / 2

$room = $doc.GetRoomAtPoint($eloc)
$roomname = $room.LookupParameter('Name')

return $roomname.AsString()

Always try going higher in the hierarchy

When experiencing seemingly unknown problems, always try to go higher in the hierarchy call you are using. An example is below for reference.


Instead of

Message Box:

Message boxes are a great tool for troubleshooting issues or providing user's info. A basic syntax to accomplish this is below.

$x=[System.Windows.Forms.MessageBox]::Show("Any text you want or a $variable")

Using Variables

Assign Variable

$var = "string"

Assign Multiple Variables

$a,$b = 0 or $a,$b = 'a','b'

Flip Variables

$a,$b = $b,$a

Strongly Typed Variable

$var = [int]5

How did we do?

Powered by HelpDocs (opens in a new tab)