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;
using Autodesk.Revit.DB.Structure;
namespace 创建门
{
[Transaction(TransactionMode.Manual)]
class 创建门 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;//获取活动文档
Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "请选择墙");
Element elem = doc.GetElement(refer);
Wall awall = elem as Wall;//把选择的元素转换为墙
Line line = (awall.Location as LocationCurve).Curve as Line;//获取墙的线
XYZ midPoint = (line.GetEndPoint(0) + line.GetEndPoint(1)) / 2;//获取中点
Level wallLevel = doc.GetElement(awall.LevelId) as Level;//获取标高,元素转换为标高
//创建过滤器
FamilySymbol familySymbol = null;
FilteredElementCollector doorCollector = new FilteredElementCollector(doc);
//创建过滤收集器
ElementCategoryFilter doorFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
//过滤出门类别
ElementClassFilter familySymbolFilter = new ElementClassFilter(typeof(FamilySymbol));
//过滤出门
LogicalAndFilter doorAndFilter = new LogicalAndFilter(doorFilter, familySymbolFilter);
//逻辑过滤器
doorCollector.WherePasses(doorAndFilter);
foreach (FamilySymbol symbol in doorCollector)
{
if (symbol.Name == "600 x 1800mm")
{
familySymbol = symbol;
break;
}
}
TaskDialog.Show("revit", familySymbol.Name);
Transaction trans = new Transaction(doc);
trans.Start("创建门");
if (!familySymbol.IsActive)//激活familysymbol,这个要放在事务里面才算激活(重点)
{
familySymbol.Activate();
}
doc.Create.NewFamilyInstance(midPoint, familySymbol, elem, wallLevel, StructuralType.NonStructural);
trans.Commit();
return Result.Succeeded;
}
}
}
菜鸟一枚,请多包涵