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,956 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,957 Views
28 Replies
Replies (28)
Message 21 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Welbilt_Designer_3,

 

Sorry for late reply, engaged with some other work.

 

As iLogic rule is running AssemblyDocument, PartDocuments unable to access it. So, PartDocuments need to open, save and close it.

 

Try the following modified iLogic rule.

 

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)

    If filterType = "Autodesk Inventor Part Files (*.ipt)|*.ipt" Then
		oDoc = ThisApplication.Documents.Open(ThisDoc.WorkspacePath() & "\" & docType & ".ipt", True)
	Else
		'define the active document
    	oDoc = ThisDoc.Document
	End If
    
    '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()
    
    'set the file name string to use in the input box    
    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, False) 'True = Save As Copy & False = Save As
		If filterType = "Autodesk Inventor Part Files (*.ipt)|*.ipt" Then
			oDoc.Close()
		End If
    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 22 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

No problem, I just got back to this project yesterday myself.

The tweaked code works great within its own folder but produces an error when placed in an assembly, great progress, Thanks for all of your help.

 

I was unable to test weather the renamed frame and plug would be able to unique extension numbers if multiple sub assemblies are placed within a larger assembly. I am concerned that if each sub assembly only updates the extension number to +1 from the originals, leaving multiple doors with components with the same name, I may not be able to make changes to one without the other changing. Is there a way to make the frame and plug use the extension number that the assembly is using?

 

 

 

 

Capture.PNGCapture2.PNG

0 Likes
Message 23 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Welbilt_Designer_3,

 

Yes, part files will works only in the current folder. We can modify the code to other folder.

 

Meanwhile, Can you please explain the steps with example for larger assembly?

 

Thanks and regards,,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 24 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

First a little background. We build custom walk-in coolers and freezers for food service companies. We have design software that creates basic assemblies based on customer specs but invariably need tweaking since these are far too complex for full automation.

 

While in an assembly that already has one or more doors in it, if an additional door is needed, we use Inventors “place” command to bring in a new door or other component from the prototype folder. I already have the prototype panels set to trigger the auto rename function (thanks to your help) when placed using a form set to activate when placed. All our users have to do is click on the rename button and it finds the panel with the same name and the largest extension number and ups it by one. This operation is not run in the background so users are aware of the rename process and are provided the opportunity to alter the new name if desired.

 

 

Rename.pngRename 2.png

 

This all works fine for individual components but sub-assemblies like doors are a little different. Since the components within the assemblies also need unique names to prevent the others from being changed when one is altered.

 

Our doors can not only be different sizes but can also be moved within the frame to achieve the centerline specified by the customer and have different accessories such as viewport cutouts. So, the sub assembly needs a unique extension number and the components within it also need extension numbers that are different from the existing doors. If there are two existing doors with frame / plug extensions of -1 and -2 then a new frame / plug must have an extension of -3 or greater to separate them.

 

The door assembly is currently updating its extension number correctly when placed into a larger assembly. So, if we set the frame and plug to use the extension number used in the sub assembly it would insure that we don’t have multiple door components with the same extension numbers.

 

It is worth noting that our bill of materials generator relies on specific name formats to operate so the only part of the assembly and its components that can change is the extension numbers.

0 Likes
Message 25 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Welbilt_Designer_3,

 

Hope the following iLogic code works fine.

 

Imports System.IO

Sub Main()

    Rename("DW-" & 1 & ".iam","Autodesk Inventor Part Files (*.iam)|*.iam", "DW", "")
	oDoc = ThisDoc.Document
	For Each referDoc in oDoc.AllReferencedDocuments
		If referDoc.DisPlayName = "Frame.ipt" Then
		 	Rename("Frame-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Frame", referDoc.FullDocumentName)
		Else If referDoc.DicplayName = "Plug.ipt" Then
			Rename("Plug-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Plug", referDoc.FullDocumentName)   
		End If
	Next
End Sub

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

    If filterType = "Autodesk Inventor Part Files (*.ipt)|*.ipt" Then
		oDoc = ThisApplication.Documents.Open(filePath, True)
	Else
		'define the active document
    	oDoc = ThisDoc.Document
	End If
    
    '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()
    
    'set the file name string to use in the input box    
    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, False) 'True = Save As Copy & False = Save As
		If filterType = "Autodesk Inventor Part Files (*.ipt)|*.ipt" Then
			oDoc.Close()
		End If
    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 26 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

I want to repeat my appreciation of your help with this.

There seems to be something wrong, I am getting an error when it gets to the renaming of the frame.Capture.PNG 

0 Likes
Message 27 of 29

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Welbilt_Designer_3,

 

Try the following changed iLogic code.

 

Imports System.IO

Sub Main()

    Rename("DW-" & 1 & ".iam","Autodesk Inventor Part Files (*.iam)|*.iam", "DW", "")
	oDoc = ThisDoc.Document
	For Each referDoc in oDoc.AllReferencedDocuments
		If referDoc.DisPlayName = "Frame.ipt" Then
		 	Rename("Frame-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Frame", referDoc.FullDocumentName)
		Else If referDoc.DicplayName = "Plug.ipt" Then
			Rename("Plug-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Plug", referDoc.FullDocumentName)   
		End If
	Next
End Sub

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

    If filterType = "Autodesk Inventor Part Files (*.ipt)|*.ipt" Then
		oDoc = ThisApplication.Documents.Open(filePath, True)
	Else
		'define the active document
    	oDoc = ThisDoc.Document
        'set the assembly part name to = filename 
        oDoc.DisplayName = ""

	End If
    
    'create a file dialog box
    Dim oFileDlg As inventor.FileDialog = Nothing
    InventorVb.Application.CreateFileDialog(oFileDlg)
    
    
    
    'set Part type
    oFileDlg.Filter = filterType
      
    'set the directory to open the dialog at
    oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
    
    'set the file name string to use in the input box    
    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, False) 'True = Save As Copy & False = Save As
		If filterType = "Autodesk Inventor Part Files (*.ipt)|*.ipt" Then
			oDoc.Close()
		End If
    End If

End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 28 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast

This code has the exact same error as the last version.

0 Likes
Message 29 of 29

Welbilt_Designer_3
Enthusiast
Enthusiast
Accepted solution

Just a follow-up for anyone who is interested in setting up a rename function for simple sub assemblies. Below is an example of the code I am using to rename a simple door sub assembly. The previous versions listed all had some issues but I finally got back on this project it is now working. 

 

By simply changing the names of the assembly and its components to match your sub assembly this can easily be adapted or added to. 

 

 

 Code Snippet

 

Imports System.IO

Sub Main()

    Rename("DW-" & 1 & ".iam","Autodesk Inventor Part Files (*.iam)|*.iam", "DW", "")
    oDoc = ThisDoc.Document
    For Each referDoc in oDoc.AllReferencedDocuments
    DisplayName = ""
        If referDoc.DisPlayName = "Frame.ipt" Then
             Rename("Frame-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Frame", referDoc.FullDocumentName)
        Else If referDoc.DisPlayName = "Plug.ipt" Then
            Rename("Plug-" & 1 & ".ipt", "Autodesk Inventor Part Files (*.ipt)|*.ipt", "Plug", referDoc.FullDocumentName)   
                End If
    Next
End Sub

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

    If filterType = "Autodesk Inventor Part Files (*.ipt)|*.ipt" Then
        oDoc = ThisApplication.Documents.Open(filePath, True)
    Else
        'define the active document
        oDoc = ThisDoc.Document
        'set the assembly part name to = filename 
        oDoc.DisplayName = ""

    End If
    
    'create a file dialog box
    Dim oFileDlg As inventor.FileDialog = Nothing
    InventorVb.Application.CreateFileDialog(oFileDlg)
    
    
    
    'set Part type
    oFileDlg.Filter = filterType
      
    'set the directory to open the dialog at
    oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
    
    'set the file name string to use in the input box    
    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, False) 'True = Save As Copy & False = Save As
        If filterType = "Autodesk Inventor Part Files (*.ipt)|*.ipt" Then
            oDoc.Close()
        End If
    End If

End Sub

 

0 Likes