Code:
Option ExplicitPublic Sub Export_Solids_To_Step_Separate()
Dim invApp As Application
Dim invDoc As Document
Dim partDoc As PartDocument
Dim compDef As PartComponentDefinition
Dim bodies As SurfaceBodies
Dim oBody As SurfaceBody
Dim oHideBody As SurfaceBody
Dim stepAddIn As TranslatorAddIn
Dim context As TranslationContext
Dim options As NameValueMap
Dim dataMedium As dataMedium
Dim originalPath As String
Dim stepFolder As String
Dim exportFile As String
Dim exportList As String
'-----------------------------------------
' Inventor Anwendung
'-----------------------------------------
Set invApp = ThisApplication
'-----------------------------------------
' 0) Dokument prüfen
'-----------------------------------------
If invApp.ActiveDocument Is Nothing Then
MsgBox "Es ist keine Datei geöffnet!", vbExclamation
Exit Sub
End If
Set invDoc = invApp.ActiveDocument
If invDoc.DocumentType <> kPartDocumentObject Then
MsgBox "Dieses Makro funktioniert nur mit IPT-Dateien!", vbExclamation
Exit Sub
End If
'-----------------------------------------
' Datei speichern falls nötig (mit Abbruch-Erkennung)
'-----------------------------------------
If invDoc.FullFileName = "" Then
MsgBox "Die Datei ist noch nicht gespeichert." & vbCrLf & _
"Bitte Speicherort wählen.", vbInformation
On Error Resume Next
invDoc.Save
On Error GoTo 0
If invDoc.FullFileName = "" Then
MsgBox "Vorgang abgebrochen." & vbCrLf & _
"Die Datei muss gespeichert sein, um den Export durchzuführen.", _
vbExclamation
Exit Sub
End If
End If
Set partDoc = invDoc
Set compDef = partDoc.ComponentDefinition
'-----------------------------------------
' Auf ausstehende Aktualisierungen prüfen
'-----------------------------------------
If partDoc.RequiresUpdate Then
On Error Resume Next
partDoc.Update
If Err.Number <> 0 Then
MsgBox "Das Modell konnte nicht aktualisiert werden." & vbCrLf & _
"Bitte Modell prüfen (Fehler, unterdrückte Features).", _
vbCritical
Exit Sub
End If
On Error GoTo 0
End If
Set bodies = compDef.SurfaceBodies
'-----------------------------------------
' 1) Prüfen ob Volumenkörper existieren
'-----------------------------------------
If bodies.Count = 0 Then
MsgBox "Es wurden keine Volumenkörper gefunden!", vbExclamation
Exit Sub
End If
Dim solidFound As Boolean
solidFound = False
For Each oBody In bodies
If oBody.IsSolid Then
solidFound = True
Exit For
End If
Next
If Not solidFound Then
MsgBox "Es wurden keine Solid Bodies gefunden!", vbExclamation
Exit Sub
End If
'-----------------------------------------
' STEP-Ordner vorbereiten
'-----------------------------------------
originalPath = Left(invDoc.FullFileName, InStrRev(invDoc.FullFileName, "\") - 1)
stepFolder = originalPath & "\STEP"
If Dir(stepFolder, vbDirectory) = "" Then
MkDir stepFolder
End If
'-----------------------------------------
' STEP-Translator vorbereiten
'-----------------------------------------
Set stepAddIn = invApp.ApplicationAddIns.ItemById( _
"{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
If stepAddIn Is Nothing Then
MsgBox "STEP-Translator nicht gefunden!", vbCritical
Exit Sub
End If
Set context = invApp.TransientObjects.CreateTranslationContext
context.Type = kFileBrowseIOMechanism
Set options = invApp.TransientObjects.CreateNameValueMap
options.Value("ApplicationProtocolType") = 3 ' AP242
Set dataMedium = invApp.TransientObjects.CreateDataMedium
'-----------------------------------------
' Alle Bodies zuerst ausblenden
'-----------------------------------------
For Each oHideBody In bodies
oHideBody.Visible = False
Next
'-----------------------------------------
' Schleife: Export je Volumenkörper
'-----------------------------------------
For Each oBody In bodies
If oBody.IsSolid Then
For Each oHideBody In bodies
oHideBody.Visible = False
Next
oBody.Visible = True
exportFile = stepFolder & "\" & oBody.Name & ".step"
dataMedium.FileName = exportFile
If stepAddIn.HasSaveCopyAsOptions(invDoc, context, options) Then
stepAddIn.SaveCopyAs invDoc, context, options, dataMedium
exportList = exportList & oBody.Name & ".step" & vbCrLf
End If
End If
Next
'-----------------------------------------
' Alle Volumenkörper wieder sichtbar
'-----------------------------------------
For Each oBody In bodies
oBody.Visible = True
Next
'-----------------------------------------
' 2) Abschlussmeldung
'-----------------------------------------
MsgBox "Export abgeschlossen!" & vbCrLf & vbCrLf & _
"Speicherort:" & vbCrLf & stepFolder & vbCrLf & vbCrLf & _
"Erstellte Dateien:" & vbCrLf & exportList, _
vbInformation, "Fertig"
End Sub