简体   繁体   English

使用Revit API提取建筑面积

[英]Extracting Floor Area using Revit API

So far I have written C# code to allow the user to select multiple parts of a model within revit, and it will post the id of the selected elements. 到目前为止,我已经编写了C#代码,以允许用户在revit中选择模型的多个部分,并且它将发布所选元素的ID。 I now want to adapted this in two ways: 我现在想通过两种方式对此进行调整:

1, To check whether the element selected is a room. 1,检查所选元素是否为房间。 (has a room tag) so then I only work with rooms. (有一个房间标签),所以我只能在房间里工作。

2, post the area of said room instead of just the ID of the element. 2,张贴所说房间的面积,而不只是元素的ID。

I am fairly new to C# and the Revit API so would appreiate any pushes in the right direction, thanks. 我对C#和Revit API还是很陌生,所以感谢任何朝正确方向的推动。

My current code: 我当前的代码:

using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Linq;
using System.Text;

namespace HelloWorld
{

    [Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;

            IList<Reference> pickedObjs = uidoc.Selection.PickObjects(ObjectType.Element, "Select elements");
            List<ElementId> ids = (from Reference r in pickedObjs select r.ElementId).ToList();

            using (Transaction tx = new Transaction(doc))
            {
                StringBuilder sb = new StringBuilder();
                tx.Start("transaction");
                if (pickedObjs != null && pickedObjs.Count > 0)
                {
                    foreach (ElementId eid in ids)
                    {
                        Element e = doc.GetElement(eid);
                        sb.Append("/n" +e.Name);
                    }
                    TaskDialog.Show("Area Calculator", sb.ToString());
                }
                tx.Commit();
            }
            return Result.Succeeded;
        }

    }
}

If you are new to Revit API then I recommend to get the latest version of RevitLookup from GitHub, deploy it on your Revit and start using it. 如果您不熟悉Revit API,那么我建议您从GitHub获取最新版本的RevitLookup,将其部署到Revit上并开始使用它。 It will help you to figure out which Revit API objects you can use to get your tools working. 它将帮助您确定可以使用哪些Revit API对象来使工具正常工作。

As per your current problem. 根据您当前的问题。 To find out if given element is a Room: 要确定给定元素是否为房间:

Room room = e as Room;
if (room!=null) ... ; //then you know it's a Room

alternatively: 或者:

if (e is Room) ... ; //then you know it's a Room

Second part: to query element's parameters you write: 第二部分:要查询元素的参数,请编写:

Parameter par = e.get_Parameter(BuiltInParameter.ROOM_AREA);
string valSting = par.AsValueString();
double valDouble = par.AsDouble(); //mind the value is in native Revit units, not project units. So square feet in this case

You could as well use par = e.LookupParameter("Area"); 您也可以使用par = e.LookupParameter("Area"); but if you work with system parameters, it's better to use built-in enums to refer to them (eg because they are language proof) 但是如果使用系统参数,最好使用内置枚举来引用它们(例如,因为它们是语言证明的)

Normally I develop tools and macros for MEP, all this I figured out in 10 seconds using the RevitLookup add-on. 通常,我为MEP开发工具和宏,所有这些我都是使用RevitLookup附加组件在10秒内发现的。 :) :)

If you only want to allow people to work with Room s then why not add a ISelectionFilter to the selection tool, and only allow them to select rooms to begin with. 如果只允许人们使用Room进行工作,那为什么不将ISelectionFilter添加到选择工具中,而只允许他们选择开始的房间。 Then you don't have to check all objects. 然后,您不必检查所有对象。 Here's more info on creating selection filters. 这是有关创建选择过滤器的更多信息。

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/Revit-API/files/GUID-ECB1EE82-EA91-451C-995C-7683C1F676CB-htm.html https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/Revit-API/files/GUID-ECB1EE82-EA91-451C-995C-7683C1F676CB-htm.html

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM