Results 1 to 8 of 8

Thread: delete empty View/sheet set

  1. #1
    Member
    Join Date
    2008-11
    Posts
    27
    Login to Give a bone
    0

    Default delete empty View/sheet set

    hi
    i am at 0 step to Revit API and fighting hard to get my feet wet in this interesting piece of creativity tools. i have created a sheet set that is divided by sheet Size for easy printing with no suffer.i have done this through identifying the title block name in each sheetview whether it contains A3 or A4 ...etc.

    my question is , i want the program to check if the there is no sheet fr the "A1" size for example is to delete this set. but i can't find the right statement for that.

    any adice for this?

    Code:
    UIDocument doc = commandData.Application.ActiveUIDocument;
    
                {
    			//1-	Creating a sheetset
    			ViewSet vsA4 = new ViewSet();
    			ViewSet vsA3 = new ViewSet();
    			ViewSet vsA2 = new ViewSet();
    			ViewSet vsA1 = new ViewSet();
    			ViewSet vsA0 = new ViewSet();
    			PrintManager PrnMng = doc.Document.PrintManager;
    			PrnMng.PrintRange = PrintRange.Select;
    			ViewSheetSetting Vset = PrnMng.ViewSheetSetting;
    			
    			//2-	Collector/Filter for getting Sheet Views
    			FilteredElementCollector VwFilter = new FilteredElementCollector(doc.Document);
    			
    			VwFilter.OfCategory(BuiltInCategory.OST_Sheets);
    			
    			//3-	Collector/filter for getting the built in Family instance Title Block.
    			foreach (Element element in VwFilter)
    			{
    				doc.ActiveView = element as Autodesk.Revit.DB.View;
    				
    				//a.	Identify filter type.
    				FilteredElementCollector FICol = new FilteredElementCollector(doc.Document, doc.Document.ActiveView.Id);
    				
    				//b.	Identify collector.
    				FICol.OfCategory(BuiltInCategory.OST_TitleBlocks);
    				
    				//c.	Check if the element is in the active view and send a message for that.
    				
    				//4-	adding A3 sheet to a set
    				foreach (Element elen in FICol.ToElements())
    				{
    					
    					if (elen.Name.Contains("A4"))
    					{
    						
    						vsA4.Insert(doc.ActiveView);
    					}
    					else if(elen.Name.Contains("A3"))
    					{
    						vsA3.Insert(doc.ActiveView);
    					}
    					else if (elen.Name.Contains("A2"))
    					{
    						vsA2.Insert(doc.ActiveView);
    					}
    					else if (elen.Name.Contains("A1"))
    					{
    						vsA1.Insert(doc.ActiveView);
    					}
    					else if(elen.Name.Contains("A0"))
    					{
    						vsA0.Insert(doc.ActiveView);
    					}
    					
    				}
    				
    			}
    			
    			
    			// you must start a transaction as long as you will make changes in the document
    			
    			
    			
    			for (int i = 0; i < 5; i++)
    			{
    				using (Transaction t = new Transaction(doc.Document,"ViewSet"))
    					
    				{
    					t.Start();
    					
    					{
    						
    						/*if (vsA0.Size == 0 ||  vsA1.Size==0 ||  vsA2.Size==0 ||  vsA3.Size==0 || vsA4.Size==0)
    						{	
    							
    							continue;
    						}
    						*/
    						switch (i) {
    							case 0:
    
                                    Vset.CurrentViewSheetSet.Views = vsA0;
    							
                                         
    								
    								//Vset.Delete();
    								try {
    									
    								
    								Vset.SaveAs("A0");
    								MessageBox.Show("A0 set has been Created Done, " + vsA0.Size+ " sheets has been added!");
    								} catch (Exception) {
    									
                                        
    									Vset.Save();
    									MessageBox.Show("A0 set Saved");
    								}
    								
    								Vset.CurrentViewSheetSet.Views = new ViewSet();
    								break;
    								
    								
    							case 1:
    								
    								
    								Vset.CurrentViewSheetSet.Views = vsA1;
    								try {
    								
    								Vset.SaveAs("A1");
    								MessageBox.Show("A1 set has been Created Done, " + vsA1.Size+ " sheets has been added!");
    								} catch (Exception) {
    									if(vsA1.Size == 0)
    									{
    									
    									Vset.Delete();
    									MessageBox.Show("A1 set Deleted");
    									}
    									else
    									{
    										Vset.Save();
    										MessageBox.Show("A1 set Saved");
    									}
    								}
    								
    								Vset.CurrentViewSheetSet.Views = new ViewSet();
    								break;
    								
    								
    							case 2:
    								
    								
    								Vset.CurrentViewSheetSet.Views = vsA2;
    								//Vset.Delete();
    								Vset.SaveAs("A2");
    								MessageBox.Show("A2 set has been Created Done, " + vsA2.Size+ " sheets has been added!");
    								Vset.CurrentViewSheetSet.Views = new ViewSet();
    								break;
    								
    							case 3 :
    								
    								Vset.CurrentViewSheetSet.Views = vsA3;
    								//Vset.Delete();
    								Vset.SaveAs("A3");
    								MessageBox.Show("A3 set has been Created Done, " + vsA3.Size+ " sheets has been added!");
    								Vset.CurrentViewSheetSet.Views = new ViewSet();
    								break;
    								
    							case 4:
    								
    								Vset.CurrentViewSheetSet.Views = vsA4;
    								//Vset.Delete();
    								
    								Vset.SaveAs("A4");
    								
    								MessageBox.Show("A4 set has been Created Done, " + vsA4.Size+ " sheets has been added!");
    								Vset.CurrentViewSheetSet.Views = new ViewSet();
    							
    								break;
    								
    						}
    						
    						
    					}
    					
    					
    					
    					t.Commit();
    				}
    			}
    		} //end of Try
    		
    	 // end of for loop
    
                return Autodesk.Revit.UI.Result.Succeeded;
            }
        }
    }
    Last edited by mostafa90; 2013-12-22 at 11:48 AM.

  2. #2
    Member
    Join Date
    2008-11
    Posts
    27
    Login to Give a bone
    0

    Default Re: delete empty View/sheet set

    i think i found the answer, after a huge tryies the answer is to set the required set you want to delete to current set and then delete

  3. #3
    I could stop if I wanted to
    Join Date
    2007-07
    Location
    London, UK
    Posts
    361
    Login to Give a bone
    0

    Default Re: delete empty View/sheet set

    Checking for the empty ViewSet goes like this: "!vs.IsEmpty" , where the "!" means Not.

    Not 100% sure if this would work (never worked with ViewSet before), but I think you can shorten your code and get rid of the for loop and the switch.
    Instead of creating several ViewSet instances, I would suggest a Dictionary that would go like this:

    Code:
    public static void vs(this Document document)
    {
        PrintManager PrnMng = document.PrintManager;
    	PrnMng.PrintRange = PrintRange.Select;
    	ViewSheetSetting Vset = PrnMng.ViewSheetSetting;
    
        Dictionary<String, ViewSet> d = new Dictionary<string, ViewSet>();
        String[] sizes = { "A0", "A1", "A2", "A3", "A4" };
        foreach (String s in sizes)
            d.Add(s, new ViewSet());
    
        foreach (Element v in new FilteredElementCollector(document).OfClass(typeof(View)))
    	{
            Element t = new FilteredElementCollector(document, v.Id)
                .OfCategory(BuiltInCategory.OST_TitleBlocks).First();
            foreach (String s in sizes)
                if (t.Name.Contains(s))
                    d[s].Insert(v as View);
        }
        Transaction tr = new Transaction(document);
        tr.Start();
        foreach (String s in sizes)
        {
            ViewSet vs = d[s];
            if (!vs.IsEmpty)
            {
                Vset.CurrentViewSheetSet.Views = vs; // Here is where I am not sure...
                Vset.SaveAs(s);
            }
        }
        if (tr.GetStatus().Equals(TransactionStatus.Error))
            tr.RollBack();
        else
            tr.Commit();
    }

  4. #4
    Member
    Join Date
    2008-11
    Posts
    27
    Login to Give a bone
    0

    Default Re: delete empty View/sheet set

    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
    Last edited by mostafa90; 2013-12-27 at 02:04 PM.

  5. #5
    I could stop if I wanted to
    Join Date
    2007-07
    Location
    London, UK
    Posts
    361
    Login to Give a bone
    0

    Default Re: delete empty View/sheet set

    You should probably collect the existing ViewSheetSet(s) first.
    use a FilteredElementCollector(document).OfClass(typeof(ViewSheetSet) and check their names against the sheet sizes.
    For the already existing ones, instead of creating new versions, clear it´s views and add the fresh ones.

  6. #6
    Member
    Join Date
    2008-11
    Posts
    27
    Login to Give a bone
    0

    Default Re: delete empty View/sheet set

    could be yes another way to save new sheets as you said... any way thanks for help ....if you don't mind... i barley hard understand the mechanism of the API... for example one of the biggest reason my code was not performing is to add the line " pm.PrintRange = PrintRange.Select;" before defining viewsheetsettings.... so how on earth i could discover this, unless i got informed with guys like you!!! so what do you advise me to read or todo , to get to understand the basic concepts to carry on??
    Last edited by mostafa90; 2013-12-27 at 03:11 PM.

  7. #7
    Member
    Join Date
    2013-11
    Location
    Oregon
    Posts
    8
    Login to Give a bone
    0

    Default Re: delete empty View/sheet set

    Are you developing this internally or for public release? if it is for public consumption, I highly recommend avoiding and family name references in the code. Each firm is soo specific in family naming, you will most likely break.

  8. #8
    Member
    Join Date
    2008-11
    Posts
    27
    Login to Give a bone
    0

    Default Re: delete empty View/sheet set

    You are right mattbenimble ... I am developing this for internal use ... if it where for public I would have asked for recording a settings file for the first time to be used something like ini file.

Similar Threads

  1. Adding an empty row in Sheet List index
    By sovby254640 in forum AutoCAD Customization
    Replies: 7
    Last Post: 2016-05-17, 06:34 PM
  2. Replies: 0
    Last Post: 2012-03-01, 08:03 PM
  3. Sheet Sets became empty
    By LGB in forum AutoCAD General
    Replies: 3
    Last Post: 2011-12-05, 01:04 PM
  4. Cnnot Delete Sheet from Sheet List
    By laryssa in forum Revit Architecture - General
    Replies: 2
    Last Post: 2010-09-03, 10:20 AM
  5. Tip - Delete all your empty folders on a drive
    By madcadder in forum CAD Management - General
    Replies: 11
    Last Post: 2008-06-23, 02:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •