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.Attributes; namespace 放置固定类型的门 { [TransactionAttribute(TransactionMode.Manual)] public class Class1 : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; string doorTypeName = "0762 x 2032mm"; FamilySymbol doorType = null; //在文档中找到名字为"0762 x 2032mm"的门类型。 ElementFilter doorCategory = new ElementCategoryFilter(BuiltInCategory.OST_Doors); ElementFilter familySymbolFilter = new ElementClassFilter(typeof(FamilySymbol)); LogicalAndFilter andFilter = new LogicalAndFilter(doorCategory, familySymbolFilter); FilteredElementCollector doorSymbols = new FilteredElementCollector(doc); doorSymbols = doorSymbols.WherePasses(andFilter); bool symbolFound = false; foreach (FamilySymbol element in doorSymbols) { if (element.Name == doorTypeName) { symbolFound = true; doorType = element; break; } } //使用族类型创建门。 if (doorType != null) { ElementFilter wallfilter = new ElementClassFilter(typeof(Wall)); FilteredElementCollector filteredElements = new FilteredElementCollector(doc); 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 = doc.GetElement(wall.LevelId) as Level; //创建门:传入标高参数,作为门的默认标高。 //对文档进行修改必须使用事务。 Transaction transaction = new Transaction(doc); transaction.Start("放置固定门"); FamilyInstance door = doc.Create.NewFamilyInstance(midPoint, doorType, wall, wallLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); transaction.Commit(); TaskDialog.Show("Succeeed", door.Id.ToString()); } else { TaskDialog.Show("Failed", "没有找到符合条件的墙"); } } else { TaskDialog.Show("Failed", "没有族类型" + doorTypeName); } return Result.Succeeded; } } }