创建图纸并向图纸中添加视图,主要用到了两个静态方法:
public static ViewSheet Create(Document document, ElementId titleBlockTypeId)//创建图纸
public static Viewport Create(Document document, ElementId viewSheetId, ElementId viewId, XYZ point)
//在图纸的给定位置添加视图
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfCategory(BuiltInCategory.OST_TitleBlocks).OfClass(typeof(FamilySymbol));
FamilySymbol fs = collector.FirstElement() as FamilySymbol;
//创建图纸
ViewSheet viewSheet = null;
if (fs != null)
{
using (Transaction trans = new Transaction(doc))
{
trans.Start("Create a new Viewsheet");
try
{
viewSheet = ViewSheet.Create(doc,fs.Id);
if (null == viewSheet)
{
throw new Exception("failed to create new ViewSheet.");
}
trans.Commit();
}
catch (Exception )
{
trans.RollBack();
}
}
}
//向图纸中添加视图
FilteredElementCollector collectoView = new FilteredElementCollector(doc);
collectoView.OfClass(typeof(View)).OfCategory(BuiltInCategory.OST_Views);
View view = collectoView.FirstElement() as View;
UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2, (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);
using (Transaction trans = new Transaction(doc))
{
trans.Start("create viewPort");
try
{
Viewport.Create(doc, viewSheet.Id, view.Id, new XYZ(location.U, location.V, 0));
trans.Commit();
}
catch
{
trans.RollBack();
}
}
return Result.Succeeded;
}