OKAY
So again sorry for all the back and forth on this, again , BUT, i had A LOT of time this weekend to experiment and test some things out, so i think i have successfully figured it out. I ended up finding a thread you were commenting on, using a message box to return a boolean value, so after alot of trial and error i got it to work that way. If you could maybe skim over it, and see if you see anything wrong with it, as far as i can tell across the couple smaller assemblies i have tested it on, it seems to work 100%, and now the code steps through a yes no process, asking if you want to save each found component with a referenced IDW, and also to print, if yes to print it goes through the print settings, if print settings are incorrect and NO is selected cancels the whole rule. If yes on any of the save as variables, creates the needed folder and continues, and ANY visible component found without an IDW, it will add the name to a list at the end.
----------------------IM SO EXCITED, AND THANK YOU SO MUCH FOR THE HELP!!!!!! If you see anything that should be added, or any kind of fail proofing or anything that would help let me know.
*EDIT, if and when you get a moment, i have been trying to get this same code to work, but from a part file instead of assembly.. Say for example the code is ran and batch sent all the needed files, later on i find a missing hole on a part, so now i need a new STP, and new drawing, i would like to set this same rule, but only run from one part, having a hard time getting the code to recognize the reference drawing from the component i think, something isnt communicating properly. But essentially i would change each of the save as parameters so save over an existing instead of exiting sub. If you could work the definition into finding the reference drawing instead of doing it from as assembly that would be great.
Full working code with all the Yes No variables
Sub Main
'Rule only finds visible components within main assembly that have a referenced IDW, so will only process a file if an IDW of a visible component is found
'If sub-assembly is found, only finds visible components from the default saved assembly of said sub-assembly, can set visibily and save assembly of needed components
'If no referenced IDW is found for any visible component, add name to list and export list to Notepad
'Export list is to verify if a needed component was missed that needs an IDW and export
'If export file is found with the same name, skip save as process of component and or IDW
'Due to this rule only finding visible components and their referenced drawings, it will not print or export any assembly files or IDWs
'This rule expects the reference IDWs are in "DRAWINGS" folder in the root workspace folder of the project
'dschulte 07/31/2022
'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
'Set export file paths
Dim oWSPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
oSTEPFilesFolder = oWSPath & "\STEP FILES"
oPDFFilesFolder = oWSPath & "\PDF FILES"
oDXFFilesFolder = oWSPath & "\DXF FILES"
'Yes or No selection to save as and print
'If yes, create folder for each export, and prompt to verify printer settings
'If System Printer Settings are incorrect and select No, will cancel the rule all together
STP = MessageBox.Show("Would you like to create a .STP for every visible component?", "Create .STP?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If STP = vbNo Then
oSTP = True
Else
System.IO.Directory.CreateDirectory(oSTEPFilesFolder)
End If
PDF = MessageBox.Show("Would you like to create a .PDF of each reference drawing?", "Create .PDF?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If PDF = vbNo Then
oPDF = True
Else
System.IO.Directory.CreateDirectory(oPDFFilesFolder)
End If
DXF = MessageBox.Show("Would you like to create a .DXF of each reference drawing?", "Create .DXF?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If DXF = vbNo Then
oDXFF = True
Else
System.IO.Directory.CreateDirectory(oDXFFilesFolder)
End If
PRINTS = MessageBox.Show("Would you like to print each IDW?", "Create PRINT?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If PRINTS = vbNo Then
oPRINT = True
Else
PrintSetup(Setup)
End If
If oPrintSettings = True Then
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
'Loop through visible components and define the save as criteria'
oOccs = oADoc.ComponentDefinition.Occurrences
IterateComponentsRecursively(oOccs)
'Show number of found referenced drawings
MessageBox.Show ("There were " & numFiles & " referenced drawings found."," Job Complete ")
'Close any referenced documents'
ThisApplication.Documents.CloseAll(True)
'Open workspace folder'
ThisDoc.Launch(ThisDoc.WorkspacePath)
'Export list of missing IDWs to text'
WriteListOfStringsToTextFile(oNames)
End Sub
Sub PrintSetup(Setup)
'Show default printer and print settings
Dim oPrintMgr As PrintManager = ThisApplication.ActiveDocument.PrintManager
If MsgBox("Using Printer " & oPrintMgr.Printer & ", Letter Size, 1:1 Scale, Landscape Mode." _
& vbLf & " " _
& vbLf & " Do you want to continue?" _
, vbYesNo + vbQuestion, "System Printer Settings ") = vbNo Then
oPrintSettings = True
End if
If oPrintSettings = True Then
Exit Sub
End If
'Determine if the prints should be in color or black and white
oColorAsBlack="False"
RUsure = MessageBox.Show ( "Do you want to print in COLOR?"& vbLf & "", "Color or B/W Prints",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
oColorAsBlack="True" : Else: End If
End Sub
'Write list of missing IDWs to text'
Sub WriteListOfStringsToTextFile(oStrings As List(Of String))
'write List contents (Strings) to a Text file
Dim oTxtFile As String = "C:\Temp\MISSINGS IDWs.txt"
Dim oWriter As System.IO.StreamWriter = System.IO.File.CreateText(oTxtFile)
For Each oString In oStrings
oWriter.WriteLine(oString)
Next
oWriter.Close
ThisDoc.Launch(oTxtFile)
End Sub
Dim oSTEPFilesFolder As String
Dim oDXFFilesFolder As String
Dim oPDFFilesFolder As String
Dim oNames As New List(Of String)
Dim oProcessedDocs As List(Of PartDocument)
Dim numFiles As Integer = 0
Dim oSTP As Boolean = False
Dim oPDF As Boolean = False
Dim oDXFF As Boolean = False
Dim oPRINT As Boolean = False
Dim oPrintSettings As Boolean = False
Sub IterateComponentsRecursively(oComps As ComponentOccurrences)
If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
'initialize the List(Of PartDocument) if it has not already been initialized
'when initialized (using New), variable's Value will no longer be Nothing, but List will not contain any items yet)
'if previously initialized (using New), the List may already contain items, so don't re-create the List
If IsNothing(oProcessedDocs) Then oProcessedDocs = New List(Of PartDocument)
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 we have already processed this referenced document, skip to next component
'(this next line will only work properly if you are adding the PartDocument to the List after 'processing' it)
If oProcessedDocs.Contains(oPDoc) Then Continue For
Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oPDoc.FullFileName)
Dim oWSPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
'Find component referenced drawing file
Dim oDrawDoc As DrawingDocument = GetDrawing(oPDoc)
If Not IsNothing(oDrawDoc) Then
numFiles = numFiles + 1
Dim oSTEPFullFileName As String = oSTEPFilesFolder & "\" & oFileName & ".stp"
ExportToSTEP(oPDoc, oSTEPFullFileName)
If oPRINT = False Then
PrintIDW(oDrawDoc)
Else
End If
Dim oDXFFullFileName As String = oDXFFilesFolder & "\" & oFileName & ".dxf"
ExportIDWtoDXF(oDrawDoc, oDXFFullFileName)
Dim oPDFFullFileName As String = oPDFFilesFolder & "\" & oFileName & ".pdf"
ExportIDWtoPDF(oDrawDoc, oPDFFullFileName)
oDrawDoc.Close(True)
End If
'now that this PartDocument has been 'processed', add it to the List(Of PartDocument)
'so that it will be found in our check above and will not be processed again
oProcessedDocs.Add(oPDoc)
Next
End Sub
Sub PrintIDW(oDrawing As DrawingDocument)
oDrawing.Activate
oDrawing.Save
oDrgPrintMgr = oDrawing.PrintManager
oDrgPrintMgr.AllColorsAsBlack = oColorAsBlack
oPrintMgr = ThisApplication.ActiveDocument.PrintManager
oPrintMgr.PrintRange = kPrintAllSheets 'Prints all sheets in the idw.
oPrintMgr.NumberOfCopies = 1 ' Set to print one copies.
oPrintMgr.PaperSize = kPaperSizeCustom
oPrintMgr.PaperHeight = 27.94 '11.0 in
oPrintMgr.PaperWidth = 21.59 '8.5 in
oPrintMgr.Scalemode = 13825 'Full Scale print @1:1
oPrintMgr.SubmitPrint ' Submit the print.
End Sub
'Export to STP sub'
Sub ExportToSTEP(oDoc As Document, oNewFileName As String)
If oSTP = True Then : Exit Sub : End If
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, needs to open to be able to print'
If System.IO.File.Exists(oDrawingFile) Then
Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(oDrawingFile, True)
Return oDrawingDoc
Else
'Add name of component with no IDW to list'
oNames.Add(oFileName)
End If
Return Nothing
End Function
'Start IDW to DXF'
Sub ExportIDWtoDXF(oDrawing As DrawingDocument, oNewFullFileName As String)
If oDXFF = True Then : Exit Sub : End If
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)
If oPDF = True Then : Exit Sub : End If
'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