I have searched the forums here but could not really find an answer. I have an assembly that I am working on that will be used as a configurator, driven by I-logic. Everything is work fine but there is one part off the assembly that will be custom to each new assembly. Is there a way to use I logic to open the ipt file save it as copy with different name and replace the original in the assembly. I know this can be done manual through the productivity tab within the assembly environment, but I am trying to automate the process so the end user will just have to follow prompts that will be given by i-logic.
Solved! Go to Solution.
Solved by pcrawley. Go to Solution.
OK I have this code but I do not need it to go through all the parts, is there any way to modify this so that it only looks for a part call "shell:1" with in the assembly?
Sub Main()
Dim Path As String = ThisDoc.Path & "\"
Dim FileChange As Boolean = False
InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document
For Each refDoc in refDocs
'MessageBox.show(refDoc.DisplayName)
'input box to enter new file name
NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", refDoc.DisplayName)
'NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", iProperties.Value(refDoc.DisplayName, "Project", "Part Number"))
'if the new file name is blank then use the same name
If NewFileName = "" Then
NewFileName = refDoc.DisplayName
End If
'if the user also types in the extesion then remove the extension.
If NewFileName.EndsWith(".ipt") = True Then
NewFileName = NewFileName.substring(0,NewFileName.length-4)
'MessageBox.show(NewFileName)
End If
'if there is an change in the name then do the following.
If refDoc.DisplayName <> NewFileName & ".ipt" Then
'check to see the file already exist. if it does then replace.
If System.IO.File.Exists(Path & NewFileName & ".ipt") Then
Component.Replace(refDoc.DisplayName, Path & NewFileName & ".ipt", True)
Else
Dim oldName As String = refDoc.DisplayName
'if it does not exist then recreate the file
refDoc.saveas(Path & NewFileName & ".ipt",False)
Call UpdateDrawing(Path, oldName, NewFileName)
End If
FileChange = True
End If
Next
If FileChange = True Then
NewAssemblyName = InputBox("What is the new file name for the assembly?", "New Assembly File Name", iProperties.Value("Project", "Part Number"))
If NewAssemblyName <> "" Then
Dim oldName As String = ThisDoc.FileName(True) 'include extension
ThisDoc.Document.SaveAs(Path & NewAssemblyName & ".iam" , False)
Call UpdateDrawing(Path, oldName, NewAssemblyName)
End If
End If
iLogicVb.UpdateWhenDone = True
End Sub
Private Sub UpdateDrawing (ByVal Path As String, ByVal oldName As String, ByVal newName As String)
Dim oDestinationDoc As DrawingDocument
Dim strFileExtension As String = oldName.substring(oldName.Length-4,4)
oldName = oldName.substring(0,oldName.Length-4)
Dim sFileName As String = Path & oldName & ".idw"
Try
If System.IO.File.Exists(sFileName) Then
oDestinationDoc = ThisApplication.Documents.Open(sFileName)
oDestinationDoc.saveas(Path & newName & ".idw",False)
Dim oDocDescriptor As DocumentDescriptor
oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
Dim oFileDescriptor As FileDescriptor
oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor
oFileDescriptor.ReplaceReference(Path & newName & strFileExtension)
oDestinationDoc.Update()
oDestinationDoc.Close
End If
Catch
oDestinationDoc.Close
MessageBox.Show("error")
End Try
End Sub
I'm not in front of Inventor right now, so I can't check any of this, but using my (badly) calibrated eyeball, try the following. (I note that the code references "display name" rather than filename - so if you rename anything in your browser, this is likey to fail!)
InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document
For Each refDoc in refDocs
'MessageBox.show(refDoc.DisplayName)
If refDoc.DisplayName = "shell:1" then
'This will find only "shell:1" and execute all the following code.
'If the refDoc.DisplayName is not "Shell:1" it'll jump to the red "End If" and proceed to check the next component.
'input box to enter new file name
NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", refDoc.DisplayName)
'NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", iProperties.Value(refDoc.DisplayName, "Project", "Part Number"))
'if the new file name is blank then use the same name
If NewFileName = "" Then
NewFileName = refDoc.DisplayName
End If
'if the user also types in the extesion then remove the extension.
If NewFileName.EndsWith(".ipt") = True Then
NewFileName = NewFileName.substring(0,NewFileName.length-4)
'MessageBox.show(NewFileName)
End If
'if there is a change in the name then do the following.
If refDoc.DisplayName <> NewFileName & ".ipt" Then
'check to see the file already exist. if it does then replace.
If System.IO.File.Exists(Path & NewFileName & ".ipt") Then
Component.Replace(refDoc.DisplayName, Path & NewFileName & ".ipt", True)
Else
Dim oldName As String = refDoc.DisplayName
'if it does not exist then recreate the file
refDoc.saveas(Path & NewFileName & ".ipt",False)
Call UpdateDrawing(Path, oldName, NewFileName)
End If
FileChange = True
End If
End If
Next
'Not sure if you need any of the following as it appears to be building new sub-assemblies - and you are only replacing a part.
'If you don't need it, comment it out using a ' at the beginning of each line.
' If FileChange = True Then
' NewAssemblyName = InputBox("What is the new file name for the assembly?", "New Assembly File Name",iProperties.Value("Project", "Part Number"))
' If NewAssemblyName <> "" Then
' Dim oldName As String = ThisDoc.FileName(True) 'include extension
' ThisDoc.Document.SaveAs(Path & NewAssemblyName & ".iam" , False)
' Call UpdateDrawing(Path, oldName, NewAssemblyName)
' End If
' End If
iLogicVb.UpdateWhenDone = True
End Sub 'I'm not sure about this line.
'It could exit the iLogic code at this point. I've only ever used it to end a sub-routine, but none has been defined.
'Someone who understands code better than me will tell you what it does!
'And this next part is to do with Drawings? Again, you can probably ditch this too.
' Private Sub UpdateDrawing (ByVal Path As String, ByVal oldName As String, ByVal newName As String)
' Dim oDestinationDoc As DrawingDocument
' Dim strFileExtension As String = oldName.substring(oldName.Length-4,4)
' oldName = oldName.substring(0,oldName.Length-4)
' Dim sFileName As String = Path & oldName & ".idw"
' Try
' If System.IO.File.Exists(sFileName) Then
' oDestinationDoc = ThisApplication.Documents.Open(sFileName)
' oDestinationDoc.saveas(Path & newName & ".idw",False)
' Dim oDocDescriptor As DocumentDescriptor
' oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
' Dim oFileDescriptor As FileDescriptor
' oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor
' oFileDescriptor.ReplaceReference(Path & newName & strFileExtension)
' oDestinationDoc.Update()
' oDestinationDoc.Close
' End If
' Catch
' oDestinationDoc.Close
' MessageBox.Show("error")
' End Try
' End Sub
Hi
I have the same desire like twinz390 to rename a particular ipt file (L-B.ipt) in the collection (Ban- master- template.iam) and rename the iam to a new name.
I have tried to copy the ilogic code that are writting, but I can not get the piece of program to virke.I AM NO wizard on the iLogic programming
Does anyone have a suggestion?
Here is a code I wrote to save and replace a part in an assembly. please note that it only works for parts not subassemblies as it is.
Dim oDoc As Document = ThisApplication.ActiveDocument 'Make sure we are in an assembly If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject MsgBox("This code only works on assemblies", "Error") Return End If 'Pick the part and find the document path Dim oPart As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Pick Your Item") Dim oPartDoc As Document = oPart.Definition.Document Dim oPartPathAndName As String = oPartDoc.FullDocumentName Dim oPartPath As String = System.IO.Path.GetDirectoryName(oPartPathAndName) 'pick a new name Dim NewPartName As String = InputBox("Please enter a new file name", "Name Part",System.IO.Path.GetFileName(oPartPathAndName)) 'open the document If NewPartName = "" Then Return End If Try ThisApplication.Documents.Open(oPartPathAndName) Catch MsgBox("Part path could not be resolved") Return End Try 'Save a new version of the document Try oPartDoc.SaveAs(oPartPath & "\" & NewPartName & ".ipt", True) oPartDoc.Close() Catch MsgBox("Could not save the part") Return End Try Try oPart.Replace(oPartPath & "\" & NewPartName & ".ipt",False) Catch MsgBox("Couldn't replace the part") End Try
Edit: Fixed a bug and added handling for if the cancel button is pressed.
I might be missing something, but from the original post: "I know this can be done manual through the productivity tab within the assembly environment, but I am trying to automate the process so the end user will just have to follow prompts that will be given by i-logic."