Hello
Here's the code. Seem's in Inventor 2008 it throws exceptions or tells an iAssembly is an Assembly or an iPart is a Part and so on. In Inventor 2011 it runs fine in every test i made. Without any guarantee
Option Explicit
Private Sub ExtractAssFromiAss()
Dim oApp As Inventor.Application
Set oApp = ThisApplication
If oApp.Documents.Count = 0 Then
MsgBox "An assembly must be open", vbExclamation
Exit Sub
End If
If Not oApp.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
MsgBox "An assembly must be open", vbExclamation
Exit Sub
End If
Dim oSourceDoc As AssemblyDocument
Set oSourceDoc = oApp.ActiveDocument
Dim sBasePath As String
sBasePath = GetBasePath(oSourceDoc)
Dim sNewFileName As String
sNewFileName = sBasePath & "\" & GetDocumentName(oSourceDoc) & "-separiert" & Right(oSourceDoc.FullFileName, 4)
Call oSourceDoc.SaveAs(sNewFileName, False)
Dim oNewAss As AssemblyDocument
Set oNewAss = oSourceDoc 'oApp.Documents.Open(sNewFileName)
If ProcessRefedDocs(oApp, oNewAss, sBasePath) = False Then
MsgBox "Error", vbCritical
End If
Call oNewAss.BrowserPanes.ActivePane.Update
Call oNewAss.Update2(True)
Call oNewAss.Save2(True)
'Call oNewAss.Close()
End Sub
Private Function ProcessRefedDocs(ByVal oApp As Inventor.Application, ByVal oAssDoc As AssemblyDocument, ByVal sBasePath As String) As Boolean
Dim oRefedDoc As Document
Dim oRefedAss As AssemblyDocument
Dim oRefedPart As PartDocument
Dim oNewDoc As Document
Dim oFileSystem As Object
Dim sOldFileName As String
Dim sNewFileName As String
Dim oOcc As ComponentOccurrence
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
For Each oRefedDoc In oAssDoc.ReferencedDocuments
If oRefedDoc.DocumentType = kAssemblyDocumentObject Then
'falls es eine iAssembly ist, extrahieren
Set oRefedAss = oRefedDoc
sOldFileName = oRefedAss.FullDocumentName
If oRefedAss.ComponentDefinition.IsiAssemblyMember Then
sNewFileName = sBasePath & "\" & GetDocumentName(oRefedAss) & "-separiert" & Right(oRefedAss.FullFileName, 4)
If oFileSystem.FileExists(sNewFileName) = False Then
Call ExtractiAss(oApp, oRefedAss, sNewFileName)
End If
If Not sNewFileName = "" Then
For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
If oOcc.ReferencedDocumentDescriptor.FullDocumentName = sOldFileName Then
Call oOcc.Replace(sNewFileName, False)
End If
Next
End If
For Each oNewDoc In oAssDoc.ReferencedDocuments
If oNewDoc.FullDocumentName = sNewFileName Then
Set oRefedAss = oNewDoc
End If
Next
End If
Call processRefedDocs(oApp, oRefedAss, sBasePath) ' subassembly
ElseIf oRefedDoc.DocumentType = kPartDocumentObject Then
Set oRefedPart = oRefedDoc
sOldFileName = oRefedPart.FullDocumentName
If oRefedPart.ComponentDefinition.IsiPartMember Then
sNewFileName = sBasePath & "\" & GetDocumentName(oRefedPart) & "-separiert" & Right(oRefedPart.FullFileName, 4)
If oFileSystem.FileExists(sNewFileName) = False Then
Call ExtractiPart(oApp, oRefedPart, sNewFileName)
End If
ElseIf InStr(oRefedPart.FullDocumentName, "DUMMY") Then
sNewFileName = sBasePath & "\" & GetDocumentName(oRefedPart) & Right(oRefedPart.FullFileName, 4)
If oFileSystem.FileExists(sNewFileName) = False Then
Call oRefedPart.SaveAs(sNewFileName, False)
End If
End If
If Not sNewFileName = "" Then
For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
If oOcc.ReferencedDocumentDescriptor.FullDocumentName = sOldFileName Then
Call oOcc.Replace(sNewFileName, False)
End If
Next
End If
End If
sNewFileName = ""
Next
ProcessRefedDocs = True
End Function
Private Function GetBasePath(ByVal odoc As Document)
Dim oFileName As String
oFileName = odoc.FullDocumentName
Dim oArray() As String
oArray = Split(oFileName, "\")
Dim sName As String
Dim i As Integer
sName = oArray(LBound(oArray))
For i = 1 To UBound(oArray) - 1
sName = sName & "\" & oArray(i)
Next
GetBasePath = sName
End Function
Private Function GetDocumentName(ByVal odoc As Document)
Dim oFileName As String
oFileName = odoc.FullDocumentName
Dim oArray() As String
oArray = Split(oFileName, "\")
Dim sName As String
sName = oArray(UBound(oArray))
GetDocumentName = Left(sName, Len(sName) - 4)
End Function
Private Function ExtractiAss(ByVal oApp As Inventor.Application, ByVal oAssDoc As AssemblyDocument, ByVal sNewFileName As String)
Dim oParent As AssemblyDocument
Dim oRow As iAssemblyTableRow
Dim sMemberName As String
sMemberName = oAssDoc.ComponentDefinition.iAssemblyMember.Row.MemberName
Set oParent = oApp.Documents.Open(oAssDoc.ComponentDefinition.iAssemblyMember.ReferencedDocumentDescriptor.FullDocumentName)
Call oParent.SaveAs(sNewFileName, False)
If Not oParent.ComponentDefinition.iAssemblyFactory.DefaultRow.MemberName = sMemberName Then
For Each oRow In oParent.ComponentDefinition.iAssemblyFactory.TableRows
If oRow.MemberName = sMemberName Then
oParent.ComponentDefinition.iAssemblyFactory.DefaultRow = oRow
End If
Next
End If
Call oParent.ComponentDefinition.iAssemblyFactory.Delete
Call oParent.Save
oParent.Close
End Function
Private Function ExtractiPart(ByVal oApp As Inventor.Application, ByVal oPartDoc As PartDocument, ByVal sNewFileName As String)
Dim oParent As PartDocument
Dim oRow As iPartTableRow
Dim sMemberName As String
sMemberName = oPartDoc.ComponentDefinition.iPartMember.Row.MemberName
Set oParent = oApp.Documents.Open(oPartDoc.ComponentDefinition.iPartMember.ReferencedDocumentDescriptor.FullDocumentName)
Call oParent.SaveAs(sNewFileName, False)
If Not oParent.ComponentDefinition.iPartFactory.DefaultRow.MemberName = sMemberName Then
For Each oRow In oParent.ComponentDefinition.iPartFactory.TableRows
If oRow.MemberName = sMemberName Then
oParent.ComponentDefinition.iPartFactory.DefaultRow = oRow
End If
Next
End If
Call oParent.ComponentDefinition.iPartFactory.Delete
Call oParent.Save
oParent.Close
End Function
R. Krieg
RKW Solutions
www.rkw-solutions.com