revit API 生成墙图元

Posted beggar_1982

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
        }
    }

  

以上是关于revit API 生成墙图元的主要内容,如果未能解决你的问题,请参考以下文章

BIM入门实战Revit 图元分类有哪三种?Revit图元分类图文详解

BIM入门实战Revit 图元分类有哪三种?Revit图元分类图文详解

Revit API 加载族并生成实例图元

Revit二次开发 零件创建

为 Revit 模型 (C4R) 发布和生成房间图元

revit墙分为两段,三段颜色如何设置?