VBA Insert drawing sheet and set position in broswer pane

VBA Insert drawing sheet and set position in broswer pane

MarkyTomm
Enthusiast Enthusiast
457 Views
4 Replies
Message 1 of 5

VBA Insert drawing sheet and set position in broswer pane

MarkyTomm
Enthusiast
Enthusiast

Hi All, just throwing this out there if you can help please

I have many drawings with upwards of 100 sheets (please don't roast me on this point)

If I need to insert a few sheets it get teadeous using the New Sheet function and then scrolling to the end a trying to drag the sheet to where I was when I inserted it so I am trying to put a VBA code snippet together to achieve this.

No problem inserting the sheet, title and border

Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
Set oSheet = oDrawDoc.Sheets.Add(kA3DrawingSheetSize, kLandscapePageOrientation, "THIS IS A NEW SHEET")

Dim oBorderDef As BorderDefinition
Dim oTitleBlockDef As TitleBlockDefinition

Dim oBorder As Border
Set oBorderDef = oDrawDoc.BorderDefinitions.Item("A3")
Dim oTitleBlock As TitleBlock
Set oTitleBlockDef = oDrawDoc.TitleBlockDefinitions.Item("A3")
Set oSheet = oDrawDoc.ActiveSheet
Set oBorder = oSheet.AddBorder(oBorderDef)
Set oTitleBlock = oSheet.AddTitleBlock(oTitleBlockDef)

 

just struggling to reposition the sheet in the browser pane to the position I was at when I ran the code

 

first I want to store the current browser position, which I will do before running the add sheet code

 

Dim oBroswerPane As BrowserPane

Dim oDesitinationSheetNode As BrowserNode
Set oDesitinationSheetNode SheetNode = obrowserpane.GetBrowserNodeFromObject(Sheet)



Then move the new sheet to the desitnation position (oDestinationSheetNode)

 

Dim oNewSheetNode As BrowserNode
Set oNewSheetNode = obrowserpane.GetBrowserNodeFromObject(Sheet)
Call obrowserpane.Reorder ?????????

 

Digging myself into a hole here - any help please?

 

Regards

Mark

 

0 Likes
Accepted solutions (1)
458 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @MarkyTomm.  That seems like a common need, but seemingly more complicated than it should be.  I copied your code, to a local Module, then edited it some.  Here, try this version.

Sub InsertSheet()
    If ThisApplication.ActiveDocumentType <> kDrawingDocumentObject Then Exit Sub
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
    Dim oSheet As Inventor.Sheet
    Set oSheet = oDrawDoc.Sheets.Add(kA3DrawingSheetSize, kLandscapePageOrientation, "THIS IS A NEW SHEET")
    Call oSheet.Activate
    Dim oBorderDef As BorderDefinition
    Set oBorderDef = oDrawDoc.BorderDefinitions.Item("A3")
    
    Dim oTitleBlockDef As TitleBlockDefinition
    Set oTitleBlockDef = oDrawDoc.TitleBlockDefinitions.Item("A3")
    
    Dim oBorder As Border
    Set oBorder = oSheet.AddBorder(oBorderDef)
    
    Dim oTitleBlock As TitleBlock
    Set oTitleBlock = oSheet.AddTitleBlock(oTitleBlockDef)
    
    Dim oBPane As Inventor.BrowserPane
    Set oBPane = oDrawDoc.BrowserPanes.Item("DlHierarchy") 'Model pane's internal name in a drawing
    
    Dim oNewSheetNode As Inventor.BrowserNode
    Set oNewSheetNode = oBPane.GetBrowserNodeFromObject(oSheet)
    
    Dim oLastSheet As Inventor.Sheet
    Set oLastSheet = oDrawDoc.Sheets.Item(oDrawDoc.Sheets.Count)
    
    Dim oLastSheetNode As Inventor.BrowserNode
    Set oLastSheetNode = oBPane.GetBrowserNodeFromObject(oLastSheet)
    
    Call oBPane.Reorder(oLastSheetNode, False, oNewSheetNode)
    
    Call oDrawDoc.Update2(True)
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

WCrihfield
Mentor
Mentor

On second look, the Index number I am using to get the last 'pre-existing' sheet may be off by 1, because the new sheet you just added will have the last index number.  Try subtracting 1 from that Count property.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Here is another version of that VBA macro's code.  In this version, I loop through every sheet and get the sheet number from each one.  The true sheet number is the part of the sheet's name that is after the ":" character.  Hopefully you do not use that character anywhere else in the sheet's name, or that will cause this custom process to fail.  That Integer is automatically generated by Inventor, and they will always be shown in the correct order, even if you rearrange the sheets.  To get that last Integer, I first 'Split' the sheet's name into an Array of String objects.  Then I get the second element from that Array, which will be Item 1, since Array's are zero based.  Then I convert that String to an Integer, so we can compare it mathematically.  Then I use a second Integer type variable to keep track of which one is the largest value, and also set the value of the oLastSheet variable at the same time.

Here is the alternate code I just created for you to try out:

Sub InsertSheet()
    If ThisApplication.ActiveDocumentType <> kDrawingDocumentObject Then Exit Sub
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
    Dim oSheet As Inventor.Sheet
    Set oSheet = oDrawDoc.Sheets.Add(kA3DrawingSheetSize, kLandscapePageOrientation, "THIS IS A NEW SHEET")
    Call oSheet.Activate
    Dim oBorderDef As BorderDefinition
    Set oBorderDef = oDrawDoc.BorderDefinitions.Item("A3")
    
    Dim oTitleBlockDef As TitleBlockDefinition
    Set oTitleBlockDef = oDrawDoc.TitleBlockDefinitions.Item("A3")
    
    Dim oBorder As Border
    Set oBorder = oSheet.AddBorder(oBorderDef)
    
    Dim oTitleBlock As TitleBlock
    Set oTitleBlock = oSheet.AddTitleBlock(oTitleBlockDef)
    
    Dim oBPane As Inventor.BrowserPane
    Set oBPane = oDrawDoc.BrowserPanes.Item("DlHierarchy") 'Model pane's internal name in a drawing
    
    Dim oNewSheetNode As Inventor.BrowserNode
    Set oNewSheetNode = oBPane.GetBrowserNodeFromObject(oSheet)
    
    'get sheet number of last sheet, and get the Sheet associated with it
    Dim oLastSheet As Inventor.Sheet
    Dim iLargestSheetNumber As Integer
    Dim iSheetNumber As Integer
    Dim oRandomSheet As Inventor.Sheet
    For Each oRandomSheet In oDrawDoc.Sheets
        'get the sheet number from the end of the sheet's name (after the ":" character)
        Dim oSheetNameParts() As String
        oSheetNameParts = Split(oRandomSheet.Name, ":")
        Dim sSheetNumber As String
        sSheetNumber = oSheetNameParts(1) 'zero based, so 1 is second element
        iSheetNumber = CInt(sSheetNumber) 'convert String to Integer, so we can compare mathematically
        'if main variable has not been assigned any value yet, set it to this sheet number
        If iLargestSheetNumber = 0 Then
            iLargestSheetNumber = iSheetNumber
            Set oLastSheet = oRandomSheet
        End If
        If iSheetNumber > iLargestSheetNumber Then
            iLargestSheetNumber = iSheetNumber
            Set oLastSheet = oRandomSheet
        End If
    Next
    
    Dim oLastSheetNode As Inventor.BrowserNode
    Set oLastSheetNode = oBPane.GetBrowserNodeFromObject(oLastSheet)
    
    Call oBPane.Reorder(oLastSheetNode, False, oNewSheetNode)
    
    Call oDrawDoc.Update2(True)
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 5

MarkyTomm
Enthusiast
Enthusiast

Hi,

 

Thats awsome work, thanks very much for the effort

Using the sheet number might give an incorrect destination position as I have several sheets within the set that act as section dividers and as such are excluded from count and printing

 

All seems to be working fine until it gets to the penultimate line 

Call oBPane.Reorder(oLastSheetNode, False, oNewSheetNode)

then i get a runtime error 5 - Invalid call or argument - which is parameter out of range or procedure not valid.

 

Could be somewthing to do with the onewsheet and olastsheet parameters being the same - I think we need to reorder the code to store the newsheet position before the call to oDrawDoc.Sheets.Add is made

 

Also need to reverse the sheet variables in the reorder call

 

I have used all the great bits from your code and come up with this (just removed the error checking and title/border bits for clarity)

Public Sub CreateSheetAtPosition()
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
    Dim oBPane As Inventor.BrowserPane
    Set oBPane = oDrawDoc.BrowserPanes.Item("DlHierarchy")
    Dim oNewSheetNode As Inventor.BrowserNode
    Set oNewSheetNode = oBPane.GetBrowserNodeFromObject(oDrawDoc.ActiveSheet)
    Dim oSheet As Inventor.Sheet
    Set oSheet = oDrawDoc.Sheets.Add(kA3DrawingSheetSize, kLandscapePageOrientation, "THIS IS A NEW SHEET")
    Call oSheet.Activate
    Dim oLastSheetNode As Inventor.BrowserNode
    Set oLastSheetNode = oBPane.GetBrowserNodeFromObject(oDrawDoc.ActiveSheet)
    Call oBPane.Reorder(oNewSheetNode, False, oLastSheetNode)
End Sub

 

This works now - just going to add all the dialogues for sheet size, titles, border etc

 

Many thanks again for your help