thanks arqt49 for your reply... actually i just finished it today after some suffer ...... however, i have faced another issue, i was unable to update an existing sheet set. to cut a long story short, below is what i did:
My aim is ---. to sort all sheetviews in a set relative to paper size of titleblock
1. search for viewsheets in document
2. find the word "A4" in viewsheet blocktitle name.
3. Creat a new set with name A4
4. insert all selected viewsheets in this set.
5. Save set.
6. delete any empty view set.
all above works very fine... after i apply the command i made a change in the document,for example delete one sheet or a title block from one of the sheets. then after rerun the command it gives me error unable to save sheetset.
so i made a trick to delete all sets before the music starts.
do you think what i did is the correct follow?
Code:
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
namespace SheetSet
{
[Transaction(TransactionMode.Manual)]
public class Class2 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = commandData.Application.ActiveUIDocument.Document;
UIApplication app = commandData.Application;
string report = "Sheets Arranged Successfully!" + "\r\n";
report += SheetSet(doc, uidoc, app);
TaskDialog.Show("Arrange sheet Set", report);
return Result.Succeeded;
}
/// <summary>
/// Delete sheetset
/// </summary>
/// <param name="doc"></param>
/// <param name="nset"></param>
/// <param name="uidoc"></param>
/// <returns></returns>
public void delset(Document doc, string nset, UIDocument uidoc)
{
using (Transaction t = new Transaction(doc, "Delete ViewSet"))
{
t.Start();
PrintManager PM = doc.PrintManager;
PM.PrintRange = PrintRange.Select;
ViewSheetSetting vs = PM.ViewSheetSetting;
foreach (ViewSheetSet set in doc.ViewSheetSets)
{
if (set.Name == nset)
{
try
{
PM.PrintRange = PrintRange.Select;
vs.CurrentViewSheetSet = set;
vs.Delete();
}
catch (Exception ex1)
{
TaskDialog.Show("Error1", ex1.Message);
}
t.Commit();
}
}
}
}// end of delset
/// <summary>
/// SheetSet
/// </summary>
/// <param name="doc"></param>
/// <param name="uidoc"></param>
/// <param name="app"></param>
public string SheetSet(Document doc, UIDocument uidoc, UIApplication app)
{
string report = "";
for (int i = 0; i < 5; i++)
{
switch (i)
{
case 0:
report += test(app, uidoc, doc, "A0");
break;
case 1:
report += test(app, uidoc, doc, "A1");
break;
case 2:
report += test(app, uidoc, doc, "A2");
break;
case 3:
report += test(app, uidoc, doc, "A3");
break;
case 4:
report += test(app, uidoc, doc, "A4");
break;
}
}
return report;
}//end of sheetset
/// <summary>
///
/// </summary>
/// <param name="app"></param>
/// <param name="uidoc"></param>
/// <param name="doc"></param>
/// <param name="PSize"></param>
/// <returns></returns>
public string test(UIApplication app, UIDocument uidoc, Document doc, string PSize)
{
string report = "";
delset(doc, PSize, uidoc);
ViewSet vs = app.Application.Create.NewViewSet();
foreach (ViewSheet vSheet in new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)))
{
foreach (Element e in new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_TitleBlocks))
{
if (e.OwnerViewId == vSheet.Id && e.Name.Contains(PSize))
{
vs.Insert(vSheet);
}
}
}
if (vs.Size != 0)
{
using (Transaction t = new Transaction(doc, "Save set"))
{
t.Start();
PrintManager pm = doc.PrintManager;
pm.PrintRange = PrintRange.Select;
ViewSheetSetting vsettings = pm.ViewSheetSetting;
try
{
vsettings.SaveAs(PSize);
vsettings.CurrentViewSheetSet.Views = vs;
vsettings.Save();
report = PSize + " SheetSet---No of sheets " + vs.Size.ToString() + "\r\n";
}
catch (Exception ex3)
{
foreach (ViewSheetSet vset in doc.ViewSheetSets)
{
if (vset.Name == PSize && vs.Size !=0)
{
pm.PrintRange = PrintRange.Select;
vsettings.CurrentViewSheetSet = vset;
vsettings.CurrentViewSheetSet.Views = vs;
try
{
vsettings.Save();
report = PSize + " SheetSet---No of sheets " + vs.Size.ToString() + "\r\n";
}
catch (Exception ex2)
{
//TaskDialog.Show(PSize + " Error2" , ex2.Message);
}
//TaskDialog.Show("Error3", ex3.Message);
}
}
}
t.Commit();
}
}
return report;
}
}//end of class
}//end of namespace