Export PDF seperate iLogic not working

Export PDF seperate iLogic not working

GKPByDesign
Advocate Advocate
1,151 Views
14 Replies
Message 1 of 15

Export PDF seperate iLogic not working

GKPByDesign
Advocate
Advocate

I have this iLogic code that takes a multi sheet file and exports each sheet, taking the sheet name, and then exporting them as seperate documents,

 

The code works great, however it is now not working, and I have no idea why ! It simply show the message box, OPDF Export Complete! But nothing happens. 

 

Can someone help 

 

'['Sub Variables
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments
Dim oDoc As Document
oFolder = ThisDoc.Path
oAddIns = ThisApplication.ApplicationAddIns
oTG = ThisApplication.TransientObjects']
'['PDF Export
'['PDF Options
PDFAddIn = oAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = oTG.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = oTG.CreateNameValueMap
oDataMedium = oTG.CreateDataMedium
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet']
For Each oDoc In oRefDocs
	idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) -3) & "idw"
	If System.IO.File.Exists(idwPathName) Then
		oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
		For Each oSheet As Sheet In oDrawDoc.Sheets
			oSheet.Activate
			oDataMedium.FileName = oFolder + oSheet.Name + ".pdf"
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Next
		oDrawDoc.Close(True)
	End If
Next']
MessageBox.Show("PDF Export completed.", "PDF Export", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
0 Likes
Accepted solutions (1)
1,152 Views
14 Replies
Replies (14)
Message 2 of 15

GKPByDesign
Advocate
Advocate

Also when it does work it doesn't make a unique name on sheets of the same name. 

0 Likes
Message 3 of 15

Sergio.D.Suárez
Mentor
Mentor

Hi, I make a suggestion. Have you saved your idw file before executing the rule? because it takes the path of the active file, and if it is not saved the route will be empty, and the saved can not be made. On the other hand, the dialog box should be inside the if-then-end if
Immediately after oDrawDoc.Close (True)
In this way, if this condition is met, the export will have been created.
I hope this helps greetings


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 4 of 15

CCarreiras
Mentor
Mentor

 

Hi!

 

Well, I can create the files, but, i can't wonder why I can't achieve the pdf extension.

I change the file save location to the project folder, and i disable the close document (i guess is not a good idea to close a document from a rule this inside the document).

So, the files will be created in the project folder.

 

'['Sub Variables
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments
Dim oDoc As Document
oFolder = ThisDoc.WorkspacePath()
oAddIns = ThisApplication.ApplicationAddIns
oTG = ThisApplication.TransientObjects']
'['PDF Export
'['PDF Options
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")'oAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = oTG.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium


'oOptions = oTG.CreateNameValueMap
'oDataMedium = oTG.CreateDataMedium
'Options
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet']

For Each oDoc In oRefDocs
	idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) -3) & "dwg"
	If System.IO.File.Exists(idwPathName) Then
		oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
		
		For Each oSheet As Sheet In oDrawDoc.Sheets
			oSheet.Activate
			
			SavePath= oFolder & oSheet.Name 
			
			oDataMedium.FileName = SavePath & ".pdf"
			
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Next
		'oDrawDoc.Close(True)
		MessageBox.Show("PDF Export completed.", "PDF Export", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
	End If
Next']

 

CCarreiras

EESignature

0 Likes
Message 5 of 15

clutsa
Collaborator
Collaborator

I added a "\" in the filepath and did a replace on the sheetname so colons would be dashes... see if that helps.

'['Sub Variables
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments
Dim oDoc As Document
oFolder = ThisDoc.Path
oAddIns = ThisApplication.ApplicationAddIns
oTG = ThisApplication.TransientObjects']
'['PDF Export
'['PDF Options
PDFAddIn = oAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = oTG.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = oTG.CreateNameValueMap
oDataMedium = oTG.CreateDataMedium
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet']
For Each oDoc In oRefDocs
	idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) -3) & "idw"
	If System.IO.File.Exists(idwPathName) Then
		oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
		For Each oSheet As Sheet In oDrawDoc.Sheets
			oSheet.Activate
			oDataMedium.FileName = oFolder & "\" & Replace(oSheet.Name, ":","-") & ".pdf"
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Next
		oDrawDoc.Close(True)
	End If
