Renaming Drawings Sheets

Renaming Drawings Sheets

nstone
Contributor Contributor
2,486 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,487 Views
27 Replies
Replies (27)
Message 2 of 28

Anonymous
Not applicable

Not sure if this is what you are looking for. If not it hopefully it may point you in the right direction. This rule will rename all the sheets to the part number of the first view in the sheet.  I didn't put any error checking but you can easily add that. Copy and paste this into a rule. Hope this helps.

 

 

Dim oSheets As Sheets = ThisDrawing.Document.sheets
Dim oSheet As Sheet

For Each oSheet In oSheets
    oSheet.activate
    Dim oDrawingView As DrawingView = oSheet.DrawingViews(1)
    oModel = ActiveSheet.View(oDrawingView.Name).ModelDocument
    partnum = iProperties.Value(oModel.DisplayName, "Project", "Part Number")
    ActiveSheet.Sheet.Name = partnum
Next

0 Likes
Message 3 of 28

nstone
Contributor
Contributor

Yes, This is exactly what I want to do, I looks like I was on the right track but I keep getting this error.

 

"Object reference not set to an instance of an object."

0 Likes
Message 4 of 28

Anonymous
Not applicable

That means that something is not properly set. hummm this codes works on my end. Try the following code to see which messagebox does not show. This way you know where the error occurs. Somethings to check of are 1.make sure all sheets have a view. 2. All files have a part number. 

 

Dim oSheets As Sheets = ThisDrawing.Document.sheets
Dim oSheet As Sheet

For Each oSheet In oSheets
    oSheet.activate
    Dim oDrawingView As DrawingView = oSheet.DrawingViews(1)
    MessageBox.Show(oDrawingView.Name)
    oModel = ActiveSheet.View(oDrawingView.Name).ModelDocument
    MessageBox.Show(oModel.DisplayName)
    partnum = iProperties.Value(oModel.DisplayName, "Project", "Part Number")
    MessageBox.Show(partnum)
    ActiveSheet.Sheet.Name = partnum
Next

0 Likes
Message 5 of 28

nstone
Contributor
Contributor

The Third Message Box is not showing up

 

0 Likes
Message 6 of 28

Anonymous
Not applicable

I know what it is. I've encountered it before. In Windows, show your file extensions. This should correct the issue.

Message 7 of 28

nstone
Contributor
Contributor

Thanks Alot  for you help that worked,

 

One issue Iam having is if the Base View is created in a Different Level Of Detail.

 

 

0 Likes
Message 8 of 28

Anonymous
Not applicable

This bit of code is of great help. Just one little thing ...... I have been bringing in the seperate parts of a frame and as soon as you code gets to those sheets it stops. Can you still help?

0 Likes
Message 9 of 28

Anonymous
Not applicable
This bit of code is of great help. Just one little thing ...... I have been bringing in the seperate parts of a frame and as soon as you code gets to those sheets it stops. Can you still help?

What I tried was this but I am now getting the file name with it ext.


Dim oSheets As Sheets = ThisDrawing.Document.sheets
Dim oSheet As Sheet

For Each oSheet In oSheets
    oSheet.activate
    Dim oDrawingView As DrawingView = oSheet.DrawingViews(1)
    'MessageBox.Show(oDrawingView.Name)
    oModel = ActiveSheet.View(oDrawingView.Name).ModelDocument
    'MessageBox.Show(oModel.DisplayName)
    partnum = IO.Path.GetFileName(ActiveSheet.View(oDrawingView.Name).ModelDocument.fullFileName)

    'partnum = iProperties.Value(oModel.DisplayName, "Project", "Part Number")
    'MessageBox.Show(partnum)
    ActiveSheet.Sheet.Name = partnum
Next

 

 
0 Likes
Message 10 of 28

Anonymous
Not applicable

Sorry in the US, it was a holiday yesterday so I am just getting to your email today. The original code should work but here's an updated code. See if this code corrects your original issue. This will code will look at the model that is associated to VIEW1. So a view labeled "VIEW1" must exist. If you are still having issues, upload a sample assembly or email me personally and I can take a look at the code with your assembly.

 

Dim oSheets As Sheets = ThisDrawing.Document.sheets
Dim oSheet As Sheet

