Hello,
I'm new to Revit api. I'm wondering if it's possible to alter the length of a duct in Revit through the API. Upon trying, I've noticed that the duct length property appears to be set as read-only. Is there a workaround to modify the duct length? Any assistance on this matter would be helpful.
Thanks,
Bhavita
Yes it can be done. The API wraps the UI functionality, so the best way to address this is to determine the optimal workflow and best practices manually in the user interface first. How do you solve this in the UI?
HI @bhavita_patil
As @jeremy_tammik said we don't have API support to change the duct length. But I found a workaround that we can delete the existing one and create a new duct with a new length, then update the neighboring duct length according to that. Kindly refer to the below code for additional reference.
Reference Code
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Reference refer = uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
Duct duct = doc.GetElement(refer) as Duct;
///New Length Dimension
double newLength = UnitUtils.ConvertToInternalUnits(10000,UnitTypeId.Millimeters);
///Calculating New Length
LocationCurve curve = duct.Location as LocationCurve;
XYZ p1 = curve.Curve.GetEndPoint(0);
XYZ p2 = p1 + ((curve.Curve as Line).Direction * newLength);
using (Transaction deleteDuctAndCreateNew = new Transaction(doc, "Delete Existing Duct and Create New"))
{
deleteDuctAndCreateNew.Start();
//Create New Duct
Duct.Create(doc, duct.MEPSystem.GetTypeId(),duct.GetTypeId(), duct.ReferenceLevel.Id, p1, p2);
doc.Delete(duct.Id);
deleteDuctAndCreateNew.Commit();
}
Reference Video
Hope this Helps 🙂
@Mohamed_Arshad , your solution is great, and I admit i sometimes do it in this direction for certain cases. The issue you will face here, deleting an existing element means disconnecting it from the System, loosing all instance values such as mark or comment.
not sure if you would agree with me, I would be more inclined to only increase the length of the MepCurve (Duct, pipe, conduit...etc.)
var locCurve = ductObject.Location as LocationCurve;
locCurve.Curve = extendedCurve;
If the duct is connected to neighbouring elements, you can let Revit modify and adapt its length automatically by moving those neighbours and their connection points. Look at an exploration of different approaches to modifying pipe length in the blog post series on implementing a rolling offset:
Thanks, yep, that is the perfect approach, to just move the neighbor elements, and it will keep all the connections intact.
Just to add another approach, for those MEP curves without neighbor connections. We may also extend the curve directly by its connector, which mean no line or assign location curve is needed.
Connector connector = getMyConnector();
double extendby = 1; // extend by 1 feet for example
XYZ direction = ductCurve.Direction; // assuming the duct is linear curve
connector.Origin = connector.Origin + direction * extendby;
Thank you for the suggestions and nice discussion. I shared it on the blog for future reference:
https://thebuildingcoder.typepad.com/blog/2024/05/lengthen-ducts-and-highlight-links.html#3
Can't find what you're looking for? Ask the community or share your knowledge.