RevitAPI(四) 移动、旋转、删除、修改

一 对元素编辑的几种方法


        1.ElementTransormUtils的移动(Move)、旋转(Rotate)和镜像。
        2.文档类Document的Delete()
        3.阵列类型的创建
        4.族编辑的一些基本类和方法
        5.获取Parameter参数进行修改

二 删除

        

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

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class Delete : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //删除
            Transaction transaction = new Transaction(doc, "删除");
            transaction.Start();
            IList<Reference> references =uIDocument.Selection.PickObjects(ObjectType.Element);
            List<ElementId> eleIds = new List<ElementId>();

            foreach (Reference reference in references) 
            {
                eleIds.Add(reference.ElementId);
            }

            doc.Delete(eleIds);

            
            transaction.Commit();

            //

            return Result.Succeeded;
        }
    }
    
}

三 移动 


ElementTransformUtils类的方法
1.使用给定的向量移动一个元素MoveElement(Document,ElementId,XYZ);
2.使用给定的向量移动多个元素MoveElements(Document,ICollection<ElementId>,XYZ);
Location类的方法
1.移动一个元素bool Move(XYZ);

注意:
1.基于标高的元素不能向上或者向下移动,即不能改变Z轴坐标值,但可以移动元素到同一标高内的。任意位置。
2.当移动一个元素时候,其它元素可能会跟着移动。如移动墙,墙上的窗户会跟着动。
3.如果元素被钉住,即Pinned属性是true时,则表示这个元素不能被移动。
 

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

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class Move : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //移动
            Transaction transaction = new Transaction(doc, "移动");
            transaction.Start();

            //方向乘以距离=向量
            //移动一个元素
            XYZ trans = XYZ.BasisY * (1000.0.Tofoot());
            ElementTransformUtils.MoveElement(doc,new ElementId(544996), trans);
            //移动多个元素
            IList<Reference> references = uIDocument.Selection.PickObjects(ObjectType.Element);
            
            List<ElementId> elementIds = new List<ElementId>();

            foreach(Reference reference in references)
            {
                elementIds.Add(reference.ElementId);
            }

            //通过 ElementTransformUtils类
            ElementTransformUtils.MoveElements(doc, elementIds, trans);

            //通过location move方法
            foreach(ElementId id in elementIds)
            {
                FamilyInstance  familyInstance=doc.GetElement(id) as FamilyInstance;
                if(familyInstance != null)
                familyInstance.Location.Move(trans);
                

            }

            transaction.Commit();

            //

            return Result.Succeeded;
        }
    }
}

四 旋转

旋转元素
ElementTransammUtils类的方法
1.旋转一个无素
RotateElement Document,ElementId,Line,double);
2.旋转多个元素ElementTransformUtils类的
RotateElements(Document,ICollection<ElementId>,Line,double);
Location类的方法
1.旋转一个元素
bool Rotate(Line,double);

注意事项:
1.旋转轴是有限线,若是无限线会导致旋转失败。
2.旋转轴一般要求与元素LocationCurve所在的平面垂直,否则很可能会旋转失败。

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

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class Rotate : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //旋转
            Transaction transaction = new Transaction(doc, "移动");
            transaction.Start();

            Wall wall=doc.GetElement(new ElementId(545157)) as Wall;

            Line line = (wall.Location as LocationCurve).Curve as Line;

            XYZ strat = line.GetEndPoint(0);
            XYZ end=strat.Add(new XYZ(0,0,1));

            Line ro = Line.CreateBound(strat, end);

            ElementTransformUtils.RotateElement(doc, new ElementId(545157), ro,1.57);

            IList<Reference> references = uIDocument.Selection.PickObjects(ObjectType.Element);
            List<ElementId> elementIds = new List<ElementId>();
            foreach (Reference reference in references) 
            {
                elementIds.Add(reference.ElementId);
            }

            ElementTransformUtils.RotateElements(doc,elementIds,ro,1.57);


            doc.GetElement(elementIds[0]).Location.Rotate(ro, 1.57);
            transaction.Commit();

            //

            return Result.Succeeded;
        }
    }
}

