Revit SDK 介绍:AllViews 创建图纸

前言

如何用 API 创建图纸,并且放置视图?SDK 里面有一个例子,相当不错。
重点 API:

public static ViewSheet ViewSheet::Create(
	Document document,
	ElementId titleBlockTypeId
);
public static Viewport Viewport::Create(
	Document document,
	ElementId viewSheetId,
	ElementId viewId,
	XYZ point
);

操作步骤

选择你要放到图纸里面的视图:
Revit SDK 介绍:AllViews 创建图纸_第1张图片
生成图纸:
Revit SDK 介绍:AllViews 创建图纸_第2张图片

代码逻辑

创建图纸的 API 非常简单,不过有两个参数需要传给它。

ViewSheet.Create(doc, m_titleBlock.Id)

第一个参数可以从 commandData 里面得到,而第二个参数需要首先过滤一遍文件中的构件,然后用户自己在 UI 上选择一个。
过滤构件得到 title block:

private void GetTitleBlocks(Document doc)
{
   FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
   filteredElementCollector.OfClass(typeof(FamilySymbol));
   filteredElementCollector.OfCategory(BuiltInCategory.OST_TitleBlocks);
   m_allTitleBlocks = filteredElementCollector.ToElements();
   if (0 == m_allTitleBlocks.Count)
   {
      throw new InvalidOperationException("There is no title block to generate sheet.");
   }

   foreach (Element element in m_allTitleBlocks)
   {
      FamilySymbol f = element as FamilySymbol;
      AllTitleBlocksNames.Add(f.Family.Name + ":" + f.Name);
      if (null == m_titleBlock)
      {
         m_titleBlock = f;
      }
   }
}

创建好图纸之后需要放置视图到这个图纸里面:

  1. Viewport.Create 创建视窗
  2. tempUtempV 控制加入的视图位置
private void PlaceViews(ViewSet views, ViewSheet sheet)
{
     
   double xDistance = 0;
   double yDistance = 0;
   CalculateDistance(sheet.Outline, views.Size, ref xDistance, ref yDistance);

   Autodesk.Revit.DB.UV origin = GetOffSet(sheet.Outline, xDistance, yDistance);
   //Autodesk.Revit.DB.UV temp = new Autodesk.Revit.DB.UV (origin.U, origin.V);
   double tempU = origin.U;
   double tempV = origin.V;
   int n = 1;
   foreach (Autodesk.Revit.DB.View v in views)
   {
     
      Autodesk.Revit.DB.UV location = new Autodesk.Revit.DB.UV(tempU, tempV);
      Autodesk.Revit.DB.View view = v;
      Rescale(view, xDistance, yDistance);
      try
      {
     
         //sheet.AddView(view, location);
         Viewport.Create(view.Document, sheet.Id, view.Id, new XYZ(location.U, location.V, 0));
      }
      catch (ArgumentException /*ae*/)
      {
     
         throw new InvalidOperationException("The view '" + view.Name +
            "' can't be added, it may have already been placed in another sheet.");
      }

      if (0 != n++ % m_rows)
      {
     
         tempU = tempU + xDistance * (1 - TITLEBAR);
      }
      else
      {
     
         tempU = origin.U;
         tempV = tempV + yDistance;
      }
   }
}

你可能感兴趣的:(Revit,SDK,介绍)