Underneath the code we work with now.
It exports all assemblies en parts in the different formats and their drawings underneath, not just the first level.
(It also exports base-parts underneath derived functions)
' - - - - - - - - - - Assembly Export - - - - - - - - - - - - - - - - -
' Add required libraries and add-ins,...
AddReference "System.IO.Compression"
AddReference "System.IO.Compression.FileSystem"
' - - - - - - - - - - Main program - - - - - - - - - - - - - - - - - - -
Sub Main
'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)
'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If
'get user input
RUsure = MessageBox.Show ( _
"Er wordt een PDF-file gemaakt voor alle assembly-onderdelen die over een drawing beschikken." _
& vbLf & "Er wordt een IGES-file gemaakt van de assembly." _
& vbLf & "Deze -rule- eist dat alle drawings dezelfde naam en locatie hebben als de componenten." _
& vbLf & "" _
& vbLf & "Ben je zeker dat er PDF-tekeningen van alle assembly onderdelen moeten worden gemaakt?" _
& vbLf & "Dit kan een tijdje duren.", "iLogic - Batch Output PDFs ",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
Return
Else
End If
'- - - - - - - - - - - - -PDF en IGS setup - - - - - - - - - - - -
oPath = "\\PPLHHBB5\data\11.PRODUCTIE\Drawings\SAMENSTELLINGEN"
'Get the PDF-translator Add-Inn
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
' Get the IGES translator Add-In.
Dim oIGESTranslator As TranslatorAddIn
oIGESTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F44-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
If oIGESTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
' Set geometry type for wireframe.
' 0 = Surfaces, 1 = Solids, 2 = Wireframe
oOptions.Value("GeometryType") = 1
' To set other translator values:
oOptions.Value("SolidFaceType") = 0
' 0 = NURBS, 1 = Analytic
oOptions.Value("SurfaceType") = 0
' 0 = 143(Bounded), 1 = 144(Trimmed)
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
End If
' Get the STEP translator Add-In.
Dim oSTEPTranslator As TranslatorAddIn
oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim oSTEPContext As TranslationContext
oSTEPContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oSTEPOptions As NameValueMap
oSTEPOptions = ThisApplication.TransientObjects.CreateNameValueMap
If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oSTEPContext, oSTEPOptions) Then
' Set application protocol.
' 2 = AP 203 - Configuration Controlled Design
' 3 = AP 214 - Automotive Design
oSTEPOptions.Value("ApplicationProtocolType") = 3
' Other options...
'oSTEPOptions.Value("Author") = ""
'oSTEPOptions.Value("Authorization") = ""
'oOSTEPptions.Value("Description") = ""
'oSTEPOptions.Value("Organization") = ""
oSTEPContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
End If
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 600
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("Custom_End_Sheet") = 4
End If
'get target folder path
oFolder = oPath & "\" & oAsmName & " V" & iProperties.Value("Project", "Revision Number")
'Check for the PDF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
Else
'-----------------------------------
'MESSAGE BOX
'query user
question = MessageBox.Show("Deze folder bestaat reeds, wilt u deze opnieuw aanmaken?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
'set condition based on answer
If question = vbNo Then
'gather input from user, uses current parameter as input default
Exit Sub
'------------------------------------
Else
End If
iLogicVb.UpdateWhenDone = True
End If
'- - - - - - - - - - - - -
'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
'work the the drawing files for the referenced models
'this expects that the model has a drawing of the same path and name
For Each oRefDoc In oRefDocs
idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "idw"
'check to see that the model has a drawing of the same path and name
If(System.IO.File.Exists(idwPathName)) Then
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -4)
oRevNum = oRefDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
On Error Resume Next ' if PDF exists and is open or read only, resume next
'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & " V" & oRevNum & ".pdf"
'Write out the PDF
Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
'close the file
oDrawDoc.Close(True)
Else
'If the model has no drawing of the same path and name - do nothing
End If
Next
'- - - - - - - - - - - - - Component 3D models format export - - - - - - - - - - -
Dim oIGESpartdata As DataMedium
Dim oSTEPpartdate As DataMedium
Dim oRefParts As DocumentsEnumerator
oRefParts= oAsmDoc.AllReferencedDocuments
Dim oRefPart As Document
' Iterate through each part in the assembly
For Each oRefPart In oRefParts
iptPathName = Left(oRefPart.FullDocumentName, Len(oRefPart.FullDocumentName) -3) & "ipt"
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.Documents.Open(iptPathName, True)
oPartName = System.IO.Path.GetFileNameWithoutExtension(oRefPart.FullFileName)
oRevNumPart = oRefPart.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
'IGES export subparts
oIGESpartdata = ThisApplication.TransientObjects.CreateDataMedium
oIGESpartdata.FileName = oFolder & "\" & oPartName & " V" & oRevNumPart & ".igs"
oIGESTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oIGESpartdata)
'STEP export subparts
oSTEPpartdata = ThisApplication.TransientObjects.CreateDataMedium
oSTEPpartdata.FileName = oFolder & "\" & oPartName & " V" & oRevNumPart & ".stp"
oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oSTEPContext, oSTEPOptions, oSTEPpartdata)
oPartDoc.Close(True)
Next
'- - - - - - - - - - - - - Top Level routines - - - - - - - - - - -
'Top Level Drawing
oAsmDrawing = ThisDoc.ChangeExtension(".idw")
oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -4)
'write out the PDF for the Top Level Assembly Drawing file
On Error Resume Next ' if PDF exists and is open or read only, resume next
'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & " V" & iProperties.Value("Project", "Revision Number") & ".pdf"
'Write out the PDF
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
'Close the top level drawing
oAsmDrawingDoc.Close(True)
'Top Level IGES
Dim oData As DataMedium
oData = ThisApplication.TransientObjects.CreateDataMedium
oData.FileName = oFolder & "\" & oAsmName & " V" & iProperties.Value("Project", "Revision Number") & ".igs"
oIGESTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
'Top Level STEP
Dim oSTEPData As DataMedium
oSTEPData = ThisApplication.TransientObjects.CreateDataMedium
oSTEPData.FileName = oFolder & "\" & oAsmName & " V" & iProperties.Value("Project", "Revision Number") & ".stp"
oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oSTEPContext, oSTEPOptions, oSTEPData)
'--------------------End of routine--------------------------------
'Create ZIP-file with created files
oZIPfolderCheck = oFolder & "\" & oAsmName & " V" & iProperties.Value("Project", "Revision Number") & ".zip"
oZIPContainer = oFolder & ".zip" 'Create file first in main folder and move after creation, because nesting of folders during creation produces errors
oZIPFinalDestination = oZIPfolderCheck
'Check if file already exists
If System.IO.File.Exists(oZIPfolderCheck)
System.IO.File.Delete(oZIPfolderCheck)
End If
'Create File
System.IO.Compression.ZipFile.CreateFromDirectory(oFolder, oZIPContainer)
'Move File to correct folder
System.IO.File.Move(oZIPContainer,oZIPFinalDestination)
MessageBox.Show("Nieuwe bestanden in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)
End Sub