Skip to content

mdomurad/RevitExtensions

 
 

Repository files navigation

Improve your experience with Revit API now

Extensions minimize the writing of repetitive code, add new methods not included in RevitApi, and also allow you to write chained methods without worrying about API versioning:

new ElementId(123469)
.ToElement<Door>()
.Mirror()
.GetParameter("Height")
.AsDouble()
.ToMillimeters()
.Round()

Extensions include annotations to help ReShaper parse your code and signal when a method may return null or the value returned by the method is not used in your code.

Installation

You can install Extensions as a nuget package.

Packages are compiled for a specific version of Revit, to support different versions of libraries in one project, use RevitVersion property.

<PackageReference Include="Nice3point.Revit.Extensions" Version="$(RevitVersion).*"/>

Package included by default in Revit Templates.

Features

Table of contents

The Cast() method cast the element to the specified type.

Wall wall = element.Cast<Wall>();
Floor floor = element.Cast<Floor>();
HostObject hostObject = element.Cast<HostObject>();

The GetParameter() method retrieves a parameter from an element. For instances that do not have such a parameter, the method will retrieve that parameter from the element type

element.GetParameter(ParameterTypeId.AllModelUrl, snoopType = true);
element.GetParameter(BuiltInParameter.ALL_MODEL_URL);
element.GetParameter("URL");

The Copy() method copies an element and places the copy at a location indicated by a given transformation.

element.Copy(1, 1, 0);
element.Copy(new XYZ(1, 1, 0));

The Mirror() method creates a mirrored copy of an element about a given plane.

element.Mirror(plane);

The Move() method moves the element by the specified vector.

element.Move(1, 1, 0);
element.Move(new XYZ(1, 1, 0));

The Rotate() method rotates an element about the given axis and angle.

element.Rotate(axis, angle);

The CanBeMirrored() method determines whether element can be mirrored.

element.CanBeMirrored();

The ToElement() method returns the element from the Id for a specified document and convert to a type if necessary.

Element element = elementId.ToElement(document);
Wall wall = elementId.ToElement<Wall>(document);

The AreEquals() method checks if an ID matches BuiltInСategory or BuiltInParameter.

categoryId.AreEquals(BuiltInCategory.OST_Walls);
parameterId.AreEquals(BuiltInParameter.WALL_BOTTOM_IS_ATTACHED);

The Distance() method returns distance between two lines. The lines are considered endless.

var line1 = Line.CreateBound(new XYZ(0,0,1), new XYZ(1,1,1));
var line2 = Line.CreateBound(new XYZ(1,2,2), new XYZ(1,2,2));
var distance = line1.Distance(line2);

The JoinGeometry() method creates clean joins between two elements that share a common face.

element1.JoinGeometry(element2);

The UnjoinGeometry() method removes a join between two elements.

element1.UnjoinGeometry(element2);

The AreElementsJoined() method determines whether two elements are joined.

var isJoined = element1.AreElementsJoined(element2);

The GetJoinedElements() method returns all elements joined to given element.

var elements = element1.GetJoinedElements();

The SwitchJoinOrder() method reverses the order in which two elements are joined.

element1.SwitchJoinOrder();

The IsCuttingElementInJoin() method determines whether the first of two joined elements is cutting the second element.

var isCutting = element1.IsCuttingElementInJoin(element2);

The SetCoordinateX(), SetCoordinateY(), SetCoordinateZ() methods creates an instance of a curve with a new coordinate.

var newLine = line.SetCoordinateX(1);
var newLine = line.SetCoordinateY(1);
var newLine = line.SetCoordinateZ(1);
var newArc = arc.SetCoordinateX(1);
var newArc = arc.SetCoordinateY(1);
var newArc = arc.SetCoordinateZ(1);

The CreatePanel() method create a new panel in the default AddIn tab or the specified tab. If the panel exists on the ribbon, the method will return it.

application.CreatePanel("Panel name");
application.CreatePanel("Panel name", "Tab name");

The AddPushButton() method adds a PushButton to the ribbon.

panel.AddPushButton(typeof(Command), "Button text");
panel.AddPushButton<Command>("Button text");
pullDownButton.AddPushButton(typeof(Command), "Button text");
pullDownButton.AddPushButton<Command>("Button text");