Next']
MessageBox.Show("PDF Export completed.", "PDF Export", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 6 of 15

CCarreiras
Mentor
Mentor

HI!

 

It works!!

Just removed the close doc to avoid the final errors

CCarreiras

EESignature

0 Likes
Message 7 of 15

GKPByDesign
Advocate
Advocate

I am still have an issue with the attached drawing, my original code work with older drawings create a few weeks ago, but I have had a chance to check it on new files, is any unable to run a rebuff or something on why its not working? 

0 Likes
Message 8 of 15

clutsa
Collaborator
Collaborator

I only see one drawing in your zip file (I can't open them because I'm still rocking INV2018) don't there have to be drawings with the same name as the sub parts to run?

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 9 of 15

GKPByDesign
Advocate
Advocate

not suer what you mean, and I can't backside the files. Basically the code was working and now its not? What can I do to the code to debug or show error? 

0 Likes
Message 10 of 15

clutsa
Collaborator
Collaborator

You can add message boxes like below to check your variables... as you're on 2019.1 or newer you can also use the logger... I can't show you that but https://knowledge.autodesk.com/support/inventor-products/learn-explore/caas/CloudHelp/cloudhelp/2019... might help.

'['Sub Variables
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments
Dim oDoc As Document
oFolder = ThisDoc.Path
oAddIns = ThisApplication.ApplicationAddIns
oTG = ThisApplication.TransientObjects']
'['PDF Export
'['PDF Options
PDFAddIn = oAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = oTG.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = oTG.CreateNameValueMap
oDataMedium = oTG.CreateDataMedium
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet']
For Each oDoc In oRefDocs
	idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) -3) & "idw"
	If System.IO.File.Exists(idwPathName) Then
		MessageBox.Show("idwPathName = " & idwPathName, "Title")
		oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
		For Each oSheet As Sheet In oDrawDoc.Sheets
			oSheet.Activate
			oDataMedium.FileName = oFolder & "\" & Replace(oSheet.Name, ":","-") & ".pdf"
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Next
		oDrawDoc.Close(True)
	End If
Next']
MessageBox.Show("PDF Export completed.", "PDF Export", MessageBoxButtons.O
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 11 of 15

clutsa
Collaborator
Collaborator

@GKPByDesign Sorry I was in a bit of a hurry yesterday. My guess is there is no error being thrown your program just isn't finding anything to PDF. From what I got debugging your code is

1. It starts with an assembly file and looks at all parts/sub-components associated to that assembly

2. It looks for any idw's in the same directory as the main assembly that have the same name as any of the parts/subs

3. If an idw is found it opens that idw and prints a PDF for each sheet to the same directory as the main assembly

4. Continues loop of 2 and 3 till all parts have been looked at.

From what I saw in your zip file you didn't have any drawings that were named like any of the parts (all I saw was a "drawing2.idw" but no parts named "drawing2.ipt" or "drawing2.iam" so your code will never open drawing2 and thus no PDFs. 

The bit of code I added will open a messagebox with an "OK" button to click if it finds any drawings to open... if you don't get that message then my assumption is correct. 

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 12 of 15

GKPByDesign
Advocate
Advocate

So the error was that the top assembly was as you mentioned a different name to the drawing, for the code to work this needs to be the same. So thank you for this. My other point is I want the code to label the file name as sheet only, but if there is a file of the same name to give it a unique name, eg 1, 2, 3 but only if there is conflict. I don't want every sheet to have their sheet number on the PDF name. 

0 Likes
Message 13 of 15

GKPByDesign
Advocate
Advocate

Now my original code isn't working and is giving this error

 

Error in rule: gkp-Export PDF - Seperate, in document: 334 - 01-00 - GA - Vanity Units.idw

 

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

   at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)

   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)

   at ThisRule.Main()

   at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)

   at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes
Message 14 of 15

clutsa
Collaborator
Collaborator
Accepted solution

This isn't the cleanest code ever but might work.

'['Sub Variables
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments
Dim oDoc As Document
oFolder = ThisDoc.Path
oAddIns = ThisApplication.ApplicationAddIns
oTG = ThisApplication.TransientObjects']
'['PDF Export
'['PDF Options
PDFAddIn = oAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = oTG.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = oTG.CreateNameValueMap
oDataMedium = oTG.CreateDataMedium
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet']
For Each oDoc In oRefDocs
	idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) -3) & "idw"
	If System.IO.File.Exists(idwPathName) Then
		oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
		For Each oSheet As Sheet In oDrawDoc.Sheets
			NameUnique = False
			Count = 1
			oSheet.Activate
			oDataMedium.FileName = oFolder & "\" & Left(oSheet.Name,Len(oSheet.Name) - (Len(oSheet.Name) - InStr(oSheet.Name, ":")) - 1) & ".pdf"
			TempName = oDataMedium.FileName
			Do Until NameUnique = True
				If System.IO.File.Exists(TempName) Then 
					TempName = Left(oDataMedium.FileName,Len(oDataMedium.FileName)-4) & count & ".pdf"
					count = count + 1
						If count > 100 Then NameUnique = True 'stop infinite loop
				Else
					NameUnique = True
					oDataMedium.FileName = TempName
				End If
			Loop
			'MessageBox.Show("oDataMedium.FileName = " & oDataMedium.FileName, "Title")

			'oDataMedium.FileName = oFolder & "\" & Replace(oSheet.Name, ":","-") & ".pdf"
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Next
		oDrawDoc.Close(True)
	End If
Next']
MessageBox.Show("PDF Export completed.", "PDF Export", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 15 of 15

clutsa
Collaborator
Collaborator

When you say original code what do you mean? Is there more code then what we've been talking about or did you try to make the changes above and it didn't work?

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes