Wednesday, February 27, 2019

Replace the File Reference by Inventor API


Comments

nina.gogeshvili@business-consulting.de said...
Error: oDoc is not a Drawing.Document. it's a .iam Document. So
Dim oDoc As Inventor.Document is better :D
greetz 
Nina
Matt said...
It should say Inventor.AssemblyDocument I think

Sub ChangeFileNameReferencedFromAssembly()
  Dim asmDoc As AssemblyDocument
  Set asmDoc = ThisApplication.ActiveDocument
  
  Dim asmCompDef As AssemblyComponentDefinition
  Set asmCompDef = asmDoc.ComponentDefinition
  
  Dim asmOcc As ComponentOccurrence
  Set asmOcc = asmCompDef.Occurrences(1)
  
  Dim partDoc As PartDocument
  Set partDoc = asmOcc.Definition.Document
  
  Dim fileMgr As FileManager
  Set fileMgr = ThisApplication.FileManager
  
  Dim oldFileName As String
  oldFileName = partDoc.FullFileName
  
  ' Create new file with new file name
  Dim newFileName As String
  newFileName = oldFileName + "_new.ipt"
  
  Call fileMgr.CopyFile(oldFileName, newFileName)
  
  ' Change reference
  ' The file you replace the reference to needs
  ' to be the same file or one derived from the
  ' original file
  Call asmOcc.ReferencedDocumentDescriptor. _
    ReferencedFileDescriptor.ReplaceReference(newFileName)
      
  ' Delete the original file (if you want)
  Call fileMgr.DeleteFile(oldFileName)
End Su


No comments:

Post a Comment