By Joe Ye
Revit didn’t expose API to change the the property of a section view to make it a drafting view. Here introduce a workaround how to put the content in a section view to a drafting view.
The solution is to using the view export and dwg file import. You can export any view to dwg file via Document.Export() method. After export the dwg file, import this dwg to the target drafting view via Document.Import() method. BTW, if the drafting view doesn’t exist, create it before import via Document::NewViewDrafting() method.
Here is the full and simplest code to show the solution.
[TransactionAttribute(TransactionMode.Manual)]
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Transaction trans = new Transaction(doc, "ExComm");
trans.Start();
//export the current view
DWGExportOptions options = new DWGExportOptions();
ICollection<ElementId> views = new List<ElementId>();
//before run this command, make sure
//the target section view is the active view.
views.Add(doc.ActiveView.Id);
doc.Export("c:\\test\\","exportdwgTest.dwg",views,options);
ViewDrafting drafting = doc.Create.NewViewDrafting();
doc.Regenerate();
ElementId id;
DWGImportOptions importOptions = new DWGImportOptions();
doc.Import("c:\\test\\exportdwgTest.dwg", importOptions, drafting, out id);
trans.Commit();
return Result.Succeeded ;
}
}