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;
}
}
}