Revit API计算风管长度
一种方法通过LocationCurve另一种方法是通过连接器。
from:http://revit.5d6d.com/thread-1300-1-1.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForm = System.Windows.Forms;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.ExtensibleStorage;
using System.Xml;
namespace RevitCodes
{
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdLength : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
Selection sel = uiApp.ActiveUIDocument.Selection;
Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
ts.Start();
//
//选择一个风管
Reference refer = uiApp.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "选择一个风管");
Element elem = doc.GetElement(refer);
Duct duct = elem as Duct;
//方法一
IList<XYZ> list = new List<XYZ>();
ConnectorSetIterator csi = duct.ConnectorManager.Connectors.ForwardIterator();
while (csi.MoveNext())
{
Connector conn = csi.Current as Connector;
list.Add(conn.Origin);
}
Curve curve = Line.get_Bound(list.ElementAt(0), list.ElementAt(1)) as Curve;
TaskDialog.Show("length", curve.Length + "米");
//方法二
LocationCurve ductLocation = duct.Location as LocationCurve;
XYZ pt1 = ductLocation.Curve.get_EndPoint(0);
XYZ pt2 = ductLocation.Curve.get_EndPoint(1);
Line newDuctLine = uiApp.Application.Create.NewLineBound(pt1, pt2);
TaskDialog.Show("length", feetToMm(newDuctLine.Length) + "米");
ts.Commit();
return Result.Succeeded;
}
//Revit内部单位feet转化为mm即毫米
public double mmToFeet(double val) { return val / 304.8; }
public double feetToMm(double val) { return val * 304.8; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForm = System.Windows.Forms;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.ExtensibleStorage;
using System.Xml;
namespace RevitCodes
{
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdLength : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
Selection sel = uiApp.ActiveUIDocument.Selection;
Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
ts.Start();
//
//选择一个风管
Reference refer = uiApp.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "选择一个风管");
Element elem = doc.GetElement(refer);
Duct duct = elem as Duct;
//方法一
IList<XYZ> list = new List<XYZ>();
ConnectorSetIterator csi = duct.ConnectorManager.Connectors.ForwardIterator();
while (csi.MoveNext())
{
Connector conn = csi.Current as Connector;
list.Add(conn.Origin);
}
Curve curve = Line.get_Bound(list.ElementAt(0), list.ElementAt(1)) as Curve;
TaskDialog.Show("length", curve.Length + "米");
//方法二
LocationCurve ductLocation = duct.Location as LocationCurve;
XYZ pt1 = ductLocation.Curve.get_EndPoint(0);
XYZ pt2 = ductLocation.Curve.get_EndPoint(1);
Line newDuctLine = uiApp.Application.Create.NewLineBound(pt1, pt2);
TaskDialog.Show("length", feetToMm(newDuctLine.Length) + "米");
ts.Commit();
return Result.Succeeded;
}
//Revit内部单位feet转化为mm即毫米
public double mmToFeet(double val) { return val / 304.8; }
public double feetToMm(double val) { return val * 304.8; }
}
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。