using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;
using System.Linq;
using System.Collections.Generic;
namespace LearnTime
{
//必备写法
[Transaction(TransactionMode.Manual)]
[Journaling(JournalingMode.NoCommandData)]
[Regeneration(RegenerationOption.Manual)]
//[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Automatic)]
public class _09_createdoor : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document RevitDoc = commandData.Application.ActiveUIDocument.Document;
string doorTypeName = "0915 x 2134mm";
//FamilySymbol doorType = null;
// 在文档中找到名字为"0762 x 2032 mm"的门类型
//ElementFilter doorCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
//ElementFilter familySymbolFilter = new ElementClassFilter(typeof(FamilySymbol));
//LogicalAndFilter andFilter = new LogicalAndFilter(doorCategoryFilter, familySymbolFilter);
//FilteredElementCollector doorSymbols = new FilteredElementCollector(RevitDoc);
//doorSymbols = doorSymbols.WherePasses(andFilter);
//上面收集器写法,下面lamda写法
Element doorType2 = new FilteredElementCollector(RevitDoc)
.OfCategory(BuiltInCategory.OST_Doors).
OfClass(typeof(FamilySymbol)).FirstOrDefault(x => x.Name == "0915 x 2134mm");
FamilySymbol doorType = RevitDoc.GetElement(doorType2.Id) as FamilySymbol;
bool symbolFound = false;
if (doorType2!=null)
{
symbolFound = true;
}
//bool symbolFound = true;
//foreach (FamilySymbol element in doorSymbols)
//{
// if (element.Name == doorTypeName)
// {
// symbolFound = true;
// doorType = element;
// break;
// }
//}
// 如果没有找到,就加载一个族文件
if (!symbolFound)
{
string file = @"C:\ProgramData\Autodesk\RVT 2018\Libraries\Chinese_INTL\门\M_单-嵌板 4.rfa";
Family family;
bool loadSuccess = RevitDoc.LoadFamily(file, out family);
if (loadSuccess)
{
foreach (ElementId doorTypeId in family.GetValidTypes())
{
doorType = RevitDoc.GetElement(doorTypeId) as FamilySymbol;
if (doorType != null)
{
if (doorType.Name == doorTypeName)
{
break;
}
}
}
}
else
{
Autodesk.Revit.UI.TaskDialog.Show("Load family failed", "Could not load family file '" + file + "'");
}
}
// 使用族类型创建门
if (doorType != null)
{
// 首先找到线形的墙
ElementFilter wallFilter = new ElementClassFilter(typeof(Wall));
FilteredElementCollector filteredElements = new FilteredElementCollector(RevitDoc);
filteredElements = filteredElements.WherePasses(wallFilter);
Wall wall = null;
Line line = null;
foreach (Wall element in filteredElements)
{
LocationCurve locationCurve = element.Location as LocationCurve;
if (locationCurve != null)
{
line = locationCurve.Curve as Line;
if (line != null)
{
wall = element;
break;
}
}
}
// 在墙的中心位置创建一个门
if (wall != null)
{
XYZ midPoint = (line.GetEndPoint(0) + line.GetEndPoint(1)) / 2;
Level wallLevel = RevitDoc.GetElement(wall.LevelId) as Level;
Transaction transaction = new Transaction(RevitDoc,"开门");
//创建门:传入标高参数,作为门的默认标高
transaction.Start();
FamilyInstance door = RevitDoc.Create.NewFamilyInstance(midPoint,
doorType, wall, wallLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
transaction.Commit();
TaskDialog.Show("Succeed", door.Id.ToString());
}
else
{
TaskDialog.Show("元素不存在", "没有找到符合条件的墙");
}
}
else
{
TaskDialog.Show("族类型不存在", "没有找到族类型'" + doorTypeName + "'");
}
return Result.Succeeded;
}
}
}