如何放置類型爲“0762 x 2032 mm”的門實例?
思路:
首先在文檔中找到名字爲“0762x2032 mm”的門類型,如果沒有找到,則加載一個名稱 爲“M_單-嵌板4.rfa”的族文件,這樣就得到一個Family;
然後從Family獲取名爲“0762x2032 mm”的門類型;
最後在文檔中獲取一個直線形的牆,計算牆的中點位置,在此位置插入類型爲“0762x 2032 mm”的門。
代碼如下:
public void CreatDoorInstance(Document RevitDoc, string doorTypeName = "0762 x 2032 mm")
{
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);
bool symbolFound = false;
foreach (FamilySymbol element in doorSymbols)
{
if (element.Name == doorTypeName)
{
symbolFound = true;
doorType = element;
break;
}
}
// 如果沒有找到,就加載一個族文件
if (!symbolFound)
{
string file = @"C:\ProgramData\Autodesk\RVT 2014\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;
//創建門:傳入標高參數,作爲門的默認標高
FamilyInstance door = RevitDoc.Create.NewFamilyInstance(midPoint, doorType, wall,
wallLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
Autodesk.Revit.UI.TaskDialog.Show("Succeed", door.Id.ToString());
System.Diagnostics.Trace.WriteLine("Door created: " + door.Id.ToString());
}
else
{
Autodesk.Revit.UI.TaskDialog.Show("元素不存在", "沒有找到符合條件的牆");
}
}
else
{
Autodesk.Revit.UI.TaskDialog.Show("族類型不存在", "沒有找到族類型'" + doorTypeName + "'");
}
}
注意:這裏的門並不是插入到牆的中點位置,RevitAPI會自動調整門的Z軸的值,直至它和牆平齊。
另外,創建的時候,傳入的標高會作爲門的標高,如果不傳,則門就沒有標高。
=========【更多高級應用請關注公衆號】========
==================================