I think that is going to work but now I'm getting the attached error. How do I fix it? here is my code I used your code in line 52:
Imports System
Imports System.IO
'[ ¸.•´¯`•.¸_¸.•´¯`•.¸||Copy Files To New Directory||¸.•´¯`•.¸_¸.•´¯`•.¸
'Copies children files (parts, assemblies and drawings) to same folder (..\..\Drawings for .idw's) as the top assembly
'Notes: Turn debugMsgs to True for some simple messages that walk through some of the process.
'Version: V2.0.1 06/26/2013
']
Sub Main()
'[=====Declarations=====
Dim oDoc as Inventor.AssemblyDocument
oDoc = ThisDoc.Document
Dim debugMsgs as Boolean = True 'Set to true to display debugging messages. Currently only show messages from the drawing copy and replace portion of the rule.
Dim prereqInfo As Boolean = True'Determines if the required info has been entered
runCount = 0
' Prevents the included code from running unless project and flow numbers are present, and it hasn't been copied to a new location yet
If prereqInfo = True And Parameter("isCopied") = False Then
Dim oFileMgr As FileManager
Dim oAsmCompDef as AssemblyComponentDefinition
oAsmCompDef = oDoc.ComponentDefinition
cLocs.masterLoc = Parameter("masterLocation") & "MODELS\"
oldDwgLocation = Parameter("masterLocation") & "DRAWINGS\"
cLocs.newLoc = ThisDoc.Path
cLocs.mLocLen = Len(cLocs.masterLoc)
Call ThisApplication.FileManager.RefreshAllDocuments
'Call CreateFolders(masterLoc, newLoc)
Dim oPS as PropertySet
Try
oPS = Document.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
Catch
Call CopyFiles(oDoc)', masterLoc, newLoc) 'Call the copy function on this document. It copies all of the referenced files into the same directory as oDoc.
End Try
' Dim files As String
' oDoc.Save2
'[=====Copy Drawings Over=====
Dim oProjNum As String = Parameter("projectNum")
Dim oFlowNum as String = Parameter("flowNum")
Dim oProjAbbr As String = Parameter("projectAbbr")
Dim oProjRev As String = Parameter("projectRev")
Dim oProjYear As String = Parameter("projectYear")
Dim oProjSEQ As String = Parameter("projectSEQ")
Dim oProjDes as String = Parameter("projectDesignation")
Dim oDwgDoc as DrawingDocument
Dim oPropDoc as Document
Dim oNVM As NameValueMap
oNVM = ThisApplication.TransientObjects.CreateNameValueMap
'oNVM.Add("DeferUpdates", True)
Dim oFileDesc as FileDescriptor
'Loop through all files in the old drawing directory, check if they are .idw, copy them to the destination folder, then loop through all of the part/assembly references
'in the new drawings and determine the proper replacements
'oldDwgLocation = Parameter("dwgLocation")
newDwgLocation = System.IO.Path.GetFullPath(ThisDoc.Path & "..\..\..\..\..\Drawings\")
dwgFolder = System.IO.Directory.GetFiles(oldDwgLocation)
a = 1
' Loops through each file in the target directory and copies all of the files over to the new directory
For Each targetFile As String In dwgFolder
targetFileName = System.IO.Path.GetFileNameWithoutExtension(targetFile) 'Get file name of target file
targetFileExt = System.IO.Path.GetExtension(targetFile) 'Get file extension of target file
If debugMsgs Then MsgBox("Target Drawing File: " & targetFile)
Dim isDrawing as Boolean = False
'Copies all of the drawing files in the folder over to the new folder
If targetFileExt = ".idw" Then
'Replace the
If targetFileName.Contains("XXXXXXXX-XX") Then
newDwgName = targetFileName.Replace("XXXXXXXX-XX", oProjNum & oFlowNum & "-" & oProjSEQ) & targetFileExt
' Else If targetFileName.Contains("XXXXXXXX") Then
' newDwgName = targetFileName.Replace("XXXXXXXX", oProjNum & oFlowNum) & targetFileExt
Else
newDwgName = targetFileName & targetFileExt
End If
If Not System.IO.File.Exists(newDwgLocation & newDwgName) Then
If debugMsgs Then MsgBox("COPYING: " & newDwgLocation & newDwgName)
' MsgBox("COPYING: " & newDwgLocation & newDwgName)
Call ThisApplication.FileManager.CopyFile(targetFile, newDwgLocation & newDwgName)'If file doesn't already exist, copy file to new location
' isDrawing = True
Else
If debugMsgs Then MsgBox("Already Copied DRAWING: " & newDwgLocation & newDwgName)
End If
Dim fInfo As FileInfo = New FileInfo(newDwgLocation & newDwgName)
If fInfo.IsReadOnly = True Then fInfo.IsReadOnly = False
oDwgDoc = ThisApplication.Documents.OpenWithOptions(newDwgLocation & newDwgName, oNVM, False) 'Invisibly opens file to edit descriptions
'Loop through all of the referenced files in the drawing document, and look for the new filename by looking at all of the part/assembly files' OldName iProperties field
For Each oFileDesc in oDwgDoc.File.ReferencedFileDescriptors 'For all of the file references in the drawing
lookingFor = System.IO.Path.GetFileName(oFileDesc.FullFileName) 'Get the name only of the part we want to replace
' MsgBox("DRAWING: " & oFileDesc.FullFileName)
If debugMsgs Then MsgBox("Looking For: " & lookingFor)
' MsgBox(ConvertToNew(lookingFor, oDoc))
If lookingFor <> iProperties.Value("Custom", "OldName") Then 'If the file being looked for is not this assembly, look for it in the other file references
newFoundFile = ConvertToNew(lookingFor, oDoc)
Else
If debugMsgs Then MsgBox("This assembly is it")
newFoundFile = ThisDoc.PathAndFileName(True)
End If
If debugMsgs Then MsgBox("Found it:" & newFoundFile)
If newFoundFile <> "" Then 'If there is a file, then replace the current reference
oFileDesc.ReplaceReference(newFoundFile)
Else
' MsgBox("The file """ & lookingFor & """ referenced in drawing """ & oDoc.FullFileName & """ could not be found. Please repair this reference.")
End If
Next
' MsgBox(newDwgLocation & newDwgName)
Else
If debugMsgs Then MsgBox("Skipping " & targetFileName)
Continue For
End If
oDwgDoc.Save
oDwgDoc.Close
If a = 3 Then Exit For
a+=1
Next
']+++++\Copy Drawings Over+++++
Parameter("isCopied") = True 'Sets this to true to prevent rule from accidentally running again
oDoc.Save2
ThisApplication.Documents.CloseAll(True) 'Close all background (read: not visible or interactable documents)
showCompletionMessage = True
End If 'End of the protective IF statement that keeps the rule running W/O project or flow numbers. This allows the design view representations to be repaired.
'[ =====Set Design View Representation back to Default and Associative=====
'When replacing components, the design view representation may break causing previously hidden planes and other features to be shown. This section resets the
'design view to Associative and Default. The fileRepTypes list contains the types of files that cause an error because they have no Design View Representation, so
'the code skips those file types
'
Dim oOcc as ComponentOccurrence
'
Dim oAsmCompDefTwo As AssemblyComponentDefinition
oAsmCompDefTwo = oDoc.ComponentDefinition
For Each oOcc In oAsmCompDefTwo.Occurrences 'For every occurrence in the assembly, if the occurrence is an assembly, change its design veiw rep back to Default
If oOcc.Definition.Type = ObjectTypeEnum.kAssemblyComponentDefinitionObject Then
oOcc.SetDesignViewRepresentation("Default", "h", True)
End If
Next
'] +++++/Design View+++++
'[ =====Display Completion Message Box=====
'Allows the Design View Rep to be fixed before the completion message box appears
If Parameter("isCopied") = True And showCompletionMessage = True Then
i = MessageBox.Show("Copy Design Complete.", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End If
End Sub
Public Class cLibraryLocations
'Declares oLibraryArray as a "global" variable accessable in the other Sub/Functions by using cLibraryLocations.oLibraryArray
Public Shared oLibraryArray As New ArrayList
End Class
Public Class cLocs
'Declares oLibraryArray as a "global" variable accessable in the other Sub/Functions by using cLibraryLocations.oLibraryArray
Public Shared masterLoc As String' = Parameter.Param("masterLocation")
Public Shared newLoc as String' = "C:\Users\derek\Desktop\Save As Test\MODELS"
Public Shared mLocLen As Integer '= Len(cLocs.masterLoc)
Public Shared relPath
End Class
Private Function ConvertToNew(targetFile As String, containingDoc as Inventor.Document) As String
'Requires a filename, and an assembly document. Takes the old file name, usually from a drawing reference, loops through all of the files in
'the top level assembly and looks for the old file name in the new file's iProperties (Custom, OldName) then returns the name of the new file.
Dim oFileDesc as FileDescriptor
Dim oDocEnum as DocumentsEnumerator
Dim availDoc as Document
'MsgBox("LOOKING FOR : " & targetFile)
For Each oFileDesc in containingDoc.File.ReferencedFileDescriptors 'Look through all of the files in the assembly
oldFilePath = System.IO.Path.GetDirectoryName(oFileDesc.FullFileName)
oldFile = System.IO.Path.GetFileName(oFileDesc.FullFileName)
' MsgBox(oldFile)
If oFileDesc.ReferencedFileType = 56327 Then Continue For 'Skip OLE (object linking and embedding) File References such as embedded Excels
If cLibraryLocations.oLibraryArray.Contains(oldFilePath.ToUpper()) = True Then Continue For 'Skip library parts
availDoc = oFileDesc.ReferencedFile.AvailableDocuments.Item(1) 'Set reference to the first document in the file (only one document is in a file 99% of the time)
If debugMsgs Then MsgBox("iproperties: " & iProperties.Value(availDoc.DisplayName, "Custom", "OldName") & " TargetFile: " & targetFile)
'MsgBox(availDoc.DisplayName)
If iProperties.Value(oldFile, "Custom", "OldName") = targetFile Then 'If the old filename matches the iProperty
If debugMsgs Then MsgBox("iproperties: " & iProperties.Value(availDoc.DisplayName, "Custom", "OldName"))
ConvertToNew = availDoc.File.FullFileName 'Set the reference to the new file
End If
'If the document is a subassembly, run this function again on the subassembly to replace the references in that assembly
If oFileDesc.ReferencedFileType = 56323 Then
returnFile = ConvertToNew(targetFile, availDoc)
If returnFile <> "" Then
If debugMsgs Then MsgBox("assembly returned: " & returnFile )
ConvertToNew = returnFile
Return ConvertToNew
End If
End If
If ConvertToNew <> "" Then
Return ConvertToNew
End If
Next
End Function
Private Sub CopyFiles(sourceAssy As Inventor.Document)', masterCopyLoc as String, newCopyLoc As String)
'This sub takes an assembly document that is passed to it, and copies all of the files referenced within that assembly to the directory that the assembly is saved in. This
'is meant to be used in cojunction with another rule that saves the assembly to a new location
Dim projNum as String = Parameter("projectNum")
Dim oFlowNum as String = Parameter("flowNum")
Dim oProjAbbr As String = Parameter("projectAbbr")
Dim oProjRev As String = Parameter("projectRev")
Dim oProjYear As String = Parameter("projectYear")
Dim oProjSEQ As String = Parameter("projectSEQ")
Dim oProjDes as String = Parameter("projectDesignation")
Dim oNewFiles as New ArrayList
Dim oFileDesc as FileDescriptor
Dim subDoc as Document 'An assembly found in the assembly
Dim oCompOcc as ComponentOccurrence
Dim oAsmCompDef as AssemblyComponentDefinition
Dim oPartCompDef as PartComponentDefinition
Dim oldFileNames as New ArrayList
Dim availDoc as Document
'Dim masterLocation As String = Parameter("masterLocation")
'Dim mLocLen As Integer = Len(masterLocation)
masterLocDirs = System.IO.Directory.GetDirectories(cLocs.masterLoc)
' Loop through each referenced file in the passed assembly, copy it over to the new project folder, replace the references in the
' files with the newly copied files, and run this function again on any assemblies found in the passed assembly
testEnum = 1
For Each oFileDesc In sourceAssy.File.ReferencedFileDescriptors
If oFileDesc.ReferencedFileType <> 56327 Then 'If it is not an Embedded or linked object
oldFileName = System.IO.Path.GetFileNameWithoutExtension(oFileDesc.FullFileName) 'Collect old file information
oldFileExt = System.IO.Path.GetExtension(oFileDesc.FullFileName)
oldFilePath = System.IO.Path.GetDirectoryName(oFileDesc.FullFileName)
oldFileRelPath = Mid(oldFilePath,cLocs.mLocLen,1000) & "\"
'MsgBox(oldFileRelPath)
'projFlowNum = Parameter("projectNum") & Parameter("flowNum") & Parameter("projectSEQ") & "\"
projFlowNum = Parameter("projectNum") & Parameter("flowNum")
oldFileNameExt = oldFileName & oldFileExt
'newFilePath = cLocs.newLoc & oldFileRelPath
newFilePath = cLocs.newLoc & "\"
'If Not oldFilePath.Contains("STANDARD PARTS") Then 'Uncomment this and the EndIf to keep the STANDARD PARTS folder parts from being renamed
If oldFileName.Contains("XXXXXXXX-XX") Then
newFileName = oldFileName.Replace("XXXXXXXX-XX", projFlowNum & "-" & oProjSEQ) & oldFileExt
Else If oldFileName.Contains("XXXXXXXX") Then
newFileName = oldFileName.Replace("XXXXXXXX", projFlowNum) & oldFileExt
Else
newFileName = oldFileName & "_" & Parameter("projectNum") & Parameter("flowNum") & oldFileExt 'Create new file name
End If
'MsgBox(oldFileName & " - " & oldFileExt & " - " & oldFilePath & " - " & newFileName & " - " & newFilePath)
'End If
'MsgBox(newFilePath)
' If newFilePath.Contains("XXXXXXXX-XX") Then
' newFilePath = newFilePath.Replace("XXXXXXXX-XX", (projFlowNum & "-" & oProjSEQ))
' End If
' If newFilePath.Contains("XXXX-FILTER") Then
' newFilePath = newFilePath.Replace("XXXX-FILTER", projNum & "-FILTER")
' End If
If Not System.IO.Directory.Exists(newFilePath)
'MsgBox("Creating: " & newFilePath)
System.IO.Directory.CreateDirectory(newFilePath)
End If
If Not System.IO.File.Exists(newFilePath & newFileName) Then 'If the file doesn't exist
'MsgBox(oFileDesc.FullFileName & " - " & newFilePath & newFileName)
Try
Call ThisApplication.FileManager.CopyFile(oFileDesc.FullFileName, newFilePath & newFileName) 'Copy the file to the new location
Catch
MsgBox("ERROR COPYING: " & oFileDesc.FullFileName & " - " & newFilePath & newFileName)
End Try
Dim fInfo As FileInfo = New FileInfo(newFilePath & newFileName)
If fInfo.IsReadOnly = True Then
fInfo.IsReadOnly = False
End If
End If
Try
'MsgBox("File Desc: " & oFileDesc.FullFileName & vbCrLf & vbCrLf & "New Folder: " & newFilePath & vbCrLf & vbCrLf & "New File Name: " & newFileName & vbCrLf & vbCrLf & "Master Path: " & cLocs.masterLoc & vbCrLf & vbCrLf & "New Path: " & cLocs.newLoc & vbCrLf & vbCrLf & "Relative Path: " & oldFileRelPath )'& "oFileDesc DisplayName: " & availDoc.DisplayName)
oFileDesc.ReplaceReference(newFilePath & newFileName) 'Replace the file reference with new copy
Catch
MsgBox("Error Replacing: " & newFilePath & newFileName & vbCrLf & System.IO.File.Exists(newFilePath & newFileName))
End Try
availDoc = oFileDesc.ReferencedFile.AvailableDocuments.Item(1)
iProperties.Value(newFileName, "Custom", "OldName") = oldFileNameExt
'************ Replaces the iProperty "Part Number" with the project and flow number **************
Dim projectNumberString As String
Dim iPN As String
Dim iPNa As String
iPN = iProperties.Value(newFileName, "Project", "Part Number")
projectNumberString = projectNum & flowNum & "-" & projectSEQ
projectNumberString2 = projectNum & flowNum
b = iPN.Contains("XXXXXXXX-XX")
If b Then
iPN = iPN.Replace("XXXXXXXX-XX",(projectNumberString))
iProperties.Value(newFileName, "Project", "Part Number") = iPN
'MsgBox("iPN = " & iPN & " AND the file name is " & newFileName & " And the part number iProperty contains the X's: " & b)
availDoc.Save
Else If b Then
iPN = iPN.Replace("XXXXXXXX",(projectNumberString2))
iProperties.Value(newFileName, "Project", "Part Number") = iPN
availDoc.Save
End If
'If the document is a subassembly, run this whole function again on the subassembly
If oFileDesc.ReferencedFileType = 56323 Then
subDoc = oFileDesc.ReferencedFile.AvailableDocuments.Item(1)
Call CopyFiles(subDoc)
End If
End If
Next
'iLogicVb.RunRule("UPDATE")
End Sub
'[====List of FileTypeEnums=====
'kAssemblyFileType 56323 An Assembly file.
'kDesignElementFileType 56326 A design element file.
'kDrawingFileType 56324 A drawing file.
'kForeignFileType 56327 A foreign file.
'kPartFileType 56322 A part file.
'kPresentationFileType 56325 A presentation file.
'kUnknownFileType 56321 An unknown type Of file.