revit创建部件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;
namespace _创建部件
{
[Transaction(TransactionMode.Manual)]
class Class3 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;//获取活动文档
IList<Reference> refs = uidoc.Selection.PickObjects(ObjectType.Element);//选择需要创建部件的元素
foreach (Reference r in refs)//每个元素单独创建部件
{
List<ElementId> ids = new List<ElementId>();//创建新的元素ID列表
Element e = doc.GetElement(r);//获取元素
ids.Add(e.Id);//给列表ids添加元素ID
Transaction transaction = new Transaction(doc);//新建事务
transaction.Start("创建部件");//开始
AssemblyInstance assemblyInstance = AssemblyInstance.Create(doc, ids, e.Category.Id);//新建部件
transaction.Commit();//结束
}
return Result.Succeeded;
}
}
}