五 镜像

镜像ElementTransformUtils类的方法
1.用指定的平面创建一个元素的镜像拷贝
MirrorElement (Document,ElementId,Plane):
2.用指定的平面创建多个元素的镜像拷贝
IList<ElemertId>MirrorElements (Document ,ICollection<ElementId>,Plane,bool)
3.判断一个元素是否可以镜像
bool CanMirrorElement(Document,ElementId);
4.判断多个元素是否可以镜像
bool CanMirrorElements(Document,ICollection<ElementId>);

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

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class Mirror : IExternalCommand
    {
        List<ElementId> ids = new List<ElementId>();
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;


            //文档监听
            app.DocumentChanged += DocumentChanged;

            //镜像
            Transaction transaction = new Transaction(doc, "删除");
            transaction.Start();
            IList<Reference> references =uIDocument.Selection.PickObjects(ObjectType.Element);
            List<ElementId> eleIds = new List<ElementId>();

            foreach (Reference reference in references) 
            {
                eleIds.Add(reference.ElementId);
            }

            XYZ xyz= uIDocument.Selection.PickPoint();
            Plane plane = Plane.CreateByNormalAndOrigin(XYZ.BasisX, xyz);

            ElementTransformUtils.MirrorElements(doc, eleIds, plane, true);


            uIDocument.Selection.SetElementIds(ids);

            transaction.Commit();
            app.DocumentChanged -= DocumentChanged;

            //

            return Result.Succeeded;
        }

        public void DocumentChanged(object sender,DocumentChangedEventArgs e)
        {
            ids.AddRange(e.GetAddedElementIds());

        }
    }
}

六 阵列


线型阵列LinearArray
将一个元素阵列:
LinearArray Create(Document,View,Elementld,int,XYZ,ArrayAnchorMember);
将多个元素阵列:
LinearArray Create(Document,View,ICollection<Elementld>,int,XYZ,ArrayAnchorMember );

ArrayAnchorMember参数说明:
//Second是每个阵列之间距离按照设置的向量来如设置向量距离1000毫米则每个阵列的元素之间距离为1000毫米
//Last是阵列后最后一个到第一个的距离按照向量来如设置向量为1000毫米则阵列的第一个到最后一个距离为1000毫米

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

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class LinearArrayTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //阵列
            Transaction transaction = new Transaction(doc, "阵列");
            transaction.Start();
            IList<Reference> references =uIDocument.Selection.PickObjects(ObjectType.Element);
            List<ElementId> eleIds = new List<ElementId>();

            foreach (Reference reference in references) 
            {
                eleIds.Add(reference.ElementId);
            }
            View view = uIDocument.ActiveView;

            ElementId[] elementIds = eleIds.ToArray();
            ElementId element = elementIds[0];  
            //XYZ xyz= uIDocument.Selection.PickPoint();

            LinearArray linearArray= LinearArray.Create(doc, view, element, 5, XYZ.BasisX*1000.0.Tofoot(), ArrayAnchorMember.Second);
            
            transaction.Commit();

            //

            return Result.Succeeded;
        }
    }
}

七 元素参数Parameter

1)获取参数
        元素都有参数,可以通过Element.Parameters获取所有的参数,然后遍历找到需要的参数。代码片段3-4展示如何遍历找到一个元素的“长度”参数。

        也可以通过Element.get_Parameter(参数)来获取单个参数,这里“参数”有四种选择:string参数名字、BuiltInParameter参数枚举、Definition参数定义和Guid参数的guid。

        注意:使用元素名字查找元素的时候,需要注意本地化的问题,因为不同语言的Revit版本的参数名字是不一样的,比如中文版的叫“长度”的参数,英文叫“Length”,同样的程序(比如上面的例子),在英文里面就会出问题。对此,我们建议尽量使用BuiltInParameter。
        效率上讲,最高的是BuiltlnParameter参数,其次是Definition和Guid,最慢的是使用参数名字。

2)修改参数
获取到参数之后,进行修改就比较简单了,直接调用Parameter.Set(参数)这个函数。

3)共享参数和项目参数
我们知道Revit@除了有内建的参数(BuiltInParameter),还允许用户自定义参数,分别是:共享参数和项目参数。

4)共享参数
共享参数被定义在Revit@之外的一个共享参数文件(.txt)中。删掉这个文件,共享参数就会丢失。
在界面上,共享参数好像是一系列的参数,但是实际上只是一些参数的定义,比如参数的名字、类型、分组等。直到把它们绑定到Category之后,对应的元素才会出现新的参数。而此时,从某种意义上说,绑定之后的共享参数实际上变成项目参数。
在API里面,参数的定义对应的类是Definition。

5)项目参数
项目参数保存在Revit⑩里,所以删除共享参数或者共享参数文件,都不会对项目参数产生影响,哪怕项目参数是通过共享参数创建的。项目参数在创建的时候,就已经和类别绑定了。也就是说和类别对应的元素都加上了新的参数。
 

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 System.IO;

namespace Revit_CreatInstane
{
    public static class ShareParameter
    {
        public static void CreateShareParameter(UIApplication uIApplication, string ParameterName,BuiltInCategory builtInCategory)
        {
            var path = @"E:\Revit02\Revit_SetParameter\Test\para.txt";
            Document  document= uIApplication.ActiveUIDocument.Document;

            CheckPath(path);
            //创建共享参数文件
            uIApplication.Application.SharedParametersFilename = path;
            //打开共享参数文件
            var definitionFile = uIApplication.Application.OpenSharedParameterFile();
            //找到并创建组
            DefinitionGroup group = null;

            if(definitionFile.Groups.get_Item("参数组测试MK3")==null)
            {
                group = definitionFile.Groups.Create("参数组测试MK3");
            }
            //设置参数类型并创建参数名
            var option = new ExternalDefinitionCreationOptions(ParameterName, ParameterType.Text);
            var definitions = group.Definitions.Create(option);

            //绑定到固定元素类型

            var categorySets=new CategorySet();
            var categorySet = document.Settings.Categories.get_Item(builtInCategory);
            categorySets.Insert(categorySet);
            var parameterGroup = BuiltInParameterGroup.PG_LENGTH;

            var binding = uIApplication.Application.Create.NewInstanceBinding(categorySets);
            //参数名,绑定,参数数据类别
            document.ParameterBindings.Insert(definitions, binding, parameterGroup);

        }

        private static string CheckPath(string path)
        {
            if (File.Exists(path))
            {
                return path;
            }
            else
            {

                StreamWriter newPath = File.CreateText(path);
                newPath.Close();
                return path;
            }
        }
    }
}


八 认识向量和方向


1.向量是XYZ ,向量是包含距离的,向量就是 方向*距离即XYZ.BasY*100mm即为向上100毫米
2.方向也是XYZ,方向没有距离,XYZ.BasY就是方向
3.线是点组成的,Line的也有方向,方向是点1到点2,即0点到1点
4.XYZ BasisX、  XYZ BasisY 、XYZ BasisZ、XYZ Zero表示  向右方向、平面向上方向、三维向上方向、0点
5.Negate()表示反方向  如XYZ.BasisX.Negate()==-X
6.  点+方向*距离=新的点如XYZ*(XYZ.BasisX)*100MM=XYZ2
7.(点+点2)/2=两个点的中心点,即(XYZ+XYZ2)/2=XYZ3
8.点-点2=点到点2的向量,即XYZ-XYZ2=XYZ向量.也可用Subtract方法

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值