Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
cmcconnell
in reply to: MechMachineMan

Thanks for the response!

 

You are speaking a bit over my head. Here is what I did:

  1. I modified the line you provided for my oRevNum variable and placed that in the component drawings area. It makes oRevNum = nothing. SO I end up with filename-R.pdf.
  2. This is over my head. :disappointed_face:
  3. When I put theses 2 lines in, where you suggest, I get an error when I save the rule:  'Application' is ambiguous, imported from the namespaces or types 'Inventor, System.Windows.Forms'.
  4. Adding (true) to the close operations seems to have fixed the leaving drawings open issue.

Here is what my code looks like now:

Sub Main()
	Dim oDoc As Document
    oDoc = ThisDoc.Document
    oDocName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    
    If Not (oDoc.DocumentType = kAssemblyDocumentObject Or oDoc.DocumentType = kDrawingDocumentObject) Then
        MessageBox.Show("Please run this rule from the assembly or assembly drawing file.", "iLogic")
        Exit Sub
    End If
	
	Application.SilentOperation = True
		'get user input
		If MessageBox.Show ( _
		"This will create a PDF file for all of the assembly components that have drawing files." _
		& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
		& vbLf & " " _
		& vbLf & "Are you sure you want to create PDF Drawings for all of the assembly components?" _
		& vbLf & "This could take a while.", "iLogic - Batch Output PDFs ",MessageBoxButtons.YesNo) = vbNo Then
		Exit Sub
		End If
		Dim PDFAddIn As TranslatorAddIn
		Dim oContext As TranslationContext
		Dim oOptions As NameValueMap
		Dim oDataMedium As DataMedium
		Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
	
		oPath = ThisDoc.Path
		oFolder = "C:\PDF" & "\" & oAsmName & " PDF Files\"
		oPath = System.IO.Directory.GetParent(oPath).FullName 		
		
		If System.IO.Directory.Exists(oFolder) = False Then
			System.IO.Directory.CreateDirectory(oFolder)
		End If
		
		'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -'look at the files referenced by the assembly
		Dim oRefDoc As Document
		Dim fileName As String
		
		For Each oRefDoc In oDoc.AllReferencedDocuments
		oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) - 4)		
			
			For Each fileName In System.IO.Directory.GetFiles(oPath, "*.idw",System.IO.SearchOption.AllDirectories)
				If fileName.EndsWith(oFileName + ".idw") = True Then
					Dim oDrawDoc As DrawingDocument
					oDrawDoc = ThisApplication.Documents.Open(fileName, True)				
					On Error Resume Next
					oRevNum = iProperties.Value(System.IO.Path.GetFileName(fileName), "Project", "Revision Number")
					oDataMedium.FileName = oFolder & "\" & oFileName & "-R" & oRevNum & ".pdf"
					Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
					oDrawDoc.Close(True)
					On Error GoTo 0
				End If
			Next		
		Next
		'- - - - - - - - - - - - -
		
		'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
		Dim oAsmDrawingName As String = ThisDoc.FileName(False) 'without extension	

		For Each fileName In System.IO.Directory.GetFiles(oPath, "*.idw", System.IO.SearchOption.AllDirectories)	

			If fileName.EndsWith(oAsmDrawingName + ".idw") = True Then
				Dim oAsmDrawingDoc As DrawingDocument
				oAsmDrawingDoc = ThisApplication.Documents.Open(fileName, True)
				oAsmRevNum = iProperties.Value("Project", "Revision Number")
				On Error Resume Next
				oDataMedium.FileName = oFolder & oAsmDrawingName & "-R" & oAsmRevNum & ".pdf"		

				Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
				oAsmDrawingDoc.Close(True)
				On Error GoTo 0
			End If
		Next	
	
	Application.SilentOperation = False
	MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
	Shell("explorer.exe " & oFolder,vbNormalFocus)
End Sub

Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)
	oPath = ThisDoc.Path
	PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 0
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	'oOptions.Value("Custom_Begin_Sheet") = 1
	'oOptions.Value("Custom_End_Sheet") = 1
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub

 

Mechanix Design Solutions inc.