Revit – VB.Net Macro to make Print (Sheet) Sets from Sheet’s parameter.

Back in 2015 when I started learning  programing macros in SharpDevelop in Revit to change  different parameter’s value like Sheet names, Room numbers or custom Parameters, I decided to find a time saving solution to something more serious.

In our office there was a need to create about 35 Sheet Sets for printing purposes from few hundreds of drawings in our project. Sheets created in our project were divided to series.  Because we wanted to have more descriptive information in project browser then just a simple Sheet number we added a custom Project Parameter to Sheets called “Drawing Sheet Series”. (i.e. Sheets begging with number 20 had this parameter set to “(20) General Arrangment”)

This way all the sheets in our project were divided.

As a beginner to start with this task I needed some help. I didn’t know how to alter Revit print settings. It wasn’t easy to understand Revit API help file but I found a useful macro at Boos Your BIM web page (link is hear) which is creating a Sheet Set from Sheets containing in their number specific letters.  In my case I needed to do it for all Sheets therefor changes were required. Additionally because it was written in C# and I was more fluent in VB.Net I decided to translate it.

Below you can find a final macro prepared for Revit 2015:

 '
 ' Created by SharpDevelop.
 ' User: Pawel.Paszkiewicz
 ' Date: 28/10/2015
 ' Time: 18:35
 ' 
 ' Change "Drawing Sheet Series" text to the name of your Sheet Parameter which segragets sheets.
 '
 Imports System
 Imports Autodesk.Revit.UI
 Imports Autodesk.Revit.DB
 Imports Autodesk.Revit.DB.Architecture
 Imports Autodesk.Revit.UI.Selection
 Imports System.Collections.Generic
 Imports System.Linq
 Imports System.Diagnostics
 Imports System.Text
 <Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
 <Autodesk.Revit.DB.Macros.AddInId("80CD1D8D-FB71-49FA-94F0-2BDACB7CD2C9")> _
 Partial Public Class ThisDocument
 
     Private Sub Module_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
     
     End Sub
     
     Private Sub Module_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
     
     End Sub
     #Region "This is the code to be collapsed"
     Public Sub CreatePrintSet_from_DrawingSheetSeriesParameter()
         
           'If you are copying this macro to  Application-Level Macro  (computer specific macro) remove ".application" after "Me." below an change "Partial Public Class ThisDocument" to "ThisApplication" above. 
         Dim curDoc As Document = Me.Application.ActiveUIDocument.Document
         
             'This is a list to store all Sheet parameter values for comparison
             Dim SheetParamlist As New List(Of String)
             
             'This is a list to store all Print Sets actually created
             Dim createdPrintSets As New List(Of String)
             
             'get list of all sheets in project file
             Dim sheetList As List(Of ViewSheet) = getAllSheets(curDoc)
             'get list of all ViewSheetSets in project file
             
                    For Each curSheet As ViewSheet In sheetList
                     Dim DrawingSheetSeries As String
                     If curSheet.LookupParameter("Drawing Sheet Series").AsString IsNot Nothing
                         DrawingSheetSeries = curSheet.LookupParameter("Drawing Sheet Series").AsString
                         
                         'Make a VPrint Set only for Sheets in a specific group defined by parameter
                         If DrawingSheetSeries.Contains("") Then
                             'Leave empty if you don't need to filter parameter names
                             'If you need to filter off some parameter names add text between "". i.e DrawingSheetSeries.Contains("24")
 
                             'Check if a sheet is already added to a print set by checking if its parametre is on the list
                             If SheetParamlist.Contains(DrawingSheetSeries) Then
                             Else
                                 'add parameter to the list and make a print set from all drawings with this parameter
                                 Dim newViewSet As New ViewSet
                                 SheetParamlist.Add(DrawingSheetSeries)
                                 'this adds all drawings to a new print set
                                 For Each Sheetinlist As ViewSheet In sheetList
                                     If DrawingSheetSeries = Sheetinlist.LookupParameter("Drawing Sheet Series").AsString Then
                                         newViewSet.Insert(Sheetinlist)    
                                     End If
                                 Next
                                 'get the PrintManger from the current document
                                 Dim printManager As PrintManager = curDoc.PrintManager
                             
                                 'set this PrintManager to use the "Selected Views/Sheets" option
                                 printManager.PrintRange = PrintRange.Select
                             
                                 'get the ViewSheetSetting which manages the view/sheet set information of current document
                                 Dim viewSheetSetting As ViewSheetSetting = printManager.ViewSheetSetting
                             
                                 'set the views in this ViewSheetSetting to the newly created ViewSet
                                 viewSheetSetting.CurrentViewSheetSet.Views = newViewSet
                                 Dim setName As String = DrawingSheetSeries
                                 'create transaction
                                 Using curTrans As New Transaction(curDoc, "Create Sheet Sets")
                                 curTrans.Start()
     
                                     
                         
                                     Try
                                         'Save the current view sheet set to another view/sheet set with the specified name.
                                         viewSheetSetting.SaveAs(setName)
                                         'commit changes
                                         curTrans.Commit
                                         createdPrintSets.Add(setName)
                                         
                                     'handle the exception that will occur if there is already a view/sheet set with this name
                                     Catch ex As Autodesk.Revit.Exceptions.InvalidOperationException
                                         
                                         Dim td As New TaskDialog("TaskDialog")
                                         td.CommonButtons = TaskDialogCommonButtons.Ok
                                         td.CommonButtons = TaskDialogCommonButtons.Cancel
                                         'td.MainIcon = TaskDialogIcon.TaskDialogIconWarning
                                         td.AllowCancellation = True
                                         td.MainInstruction = "Print Set """ & setName & """ is already in use"
                                         td.ExpandedContent = "Existing print set will be renamed to: ""name_old1"" (or ""_ 2__etc.)." & Environment.NewLine & "New print set will be named: ""name_1"" (or ""_ 2__etc.)."
                                         td.MainContent = "What would you like to do:"
                                         td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Replace existing Print Set")
                                         td.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Rename existing Print Set")
                                         td.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "Rename this Print Set")
                                         Dim tdResult As TaskDialogResult = td.Show()
                                         
                                         curTrans.RollBack()
                                         
                                             'OPTION 1: If user clicks the first command link: "Replace existing Print Set"
                                             If TaskDialogResult.CommandLink1 = tdResult Then
                                                 curTrans.Start()
                                                 Dim VSSList As List(Of ViewSheetSet) = getAllViewSheetSets(curDoc)
                                                 For Each viewSheetSet0 As ViewSheetSet In VSSList
                                                     If viewSheetSet0.Name = setName Then        
                                                         viewSheetSetting.CurrentViewSheetSet.Views = newViewSet
                                                         viewSheetSetting.SaveAs(setName & "_temp")
                                                         viewSheetSetting.CurrentViewSheetSet = viewSheetSet0
                                                         viewSheetSetting.Delete
                                                         Dim VSSList3 As List(Of ViewSheetSet) = getAllViewSheetSets(curDoc)
                                                         For Each viewSheetSet1 As ViewSheetSet In VSSList3
                                                             If viewSheetSet1.Name = (setName & "_temp") Then
                                                                 viewSheetSetting.CurrentViewSheetSet = viewSheetSet1
                                                                 viewSheetSetting.Rename(setName)
                                                             End If
                                                         Next
                                                        End if
                                                 Next
                                                 curTrans.Commit
                                                 createdPrintSets.Add(setName)
                                                 
                                             'OPTION 2: If user clicks the first command link: "Rename existing Print Set"
                                             ElseIf TaskDialogResult.CommandLink2 = tdResult Then
                                                 curTrans.Start()
                                                 Dim VSSList1 As List(Of ViewSheetSet) = getAllViewSheetSets(curDoc)
                                                 For Each viewSheetSet2 As ViewSheetSet In VSSList1
                                                     If viewSheetSet2.Name = setName Then 
                                                         viewSheetSetting.CurrentViewSheetSet.Views = newViewSet
                                                         viewSheetSetting.SaveAs(setName & "_temp")
                                                         viewSheetSetting.CurrentViewSheetSet = viewSheetSet2
                                                         Try
                                                             viewSheetSetting.Rename(setName & "_old1")
                                                             TaskDialog.Show("ViewSetRenameOld","Existing print set was renamed to: """ & setName & "_old1""")
                                                         Catch ex2 As Autodesk.Revit.Exceptions.InvalidOperationException
                                                             curTrans.RollBack()
                                                             Dim VSSList4 As List(Of ViewSheetSet) = getAllViewSheetSets(curDoc)
                                                             Dim i As Integer = 1
                                                             Dim Z As Boolean = False
                                                             Do While Z = False
                                                                 For Each viewSheetSet4 As ViewSheetSet In VSSList4
                                                                     If viewSheetSet4.Name = (setName & "_old" & i) Then    
                                                                         i = i + 1
                                                                     Else
                                                                         Z = True
                                                                     End If
                                                                 Next
                                                             Loop
                                                             curTrans.Start()
                                                             viewSheetSetting.SaveAs(setName & "_old" & i)
                                                             TaskDialog.Show("ViewSetRenameOld","Existing print set was renamed to: """ & setName & "_old" & i)
                                                         End Try
                                                     End If
                                                 Next
                                                 Dim VSSList2 As List(Of ViewSheetSet) = getAllViewSheetSets(curDoc)
                                                 For Each viewSheetSet3 As ViewSheetSet In VSSList2
                                                     If viewSheetSet3.Name = setName & "_temp" Then
                                                         viewSheetSetting.CurrentViewSheetSet = viewSheetSet3
                                                         viewSheetSetting.Rename(setName)
                                                     End If
                                                 Next
                                                 curTrans.Commit
                                                 createdPrintSets.Add(setName)    
                                             
                                             'OPTION 3: If user clicks the second command link: "Rename this Print Set"
                                             ElseIf TaskDialogResult.CommandLink3 = tdResult Then
                                                 Try
                                                     curTrans.Start()
                                                     'Save the current view sheet set to another view/sheet set with the specified name.
                                                     viewSheetSetting.SaveAs(setName & "_1")
                                                     'commit changes
                                                     curTrans.Commit
                                                     createdPrintSets.Add(setName & "_1")
                                         
                                                 'handle the exception that will occur if there is already a view/sheet set with this name
                                                 Catch ex1 As Autodesk.Revit.Exceptions.InvalidOperationException
                                                     curTrans.RollBack()
                                                     Dim VSSList As List(Of ViewSheetSet) = getAllViewSheetSets(curDoc)
                                                     Dim i As Integer = 1
                                                     Dim Z As Boolean = False
                                                     Do While Z = False
                                                         For Each viewSheetSet2 As ViewSheetSet In VSSList
                                                             If viewSheetSet2.Name = (setName & "_" & i) Then    
                                                                 i = i + 1
                                                             Else
                                                                 Z = True
                                                             End If
                                                         Next
                                                     Loop
                                                     curTrans.Start()
                                                     viewSheetSetting.SaveAs(setName & "_" & i)
                                                     curTrans.Commit
                                                     createdPrintSets.Add(setName & "_" & i)
                                                 End Try
                                             Else
                                             End If
                                     End Try    
                                 
                                 End Using       
                             End If
                         Else
                         End If
                     Else
                     End If
             
                    Next
                    If createdPrintSets.Count = 0 Then
                        TaskDialog.Show("End", "NO PRINT SETS CREATED!")
                    Else
                        TaskDialog.Show("End", "No of Prints Sets Created: "& createdPrintSets.Count & Environment.NewLine & Environment.NewLine & "Names of Print Sets Created: "& Environment.NewLine & (String.Join(Environment.NewLine, createdPrintSets.ToArray)))
                    End If
    End Sub
    #End Region    
     '----------------------------------------------------
     'collector
     '----------------------------------------------------            
    Public Function getAllSheets(curDoc As Document) As List(Of ViewSheet)
         'get all views
         Dim sheetCollector As New FilteredElementCollector(curDoc)
         sheetCollector.OfCategory(BuiltInCategory.OST_Sheets)
         
         Dim SheetsCollection As New List(Of ViewSheet)
         For Each x As ViewSheet In sheetCollector.ToElements
             SheetsCollection.Add(x)
         Next
         
         Return SheetsCollection
    End Function
    
    Public Function getAllViewSheetSets(curDoc As Document) As List(Of ViewSheetSet)
         'get all  ViewSheetSets
         Dim VSSCollector As New FilteredElementCollector(curDoc)
         VSSCollector.OfClass(GetType(ViewSheetSet))
     
         Dim VSSCollection As New List(Of ViewSheetSet)
         For Each s As ViewSheetSet In VSSCollector.ToElements
             VSSCollection.Add(s)
         Next
         
         Return VSSCollection
     End Function
    
    
 End Class

Additionally I added a function which asks you what to do when an a Print Set is existing.

Please find below links download Revit files with this macro for Revit 2014, 2015 and 2016 version:
PrintSets2014
PrintSets2015
PrintSets2016

Hope it will be useful.

Leave a Reply

Your email address will not be published. Required fields are marked *