Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy selected model and its drawing if it has

3 REPLIES 3
Reply
Message 1 of 4
darrell.wcd
242 Views, 3 Replies

Copy selected model and its drawing if it has

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.

3 REPLIES 3
Message 2 of 4
dalton98
in reply to: darrell.wcd

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")

 

Message 3 of 4
yuzeaa
in reply to: dalton98

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()

 

Message 4 of 4
darrell.wcd
in reply to: darrell.wcd

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.

Post to forums  

Technology Administrators


Autodesk Design & Make Report