revit API 生成墙图元

由于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;
        }
    }

  

posted on 2017-10-29 17:16  小夏coding  阅读(444)  评论(0编辑  收藏  举报

导航