Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ilogic batch export pdf from assembly drawing

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Skanda_UNI
6720 Views, 9 Replies

ilogic batch export pdf from assembly drawing

Hello!

 

Here in this topic i try to create the pdf files but this just works for an opened assembly:

https://forums.autodesk.com/t5/inventor-forum/ilogic-batch-pdf-from-assembly/td-p/7325924

 

Now i wonder if this would also be possible from the assembly idw file.

 

this is not a code request, i just want to know if this is actually possible or if i have to think in a different way.

 

I would like to use ilogic to open the underlying assembly of a drawing this code:

ThisDoc.Save
NewFileName = ThisDoc.ChangeExtension(".idw")
ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd").Execute
'open the indexed file, false opens the file without generating the graphics
ThisApplication.Documents.Open(NewFileName, True) 

then i would run this code:

https://forums.autodesk.com/t5/inventor-forum/ilogic-batch-pdf-from-assembly/td-p/7325924

 

And close the assembly.

 

Is this even possible or do i need to approach it differently.

 

thank you.

 

9 REPLIES 9
Message 2 of 10
MechMachineMan
in reply to: Skanda_UNI

If you test the .AllReferencedDocuments.Count of the assembly document, and a drawing file that only contains that assembly, you'll see it's the same number with 1 doc difference (being the top level assembly actually being used in the drawing).

 

Seeing as the drawing document has all of the documents open to begin with, that collection can just be iterated through.

 

This is your old code, but tweaked to utilize the GENERIC document object instead of using the explicitly typed objects. Allows you to use it in both cases with 1 rule.

 

 

Sub Main()
	Dim oDoc As Document
	oDoc = ThisDoc.Document
	oDocName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) -4)
	
	If Not (oDoc.DocumentType = kAssemblyDocumentObject Or oDoc.DocumentType = kDrawingDocumentObject) Then
		MessageBox.Show("Please run this rule from the assembly or drawing files.", "iLogic")
		Exit Sub
	End If
	
	'get user input
	If MessageBox.Show ( _
		"This will create a PDF file for all of the files referenced by this document 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 referenced documents?" _
		& 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)
	
	oFolder = ThisDoc.Path & "\" & oDocName & " PDF Files"
	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If
	
	'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
	Dim oRefDoc As Document
	Dim oDrawDoc As DrawingDocument
	
	For Each oRefDoc In oDoc.AllReferencedDocuments
		oBaseName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentNaMe) - 4)
		
		If(System.IO.File.Exists(oBaseName & ".idw")) Then
			oDrawDoc = ThisApplication.Documents.Open(oBaseName & ".idw", True)

			On Error Resume Next
				oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
				Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
				oDrawDoc.Close
			On Error Goto 0
		Else
			oNoDwgString = oNoDwgString & vbLf & idwPathName
		End If
	Next
	'- - - - - - - - - - - - -
	
	'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
	If oDoc.DocumentType = kAssemblyDocumentObject Then
		oDrawDoc = ThisApplication.Documents.Open(oDocName & ".idw", True)
		
		On Error Resume Next
			oDataMedium.FileName = oFolder & "\" & oDocName & "pdf"
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
			oAsmDrawingDoc.Close
	ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
		oDataMedium.FileName = oFolder & "\" & oDocName & "pdf"
		Call PDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)	
	End If
	'- - - - - - - - - - - - -
	
	MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
	MsgBox("Files found without drawings: " & vbLf & oNoDwgString)
	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)

	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") = 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

	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 10
Skanda_UNI
in reply to: MechMachineMan

Thank you very much. Did not expect to get an answer here. Unfortunately this code doesn't generate the pdf files due to this error.

 

System.ArgumentException: Falscher Parameter. (Ausnahme von HRESULT: 0x80070057 (E_INVALIDARG))

System.ArgumentException: Wrong parameter. (Exception of HRESULT: 0x80070057 (E_INVALIDARG)) [TRANSLATION]
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.Documents.Open(String FullDocumentName, Boolean OpenVisible)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

It runs through though. So it opens each underlying idw and does something but no pdf is generated.

 

https://forums.autodesk.com/t5/inventor-customization/error-message-the-parameter-is-incorrect-excep...

I looked it up here, but could not figure it out.

 

As the other script works perfectly, this has not the highest priority.

 

thank you for your answer anyway.

 

 

 

 

Message 4 of 10
MechMachineMan
in reply to: Skanda_UNI

How to debug:

*Note: this applies to the iLogic environment only; there are much more efficient ways to debug in different languages/environments - ie; the Visual Studio debugger.  However, this requires the routine to be written in whatever language the visual studio you have works with.

 

Original Code:

'Line 1
'Line 2
'Line 3
'Line 4

Lets pretend this generates an error.

 

The first step in debugging is to isolate the incorrect line.

- How we do this is by using a message box to establish the LAST WORKING LINE OF CODE. We can assume the line after generates the error. This is verified by moving the message box below that line.

 

Lets start with the MsgBox() in the middle; using the ideas from a binary search, to narrow the amount of time it takes us to find the errored lined.

 

ITERATION 1 New code:

'Line 1
'Line 2
MsgBox("Here") 'Line 3 'Line 4

Result:

- Shows message box, followed by an error.

 

Conclusion:

- The error is either line 3 or 4, because if it was 1 or 2 we wouldn't see the message box. Thus, we move the message box down to try and make it fail before we see the message box.

 

ITERATION 2 New code:

'Line 1
'Line 2
'Line 3
MsgBox("Here") 'Line 4

 

Result:

- Does not show message box, shows error.

 

Conclusion:

- From iteration 1, we know it's line 3 or 4 that causes the error, from iteration 3, we know it occurs before line 4; thus we can say line 3 is the line that gives us errors.

- From here, we can google and search for things specifically related to that line to establish where the error is coming from.

 

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 5 of 10
Skanda_UNI
in reply to: MechMachineMan

OK. This will take me a while to test. Thank you very much.

Message 6 of 10
MechMachineMan
in reply to: Skanda_UNI

The "On Error" clauses were hiding issues with the file name & path.

 

SyntaxEditor Code Snippet

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 drawing files.", "iLogic")
        Exit Sub
    End If
    
    'get user input
    If MessageBox.Show ( _
        "This will create a PDF file for all of the files referenced by this document 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 referenced documents?" _
        & 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)
    
    oFolder = oDocName & " PDF Files"
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
    '- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
    Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
    
    For Each oRefDoc In oDoc.AllReferencedDocuments
        oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
        oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            oDrawDoc.Close
        Else
            oNoDwgString = oNoDwgString & vbLf & idwPathName
        End If
    Next
    '- - - - - - - - - - - - -
    
    '- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
    oBaseName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    oPathAndName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & oBaseName
    oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
    
    If oDoc.DocumentType = kAssemblyDocumentObject Then
        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
        Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
        oDrawDoc.Close
    ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
        Call PDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)    
    End If
    '- - - - - - - - - - - - -
    
    MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
    MsgBox("Files found without drawings: " & vbLf & oNoDwgString)
    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)

    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") = 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

    oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 7 of 10
Skanda_UNI
in reply to: MechMachineMan

So no i had time to test this code but it says that it has unknown document in it.

 

Fehler in Regel: PDF_Batch_Export_IDW in Dokument: unknown document

Unbekannter Fehler (Ausnahme von HRESULT: 0x80004005 (E_FAIL))

 

System.Runtime.InteropServices.COMException (0x80004005): Unbekannter Fehler (Ausnahme von HRESULT: 0x80004005 (E_FAIL))
bei System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
bei Inventor._DocumentClass.get_FullFileName()
bei LmiRuleScript.Main()
bei Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
bei iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

 

But you need to care about it. you already told me how to debug... but i dont have the time for now. Maybe someone else can help with this issue.

 

Furthermore I have a question:

Is there a possibility to just export those files which are listed in the BOM. actually i took this code from a topic with this name. Maybe you know the parameter which has to be altered to change the code to Just-BOM-Parts export.

 

Thank you anyway

Message 8 of 10
MechMachineMan
in reply to: Skanda_UNI

1. Are you ensuring all files are saved before running this rule?

 

2. Manually debug and find out what file is causing the issue, since I'm hoping its not all of them.

 

3. Ensure your windows is set to SHOW file extensions of known file types.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 9 of 10

I know this is an old post, but maybe you can still answer -- what would I have to add where to add the revision to the resulting PDFs?

 

So the existing [FILENAME].pdf would become [FILENAME] R#.pdf, where the # is the revision of the drawing file itself.

Message 10 of 10


@claudio.ibarra wrote:

I know this is an old post, but maybe you can still answer -- what would I have to add where to add the revision to the resulting PDFs?

 

So the existing [FILENAME].pdf would become [FILENAME] R#.pdf, where the # is the revision of the drawing file itself.


You can reference the Revision iProperty with

 

dim oRev as property
oRev = oDrawDoc.PropertySets.item("Summary Information").item("Revision Number")

 

then replace 

 

oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"

 

 with

 

oDataMedium.FileName = oFolder & "\" & oBaseName & " R" & cStr(oRev.Value) & ".pdf"

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report