ilogic batch pdf from assembly

ilogic batch pdf from assembly

Skanda_UNI
Advocate Advocate
7,287 Views
28 Replies
Message 1 of 29

ilogic batch pdf from assembly

Skanda_UNI
Advocate
Advocate

Hello!

 

I use this piece of code:

http://inventortrenches.blogspot.de/2012/11/ilogic-batch-output-pdfs-from-assembly.html

 

Which works but the thing is; there are components of the code that are not integrated in ilogic since version 2018.

like the red marked sections below. (Or difference in ilogic and VBA thingy.)

If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then

'oOptions.Value("All_Color_AS_Black") = 0

oOptions.Value("Remove_Line_Weights") = 1

oOptions.Value("Vector_Resolution") = 400

oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets

'oOptions.Value("Custom_Begin_Sheet") = 2

'oOptions.Value("Custom_End_Sheet") = 4

End If

 

So i deleted this parts and the pdf creation works well.

it creates the files in the correct folders and so on. but then there is this error:

System.NullReferenceException: Ungültiger Zeiger (Ausnahme von HRESULT: 0x80004003 (E_POINTER))
bei Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
bei Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
bei LmiRuleScript.Main()
bei Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
bei iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

i guess this error has the same root.

Actually i wonder why the rule is perfectly working, except this error message.

 

Can anyone tell me which lines i have to delete to get rid of the message?

 

i looked that up, but the answers here are already included in my code: (callPDFAddin and so on)

https://forums.autodesk.com/t5/inventor-customization/inventor-2018-ilogic-error/td-p/7073741

https://forums.autodesk.com/t5/inventor-customization/ilogic-fail/td-p/6972701

 

My code looks like this now:

SyntaxEditor Code Snippet

'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 ( _
"This will create a PDF file for all of the asembly components that have drawings 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)

If RUsure = vbNo Then
Return
Else
End If

'- - - - - - - - - - - - -PDF setup - - - - - - - - - - - -
oPath = ThisDoc.Path
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

oOptions.Value("All_Color_AS_Black") = 1
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

'get PDF target folder path
oFolder = oPath & "\" & oAsmName & " PDF Files"

'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)
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) -3)

            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 & "pdf"
                'Write out the PDF
                Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            'close the file
                oDrawDoc.Close
Else
'If the model has no drawing of the same path and name - do nothing
End If
Next
'- - - - - - - - - - - - -

'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
oAsmDrawing = ThisDoc.ChangeExtension(".idw")
oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
'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 & "pdf"
'Write out the PDF
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
'Close the top level drawing
oAsmDrawingDoc.Close
'- - - - - - - - - - - - -

MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new ffiles are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)

Thank you very much.

 

0 Likes
Accepted solutions (1)
7,288 Views
28 Replies
Replies (28)
Message 21 of 29

Anonymous
Not applicable

That what i thought it suppose to do, but it does't work with 2019 version and i can't see why ??

0 Likes
Message 22 of 29

ThomasB44
Mentor
Mentor

What is your attempt ?

As the code is running, you will not have time to see the drawings, even if they are closed at the end (so outside the For each / Next)

You could simply suppress the line oDrawDoc.Close and close them manually ?


Thomas
Mechanical Designer / Inventor Professional 2025
Inventor Professional EESignature

0 Likes
Message 23 of 29

Anonymous
Not applicable

Inventor 2019 do not recognize "oDrawDoc.Close" suppressed or not i have to close all idw manually

0 Likes
Message 24 of 29

Anonymous
Not applicable
Ok just found what was missing for closing all files by adding (True)

' For Each oRefDoc In oRefDocs = oAsmDoc.AllReferencedDocuments
For Each oRefDoc In oAsmDoc.AllReferencedDocuments idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentNaMe) - 3) & "idw" If(System.IO.File.Exists(idwPathName)) Then Dim oDrawDoc As DrawingDocument oDrawDoc = ThisApplication.Documents.Open(idwPathName, True) oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3) On Error Resume Next oDataMedium.FileName = oFolder & "\" & oFileName & "pdf" Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium) oDrawDoc.Close(True) On Error Goto 0 End If Next

 

