Revit API计算风管长度

一种方法通过
LocationCurve另一种方法是通过连接器。
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; }
    }

}
from:http://revit.5d6d.com/thread-1300-1-1.html
posted @ 2011-10-26 11:31  大气象  阅读(1609)  评论(7编辑  收藏  举报
http://www.tianqiweiqi.com