Hi David
Here is the code that can work well to save the ViewSheetSet.
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class RevitCommand4 : 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();
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(ViewPlan)).OfCategory(BuiltInCategory.OST_Views);
ViewSet vs = app.Application.Create.NewViewSet();
foreach (Element elem in collector)
{
ViewPlan plan = elem as ViewPlan;
if (plan.IsTemplate == false)
vs.Insert(plan);
}
PrintManager pm = doc.PrintManager;
ViewSheetSetting setting = pm.ViewSheetSetting;
string viewSheetSetName = "asPlans1333355";
setting.SaveAs(viewSheetSetName);
setting.CurrentViewSheetSet.Views = vs;
setting.Save();
//pm.ViewSheetSetting.SaveAs(viewSheetSetName);
//pm.ViewSheetSetting.CurrentViewSheetSet.Views = vs;
//pm.ViewSheetSetting.Save();
ViewSheetSetting setting1 = pm.ViewSheetSetting;
ViewSheetSetting setting2 = pm.ViewSheetSetting;
pm.PrintRange = PrintRange.Select;
pm.CombinedFile = true;
pm.PrintToFile = true;
trans.Commit();
pm.SubmitPrint();
return Result.Succeeded;
}
}
Several notes about this code.
1. SaveAs ViewSheetSetting, assign views to ViewSheetSet, and save the ViewSheetSetting code should be within a transaction. These actions actually changed the document. So transaction is necessary.
2. The following three line cannot be replaced the three lines after it.
//pm.ViewSheetSetting.SaveAs(viewSheetSetName);
//pm.ViewSheetSetting.CurrentViewSheetSet.Views = vs;
//pm.ViewSheetSetting.Save();
setting.SaveAs(viewSheetSetName);
setting.CurrentViewSheetSet.Views = vs;
setting.Save();
Reason: every time we call pm.ViewSheetSetting, it returns a new ViewSheetSetting instance. So for the following three lines, the three actions take place to three different ViewSheetingSetting objects. That is not we expected.
//pm.ViewSheetSetting.SaveAs(viewSheetSetName);
//pm.ViewSheetSetting.CurrentViewSheetSet.Views = vs;
//pm.ViewSheetSetting.Save();
We want the three actions take place to the same ViewSheetSetting object, so we first get a ViewSheetSetting instance, then operate on this ViewSheetSetting instance.
Simillarly, Document.PrintManager returns different PrintManager if you call it one after another.
So the following two lines take place to two different PrintManager object.
Doc.PrintManager.PrintRange = PrintRange.Select;
Doc.PrintManager..CombinedFile = true;
Make sure to return one PrintManager assign it a a PrintManager variable first, then operate on this variable.
Hope this helps!
Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network