Renaming Drawings Sheets

Renaming Drawings Sheets

nstone
Contributor Contributor
2,511 Views
27 Replies
Message 1 of 28

Renaming Drawings Sheets

nstone
Contributor
Contributor

Hi,

I am trying to find an automated way to rename multiple sheets when creating Inventor drawings.

I have an Inventor assembly, and I need to create a drawing for each component in this assembly.

I have one Inventor Drawing with multiple sheets in it.  Each sheet is for each part in the assembly.

I would like to loop though each sheet and rename the sheet name to the Part number of the part.

Is there a simple way to do this though i Logic? And how would I go about doing something like this?

 

Thanks

0 Likes
2,512 Views
27 Replies
Replies (27)
Message 21 of 28

Ray_Feiler
Advisor
Advisor

Smiley LOL

Looked right past that, thank you very much!

Ray


Product Design & Manufacturing Collection 2024
Sometimes you just need a good old reboot.
0 Likes
Message 22 of 28

Owner2229
Advisor
Advisor

You're welcomed.

 

Btw. read the last line of my signature. After seeing your code I was like: He should pray for I don't find his address. Smiley Very Happy

 

Actually it's not that bad, just way too many workarounds.

 

Also, I've forgot one line:

 

Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
For Each oSheet As Sheet In oDoc.Sheets
    If oSheet.DrawingViews.Count = 0 Then Continue For
Dim oView As DrawingView = oSheet.DrawingViews(1) Dim oModelDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument Dim oName As String = iProperties.Value(oModelDoc.DisplayName, "Project", "Part Number")
If oName = vbNullString Then Continue For If oSheet.Name = oName Then Continue For oSheet.Name = oName Next

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 23 of 28

Ray_Feiler
Advisor
Advisor

This is my first iLogic code so beware all who copy and paste. Smiley Wink


Product Design & Manufacturing Collection 2024
Sometimes you just need a good old reboot.
0 Likes
Message 24 of 28

Ray_Feiler
Advisor
Advisor

Here is my cobbled together mess of code for all who dare to try Smiley LOL

 

'Declarations
Dim oSheets As Sheets = ThisDrawing.Document.sheets
Dim oSheet As Sheet
Dim oActDoc As Document = ThisApplication.ActiveDocument
If oActDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub 'Check if the active document is a drawing
Try 'Check if the active sheet has any views
    Dim oSheetTest As Sheet = ThisDrawing.Document.ActiveSheet
    If oSheetTest.DrawingViews.Count = 0 Then
        MessageBox.Show("The active sheet must contain at least one view!", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
         Goto ErrorHandler
    Else
oActDwgViewName = oActDoc.ActiveSheet.DrawingViews(1).Name 'Retrieve the active sheet base views name
oActModelDoc = ActiveSheet.View(oActDwgViewName).ModelDocument 'Identify the model in the active sheet base view
oActPartNum = iProperties.Value(oActModelDoc.DisplayName, "Project", "Part Number") 'Retrive the part number from the models iProperties
oFirstSheetName = oActPartNum & ":1" 'Declare the first sheet name as the name of the models part number:1
    For Each oSheet In oSheets 'Iterate through all sheets in the active drawing
           oSheet.activate 'Activate each sheet
        If oSheet.DrawingViews.Count = 0 Then
            oSheetName1 = ActiveSheet.Sheet.Name
            MessageBox.Show(oSheetName1 & "  does not contain any views and will not be renamed!", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Continue For 'If no drawing views exists then continue on to the next sheet
            Else
        End If
        Dim oDrawingView As DrawingView = oSheet.DrawingViews(1) 'Retrieve the active sheet base view
           oModelDoc = ActiveSheet.View(oDrawingView.Name).ModelDocument 'Identify the model in the active sheet base view
           oPartNum = iProperties.Value(oModelDoc.DisplayName, "Project", "Part Number") 'Retrive the part number from the models iProperties
                If oPartNum = "" Then ' Check if the base view models part number has a value
                oSheetName2 = ActiveSheet.Sheet.Name
                MessageBox.Show(oSheetName2 & "  base view models part number has no value and will not be renamed!", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Else 'Resume on to the next sheet
                    If ActiveSheet.Sheet.Name = oPartNum Then 'For each sheet check if the sheet name is already set to the active part number:1
                    Exit Sub 'If true, do nothing
                    Else ActiveSheet.Sheet.Name = oPartNum 'If false, in each sheets base view, rename the sheet name to the models part number
                    End If
                End If
    Next
ActiveSheet = ThisDrawing.Sheet(oFirstSheetName) 'Set the active sheet back to the original active sheet
    End If
    Catch
End Try
ErrorHandler:
Exit Sub

 


Product Design & Manufacturing Collection 2024
Sometimes you just need a good old reboot.
0 Likes
Message 25 of 28

Owner2229
Advisor
Advisor

Now look again at my code. It does exactly the same, but faster because it doesn't activate each sheet (thus Inventor doesn't need to generate the graphics for it). I'm not trying to be harsh on you, I just want you to learn, young padawan.

The workflow of your code (red are the unnecessary parts):

1) Get all sheets

2) Get active document

