Open an iam file and select an occurrence to copy and replace.
It gives you three path choices:
1.Parent Path(whithout selection)
2.Occurrence Path(select an occurrence first)
3.Last Path(click 'cancel button')
txtfile = "C:\Users\Public\Documents\temp.txt"
If System.IO.File.Exists(txtfile) Then
oRead = System.IO.File.ReadAllText(txtfile)
LastPath = IO.Path.GetDirectoryName(oRead) & "\"
End If
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveEditDocument
Dim oOcc As ComponentOccurrence
Select Case oDoc.SelectSet.Count
Case 0
oOcc = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select part")
If oOcc Is Nothing Then Return
FileName = oOcc.Definition.Document.Fulldocumentname
NewPathName = IO.Path.GetDirectoryName(oDoc.FullDocumentName) & "\" & IO.Path.GetFileNameWithoutExtension(FileName) & "copy"
Case 1
oOcc = oDoc.SelectSet.Item(1)
FileName = oOcc.Definition.Document.Fulldocumentname
NewPathName = IO.Path.GetDirectoryName(FileName) & "\" & IO.Path.GetFileNameWithoutExtension(FileName) & "copy"
Case Else
oDoc.SelectSet.Clear
Return
End Select
idwName = Left(FileName,Len(FileName)-4) & ".idw"
extName = IO.Path.GetExtension(FileName)
Dim oFileDlg As Inventor.FileDialog = Nothing
ThisApplication.CreateFileDialog(oFileDlg)
If LCase(extName) = "iam" Then
oFileDlg.Filter = "Inventor Files (*.iam)|*.iam|All Files (*.*)|*.*"
ElseIf LCase(extName) = "ipt" Then
oFileDlg.Filter = "Inventor Files (*.ipt)|*.ipt|All Files (*.*)|*.*"
End If
oFileDlg.DialogTitle = "Replace: " & FileName
'oFileDlg.InitialDirectory = ThisDoc.Path 'do not need InitialDirectory
oFileDlg.FileName = NewPathName
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowSave()
If Err.Number <> 0 Then
oFileDlg.FileName = LastPath & IO.Path.GetFileNameWithoutExtension(FileName) & "copy"
oFileDlg.ShowSave()
selectedfile = oFileDlg.FileName
ElseIf oFileDlg.FileName <> "" Then
selectedfile = oFileDlg.FileName
End If
If selectedfile Is Nothing Then Return
If Right(selectedfile,4) <> extName Then
selectedfile = selectedfile & LCase(extName)
End If
newIdwName = Left(selectedfile, Len(selectedfile) -4) & ".idw"
oOcc.Definition.Document.SaveAs(selectedfile, True)
Call oOcc.Replace(selectedfile, False)
If System.IO.File.Exists(idwName) Then
System.IO.File.Copy(idwName, newIdwName)
Dim newIdwFile As DrawingDocument= ThisApplication.Documents.Open(newIdwName, True)
newIdwFile.Sheets(1).DrawingViews(1).referencedFile.DocumentDescriptor.ReferencedFileDescriptor.replacereference(selectedfile)
newIdwFile.Update
newIdwFile.Save
newIdwFile.Close
End If
iWrite = System.IO.File.CreateText(txtfile)
iWrite.Write(selectedfile)
iWrite.Close()