View Full Version : Adding a new sheet using VSTA...
FWSchreck
2009-03-13, 06:25 AM
Hello:
I'm writing a VSTA routine to add sheets into the active project. I'm having trouble coming up with the code to set the proper titleblock family and type at sheet creation.
Here's the code I have so far:
Private Sub AddNewSheet(ByVal strSheetNumber As String, ByVal strSheetName As String)
Dim p_Titleblock As FamilySymbol
p_Titleblock.Family = "??"
p_Titleblock.Name = "??"
Dim myDetailSheet As ViewSheet = Me.Create.NewViewSheet(p_Titleblock)
myDetailSheet.Name = strSheetName
myDetailSheet.SheetNumber = strSheetNumber
End Sub
How can I specify the proper titleblock family and type for the NewViewSheet method?
FWSchreck
2009-03-13, 05:04 PM
All:
After doing some digging, I came up with this:
-----------------------------------------------------------------------------------
Private Sub MakeNewSheet(ByVal strSheetNumber As String, ByVal strSheetName As String)
Dim CurrentFamily As FamilySymbolSet = Me.TitleBlocks
Dim p_Titleblock As FamilySymbol = Nothing
For Each f As FamilySymbol In CurrentFamily
If f IsNot Nothing Then
p_Titleblock = f
Exit For
End If
Next
Dim myDetailSheet As ViewSheet = Me.Create.NewViewSheet(p_Titleblock)
myDetailSheet.Name = strSheetName
myDetailSheet.SheetNumber = strSheetNumber
End Sub
Public Sub AddNewSheet()
MakeNewSheet("A2.01", "First Floor Plan")
MakeNewSheet("A2.02", "Second Floor Plan")
' Add more sheets here
End Sub
--------------------------------------------------------------------------------------------------
This seems to work properly. Does anyone have any suggestions for improvement?
cphubb
2009-07-13, 04:51 AM
Fred,
I tried your code out and am getting an error. VSTA is saying that 'TitleBlocks' is not a member of 'CreateSheets.ThisApplication' and'NewViewSheet' is not a member of 'Autodesk.Revit.Creation.Application
I think I have all my references and imports correct. Did you do anythig to get these two classes imported?
Thanks
TroyGates
2009-07-13, 11:09 PM
If you created the macro as an application macro instead of a document macro you should change:
Dim CurrentFamily As FamilySymbolSet = Me.TitleBlocks
to
Dim CurrentFamily As FamilySymbolSet = Me.ActiveDocument.TitleBlocks
and
Dim myDetailSheet As ViewSheet = Me.Create.NewViewSheet(p_Titleblock)
to
Dim myDetailSheet As ViewSheet = Me.ActiveDocument.Create.NewViewSheet(p_Titleblock)
You also need to use the following imports:
Imports Autodesk.Revit.Symbols
Imports Autodesk.Revit.Elements
cphubb
2009-07-14, 06:20 PM
Troy,
Thanks that fixed the build errors. However when I run the macros now they error with a general error "Attempt to modify the model outside of transaction" and then a number of error traces. Any idea why it builds but does not run? Finally after I tried to run the macro, I clicked back to the macro manager and Revit crashed.
TroyGates
2009-07-14, 11:13 PM
I posted the changes without realizing it doesn't work as an application macro, only as a document macro. I'm not sure why the code works at the document level and not at the application level.
Tried the following in Revit 2010 Application Macro and it gives the same error as you.
Private Sub MakeNewSheet(ByVal strSheetNumber As String, ByVal strSheetName As String)
Dim doc As Document = Me.ActiveDocument
Dim CurrentFamily As FamilySymbolSet = doc.TitleBlocks
Dim p_Titleblock As FamilySymbol = Nothing
For Each f As FamilySymbol In CurrentFamily
If f IsNot Nothing Then
p_Titleblock = f
Exit For
End If
Next
Dim myDetailSheet As ViewSheet = doc.Create.NewViewSheet(p_Titleblock)
myDetailSheet.Name = strSheetName
myDetailSheet.SheetNumber = strSheetNumber
End Sub
Public Sub AddNewSheet()
MakeNewSheet("A2.01", "First Floor Plan")
MakeNewSheet("A2.02", "Second Floor Plan")
End Sub
Yet I try the same code in Revit 2009 Application Macro and it works fine.
Something in Revit 2010 VSTA is different in accessing the title blocks from the Application level. I am not sure what it is, but I will try to figure it out.
thanhphuynh82
2009-07-17, 02:38 PM
try to use :
Private Sub MakeNewSheet(ByVal strSheetNumber As String, ByVal strSheetName As String)
Dim doc As Autodesk.Revit.Document = Application.ActiveDocument
Dim CurrentFamily As Autodesk.Revit.Symbols.FamilySymbolSet = doc.TitleBlocks
Dim p_Titleblock As Autodesk.Revit.Symbols.FamilySymbol = Nothing
For Each f As Autodesk.Revit.Symbols.FamilySymbol In CurrentFamily
If f IsNot Nothing Then
p_Titleblock = f
Exit For
End If
Next
Dim myDetailSheet As Autodesk.Revit.Elements.ViewSheet = doc.Create.NewViewSheet(p_Titleblock)
myDetailSheet.Name = strSheetName
myDetailSheet.SheetNumber = strSheetNumber
End Sub
Public Sub AddNewSheet()
MakeNewSheet("A2.01", "First Floor Plan")
MakeNewSheet("A2.02", "Second Floor Plan")
End Sub
cphubb
2009-07-20, 06:01 AM
THAN,
tried your modification. I see why it should work, because the application was not in scope for the active document and ME does not appear to work in that manner for 2010. However I am still getting the same error of being outside the transaction. I also went back to 2009 and tried the code both ways and it worked either way.
joseguia
2009-07-28, 08:20 PM
In document level you have to begin and end the transactions, so in this case just a few tweaks to make this work as a Document Level Macro :
Imports:
Imports Autodesk.Revit.Creation
Imports Autodesk.Revit.Symbols
Imports Autodesk.Revit.Elements
Private Sub MakeNewSheet(ByVal strSheetNumber As String, ByVal strSheetName As String)
Me.ActiveDocument.BeginTransaction()
Dim CurrentFamily As FamilySymbolSet = Me.ActiveDocument.TitleBlocks
Dim p_Titleblock As FamilySymbol = Nothing
For Each f As FamilySymbol In CurrentFamily
If f IsNot Nothing Then
p_Titleblock = f
Exit For
End If
Next
Dim myDetailSheet As ViewSheet = Me.ActiveDocument.Create.NewViewSheet(p_Titleblock)
myDetailSheet.Name = strSheetName
myDetailSheet.SheetNumber = strSheetNumber
Me.ActiveDocument.EndTransaction()
End Sub
Calling the SUB:
Public Sub AddNewSheet()
MakeNewSheet("A2.01", "First Floor Plan")
MakeNewSheet("A2.02", "Second Floor Plan")
End Sub
of course you may want to wrap this bad boy in some Try-Catching if ya know what I mean!
;-)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.