revit api 加载族文件+放置族实例 创建门图元
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.get_EndPoint(0) + line.get_EndPoint(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());
      Trace.WriteLine("Door created: " + door.Id.ToString());
   }
   else
   {
      Autodesk.Revit.UI.TaskDialog.Show("元素不存在", "没有找到符合条件的墙");
   }
}
else
{
   Autodesk.Revit.UI.TaskDialog.Show("族类型不存在", "没有找到族类型'" + doorTypeName + "'");
}创建柱子图元以及移动图元
 ElementTransformUtils
Document projectDoc = ActiveUIDocument.Document;
           
using(Transaction moveColumnTran = new Transaction(projectDoc, "Move a new column to the new place"))
{
 moveColumnTran.Start();
               
 // 获取Revit文档的创建句柄
 Autodesk.Revit.Creation.Document creater = projectDoc.Create;
 // 创建一根柱子:使用给定的位置(坐标原点),柱子类型和标高(高度为0)
 XYZ origin = new XYZ(0, 0, 0);
 Level level = GetALevel(projectDoc);
 FamilySymbol columnType = GetAColumnType(projectDoc);
 FamilyInstance column = creater.NewFamilyInstance(origin, columnType, level, Autodesk.Revit.DB.Structure.StructuralType.Column);
 // 把柱子移动到新的位置
 XYZ newPlace = new XYZ(10, 20, 30);
 ElementTransformUtils.MoveElement(projectDoc, column.Id, newPlace);
           
 moveColumnTran.Commit();       
}载入族选项 Ifamilyloadoptiongs
class projectFamLoadOption : IFamilyLoadOptions
{
    bool IFamilyLoadOptions.OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
    {
        overwriteParameterValues = true;
        return true;
    }
    bool IFamilyLoadOptions.OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
    {
        source = FamilySource.Project;
        overwriteParameterValues = true;
        return true;
    }
}; 
                    
                     
                    
                 
                    
                 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号