Help about moving assembly file and referenced part files

Help about moving assembly file and referenced part files

sohaib.as01
Advocate Advocate
1,029 Views
2 Replies
Message 1 of 3

Help about moving assembly file and referenced part files

sohaib.as01
Advocate
Advocate

Hello everyone, hope you all having a great day.

So, I have been working on a project which requires a bit of automation, my requirement is to open a template assy, do necessary changes which affects the dimensions of underlying parts and then I have to save it as new file alongwith all referenced parts and drawings.

For that purpose I was able to put together a code for my assembly file from various inventor forums and self help, which pretty much accomplishes what I want,

but

what I dont want it to do is dont do anything to the original assembly document, currently what it does is, it also replaces the part in the model tree which I am renaming and doing "Save as", and then saving the original document so each time I run this code, I have to manually replace the part with the original one in the model tree.

Any help will be enormously appreciated in this regard. Also if someone can guide me as to save all other parts of the assy as is to new folder while maintaining  reference to the new assy file which will be created then that would be a bonus.

Here is my code;

Sub Main
Dim oldPath As String = ThisDoc.Path & "\"

InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document

Dim PartN As String = PN_Acrylic 'Getting values from linked excel parameters
Dim NewAssyN As String = PN_Assy  'Getting values from linked excel parameters
Dim OrigName As String = InvDoc.DisplayName

	'Create new directory
	parentDirectoryPath = oldPath.Substring(0, oldPath.LastIndexOf("\"))
	newPath = parentDirectoryPath & "\" & "Assembly " & PN_Assy
	IO.Directory.CreateDirectory(newPath)
		
	newPath = newPath & "\"
	oldPath = oldPath & "\"	

For Each refDoc In refDocs
    'MessageBox.Show(refDoc.DisplayName)
    If refDoc.DisplayName.EndsWith("acrylic.ipt")= True Then
        'MessageBox.Show(refdoc.ToString)
        NewFileName = PartN & ".ipt"
		MessageBox.Show(NewFileName)
        refDoc.SaveAs(newPath & NewFileName, False)
		
    End If
Next

ThisDoc.Document.SaveAs(newPath & NewAssyN & ".iam", True)

iLogicVb.UpdateWhenDone = True
Call UpdateDrawing(oldPath, newPath, OrigName, NewAssyN)
End Sub
Private Sub UpdateDrawing (ByVal oldPath As String, ByVal newPath As String,ByVal oldName As String, ByVal newName As String)
    'declare variable for drawing document
    Dim oldDrawingFile As DrawingDocument
    'declare variable for the original idw.
    Dim oldDrawingFilePath As String = oldPath & oldName.Replace(".iam",".idw")
    
	Try
        'if the original idw file exist then.....
        If System.IO.File.Exists(oldDrawingFilePath) Then
            'open the original idw
            oldDrawingFile = ThisApplication.Documents.Open(oldDrawingFilePath)
            'save the original idw as the new file name. The new idw will be the same name as the iam
            oldDrawingFile.saveas(newPath & newName & ".idw", True)
            'close the drawing
            oldDrawingFile.Close
            
			'Update new drawing reference with new part
			
			newDrawingFile = ThisApplication.Documents.Open(newPath & newName & ".idw")
            oDocDescriptor = newDrawingFile.ReferencedDocumentDescriptors.Item(1)
            newDrawingFileDescriptor = oDocDescriptor.ReferencedFileDescriptor
			
			'Replace with new part file path
			newPartFilePath = newPath & newName & ".iam"
            newDrawingFileDescriptor.ReplaceReference(newPartFilePath)
			
			'Update and close new drawing file
			newDrawingFile.Update()
			newDrawingFile.Close

        End If
    Catch
    'if there's an issue, close the drawing and show error
        oldDrawingFile.Close
        MessageBox.Show("Cannot update the drawing")
    End Try

End Sub
 

 

0 Likes
1,030 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

Hello @sohaib.as01 . You maybe consider this.

Message 3 of 3

Anonymous
Not applicable

0 Likes