Triggering a part rule to run fron an assembly rule.

Triggering a part rule to run fron an assembly rule.

Welbilt_Designer_3
Enthusiast Enthusiast
1,934 Views
28 Replies
Message 1 of 29

Triggering a part rule to run fron an assembly rule.

Welbilt_Designer_3
Enthusiast
Enthusiast

I have an adapted rule from a previous discussion, Re: File rename popup that renames parts when placed into assemblies. I have adapted it to rename a sub assembly when placed into the main assembly. What I need is to either trigger the "Rename" rule to run in the sub assembly's parts when it runs in the assembly or, if possible, have the assembly rule also rename the parts.  

 

To avoid confusion I should mention that the assembly is a door with two parts, a frame and a door. There are also many accessories but they do not require renaming as they are static add-ons.

 

Here is the code being used on the assembly

 

 Code Snippet

Imports System.IO

'define the active document
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

'set the assembly part name to = filename 
oDoc.DisplayName = ""

'set Part type
oFileDlg.Filter = "Autodesk Inventor Part Files (*.iam)|*.iam"

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

Dim fileName As String = "DW-" & 1 & ".iam"
Dim fInfo As FileInfo()
Dim dirInfo As New DirectoryInfo(oFileDlg.InitialDirectory)
fInfo = dirInfo.GetFiles()

Dim i As Integer = 1
Dim file As FileInfo
Reiterate:For Each file In fInfo
    If file.Name = fileName Then
            i = i + 1
            fileName = "DW-" & i & ".iam"
            Goto Reiterate
        End If
    Next

'set the file name string to use in the input box
oFileDlg.FileName = fileName                  

'work with an error created by the user backing out of the save 
oFileDlg.CancelError = True
On Error Resume Next

'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()


'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName

'save the file 
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
End If

 

And the code for one of the parts within the assembly.

 

 Code Snippet

Imports System.IO

'define the active document
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

'set the assembly part name to = filename 
oDoc.DisplayName = ""

'set Part type
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

Dim fileName As String = "Frame-" & 1 & ".ipt"
Dim fInfo As FileInfo()
Dim dirInfo As New DirectoryInfo(oFileDlg.InitialDirectory)
fInfo = dirInfo.GetFiles()

Dim i As Integer = 1
Dim file As FileInfo
Reiterate:For Each file In fInfo
    If file.Name = fileName Then
            i = i + 1
            fileName = "Frame-" & i & ".ipt"
            Goto Reiterate
        End If
    Next

'set the file name string to use in the input box
oFileDlg.FileName = fileName                  

'work with an error created by the user backing out of the save 
oFileDlg.CancelError = True
On Error Resume Next

'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()


'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName

'save the file 
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
End If

 

As you can see they are nearly identical, My attempts at triggering the ipt rule from the iam have been unsuccessful and since there may be other doors with frame names containing the same extension number this may not be the best way to go anyway, unless I can transfer the extension number from the assembly to the part.

0 Likes
Accepted solutions (2)
1,935 Views
28 Replies
Replies (28)
Message 2 of 29

JaneFan
Autodesk
Autodesk

Hello @Welbilt_Designer_3,

 

The error for running the part rule in an assembly file is because of the line calling SaveAs().

From the code we can see that oDoc is an assembly document, while the file extension of MyFile is .ipt.

I modified your code snippet to make it work for both sub assembly and sub part, please try it:

 

Imports System.IO

'define the active document
Dim oDoc As AssemblyDocument  
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

'set the assembly part name to = filename
oDoc.DisplayName = ""

'set Part type
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

Dim fileName As String = "Frame-" & 1 & ".ipt"
Dim fInfo As FileInfo()
Dim dirInfo As New DirectoryInfo(oFileDlg.InitialDirectory)
fInfo = dirInfo.GetFiles()

Dim i As Integer = 1
Dim file As FileInfo
Dim oSubDoc As Document
oSubDoc = oDoc.ComponentDefinition.Occurrences(1).Definition.Document

Reiterate:For Each file In fInfo
    If file.Name = fileName Then
            i = i + 1
            If oSubDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
                fileName = "Frame-" & i & ".ipt"
            Else If oSubDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject  Then
                fileName = "Frame-" & i & ".iam"
            End If
            Goto Reiterate
        End If
    Next

'set the file name string to use in the input box
oFileDlg.FileName = fileName                  

'work with an error created by the user backing out of the save
oFileDlg.CancelError = True
On Error Resume Next

'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()

'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName
End If

'save the file
oSubDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 3 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Welbilt_Designer_3,

 

Try the following iLogic rule. Which renames both subassembly and part at one time.

 

Sub Main()

	Rename("DW-" & 1 & ".iam","Autodesk Inventor Part Files (*.iam)|*.iam", "Assembly")
	Rename("Frame-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Part")

End Sub

Sub Rename(ByVal docName As String, ByVal filterType As String, ByVal docType As String)

	'define the active document
	oDoc = ThisDoc.Document
	
	'create a file dialog box
	Dim oFileDlg As inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	
	'set the assembly part name to = filename 
	oDoc.DisplayName = ""
	
	'set Part type
	oFileDlg.Filter = filterType
	
	'set the directory to open the dialog at
	oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
	
	Dim fileName As String = docName
	Dim fInfo As FileInfo()
	Dim dirInfo As New DirectoryInfo(oFileDlg.InitialDirectory)
	fInfo = dirInfo.GetFiles()
	
	Dim i As Integer = 1
	Dim file As FileInfo
	Reiterate:For Each file In fInfo
		If file.Name = fileName Then
				i = i + 1
				If docType = "Assembly" Then
					fileName = "DW-" & i & ".iam"
				Else If docType = "Part" Then
					fileName = "Frame-" & i & ".ipt"
				End If
				
				Goto Reiterate
			End If
		Next
	
	'set the file name string to use in the input box
	oFileDlg.FileName = fileName                  
	
	'work with an error created by the user backing out of the save 
	oFileDlg.CancelError = True
	On Error Resume Next
	
	'specify the file dialog as a save dialog (rather than a open dialog)
	oFileDlg.ShowSave()
	
	
	'catch an empty string in the imput
	If Err.Number <> 0 Then
	MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
	ElseIf oFileDlg.FileName <> "" Then
	MyFile = oFileDlg.FileName
	
	'save the file 
	oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
	End If

End Sub

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 4 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

This looks good but it is giving me a few errors that I am having a hard time understanding because it didn't give errors with the original code that hasn't changed. Any ideas?

Capture.PNG

0 Likes
Message 5 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

Hello JaneFan,

Thanks for the helpful advice

 

 The modification you made makes it easier to apply the code to subassemblies and parts with less tweaking but it still does not trigger the renaming of the parts within the subassembly when it is placed, it still only renames the assembly.

0 Likes
Message 6 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @Welbilt_Designer_3,

 

Oops.. I forget to add reference Imports System.IO. Try the following corrected iLogic rule.

 

Imports System.IO

Sub Main() Rename("DW-" & 1 & ".iam","Autodesk Inventor Part Files (*.iam)|*.iam", "Assembly") Rename("Frame-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Part") End Sub Sub Rename(ByVal docName As String, ByVal filterType As String, ByVal docType As String) 'define the active document oDoc = ThisDoc.Document 'create a file dialog box Dim oFileDlg As inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(oFileDlg) 'set the assembly part name to = filename oDoc.DisplayName = "" 'set Part type oFileDlg.Filter = filterType 'set the directory to open the dialog at oFileDlg.InitialDirectory = ThisDoc.WorkspacePath() Dim fileName As String = docName Dim fInfo As FileInfo() Dim dirInfo As New DirectoryInfo(oFileDlg.InitialDirectory) fInfo = dirInfo.GetFiles() Dim i As Integer = 1 Dim file As FileInfo Reiterate:For Each file In fInfo If file.Name = fileName Then i = i + 1 If docType = "Assembly" Then fileName = "DW-" & i & ".iam" Else If docType = "Part" Then fileName = "Frame-" & i & ".ipt" End If Goto Reiterate End If Next 'set the file name string to use in the input box oFileDlg.FileName = fileName 'work with an error created by the user backing out of the save oFileDlg.CancelError = True On Error Resume Next 'specify the file dialog as a save dialog (rather than a open dialog) oFileDlg.ShowSave() 'catch an empty string in the imput If Err.Number <> 0 Then MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled") ElseIf oFileDlg.FileName <> "" Then MyFile = oFileDlg.FileName 'save the file oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As End If End Sub

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 7 of 29

JaneFan
Autodesk
Autodesk

Seems the reference to System.IO is missing here:

Add this line at the top of the code:

Imports System.IO




Jane Fan
Inventor/Fusion QA Engineer
Message 8 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @JaneFan,

 

You are right. It is updated at previous post 🙂

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 9 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

Still not working, I get an error stating "the path is nor of a legal form", I am not familiar enough with coding error to even attempt to fix it myself. It looks like we are on the correct path but still need a little tweaking, thanks for all of the help. 

 

Capture.PNG

0 Likes
Message 10 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

I am going to blame this one on Monday morning, I didn't open the door from the project the first time. The code worked fine the way you have it once I applied it properly. Please disregard mu previous post and thank you for the help.

0 Likes
Message 11 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

I spoke too soon, the code runs through all of the rename process for the assembly, frame and door but does not add the renamed frame and door to the assembly. I did alter it some to accommodate the other door component but it wasn't working before I did that. I could send you the assembly if it would help but I don't feel comfortable posting it here.

 

 

 Code Snippet

Imports System.IO

Sub Main()

    Rename("DW-" & 1 & ".iam","Autodesk Inventor Part Files (*.iam)|*.iam", "DW")
    Rename("Frame-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Frame")
    Rename("Plug-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Plug")    
End Sub

Sub Rename(ByVal docName As String, ByVal filterType As String, ByVal docType As String)

    'define the active document
    oDoc = ThisDoc.Document
    
    'create a file dialog box
    Dim oFileDlg As inventor.FileDialog = Nothing
    InventorVb.Application.CreateFileDialog(oFileDlg)
    
    'set the assembly part name to = filename 
    oDoc.DisplayName = ""
    
    'set Part type
    oFileDlg.Filter = filterType
    
    'set the directory to open the dialog at
    oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
    
    Dim fileName As String = docName
    Dim fInfo As FileInfo()
    Dim dirInfo As New DirectoryInfo(oFileDlg.InitialDirectory)
    fInfo = dirInfo.GetFiles()
    
    Dim i As Integer = 1
    Dim file As FileInfo
    Reiterate:For Each file In fInfo
        If file.Name = fileName Then
                i = i + 1
                If docType = "DW" Then
                    fileName = "DW-" & i & ".iam"
                Else If docType = "Frame" Then
                    fileName = "Frame-" & i & ".ipt"
                    Else If docType = "Plug" Then
                    fileName = "Plug-" & i & ".ipt"
                End If
                
                Goto Reiterate
            End If
        Next
    
    'set the file name string to use in the input box
    oFileDlg.FileName = fileName                  
    
    'work with an error created by the user backing out of the save 
    oFileDlg.CancelError = True
    On Error Resume Next
    
    'specify the file dialog as a save dialog (rather than a open dialog)
    oFileDlg.ShowSave()
    
    
    'catch an empty string in the imput
    If Err.Number <> 0 Then
    MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
    ElseIf oFileDlg.FileName <> "" Then
    MyFile = oFileDlg.FileName
    
    'save the file 
    oDoc.SaveAs(MyFile, True) 'True = Save As Copy & False = Save As
    End If

End Sub

 

0 Likes
Message 12 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Welbilt_Designer_3,

 

Code is absolutely fine. There might be problem with ThisDoc.WorkspacePath().

 

frame and door parts are not available at this location.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 13 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

