Skip to content

Instantly share code, notes, and snippets.

@ricaun
Last active December 23, 2022 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricaun/693470e914295786fa62a2be6c67e662 to your computer and use it in GitHub Desktop.
Save ricaun/693470e914295786fa62a2be6c67e662 to your computer and use it in GitHub Desktop.
Connector Set Value Extension for Revit to 'fix' the ArgumentOutOfRangeException when setting Radius, Width, and Height.
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace RevitAddin.Commands
{
[Transaction(TransactionMode.Manual)]
public class CommandConnectorSetValue : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document document = uidoc.Document;
Selection selection = uidoc.Selection;
var elements = selection.GetElementIds().Select(id => document.GetElement(id));
using (Transaction transaction = new Transaction(document))
{
transaction.Start("Change Connector Value");
foreach (var element in elements)
{
var connectors = GetConnectors(element);
foreach (var connector in connectors)
{
if (connector.Shape == ConnectorProfileType.Round)
{
connector.SetRadius(connector.Radius + 0.05);
}
if (connector.Shape == ConnectorProfileType.Rectangular || connector.Shape == ConnectorProfileType.Oval)
{
connector.SetWidth(connector.Width + 0.05);
connector.SetHeight(connector.Height + 0.05);
}
}
}
transaction.Commit();
}
return Result.Succeeded;
}
private IEnumerable<Connector> GetConnectors(Element element)
{
ConnectorManager connectorManager = null;
if (element is FamilyInstance familyInstance)
{
connectorManager = familyInstance.MEPModel.ConnectorManager;
}
if (element is MEPCurve mepCurve)
{
connectorManager = mepCurve.ConnectorManager;
}
if (connectorManager is not null)
{
return connectorManager.Connectors.OfType<Connector>();
}
return Enumerable.Empty<Connector>();
}
}
/// <summary>
/// ConnectorSetValueExtension
/// </summary>
public static class ConnectorSetValueExtension
{
/// <summary>
/// Set the radius of the connector.
/// </summary>
/// <param name="connector"></param>
/// <param name="radius"></param>
public static void SetRadius(this Connector connector, double radius)
{
using (new CultureInfoChanger())
{
connector.Radius = radius;
}
}
/// <summary>
/// Set the width of the connector.
/// </summary>
/// <param name="connector"></param>
/// <param name="width"></param>
public static void SetWidth(this Connector connector, double width)
{
using (new CultureInfoChanger())
{
connector.Width = width;
}
}
/// <summary>
/// Set the height of the connector.
/// </summary>
/// <param name="connector"></param>
/// <param name="height"></param>
public static void SetHeight(this Connector connector, double height)
{
using (new CultureInfoChanger())
{
connector.Height = height;
}
}
/// <summary>
/// CultureInfoChanger
/// </summary>
public class CultureInfoChanger : IDisposable
{
private readonly CultureInfo CultureInfo;
/// <summary>
/// CultureInfoChanger
/// </summary>
/// <param name="name"></param>
public CultureInfoChanger(string name = "en")
{
CultureInfo = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo(name);
}
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
CultureInfo.CurrentCulture = CultureInfo;
}
}
}
}
@ricaun
Copy link
Author

ricaun commented Dec 23, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment