revit API 生成墻圖元
阿新 • • 發佈:2017-10-29
bound with des null 圖元 light 2.0 return eve
由於Revit的版本問題,在網上找的生成墻圖元的代碼,在我機器上的Revit 2016中編譯不能通過,通過多次調試,終於找到在revit 2016中使用API生成墻圖元的代碼,現在貼出來。
下面的代碼在Revit 2016 + VS2013的環境下通過。
class CreateWall : IExternalCommand { public static Document RevitDoc; public static Autodesk.Revit.ApplicationServices.Application RevitApp; public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { RevitApp = commandData.Application.Application; var uiDoc = commandData.Application.ActiveUIDocument; if (uiDoc == null) { message = "Please open a document"; return Result.Failed; } RevitDoc = commandData.Application.ActiveUIDocument.Document; var uiSel = commandData.Application.ActiveUIDocument.Selection; Transaction transaction = new Transaction(RevitDoc, "TestScript.CreateNewInstanceWithNewType"); transaction.Start(); try { var typeFilter = new ElementClassFilter(typeof(WallType)); FilteredElementCollector wallTypes = new FilteredElementCollector(RevitDoc); wallTypes = wallTypes.WherePasses(typeFilter); WallType walltype = null; foreach (WallType wallType in wallTypes) { walltype = wallType; break; } if (walltype != null) { //創建新的墻類型,使用Duplicate方法 var newtype = walltype.Duplicate(walltype.Name + "_new"); //可以在這裏修改墻類型的參數,例如修改吸收率 var para = walltype.get_Parameter(BuiltInParameter.ANALYTICAL_ABSORPTANCE); if (para != null && para.StorageType == StorageType.Double && para.IsReadOnly == false) { para.Set(2.0); } //找到一個標高 Level level = null; var levelFilter = new ElementClassFilter(typeof(Level)); FilteredElementCollector levels = new FilteredElementCollector(RevitDoc); levels = levels.WherePasses(levelFilter); foreach (Level element in levels) { level = element; break; } //創建墻 var wall = Wall.Create(RevitDoc, Line.CreateBound(new XYZ(0, 0, 0), new XYZ(10, 0, 0)), newtype.Id, level.Id, 20, 0, false, false); TaskDialog.Show("wall creation", "wall created, id = " + wall.Id); } transaction.Commit(); } catch (Exception ex) { message = ex.ToString(); transaction.RollBack(); return Result.Failed; } return Result.Succeeded; } }
revit API 生成墻圖元