The AddPullDownButton() method adds a PullDownButton to the ribbon.

panel.AddPullDownButton("Button name", "Button text");

The AddSplitButton() method adds a SplitButton to the ribbon.

panel.AddSplitButton("Button name", "Button text");

The AddRadioButtonGroup() method adds a RadioButtonGroup to the ribbon.

panel.AddRadioButtonGroup("Button name");

The AddComboBox() method adds a ComboBox to the ribbon.

panel.AddComboBox("Button name");

The AddTextBox() method adds a TextBox to the ribbon.

panel.AddTextBox("Button name");

The SetImage() method adds an image to the RibbonButton.

button.SetImage("/RevitAddIn;component/Resources/Icons/RibbonIcon16.png");
button.SetImage("http://example.com/RibbonIcon16.png");
button.SetImage("C:\Pictures\RibbonIcon16.png");

The SetLargeImage() method adds a large image to the RibbonButton.

button.SetLargeImage("/RevitAddIn;component/Resources/Icons/RibbonIcon32.png");
button.SetLargeImage("http://example.com/RibbonIcon32.png");
button.SetLargeImage("C:\Pictures\RibbonIcon32.png");

The SetAvailabilityController() method specifies the class that decides the availability of PushButton

pushButton.SetAvailabilityController<CommandController>();

The FromMillimeters() method converts millimeters to internal Revit number format (feet).

double(69).FromMillimeters() => 0.226

The ToMillimeters() method converts a Revit internal format value (feet) to millimeters.

double(69).ToMillimeters() => 21031

The FromMeters() method converts meters to internal Revit number format (feet).

double(69).FromMeters() => 226.377

The ToMeters() method converts a Revit internal format value (feet) to meters.

double(69).ToMeters() => 21.031

The FromInches() method converts inches to internal Revit number format (feet).

double(69).FromInches() => 5.750

The ToInches() method converts a Revit internal format value (feet) to inches.

double(69).ToInches() => 827.999

The FromDegrees() method converts degrees to internal Revit number format (radians).

double(69).FromDegrees() => 1.204

The ToDegrees() method converts a Revit internal format value (radians) to degrees.

double(69).ToDegrees() => 3953

The FromUnit(UnitTypeId) method converts specified unit type to internal Revit number format.

double(69).FromUnit(UnitTypeId.Celsius) => 342.15

The ToUnit(UnitTypeId) method converts a Revit internal format value to to specified unit type.

double(69).ToUnit(UnitTypeId.Celsius) => -204.15

The FormatUnit() method formats a number with units into a string.

document.FormatUnit(SpecTypeId.Length, 69, false) => 21031
document.FormatUnit(SpecTypeId.Length, 69, false, new FormatValueOptions {AppendUnitSymbol = true}) => 21031 mm

The GetBottomFaces() method returns the bottom faces for the host object.

floor.Cast<HostObject>().GetBottomFaces();

The GetTopFaces() method returns the top faces for the host object.

floor.Cast<HostObject>().GetTopFaces();

The GetSideFaces() method returns the major side faces for the host object.

wall.Cast<HostObject>().GetSideFaces(ShellLayerType.Interior);

The ToLabel() method convert Enum to user-visible name.

BuiltInCategory.OST_Walls.ToLabel() => "Walls"
BuiltInParameter.WALL_TOP_OFFSET.ToLabel() => "Top Offset"
BuiltInParameter.WALL_TOP_OFFSET.ToLabel(LanguageType.Russian) => "Смещение сверху"
BuiltInParameterGroup.PG_LENGTH.ToLabel() => "Length"
DisplayUnitType.DUT_KILOWATTS.ToLabel() => "Kilowatts"
ParameterType.Length.ToLabel() => "Length"

DisciplineTypeId.Hvac.ToLabel() => "HVAC"
GroupTypeId.Geometry.ToLabel() => "Dimensions"
ParameterTypeId.DoorCost.ToLabel() => "Cost"
SpecTypeId.SheetLength.ToLabel() => "Sheet Length"
SymbolTypeId.Hour.ToLabel() => "h"
UnitTypeId.Hertz.ToLabel() => "Hertz"

