LazJS API

This section will scripting using Javascript and Revit API

$d (Doc function)

$d is an instance of a doc function. Doc is a Javascript function (Doc.js) that provides simplified methods to retrieving elements.

Member Description
$d.base Returns .NET Revit Document
$d.getRooms() Return all the Room in the current Revit file in an Array
$d.getSpaces() Return all the MEP Space in the current Revit file in an Array
$d.getWindows() Return all the Window in the current Revit file in an Array
$d.getDoors() Return all the Door in the current Revit file in an Array
$d.getViews() Return all the View in the current Revit file in an Array
$d.getViewSheets() Return all the ViewSheet in the current Revit file in an Array
$d.getViewSheetSets() Return all the ViewSheetSet in the current Revit file in an Array
$d.getPhase(phaseName)

Return Phase in the current Revit file by it's name

$d.getPhase('New Construction');
$d.getPhases() Return all the Phase in the current Revit file in an Array
$d.getLevels() Return all the Level in the current Revit file in an Array
$d.getFamily(name)

Return Family in the current Revit file by it's name

$d.getFamily('M_Area Tag');

$d.getFamilyInstancesByName(bic, name)

bic BuiltInCategory

Return allFamilyInstances in the current Revit file by it's name

$d.getFamilyInstancesByName(BuiltInCategory.OST_Windows, 'M_Fixed');

$d.getElementsByCategory(bic, type)

bic BuiltInCategory

Return all elements of the provided BuiltInCategory and type in the current Revit file in an Array

$d.getElementsByCategory(BuiltInCategory.OST_Rooms, Room);
$d.getElementsByCategory(BuiltInCategory.OST_Walls, Wall);
$d.getElementsByCategory(BuiltInCategory.OST_Revisions); //Type default to Element

$d.getElementTypesByCategory(bic, type)

bic BuiltInCategory

Return all element Types of the provided BuiltInCategory and type in the current Revit file in an Array

$d.getElementTypesByCategory(BuiltInCategory.OST_Walls, WallType);
$d.getElementsByClass(type)

Return all elements of the provided .NET Type (Element) in the current Revit file in an Array


                    
$d.getElementTypesByClass(type)

Return all element Types of the provided .NET Type (Element) in the current Revit file in an Array

$d.getElementTypesByClass(LinePatternElement);
$d.forActiveDesignOption()

Return $d object which is filtered only for elements in Active Design Option

var activeDoc = $d.forActiveDesignOption();
activeDoc.getRooms();
activeDoc.getSpaces();
$d.forPrimaryDesignOption()

Return $d object which is filtered only for elements in Primary Design Option

$d.forPrimaryDesignOption().getRooms();
$d.forNoDesignOption()

Return $d object which is filtered only for elements not in any Design Option

$d.forNoDesignOption().getRooms();
$d.forPhase(phaseName)

Return $d object which is filtered only for elements in a given phase

var exd = $d.forPhase('Existing');
exd.getRooms();
exd.getSpaces();
$d.forCurrentView()

Return $d object which is filtered only for elements in the active view

$d.forCurrentView().getRooms();
$d.deleteElementById(elementId)

Delete an element by its element ID

var room = $d.getRooms()[0];
$d.deleteElementById(room.Id);
$d.deleteElementsById(elementIds)

Delete elements by its element IDs

var doorIds = $d.getDoors().map(function(door){
    return door.Id;
});

$d.deleteElementsById(doorIds);
$d.getUserWorksets()

Get all Worksets in the document

$d.getUserWorksetByName(worksetName)

Get a user workset by its name

//Move all grids into a workset

var gridWorkset = $d.getUserWorksetByName('Shared Levels and Grids');
var worksetInt = gridWorkset.Id.IntegerValue;

var grids = $d.getElementsByCategory(BuiltInCategory.OST_Grids);
grids.forEach(function(grid){
    grid.set(BuiltInParameter.ELEM_PARTITION_PARAM, worksetInt);
});

Extending $d

$d or Doc function can be extended by editing Doc.js. To provide intellisense in the editor, javascript will needs to be annotated with comments according to visual studio javascript xml annotation rules

$e

