Revit API计算风管水管的总长度

通过Line的Length属性
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 Autodesk.Revit.DB.Plumbing;

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();
            
//遍历全部风管
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            collector.OfClass(
typeof(Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);
            
double lengthDuct = 0;
            
foreach (Element el in collector)
            {
                Duct duct 
= el as Duct;
                lengthDuct 
+= getDuctLength(uiApp, duct);
            }
            
//遍历全部水管
            FilteredElementCollector collectorPipe = new FilteredElementCollector(doc);
            collectorPipe.OfClass(
typeof(Pipe)).OfCategory(BuiltInCategory.OST_PipeCurves);
            
double lengthPipe = 0;
            
foreach (Element el in collectorPipe)
            {
                Pipe pipe 
= el as Pipe;
                lengthPipe 
+= getPipeLength(uiApp, pipe);
            }
            TaskDialog.Show(
"工程量统计",
                
"风管:" + collector.Count() + "个\n" +
               
"总长度:" + lengthDuct + "毫米\n" +
                
"水管:" + collectorPipe.Count() + "个\n" +
               
"总长度:" + lengthPipe + "毫米\n");

            

            ts.Commit();

            
return Result.Succeeded;
        }
        
//取得风管长度
        private double getDuctLength(UIApplication uiApp, Duct duct)
        {
            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);
            
return feetToMm(newDuctLine.Length);
        }
        
//取得水管长度
        private double getPipeLength(UIApplication uiApp, Pipe pipe)
        {
            LocationCurve pipeLocation 
= pipe.Location as LocationCurve;
            XYZ pt1 
= pipeLocation.Curve.get_EndPoint(0);
            XYZ pt2 
= pipeLocation.Curve.get_EndPoint(1);
            Line newPipeLine 
= uiApp.Application.Create.NewLineBound(pt1, pt2);
            
return feetToMm(newPipeLine.Length);
        }
        
//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-1303-1-1.html
posted @ 2011-10-31 10:48  大气象  阅读(1537)  评论(0编辑  收藏  举报
http://www.tianqiweiqi.com