For Each oSheet In oSheets
    oSheet.activate
    oModel = IO.Path.GetFileName(ActiveSheet.View("VIEW1").ModelDocument.FullFileName)
    'MessageBox.Show(oModel)
    partnum = iProperties.Value(oModel, "Project", "Part Number")
    'MessageBox.Show(partnum)
    ActiveSheet.Sheet.Name = partnum
Next

 

Dan

Message 11 of 28

Anonymous
Not applicable

Thanks for your fast reply. No  ... Still not.

 

The modification I added still works and is no a big problem but it would be good to get rid of the ext.

0 Likes
Message 12 of 28

Anonymous
Not applicable

Just curious. From my code, if you uncomment the "MessageBox.Show(partnum)" and run the rule. What will show in the messagebox? The code is written to get the 3D model associated to View1 and then get the part number iproperty for that 3D model. It will set the sheet name to the the part number which has no file extension. So if that's not working, I am wondering of the part number is blank in the 3D model. Not sure until I see a dataset.

 

For your code, your are getting the file name associated to the sheet. This works but you are getting the file extension, correct? If you don't want the extension you can use the following code to remove the extension before setting it as the sheet name. This will check if it contains an extension. If it does then remove it.

 

Hope that helps but still curious why the original code does not tho. It works on the files I tested here.

 

    partnumtemp = partnum.tolower
    If partnumtemp.endswith(".ipt") = True Or partnumtemp.endswith(".iam") = True Then
        Dim extensionloc as Integer = partnum.lastindexof(".")
        partnum = partnum.Remove(extensionloc, 4)
        MessageBox.Show(partnum)
    End If

0 Likes
Message 13 of 28

Rusdesign
Explorer
Explorer

Hi Doug,

I just stumble across this thread and thought it looked like a handy piece of code. So I gave it a go and it worked well first time. Great Job!

Just one thing though - could you direct me to what I would need to change to get it to rename the sheet the the model i Property "Project", "Stock Number". I tried a few things but failed badly. Your help would be appreciated.

Thanks,

Russell

0 Likes
Message 14 of 28

Ray_Feiler
Advisor
Advisor

Try this.

 

Dim oSheets As Sheets = ThisDrawing.Document.sheets
Dim oSheet As Sheet

For Each oSheet In oSheets
    oSheet.activate
       Dim oDrawingView As DrawingView = oSheet.DrawingViews(1)
    oModel = ActiveSheet.View(oDrawingView.Name).ModelDocument
    stocknum = iProperties.Value(oModel.DisplayName, "Project", "Stock Number")
        If stocknum = "" Then
            MessageBox.Show("Stock Number hase no value.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
            Else ActiveSheet.Sheet.Name = stocknum
        End If
Next

 


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

Ray_Feiler
Advisor
Advisor

I made some changes to the original code in this post.

 

 

Dim oSheets As Sheets = ThisDrawing.Document.sheets
Dim oSheet As Sheet
Dim oActDoc As DrawingDocument
oActDoc = ThisApplication.ActiveDocument
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
        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 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
    Next
ActiveSheet = ThisDrawing.Sheet(oFirstSheetName) 'Set the active sheet back to the original active sheet

 


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

Rusdesign
Explorer
Explorer

Hi Ray,

Thanks for the reply. I have just tried your code and I keep getting an "Error in Rule" - "Object reference not set to an instance of an object".

What am I missing?

 

Thanks,

Russell

0 Likes
Message 17 of 28

Owner2229
Advisor
Advisor

Here's my bit to this topic:

 

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 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 18 of 28

Ray_Feiler
Advisor
Advisor

This is a work in progress and even though if works on my end doesn't mean it will work for all. I will be adding some error handling today and will post back when done. In the mean time take a look at what @Owner2229 posted to see if that will get you on your way to discovering were the code is failing on your end. Good luck Smiley Happy


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

Ray_Feiler
Advisor
Advisor

@Owner2229 that is a great way to check if the document running the rule is a drawing (Kudo's). Do you have any ideas on how to check if the drawing contains a view?

Thanks in advance, Ray


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

Owner2229
Advisor
Advisor

That's the first line in the FOR loop:

If oSheet.DrawingViews.Count = 0 Then Continue For
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