The ToDisciplineLabel() method convert ForgeTypeId to user-visible name a discipline.

DisciplineTypeId.Hvac.ToDisciplineLabel() => "HVAC"

The ToGroupLabel() method convert ForgeTypeId to user-visible name for a built-in parameter group.

GroupTypeId.Geometry.ToGroupLabel() => "Dimensions"

The ToParameterLabel() method convert ForgeTypeId to user-visible name for a built-in parameter.

ParameterTypeId.DoorCost.ToParameterLabel() => "Cost"

The ToSpecLabel() method convert ForgeTypeId to user-visible name for a spec.

SpecTypeId.SheetLength.ToSpecLabel() => "Sheet Length"

The ToSymbolLabel() method convert ForgeTypeId to user-visible name for a symbol.

SymbolTypeId.Hour.ToSymbolLabel() => "h"

The ToUnitLabel() method convert ForgeTypeId to user-visible name for a unit.

UnitTypeId.Hertz.ToUnitLabel() => "Hertz"

The Clone() method creates a new Solid which is a copy of the input Solid.

solid.Clone();

The CreateTransformed() method creates a new Solid which is the transformation of the input Solid.

solid.CreateTransformed(Transform.CreateRotationAtPoint());
solid.CreateTransformed(Transform.CreateReflection());

The SplitVolumes() method splits a solid geometry into several solids.

solid.SplitVolumes();

The IsValidForTessellation() method tests if the input solid or shell is valid for tessellation.

solid.IsValidForTessellation();

The TessellateSolidOrShell() method facets (i.e., triangulates) a solid or an open shell.

solid.TessellateSolidOrShell();

The FindAllEdgeEndPointsAtVertex() method find all EdgeEndPoints at a vertex identified by the input EdgeEndPoint.

edgeEndPoint.FindAllEdgeEndPointsAtVertex();

The SaveEntity() method stores data in the element. Existing data is overwritten.

document.ProjectInformation.SaveEntity(schema, "data", "schemaField");
door.SaveEntity(schema, "white", "doorColorField");

The LoadEntity() method retrieves the value stored in the schema from the element.

var data = document.ProjectInformation.LoadEntity<string>(schema, "schemaField");
var color = door.LoadEntity<string>(schema, "doorColorField");

The AsBool() method provides access to the boolean value within the parameter

bool value = element.GetParameter("IsClosed").AsBool();

The AsColor() method provides access to the Color within the parameter

Color value = element.GetParameter("Door color").AsColor();

The AsElement() method provides access to the Element within the parameter

Element value = element.GetParameter("Door material").AsElement();
Material value = element.GetParameter("Door material").AsElement<Material>();

The Set() method sets the parameter to a new value

parameter.Set(true);
parameter.Set(new Color(66, 69, 96);

The Show() method opens a window and returns without waiting for the newly opened window to close. Sets the owner of a child window. Applicable for modeless windows to be attached to Revit.

new RevitAddinView.Show(uiApplication.MainWindowHandle)

This set of extensions encapsulates all the work of searching for elements in the Revit database.

The GetElements() a generic method which constructs a new FilteredElementCollector that will search and filter the set of elements in a document. Filter criteria are not applied to the method.

document.GetElements().WhereElementIsViewIndependent().ToElements();
document.GetElements(elementIds).WhereElementIsViewIndependent.ToElements();
document.GetElements(viewId).ToElements();

The remaining methods contain a ready implementation of the collector, with filters applied:

document.GetInstances();
document.GetInstances(new ElementParameterFilter());
document.GetInstances(new []{elementParameterFilter, logicalFilter});

document.GetInstances(BuiltInCategory.OST_Walls);
document.GetInstances(BuiltInCategory.OST_Walls, new ElementParameterFilter());
document.GetInstances(BuiltInCategory.OST_Walls, new []{elementParameterFilter, logicalFilter});    

document.EnumerateInstances();
document.EnumerateInstances(new ElementParameterFilter());
document.EnumerateInstances(new []{elementParameterFilter, logicalFilter});

document.EnumerateInstances(BuiltInCategory.OST_Walls);
document.EnumerateInstances(BuiltInCategory.OST_Walls, new ElementParameterFilter());
document.EnumerateInstances(BuiltInCategory.OST_Walls, new []{elementParameterFilter, logicalFilter});   

document.EnumerateInstances<Wall>();
document.EnumerateInstances<Wall>(new ElementParameterFilter());
document.EnumerateInstances<Wall>(new []{elementParameterFilter, logicalFilter});

document.EnumerateInstances<Wall>(BuiltInCategory.OST_Walls);
document.EnumerateInstances<Wall>(BuiltInCategory.OST_Walls, new ElementParameterFilter());
document.EnumerateInstances<Wall>(BuiltInCategory.OST_Walls, new []{elementParameterFilter, logicalFilter});   

The same overloads exist for InstanceIds, Type, TypeIds:

document.GetTypes();
document.GetTypeIds();
document.GetInstanceIds();
document.EnumerateTypes();
document.EnumerateTypeIds();
document.EnumerateInstanceIds();

For instances, overloads are available with viewId. The collector will search and filter the visible elements in the view:

document.GetInstances(viewId);
document.GetInstanceIds(viewId);
document.EnumerateInstances(viewId);
document.EnumerateInstanceIds(viewId);

Remarks: Get methods are faster than Enumerate due to RevitApi internal optimizations. However, enumeration allows for more flexibility in finding elements. Don't try to call GetInstances().Select().Tolist() instead of EnumerateInstances().Select().Tolist(), you will degrade performance.

The ToFraction() method converts a number to Imperial fractional format.

int(1).ToFraction() => 1-0double(0.0123).ToFraction() => 0 5/32double(15.125).ToFraction() => 15-1 1/2double(-25.222).ToFraction() => 25-2 21/32double(-25.222).ToFraction(4) => 25-2 3/4

The FromFraction() method converts the textual representation of the Imperial system number to number.

string("").FromFraction() => double(0)
string(1 17/64).FromFraction() => double(0.105)
string(11.75).FromFraction() => double(1.145)
string(-69-69).FromFraction() => double(-74.75)

string(-2-1 15/64).FromFraction(out var value) => true
string("-").FromFraction(out var value) => true
string(".").FromFraction(out var value) => false
string("value").FromFraction(out var value) => false
string(null).FromFraction(out var value) => false

The Round() method rounds the value to the specified precision or 1e-9 precision specified in Revit Api.

double(6.56170000000000000000000001).Round() => 6.5617
double(6.56170000000000000000000001).Round(0) => 7

The IsAlmostEqual() method compares two numbers within specified precision or 1e-9 precision specified in Revit Api.

double(6.56170000000000000000000001).IsAlmostEqual(6.5617) => true
double(6.56170000000000000000000001).IsAlmostEqual(6.6, 1e-1) => true

The IsNullOrEmpty() method same as string.IsNullOrEmpty().

string("").IsNullOrEmpty() => true
string(null).IsNullOrEmpty() => true

The IsNullOrWhiteSpace() method same as string.IsNullOrWhiteSpace().

string(" ").IsNullOrWhiteSpace() => true
string(null).IsNullOrWhiteSpace() => true

The AppendPath() method combines 2 paths.

"C:\Folder".AppendPath("AddIn") => "C:\Folder\AddIn"
"C:\Folder".AppendPath("AddIn", "file.txt") => "C:\Folder\AddIn\file.txt"

The Contains() indicating whether a specified substring occurs within this string with StringComparison support.

"Revit extensions".Contains("Revit", StringComparison.OrdinalIgnoreCase) => true
"Revit extensions".Contains("revit", StringComparison.OrdinalIgnoreCase) => true
"Revit extensions".Contains("REVIT", StringComparison.OrdinalIgnoreCase) => true
"Revit extensions".Contains("invalid", StringComparison.OrdinalIgnoreCase) => false

Symbol server

When debugging, sometimes the library symbols are not available on your local machine. In this case, you can use symbol servers. Then, you can point your debugger to the symbol server to resolve symbol names.

The symbols for this package are available at https://symbols.nuget.org/download/symbols

Technology Sponsors

Thanks to JetBrains for providing licenses for Rider and dotUltimate tools, which both make open-source development a real pleasure!

About

Extensions for Revit plugin development

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 98.3%
  • PowerShell 1.7%