SDK原版参考
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
第二种版本:
Public Sub Copy_Design(ByVal oLocation As String,
ByVal oName As String,
ByVal oPrefix As String)
Dim oDoc As Document
oDoc = oInvApp.ActiveDocument
Dim oNewLocation As String
oNewLocation = IO.Path.GetDirectoryName(oDoc.FullFileName) & "\Nozzles\" & oName & "\"
Try
' Controleren als standaard folder bestaat
If Not System.IO.Directory.Exists(oLocation) Then
MsgBox("Deze standaard bestaat niet op de locatie. Haal deze uit de Vault of vraag hulp.")
Exit Sub
End If
' Controleren als er een prefix & folder locatie is ingegeven
If oPrefix = "" Then
MsgBox("Geef eerst een projectnummer in.", MsgBoxStyle.Information)
Exit Sub
ElseIf oName = "" Then
MsgBox("Geef eerst een positienummer in.", MsgBoxStyle.Information)
Exit Sub
ElseIf oNewLocation = "" Then
MsgBox("Geef eerst een bestands locatie in.", MsgBoxStyle.Information)
Exit Sub
End If
' Controleren als de nieuwe folder al bestaat
If System.IO.Directory.Exists(oNewLocation) Then
If MsgBox("De opgegeven folder voor de copy bestaat al." & vbLf &
"Bestanden in deze folder kunnen mogelijk overschreven worden (indien deze dezelfde naam hebben)." & vbLf &
" Wil je doorgaan?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then
Exit Sub
End If
Else
' Create the output directory.
Try
System.IO.Directory.CreateDirectory(oNewLocation)
Catch ex As Exception
MsgBox("Unable to create the directory for the new design: """ & oNewLocation & """")
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 = oLocation
Dim existingDesignName As String = fullPath.Substring(fullPath.LastIndexOf("\") + 1)
' Get the name of the new design.
Dim newPath As String = oNewLocation
Dim newDesignName As String = newPath.Substring(newPath.LastIndexOf("\") + 1)
' 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 & existingD
esignName, "*.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 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 ApprenticeServerDocument
drawingDoc = apprenticeServer.Open(currentDrawingFilename)
' Get the FileSaveAs object.
Dim fileSaveAs As FileSaveAs = apprenticeServer.FileSaveAs
' Add the drawing to the FileSaveAs object.
Dim newDrawingFilename As String = drawingDoc.FullFileName
newDrawingFilename = oNewLocation + "\" + newDrawingFilename.Substring(fullPath.Length + 1)
' Prepend design name to filename
If oPrefix <> "" Then
Dim prependPos As Integer = newDrawingFilename.LastIndexOf("\") + 1
newDrawingFilename = newDrawingFilename.Substring(0, prependPos) + oPrefix + "_" + 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 ReferencedOLEFileDescriptor
For Each referencedOLEFileDes In drawingDoc.ReferencedOLEFileDescriptors
replaceOLEReferencedFiles(referencedOLEFileDes, existingDesignName, newDesignName, oLocation, oNewLocation, oPrefix)
Next
' Get all of the documents referenced by this document.
Dim allReferencedDocs As ApprenticeServerDocuments
allReferencedDocs = drawingDoc.AllReferencedDocuments
Dim referencedDoc As ApprenticeServerDocument
For Each referencedDoc In allReferencedDocs
' Get the full filename of the current document.
Dim designFilename As String = referencedDoc.FullFileName
If designFilename.Contains("Content Center Files") Then Continue For
' Replace instances of the old design name with the new name.
designFilename = oNewLocation + "\" + designFilename.Substring(fullPath.Length + 1)
' Prepend design name to filename
If oPrefix <> "" Then
&
nbsp; Dim prependPos As Integer = designFilename.LastIndexOf("\") + 1
designFilename = designFilename.Substring(0, prependPos) + oPrefix + "_" + 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, oLocation, oNewLocation, oPrefix)
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.")
Exit Sub
End Try
End Sub
Private Sub replaceOLEReferencedFiles(ByRef referencedOLEFileDes As ReferencedOLEFileDescriptor,
ByVal existingDesignName As String,
ByVal newDesignName As String,
ByVal oLocation As String,
ByVal oNewLocation As String,
ByVal oPrefix As String)
' only linked OLE files will be copied and re-referenced to new design documents.
If referencedOLEFileDes.OLEDocumentType = 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 = oLocation
' Get the full path of the base directory
Dim baseDirectoryPath As String = fullPath.Substring(0, fullPath.LastIndexOf("\") + 1)
If System.IO.File.Exists(originDesignOLEFileName) Then
' Replace the old design name with new name.
designOLEFileName = oNewLocation + "\" + designOLEFileName.Substring(fullPath.Length + 1)
If Not (System.IO.Directory.Exists(designOLEFileName.Substring(0, designOLEFileName.LastIndexOf("\")))) Then
System.IO.Directory.CreateDire
ctory(designOLEFileName.Substring(0, designOLEFileName.LastIndexOf("\")))
End If
' Prepend design name to filename
If oPrefix <> "" Then
Dim prependPos As Integer = designOLEFileName.LastIndexOf("\") + 1
designOLEFileName = designOLEFileName.Substring(0, prependPos) + oPrefix + "_" + designOLEFileName.Substring(prependPos, designOLEFileName.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