VBA级别,可参考,具体如下
Sub Main()
Dim doc As Document
Dim DrawingName As String = Nothing
If ThisApplication.ActiveDocumentType = DocumentTypeEnum.kPartDocumentObject Or ThisApplication.ActiveDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
doc = ThisApplication.ActiveDocument
DrawingName = GetDrawingType(doc.FullDocumentName)
If DrawingName Is Nothing Then
MsgBox("No drawing name could be found with this name in this path!", MsgBoxStyle.Exclamation, "Drawing not found")
Exit Sub
End If
Else
MsgBox("This application will only run from a part or assembly document!", MsgBoxStyle.Exclamation, "Document Type Error")
Exit Sub
End IfDim dwg As DrawingDocument
dwg = ThisApplication.Documents.Open(DrawingName, False)Dim propSetPart As PropertySet
propSetPart = doc.PropertySets.Item("D5CDD505-2E9C-101B-9397-08002B2CF9AE")Dim propSetDwg As PropertySet
propSetDwg = dwg.PropertySets.Item("D5CDD505-2E9C-101B-9397-08002B2CF9AE")Dim prop As Inventor.Property
For Each prop In propSetDwg
Try
propSetPart.Item(prop.Name).Value = prop.Value
Catch ex As Exception
propSetPart.Add(prop.Value, prop.Name)
End Try
NextEnd Sub
Private Function GetDrawingType(ByVal FileName As String) As String
Dim FoundFile As String = Nothing
Dim Dir As String = GetDirFromPath(FileName)
Dim _FileName As String = GetFileNameWithoutExtFromPath(FileName)
If My.Computer.FileSystem.FileExists(Dir & _FileName & ".idw") Then
FoundFile = Dir & _FileName & ".idw"
End If
If My.Computer.FileSystem.FileExists(Dir & _FileName & ".dwg") Then
FoundFile = Dir & _FileName & ".dwg"
End If
Return FoundFile
End Function
Private Function GetFileNameWithoutExtFromPath(ByVal path _
As String) As StringTry
Dim filename As String = _
path.Substring(path.LastIndexOf("\") + 1)
Dim pos As Integer = filename.LastIndexOf(".")If pos <> -1 Then
Return filename.Substring(0, pos)
Else
Return filename
End IfCatch ex As Exception
‘ error
Return ""
End TryEnd Function
Private Function GetDirFromPath(ByVal path As String) _
As StringTry
Return path.Substring(0, path.LastIndexOf("\") + 1)Catch ex As Exception
‘ error
Return ""
End TryEnd Function
言简意赅,赞一个!