revitAPI(3)-----元素

本节主要是获取单个元素以及其参数,详情见注释

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Exceptions;

namespace 元素
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                UIDocument uIDocument = commandData.Application.ActiveUIDocument;
                Document document = uIDocument.Document;
                //通过ID与选择获取元素
                List<Element> walls = new List<Element>();
                var ids = uIDocument.Selection.GetElementIds();
                foreach (var id in ids)
                {
                    Element element = document.GetElement(id);
                    if (element is Wall)
                    {
                        walls.Add(element);
                    }
                        
                }
                //通过过滤器获取元素
                FilteredElementCollector filterelements = new FilteredElementCollector(document);//建立一个构造器收集document中的所有元素用于过滤
                ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Wall));//建立一个过滤器,并设定过滤条件
                filterelements.WherePasses(elementClassFilter);//调用收集器的WherePasses方法对过滤器中的条件进行过滤,WherePasses方法可以连续调用进行多次过滤
                filterelements.WherePasses(elementClassFilter).WherePasses(new ElementCategoryFilter(BuiltInCategory.OST_StackedWalls));
                //收集器是固定的,过滤器有很多类型,如按照element过滤,按照category过滤或是按照族类型(familysymbol)过滤
                foreach (var wall in walls)
                {
                    var functionPara = wall.get_Parameter(BuiltInParameter.FUNCTION_PARAM);//类似于builtincategory,取出了这个实例下,相应枚举的属性
                    if(functionPara!=null && functionPara.StorageType==StorageType.Integer)
                    {
                        if (functionPara.AsInteger() == (int)WallFunction.Exterior)
                        {
                            //使用wall
                        }

                    }

                }
                //获取或修改元素的参数
                foreach (var w in walls)
                {
                    var Para = w.Parameters;//Element都可以通过这种方式获取相关的参数集合,相关的参数集合,继续。。。。

                    var p = w.get_Parameter(BuiltInParameter.CONTINUOUSRAIL_LENGTH_PARAM);//要想获取单个参数最推荐这么做
                    //理论上手册中可以利用string, builtinpara,parameter.definition,和guid获取参数,不知怎么回事这里只有上面一种方式可以做到
                    //因而如果想获得参数集合,最好使用parameters,想获取单个参数,直接用get_parameter()方法。
                    if(p!=null && p.StorageType==StorageType.Double)
                    {
                        double d = p.AsDouble();
                        //TaskDialog.Show("参数是",d.ToString());
                    }
                    //参数的修改要用parameter.set()这个函数
                    if(!p.IsReadOnly)//p的属性,是不是只读
                    {
                        bool success = p.Set(10);
                        if(!success)
                        {
                            //更新错误报告
                        }
                    }
                    //共享参数,指保存在txt文件中的参数设置,绑定后变成项目参数。在API中对应Definition类,但不知道怎么调用
                    
                    
                }
                return Result.Succeeded;
            }
            catch (Exception)
            {

                return Result.Failed;
            }
            //throw new NotImplementedException();
        }
    }
}

revitAPI(3)-----元素

上一篇:移动三大平台和三大开发模式对比分析


下一篇:Android的视频通话的Java代码