I've searched a bunch for something close to what I want to do.... and cant seem to find something workable.
What I want is similar to copy and replace, but I want to be able to select the part or sub assembly, and copy design that and the drawing of it if it has one.
The attached ilogic found from Luke Davenport works if you have the document open that you want to copy... but it doesn't allow for file save location... it just saves it in the same folder as the open file.
if someone would have done this before or be able to help me figure this out would be much appreciated.
You would need to change the 'filePath' value in your code to change the save location
NewFileName = filePath &"\"& FileName
Copying a drawing might be more difficult. Are your drawings all in the same folder? Do they share the same name as the part? If so then you could do this:
System.IO.File.Copy(drawingFolder & iFileName & ".idw", NewFileName & ".idw")
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()
I have this iLogic that works.. kind of... I did notice that sometime when I'm copying and making different lengths that it actually replaced the model in the wrong drawing... not sure how. Also it doesn't have much for error handling, and lets the user copy over existing files..... and if there is not a drawing... can a user be prompted to either just copy the model, or if there is a drawing then just copy the model and drawing without prompting?
Also would like to have the option that if nothing is selected then the active model gets copied, but if a model, either a sub assembly or part... then it would copy that file and its drawing.
And the dialogue directory... need it to take the user to the same location as the file getting copied, by default, but also let the user navigate to a different folder if needed.
Any suggestions and or revisions, cleanup, making this work as stated above and work more efficient would be appreciated.
Can't find what you're looking for? Ask the community or share your knowledge.