I don't know what the issue is, but when the code runs it should rename the assembly then the frame and plug. The frame rename is successful but the frame and plug do not get renamed, the save as procedure runs for all components but when it is finished there are no new frame or plug in the assembly. The originals are in the directory so I am at a loss.

 

When the code is activated in the assembly the results are:

1. Runs the rename procedure for the assembly resulting in a new assembly added to the project folder with a -1 extension.

2. Runs the rename procedure for the frame which does not result in a new frame being added to the project folder.

3. Runs the rename procedure for the plug which does not result in a new plug being added to the project folder.

 

The screenshot was taken while the frame was being renamed, as you can see the original frame and plug are both available in the project folder. 

 

Capture.PNG

 

I can't help but think that there is some setting that is blocking the components from being renamed from the assembly level. Is there a way to get around this?

 

0 Likes
Message 14 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Welbilt_Designer_3,

 

Try the following changes in code.

 

Imports System.IO

Sub Main()

    Rename("DW-" & 1 & ".iam","Autodesk Inventor Part Files (*.iam)|*.iam", "DW")
    Rename("Frame.ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Frame")
    Rename("Plug.ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Plug")    
End Sub

Sub Rename(ByVal docName As String, ByVal filterType As String, ByVal docType As String)

    'define the active document
    oDoc = ThisDoc.Document
    
    'create a file dialog box
    Dim oFileDlg As inventor.FileDialog = Nothing
    InventorVb.Application.CreateFileDialog(oFileDlg)
    
    'set the assembly part name to = filename 
    oDoc.DisplayName = ""
    
    'set Part type
    oFileDlg.Filter = filterType
    
    'set the directory to open the dialog at
    oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
    
    Dim fileName As String = docName
    Dim fInfo As FileInfo()
    Dim dirInfo As New DirectoryInfo(oFileDlg.InitialDirectory)
    fInfo = dirInfo.GetFiles()
    
    Dim i As Integer = 1
    Dim file As FileInfo
    Reiterate:For Each file In fInfo
        If file.Name = fileName Then
                i = i + 1
                If docType = "DW" Then
                    fileName = "DW-" & i & ".iam"
                Else If docType = "Frame" Then
                    fileName = "Frame-" & i & ".ipt"
                    Else If docType = "Plug" Then
                    fileName = "Plug-" & i & ".ipt"
                End If
                
                Goto Reiterate
            End If
        Next
    
    'set the file name string to use in the input box
    oFileDlg.FileName = fileName                  
    
    'work with an error created by the user backing out of the save 
    oFileDlg.CancelError = True
    On Error Resume Next
    
    'specify the file dialog as a save dialog (rather than a open dialog)
    oFileDlg.ShowSave()
    
    
    'catch an empty string in the imput
    If Err.Number <> 0 Then
    MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
    ElseIf oFileDlg.FileName <> "" Then
    MyFile = oFileDlg.FileName
    
    'save the file 
    oDoc.SaveAs(MyFile, True) 'True = Save As Copy & False = Save As
    End If

End Sub

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 15 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

Thanks chandra.shekar.g 

 

I appreciate your willingness to help but that didn't work either, I have tried that approach before but it was worth giving it another go. After the assembly is renamed it just runs through the motions of renaming the frame and plug without actually doing anything.

 

I have tried to run the rename function individually in each component which works fine but if I trigger the process from the assembly using an external rule or a global form I get the same results as using this approach, a renamed assembly but no renamed parts.

 

 

0 Likes
Message 16 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Welbilt_Designer_3,

 

Is it possible to provide assembly file (non confidential one)?

 

I can investigate on assembly.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 17 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

Here is a blocky assembly I made to represent the door I am attempting to rename, it should do the trick for testing the renaming rule. I cannot send the entire file in one post so I will add the project file and assembly with this post and fallow with the door and plug.  

0 Likes
Message 18 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

And the Frame / Plug

0 Likes
Message 19 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

It seems that I cannot add the project file but I am sure you know how to get around that.

0 Likes
Message 20 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

I have returned to this project and tried a couple of different ways to get around the issues I have been having with no luck. Any ideas?

0 Likes