Okay so, as of now i accepted some of your comments as solutions, I made more adjustments, probably will continue to do so as workflow requests it. BUT, so far, what this code does, makes folders for each STP, DXF, and PDF files in the project workspace, loops through components in the active assembly, if it is a part and has a referenced IDW, save each component as a STP, then save the IDW as a DXF and PDF in the appropriate workspace folder.
I have it configured to skip the STP if it doesn't have a referenced IDW, will show a message showing the filename, so if that pops up i can write it down quick, the rule will skip the rest of the steps and move onto the next component, then can just rerun the code once i resolve the missing IDWs. When i get extra time to play with the code, i would like to maybe add a filename list that shows each filename that doesn't have a referenced idw like you mentioned but for now showing each one individually works for now. Eventually when i get more time, i might look into adding code to check each component, if the material is PURCHASED, to skip the code altogether. I use the material property of the compent to drive the IDW to send to the customer, so PURCHASED would be a bought component we buy for the project, so a customer wouldn't be building that to begin with, so that would save some time having to make parts visible to run the code.
I tried to pretty up the code a little, theres parts of it i dont quite understand yet, but this experience with you has helped me understand alot more of what is going on in the code., for example, IterateComponentsRecursively sub, i feel like the way its written it should, if it finds a component with the same name, skip, so it doesn't have to cycle through the rest of the code so it doesn't prompt to save a copy over the existing file, but i set those subs to skip as of now anyways, the only time i see this being an issue, is if i send out the final stp, dxf, and pdf, and end up making a change to a component, at that point i would need to manually save each as, but at the same time for this specific workflow, the STP and DXF is what the customer would receive to build, so it should be finalized anyways at this point to run the code.
But all in all, this is the full working code. THANK YOU SO MUCH!
Sub Main
'Make sure ASM document is active'
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
'Create folders in project workspace folder if not already there'
Dim oWSPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
oSTEPFilesFolder = oWSPath & "\STEP FILES"
If Not System.IO.Directory.Exists(oSTEPFilesFolder) Then
System.IO.Directory.CreateDirectory(oSTEPFilesFolder)
End If
oDXFFilesFolder = oWSPath & "\DXF FILES"
If Not System.IO.Directory.Exists(oDXFFilesFolder) Then
System.IO.Directory.CreateDirectory(oDXFFilesFolder)
End If
oPDFFilesFolder = oWSPath & "\PDF FILES"
If Not System.IO.Directory.Exists(oPDFFilesFolder) Then
System.IO.Directory.CreateDirectory(oPDFFilesFolder)
End If
'Loop through components and define the save as criteria'
oOccs = oADoc.ComponentDefinition.Occurrences
IterateComponentsRecursively(oOccs)
'After completion show exports complete'
MsgBox("All Exports Complete.", vbInformation, "")
'Close any referenced documents'
ThisApplication.Documents.CloseAll(True)
'Open workspace folder'
ThisDoc.Launch(ThisDoc.WorkspacePath)
End Sub
Dim oSTEPFilesFolder As String
Dim oDXFFilesFolder As String
Dim oPDFFilesFolder As String
Dim oProcessedDocs As List(Of PartDocument)
Sub IterateComponentsRecursively(oComps As ComponentOccurrences)
If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
For Each oComp As ComponentOccurrence In oComps
'Only process Visible components'
If oComp.Visible = False Then Continue For
'If component is Visible, and an Assembly, recursively process its components'
If oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
IterateComponentsRecursively(oComp.Definition.Occurrences)
End If
'Make sure it is a Part'
If oComp.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
If Not TypeOf oComp.Definition Is PartComponentDefinition Then Continue For
Dim oPDoc As PartDocument = oComp.ReferencedDocumentDescriptor.ReferencedDocument
If IsNothing(oProcessedDocs) Then oProcessedDocs = New List(Of PartDocument)
'If we have already processed this referenced document, skip to next component'
If oProcessedDocs.Contains(oPDoc) Then Continue For
Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oPDoc.FullFileName)
Dim oSTEPFullFileName As String = oSTEPFilesFolder & "\" & oFileName & ".stp"
'Find component referenced drawing file'
Dim oDrawDoc As DrawingDocument = GetDrawing(oPDoc)
If Not IsNothing(oDrawDoc) Then
'Start sub routines to loop through save copy of component as STP and referenced IDW to DXF and PDF'
ExportToSTEP(oPDoc, oSTEPFullFileName)
Dim oDXFFullFileName As String = oDXFFilesFolder & "\" & oFileName & ".dxf"
ExportIDWtoDXF(oDrawDoc, oDXFFullFileName)
Dim oPDFFullFileName As String = oPDFFilesFolder & "\" & oFileName & ".pdf"
ExportIDWtoPDF(oDrawDoc, oPDFFullFileName)
End If
Next
End Sub
'Export to STP sub'
Sub ExportToSTEP(oDoc As Document, oNewFileName As String)
Dim oSTEP As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById( _
"{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
If IsNothing(oSTEP) Then
MsgBox("STEP Translator Add-in not found. Exiting.", vbCritical, "")
'Logger.Debug("STEP Translator Add-in not found.")
Exit Sub
End If
'Create needed variables for translator'
oTO = ThisApplication.TransientObjects
oContext = oTO.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = oTO.CreateNameValueMap
oDataMedium = oTO.CreateDataMedium
'Exit sub if file extension exists otherwise continue save as process"
If System.IO.File.Exists(oNewFileName) Then
Exit Sub
End If
oDataMedium.FileName = oNewFileName
If oSTEP.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
' Set application protocol.
' 2 = AP 203 - Configuration Controlled Design
' 3 = AP 214 - Automotive Design
'oOptions.Value("ApplicationProtocolType") = 3
'oOptions.Value("IncludeSketches") = True
'oOptions.Value("export_fit_tolerance") = .000393701 'minimum
'oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
'oOptions.Value("Authorization") = ""
'oOptions.Value("Description") = iProperties.Value("Summary", "Title")
'oOptions.Value("Organization") = iProperties.Value("Summary", "Company")
Try
oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
Catch
MsgBox("Your attempt to export this document as a STEP file FAILED!", vbExclamation, "Export to STEP Error")
'Logger.Error("Export to STEP failed.")
End Try
End If
End Sub
'Find referenced IDW and continue save as process'
Function GetDrawing(oModelDoc As Document) As DrawingDocument
oWSPath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
Dim oDrawingsFolder As String = oWSPath & "\DRAWINGS"
oFileName = System.IO.Path.GetFileNameWithoutExtension(oModelDoc.FullFileName)
Dim oDrawingFile As String = oDrawingsFolder & "\" & oFileName & ".idw"
'Reference IDW but don't open'
If System.IO.File.Exists(oDrawingFile) Then
Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(oDrawingFile, False)
Return oDrawingDoc
Else
MsgBox("A Drawing File Named:" & vbCrLf & oFileName & vbCrLf & _
"Could not be found.", vbExclamation, "")
End If
Return Nothing
End Function
'Start IDW to DXF'
Sub ExportIDWtoDXF(oDrawing As DrawingDocument, oNewFullFileName As String)
Dim oDXF As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
If IsNothing(oDXF) Then
MsgBox("DXF Translator Add-in not found. Exiting.", vbCritical, "")
'Logger.Debug("DXF Translator Add-in not found.")
Exit Sub
End If
'Create needed variables for translator'
oTO = ThisApplication.TransientObjects
oContext = oTO.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = oTO.CreateNameValueMap
oDataMedium = oTO.CreateDataMedium
'Exit sub if file extension exists otherwise continue save as process"
If System.IO.File.Exists(oNewFullFileName) Then
Exit Sub
End If
oDataMedium.FileName = oNewFullFileName
'<<<< CHANGE THIS IF NEEDED, INI FILE CONFIGURATION LOCATION >>>>'
Dim oINI_File As String = "C:\Users\Public\Documents\Autodesk\Inventor 2021\Design Data\DWG-DXF\exportdxf.ini"
If Not System.IO.File.Exists(oINI_File) Then
MsgBox("Couldn't find this INI file: " & oINI_File & ". Exiting.", vbExclamation, "")
Exit Sub
End If
'Set save as options'
If oDXF.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
oOptions.Value("Export_Acad_IniFile") = oINI_File
End If
Try
oDXF.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
Catch oEx As Exception
MsgBox("SaveCopyAs method of the ExportIDWtoDXF Sub routine failed." & vbCrLf & _
"While trying to export the following Drawing file: " & vbCrLf & _
oDrawing.FullFileName & vbCrLf & _
"as the following dxf file: " & vbCrLf & _
oNewFileName & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "")
'Logger.Error(oEx.Message & vbCrLf & oEx.StackTrace)
oDrawing.ReleaseReference
End Try
End Sub
'Start IDW to PDF'
Sub ExportIDWtoPDF(oDrawing As DrawingDocument, oNewFullFileName As String)
'get the PDF Translator Add-in
Dim oPDFAddin As TranslatorAddIn
For Each oAddin As ApplicationAddIn In ThisApplication.ApplicationAddIns
If oAddin.DisplayName = "Translator: PDF" Then
oPDFAddin = oAddin
End If
Next
'Create the variables needed by the Translator Add-in
oTO = ThisApplication.TransientObjects
oContext = oTO.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = oTO.CreateNameValueMap
oDataMedium = oTO.CreateDataMedium
oDataMedium.FileName = oNewFullFileName
'Exit sub if file extension exists otherwise continue save as process"
If System.IO.File.Exists(oNewFullFileName) = True Then
Exit Sub
End If
oDataMedium.FileName = oNewFullFileName
'The following If-Then statement defines the Options available, and their Values.
If oPDFAddin.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
oOptions.Value("Publish_All_Sheets") = 1 ' 0 = False, 1 = True
'oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 1
'oOptions.Value("Custom_End_Sheet") = 4
oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
oOptions.Value("Vector_Resolution") = 720 '150, 200, 400, 600, 720, 1200, 2400, 4800 ' DPI
oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
oOptions.Value("Launch_Viewer") = 1 ' 0 = False, 1 = True
End If
'Publish PDF
oPDFAddin.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
End Sub