3) Check document type

4) Set temp active sheet (oSheetTest)

5) Check for views in (4)

6) Get model document from active view

7) Get part number from (6)

8) Loop throught all sheets

9) Activate each sheet

10) Check for views in sheet

11) Get first view

12) Get model document from active sheet

13) Get part number

14) Check the active sheet name

15) Set the active sheet name

- end of loop -

16) Re-Activate the first sheet

 

And mine:

1) Get active document

2) Check document type

3) Loop throught all sheets

4) Check for views in sheet

5) Get first view

6) Get model document

7) Get part number

8) Check the part number

9) Check if the sheet name = part number

10) Set the sheet name

- end of loop -

 

On row 32 you're calling "Exit Sub" instead of "Continue For"

If you realy want to have there the message boxes and spam yourself with messages, here you go:

 

Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
For Each oSheet As Sheet In oDoc.Sheets
    If oSheet.DrawingViews.Count = 0 Then
MsgBox("The sheet (" & oSheet.Name & ") does not contain any views!")
Continue For
End If
Dim oView As DrawingView = oSheet.DrawingViews(1) Dim oModelDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument Dim oName As String = iProperties.Value(oModelDoc.DisplayName, "Project", "Part Number")
If oName = vbNullString Then
MsgBox("The sheet's (" & oSheet.Name & ") model's part number is empty!")
Continue For End If
If oSheet.Name = oName Then Continue For oSheet.Name = oName Next

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 26 of 28

Ray_Feiler
Advisor
Advisor

Wow, that is awesome. Yeah I'm defiantly no programmer or even a hacker, just a copy and paste and see if it works guy. And yes, I know it is sad I have to spam myself with messages like that. Smiley Wink


Product Design & Manufacturing Collection 2024
Sometimes you just need a good old reboot.
0 Likes
Message 27 of 28

Owner2229
Advisor
Advisor

You've just described every beginning programmer, hehe.

All it takes is practise.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 28 of 28

Ray_Feiler
Advisor
Advisor

Updated this iLogic code to support model states.

 

trigger = iTrigger0
Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
For Each oSheet As Sheet In oDoc.Sheets
    If oSheet.DrawingViews.Count = 0 Then
        MessageBox.Show(oSheet.Name & "  does not contain any views and will not be renamed!", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
		Continue For
    End If
    Dim oView As DrawingView = oSheet.DrawingViews(1)
    Dim oModelDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oFullModelName As String = oModelDoc.DisplayName
	Dim oModelStateName As String = oModelDoc.ModelStateName
	Dim oName As String
	If oModelStateName = "[Primary]" Then
		Dim oModelName = Mid(oFullModelName, Len(oFullModelName) -Len(oFullModelName) + 1, Len(oFullModelName) - 4)
		oName = oModelName
		Else
		Dim oModelName = Mid(oFullModelName, Len(oFullModelName) -Len(oFullModelName) + 1, Len(oFullModelName) -Len(oModelStateName) -7)
		Dim oModelNameModelState As String = oModelName & "-" & oModelStateName
		oName = oModelNameModelState
	End If
	If oName = vbNullString Then
		MessageBox.Show(oSheet.Name & "  base view models part number has no value and will not be renamed!", "ERROR!" , MessageBoxButtons.OK, MessageBoxIcon.Error)
        Continue For
    End If
    If oSheet.Name = oName Then Continue For
    oSheet.Name = oName
Next

 


Product Design & Manufacturing Collection 2024
Sometimes you just need a good old reboot.
0 Likes