SDK原版参考,代码采用ApprenticeServer模式开发,可用于独立exe程序插件,内置插件想参考的话,需要改写。Inventor api 禁止内置插件对ApprenticeServer访问。
这个工具可以将指定的图纸文件从一个现有的目录位置复制到一个新的目录位置。除了复制图纸文件,它还会复制每个工程图的所有引用文件,并更新复制的图纸中的引用,使其指向这些新复制的文件。
复制文件和更新引用的最佳方法取决于你的需求。如果你想要相同的文件名,那么除了将一组文件复制到一个新目录外,你不需要做任何事情,然后确保你的项目只引用那个目录。由于所有名字都相同,Inventor 将能够找到引用的文件。
如果你需要更改任何名称,那么你需要更新引用。这可能是最典型的场景,也是这个工具所展示的。Apprentice 是解决这一问题的最佳方案。它提供了关于更改文件引用最多的功能。Apprentice 不应该在 Inventor 的 VBA 中使用。你可以在另一个应用程序(例如 Excel)中使用 VBA,或选择另一种允许你创建可执行文件的语言。
Public Class Form1
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
' Get the full path of the folder of the existing design.
dlgFolderBrowser.Description = "Select path of existing files."
dlgFolderBrowser.ShowDialog()
Dim filePath As String
filePath = dlgFolderBrowser.SelectedPath
' Check to see that a path was specified.
If filePath <> "" Then
' Set the text field to the specified path.
txtDirectoryPath.Text = filePath
' Update the displayed path of the new project.
UpdateNewProjectName()
End If
End Sub
Private Sub txtDirectoryPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDirectoryPath.TextChanged
' Update the displayed path of the new project.
UpdateNewProjectName()
End Sub
Private Sub txtNewName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNewName.TextChanged
' Update the displayed path of the new project.
UpdateNewProjectName()
End Sub
Private Sub UpdateNewProjectName()
' Strip off the last directory from the full path.
Dim fullPath As String = txtDirectoryPath.Text
fullPath = fullPath.Substring(0, fullPath.LastIndexOf("\") + 1)
' Update the displayed path of the new project.
lblNewDesignPath.Text = fullPath & txtNewName.Text
' Update the pre-pend name for the file names to be copied.
If chkPrependDesignNameToFileNames.Checked Then
txtPrependDesignName.Text = txtNewName.Text
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
' Disable the buttons while it's processing
btnOK.Enabled = False
btnCancel.Enabled = False
Try
' Validate that the specified path for the existing project exists.
If Not System.IO.Directory.Exists(txtDirectoryPath.Text) Then
MsgBox("The specified path to the existing design does not exist.")
Exit Sub
End If
' Check to see if the specified path for the new project exists.
If System.IO.Directory.Exists(lblNewDesignPath.Text) Then
If MsgBox("The specified path for the new design already exists. Any existing files in this directory may be overwritten. Do you want to continue?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then
Exit Sub
End If
Else
' Create the output directory.
Try
System.IO.Directory.CreateDirectory(lblNewDesignPath.Text)
Catch ex As Exception
MsgBox("Unable to create the directory for the new design: """ & lblNewDesignPath.Text & """")
Exit Sub
End Try
End If
' Get the name of the existing design by getting the name of the directory from the full path.
Dim fullPath As String = txtDirectoryPath.Text
Dim existingDesignName As String = fullPath.Substring(fullPath.LastIndexOf("\") + 1)
' Get the name of the new design.
Dim newDesignName As String = txtNewName.Text
' Get the full path of the base directory
Dim baseDirectoryPath As String = fullPath.Substring(0, fullPath.LastIndexOf("\") + 1)
' Get the filenames of any drawings.
'Dim drawingFilenames() As String = System.IO.Directory.GetFiles(baseDirectoryPath & existingDesignName, "*.idw")
Dim idwdrawingFilenames() As String = System.IO.Directory.GetFiles(baseDirectoryPath & existingDesignName, "*.idw")
Dim dwgDrawingFilenames() As String = System.IO.Directory.GetFiles(baseDirectoryPath & existingDesignName, "*.dwg")
Dim drawingFilenames(idwdrawingFilenames.Length + dwgDrawingFilenames.Length - 1) As String
Dim i As Long
For i = 0 To idwdrawingFilenames.Length - 1
drawingFilenames(i) = idwdrawingFilenames(i)
Next
Dim j As Long
For j = 0 To dwgDrawingFilenames.Length - 1
drawingFilenames(idwdrawingFilenames.Length + j) = dwgDrawingFilenames(j)
Next
' Create an instance of Apprentice.
Dim apprenticeServer As New Inventor.ApprenticeServerComponent
' Iterate through the drawing documents. This assumes that all other documents
' are somehow referenced by the drawings. This can be an indirect reference. For
' example, a part that is used in an assembly that is shown in the drawing is ok.
Dim currentDrawingFilename As String
For Each currentDrawingFilename In drawingFilenames
' Open the drawing in Apprentice.
If currentDrawingFilename.Substring(currentDrawingFilename.Length - 3) = "dwg" Then
'Skip the file if it's not an Inventor DWG.
If Not apprenticeServer.FileManager.IsInventorDWG(currentDrawingFilename) Then
Continue For
End If
End If
Dim drawingDoc As Inventor.ApprenticeServerDocument
drawingDoc = apprenticeServer.Open(currentDrawingFilename)
' Get the FileSaveAs object.
Dim fileSaveAs As Inventor.FileSaveAs = apprenticeServer.FileSaveAs
' Add the drawing to the FileSaveAs object.
Dim newDrawingFilename As String = drawingDoc.FullFileName
newDrawingFilename = baseDirectoryPath + newDesignName + "\" + newDrawingFilename.Substring(fullPath.Length + 1)
' Prepend design name to filename
If txtPrependDesignName.Text <> "" Then
Dim prependPos As Integer = newDrawingFilename.LastIndexOf("\") + 1
newDrawingFilename = newDrawingFilename.Substring(0, prependPos) + txtPrependDesignName.Text + "_" + newDrawingFilename.Substring(prependPos, newDrawingFilename.Length - prependPos)
End If
' Delete the document in case it already exists.
If System.IO.File.Exists(newDrawingFilename) Then System.IO.File.Delete(newDrawingFilename)
fileSaveAs.AddFileToSave(drawingDoc, newDrawingFilename)
' Copy all the referenced OLE files and re-reference the new copied ones to the drawing document.
Dim referencedOLEFileDes As Inventor.ReferencedOLEFileDescriptor
For Each referencedOLEFileDes In drawingDoc.ReferencedOLEFileDescriptors
replaceOLEReferencedFiles(referencedOLEFileDes, existingDesignName, newDesignName)
Next
' Get all of the documents referenced by this document.
Dim allReferencedDocs As Inventor.ApprenticeServerDocuments
allReferencedDocs = drawingDoc.AllReferencedDocuments
Dim referencedDoc As Inventor.ApprenticeServerDocument
For Each referencedDoc In allReferencedDocs
' Get the full filename of the current document.
Dim designFilename As String = referencedDoc.FullFileName
' Replace instances of the old design name with the new name.
designFilename = baseDirectoryPath + newDesignName + "\" + designFilename.Substring(fullPath.Length + 1)
' Prepend design name to filename
If txtPrependDesignName.Text <> "" Then
Dim prependPos As Integer = designFilename.LastIndexOf("\") + 1
designFilename = designFilename.Substring(0, prependPos) + txtPrependDesignName.Text + "_" + designFilename.Substring(prependPos, designFilename.Length - prependPos)
End If
' Check to see if the specified path for the file exists.
If Not System.IO.Directory.Exists(designFilename.Substring(0, designFilename.LastIndexOf("\") + 1)) Then
' Create the output directory.
Try
System.IO.Directory.CreateDirectory(designFilename.Substring(0, designFilename.LastIndexOf("\") + 1))
Catch ex As Exception
MsgBox("Unable to create the directory for the file: """ & designFilename & """")
Exit Sub
End Try
End If
' Add the current file to the FileSaveAs object.
fileSaveAs.AddFileToSave(referencedDoc, designFilename)
' Delete the document in case it already exists.
If System.IO.File.Exists(designFilename) Then System.IO.File.Delete(designFilename)
' Copy all the referenced OLE files and re-reference the new copied ones to the drawing document.
For Each referencedOLEFileDes In referencedDoc.ReferencedOLEFileDescriptors
replaceOLEReferencedFiles(referencedOLEFileDes, existingDesignName, newDesignName)
Next
Next
' Do the save copy as of all the files.
fileSaveAs.ExecuteSaveCopyAs()
' Close the drawing.
For Each referencedDoc In allReferencedDocs
referencedDoc.Close()
Next
apprenticeServer.Close()
Next
MsgBox("Finished copying design.")
Catch ex As Exception
MsgBox("Unexpected error while processing files.")
End Try
btnOK.Enabled = True
btnCancel.Enabled = True
End Sub
Private Sub chkPrependDesignNameToFileNames_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPrependDesignNameToFileNames.CheckedChanged
If chkPrependDesignNameToFileNames.Checked Then
txtPrependDesignName.Text = txtNewName.Text
Else
txtPrependDesignName.Text = ""
End If
End Sub
' Copy and replace the OLE referenced files
Private Sub replaceOLEReferencedFiles(ByRef referencedOLEFileDes As Inventor.ReferencedOLEFileDescriptor, ByVal existingDesignName As String, ByVal newDesignName As String)
' only linked OLE files will be copied and re-referenced to new design documents.
If referencedOLEFileDes.OLEDocumentType = Inventor.OLEDocumentTypeEnum.kOLEDocumentLinkObject Then
' Get the full filename of the referenced OLE file.
Dim originDesignOLEFileName As String = referencedOLEFileDes.FullFileName
Dim designOLEFileName As String = originDesignOLEFileName
Dim fullPath As String = txtDirectoryPath.Text
' Get the full path of the base directory
Dim baseDirectoryPath As String = fullPath.Substring(0, fullPath.LastIndexOf("\") + 1)
Dim sOleDirSplit() As String, sDirPathSplit() As String, i As Integer, iLess As Integer, sSubDir As String = "", bContinueCompare As Boolean = True
sOleDirSplit = Split(originDesignOLEFileName, "\")
sDirPathSplit = Split(fullPath, "\")
iLess = IIf(sOleDirSplit.Length > sDirPathSplit.Length, sDirPathSplit.Length, sOleDirSplit.Length)
For i = 1 To iLess
If bContinueCompare Then
If System.String.Compare(LCase(sOleDirSplit(i - 1)), LCase(sDirPathSplit(i - 1))) = 0 Then
Else
bContinueCompare = False
If i <> 1 Then
sSubDir = sSubDir + sOleDirSplit(i - 1) + "\"
End If
End If
Else
If i = iLess Then
sSubDir = sSubDir + sOleDirSplit(i - 1)
Else
sSubDir = sSubDir + sOleDirSplit(i - 1) + "\"
End If
End If
Next
If System.IO.File.Exists(originDesignOLEFileName) Then
' Replace the old design name with new name.
designOLEFileName = baseDirectoryPath + newDesignName + "\" + sSubDir
If Not (System.IO.Directory.Exists(designOLEFileName.Substring(0, designOLEFileName.LastIndexOf("\")))) Then
System.IO.Directory.CreateDirectory(designOLEFileName.Substring(0, designOLEFileName.LastIndexOf("\")))
End If
' Prepend design name to filename
If txtPrependDesignName.Text <> "" Then
Dim prependPos As Integer = originDesignOLEFileName.LastIndexOf("\") + 1
designOLEFileName = designOLEFileName + txtPrependDesignName.Text + "_" + originDesignOLEFileName.Substring(prependPos, originDesignOLEFileName.Length - prependPos)
End If
System.IO.File.Copy(originDesignOLEFileName, designOLEFileName, True)
' reference to new OLE files.
referencedOLEFileDes.FileDescriptor.ReplaceReference(designOLEFileName)
End If
End If
End Sub
End Class