$e refers to the current Revit API Element Type of the selected category in ParamJS. The table below list out all extension methods for Element provided by the Lazjs.Revit.Extensions.ElementExtensions class defined in revitscriptsettings.xml.

Member Description

$e.get(BuiltInParameter, showUnits)

showUnits is optional and false by default

Get parameter value using BuiltInParameter.

$e.get(BuiltInParameter.RoomArea);
$e.get(BuiltInParameter.RoomArea, true)

$e.get(paramName, showUnits)

showUnits is optional and false by default

Get parameter value using parameter name.

$e.get('Area');
$e.get('Area', true);
$e.set(BuiltInParameter, value)

Set parameter value using BuiltInParameter

$e.set(BuiltInParameter.RoomName, 'OFFICE')
$e.set(paramName, value)

Set parameter value using parameter name

$e.set('Room OCL', 10)

Selection

Several methods are provided to select elements in the Revit.

Member Description
Selection.getSelectedElements()

Returns currently selected elements.

Selection.selectElement()

Pick an element and returns the element.

Selection.selectElements()

Pick multiple elements and returns the results.

Extending Selection

Selection can be extended by editing Selection.js.

Accessing Revit .NET API

LazJs provide full access to Revit .NET API through Javascript

$d.base provide access to Revit API Document object. Most of the member can be accessed from Javascript.

$d.base.Delete(...
$d.base.Export(...
$d.base.LoadFamily(...
$d.base.Link(...
$d.base.PathName;
$d.base.ProjectInformation.Number;

$d.getRooms() returns an Array of Revit API Room object. Most of the member can be accessed from Javascript.

    $d.getRooms().forEach(function(room){
        room.Unplace();
        room.Area;
        room.UnboundedHeight;
        room.UpperLimit.Elevation;
    })

BuiltInCategory

All Revit categories. This list is not complete.

BuiltInCategory Category Name Element Type
OST_Rooms Rooms Room
OST_MEPSpaces Spaces Space
OST_Windows Windows FamilyInstance
OST_Doors Doors FamilyInstance

BuiltInParameter

All Revit Built in Parameters. This list is not complete.

BuiltInParameter Description
ROOM_NAME Name
ROOM_FINISH_CEILING Ceiling Finish
ALL_MODEL_TYPE_NAME Type Name
ALL_MODEL_COST Cost

Element Class

All Revit Class inherited from Element. This list is not complete.

Element Type Namespace Description
Element Autodesk.Revit.DB
FamilyInstance Autodesk.Revit.DB
LinePatternElement Autodesk.Revit.DB
Room Autodesk.Revit.DB.Architecture
Space Autodesk.Revit.DB.Mechanical
FlexPipe Autodesk.Revit.DB.Plumbing
Pipe Autodesk.Revit.DB.Plumbing

RevitScriptSettings.xml

Configuration file that defines what is loaded into the editor and JavaScript engine. The file is located in the LazJS install directory.

Array Element Description
locations location

Defines the location where scripts are automatically loaded from.

<locations>
    <location path="lib" isdirectory="true" />
</locations>
namespaces namespace

Defines .NET Type that will be available to LazJS based on namespace. All types inside the provided assembly name with namespace start with the provided name will be available.

<namespaces>
    <namespace name="Autodesk.Revit.DB" assembly="RevitAPI" />
    <namespace name="Autodesk.Revit.Utility" assembly="RevitAPI" />
</namespaces>
types type

Additional type to be made available to LazJS using the class name and assembly name

<types>
    <type name="Autodesk.Revit.UI.UIDocument" assembly="RevitAPIUI" />
    <type name="Lazjs.Revit.Scripting.FamilyLoadOptions" assembly="Lazjs.Revit" />
</types>
extensions type

Include extensions method in LazJS

<extensions>
    <type name="Lazjs.Revit.Extensions.ElementExtensions" assembly="Lazjs.Revit" />
</extensions>
variables variable

Assign a variable to a script.

<variables>
    <variable name="alert">Window.alert</variable>
    <variable name="prompt">Window.prompt</variable>
    <variable name="confirm">Window.confirm</variable>
    <variable name="$d">new Doc(RevitDocument)</variable>
</variables>