0 Likes
Message 25 of 29

Anonymous
Not applicable

Hi!

 

I am trying to use this  code and it seems to work well - there are no errors occuring when rule is working,

it opened and closed all of the drawings and even box message poped up, that PDF files are in folder (here is path to this folder), and it even opens this folder, but... there is nothing inside. The rule get all trough the process, but it didn't save the files.

 

Anyone who can help?

 

Edit.: I work on Inventor 2018 version.

 

0 Likes
Message 26 of 29

aronmatheus
Advocate
Advocate

How choose the specific sheets?

0 Likes
Message 27 of 29

aronmatheus
Advocate
Advocate

How choose the specific sheets?

0 Likes
Message 28 of 29

MANOLO007565
Explorer
Explorer

Hi  Skanda_UNI, here the code, I modified some lines. The code run correctly.

Sub main()
Call Pdfcreator()
Call message()
End Sub


Sub Pdfcreator()
	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
	If MessageBox.Show( _
		"This will create a PDF file for all of the asembly components that have drawings 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 = oPath & "\" & " PDF Files"

	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If

	'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -'look at the files referenced by the assembly
	Dim oRefDoc As Document

	For Each oRefDoc In oAsmDoc.AllReferencedDocuments

				idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "idw"

			If (System.IO.File.Exists(idwPathName)) Then
				Dim oDrawDoc As DrawingDocument
				oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
				oFileName = oRefDoc.DisplayName

				On Error Resume Next
				oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf"
				Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
				oDrawDoc.Close
			
		End If
	Next
	'- - - - - - - - - - - - -

	'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
	Dim oAsmDrawingDoc As DrawingDocument
	oAsmDrawingDocName = ThisDoc.PathAndFileName(False) & ".idw"

	If (System.IO.File.Exists(oAsmDrawingDocName)) Then
		oAsmDrawingDoc = ThisApplication.Documents.Open(ThisDoc.ChangeExtension(".idw"), True)

		oAsmDrawingName = oAsmDrawingDoc.DisplayName

		On Error Resume Next
		oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & ".pdf"
		Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
		oAsmDrawingDoc.Close
	End If

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("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

Sub message()

	MessageBox.Show("All pdfs were created, review the PDF FOLDER", "PDF creator", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

 

0 Likes
Message 29 of 29

MANOLO007565
Explorer
Explorer

Hi  Skanda_UNI, here the code, I modified some lines. The code run correctly.

Sub main()
Call Pdfcreator()
Call message()
End Sub


Sub Pdfcreator()
	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
	If MessageBox.Show( _
		"This will create a PDF file for all of the asembly components that have drawings 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 = oPath & "\" & " PDF Files"

	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If

	'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -'look at the files referenced by the assembly
	Dim oRefDoc As Document

	For Each oRefDoc In oAsmDoc.AllReferencedDocuments
			idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "idw"

			If (System.IO.File.Exists(idwPathName)) Then
				Dim oDrawDoc As DrawingDocument
				oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
				oFileName = oRefDoc.DisplayName

				On Error Resume Next
				oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf"
				Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
				oDrawDoc.Close
			
		End If
	Next
	'- - - - - - - - - - - - -

	'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
	Dim oAsmDrawingDoc As DrawingDocument
	oAsmDrawingDocName = ThisDoc.PathAndFileName(False) & ".idw"

	If (System.IO.File.Exists(oAsmDrawingDocName)) Then
		oAsmDrawingDoc = ThisApplication.Documents.Open(ThisDoc.ChangeExtension(".idw"), True)

		oAsmDrawingName = oAsmDrawingDoc.DisplayName

		On Error Resume Next
		oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & ".pdf"
		Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
		oAsmDrawingDoc.Close
	End If

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("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

Sub message()

	MessageBox.Show("All pdfs were created, review the PDF FOLDER", "PDF creator", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub