Revit2018复现《AUTODESK REVIT二次开发基础教程》代码10

using System;

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

namespace LearnTime
{
    //必备写法
    [Transaction(TransactionMode.Manual)]
    [Journaling(JournalingMode.NoCommandData)]
    [Regeneration(RegenerationOption.Manual)]
    public class _10_move : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Document projectDoc = commandData.Application.ActiveUIDocument.Document;

            using (Transaction moveColumnTran = new Transaction(projectDoc, "Move a new column to the new place"))
            {
                moveColumnTran.Start();

                // 获取Revit文档的创建句柄
                Autodesk.Revit.Creation.Document creater = projectDoc.Create;
                // 创建一根柱子:使用给定的位置(坐标原点),柱子类型和标高(高度为0)
                XYZ origin = new XYZ(0, 0, 0);
                Level level = new FilteredElementCollector(projectDoc)
                    .OfClass(typeof(Level)).FirstOrDefault(x => x.Name == "标高 1") as Level;

                FilteredElementCollector collector = new FilteredElementCollector(projectDoc);
                Element ele = collector.OfCategory(BuiltInCategory.OST_Columns).OfClass(typeof(FamilySymbol))
                    .FirstOrDefault(X => X.Name == "610 x 610mm");
                FamilySymbol columType = projectDoc.GetElement(ele.Id) as FamilySymbol;
                //将Element 转为 FamilySymbol 上面(一行)一种写法,下面(两行)一种写法,下面的写法第二句是激活。
                //FamilySymbol columType = ele as FamilySymbol;
                //columType.Activate();

                FamilyInstance column = creater.NewFamilyInstance
                    (origin, columType, level, Autodesk.Revit.DB.Structure.StructuralType.Column);
                // 把柱子移动到新的位置
                XYZ newPlace = new XYZ(10, 20, 30);
                ElementTransformUtils.MoveElement(projectDoc, column.Id, newPlace);

                LocationPoint columnPoint = column.Location as LocationPoint;
                XYZ newLocation = new XYZ(-10, 20, 0);
                // 把柱子移动到新的位置
                columnPoint.Point = newLocation;

                //旋转
                LocationPoint columnPoint2 = column.Location as LocationPoint;
                XYZ aa = columnPoint2.Point;
                XYZ bb = new XYZ(aa.X, aa.Y, aa.Z + 10);
                Line axis = Line.CreateBound(aa, bb);
                columnPoint2.Rotate(axis, Math.PI / 3.0);


                //镜像
                Plane plane = Plane.CreateByNormalAndOrigin(XYZ.BasisX, XYZ.Zero);
                ElementTransformUtils.MirrorElement(projectDoc, column.Id, plane);


                moveColumnTran.Commit();
            }

            return Result.Succeeded;
        }
    }
}
 

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值