iLogic PDF Per Sheet

iLogic PDF Per Sheet

afinney8E9VH
Contributor Contributor
1,281 Views
8 Replies
Message 1 of 9

iLogic PDF Per Sheet

afinney8E9VH
Contributor
Contributor

Dear Technical Guru's,

 

I am trying to adapt a PDF Save iLogic script so that individual sheets of the drawing save as a separate file. My company use a suffix in the part number that determines the material of the part to be manufactured, which can alter dependent upon specific requirements. Where a component can be made of multiple materials (with all other features remaining the same), we use iPart and the corresponding DWG file has multiple sheets with a different material/part number per sheet.

 

My question is this, is it possible to write a script where each sheet is exported as a separate PDF file using the base view of the iPart part number as the file name? Our current script is below.

 

Many thanks

 

Adam

 

Sub Main ()
FileName = ThisDoc.FileName(True) 'with extension

FileExtension = Right(FileName, 3)

If FileExtension = "dwg" Then
Save_As_PDF
Else
ErrorMessage
End If
End Sub


Sub Save_As_PDF
oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById ("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

'Gets the Workspace Path
WorkspacePath= ThisDoc.WorkspacePath()

'Gets the Length of the WorkspacePath String
WorkspacePathLength = Len(WorkspacePath)

'Gets just the Path of the file
PathOnly = ThisDoc.Path

'Removes the Workspace Path from FullPath
DirectoryPath = Strings.Right(PathOnly, PathOnly.Length-WorkspacePathLength)

'Sets the Dirctory that the PDF should be saved in
PDFPath = "X:\Drawing Registers\AutoSave PDF Drawings" & DirectoryPath
PDFName = PDFPath & "\" & ThisDoc.FileName(False) & "_" & iProperties.Value("Summary", "Revision Number") & ".pdf"

'Set the PDF target file name
oDataMedium.FileName = PDFName

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

'Checks to see if that directory exists, if not, it is created
If(Not System.IO.Directory.Exists(PDFPath)) Then
System.IO.Directory.CreateDirectory(PDFPath)
End If

'Saves the PDF in the desired location
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
End Sub

Sub ErrorMessage
i = MessageBox.Show("This is not a drawing file. No PDF will be created.", "Create PDF", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End Sub

 

 

0 Likes
Accepted solutions (2)
1,282 Views
8 Replies
Replies (8)
Message 2 of 9

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @afinney8E9VH 

If I understand correctly you want to print each sheet to pdf. Naming the pdf as the part number of the first views referenced document?

 

I tried to make it work by setting the sheetrange and using kPrintSheetRange but I didnt get it to actually change the sheetrange... (see deactivated lines)

I did manage to get it to work though by looping through each sheet and setting it to active, with the "Sheet_Range" set to kCurrentSheet.

 

Try this rule:

Sub Main()
FileName = ThisDoc.FileName(True) 'with extension

FileExtension = Right(FileName, 3)

If FileExtension = "dwg" Then
	Save_As_PDF
Else
	ErrorMessage
End If
End Sub


Sub Save_As_PDF
	oPath = ThisDoc.Path
	oFileName = ThisDoc.FileName(False) 'without extension
	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oDocument = ThisApplication.ActiveDocument
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
	'Dim oPrintMgr As DrawingPrintManager = ThisDrawing.Document.PrintManager
	'Gets the Workspace Path
	WorkspacePath = ThisDoc.WorkspacePath()

	'Gets the Length of the WorkspacePath String
	WorkspacePathLength = Len(WorkspacePath)

	'Gets just the Path of the file
	PathOnly = ThisDoc.Path

	'Removes the Workspace Path from FullPath
	DirectoryPath = Strings.Right(PathOnly, PathOnly.Length - WorkspacePathLength)

	'Sets the Dirctory that the PDF should be saved in
	PDFPath = "X:\Drawing Registers\AutoSave PDF Drawings" & DirectoryPath

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

	'Checks to see if that directory exists, if not, it is created
	If (Not System.IO.Directory.Exists(PDFPath)) Then
		System.IO.Directory.CreateDirectory(PDFPath)
	End If
	'Dim i As Integer = 1
	For Each oSheet As Sheet In ThisDrawing.Document.Sheets
		oSheet.Activate
		PDFName = PDFPath & "\" & oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument _
		.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value & ".pdf"
		'Set the PDF target file name
		oDataMedium.FileName = PDFName
		'Call oPrintMgr.SetSheetRange(i, i)
		'Saves the PDF in the desired location
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
		'i += 1
	Next
End Sub

Sub ErrorMessage
	i = MessageBox.Show("This is not a drawing file. No PDF will be created.", "Create PDF", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End Sub

 

0 Likes
Message 3 of 9

afinney8E9VH
Contributor
Contributor

Thank you so much. It works perfectly.

 

Reagrds,


Adam

Message 4 of 9

afinney8E9VH
Contributor
Contributor

Hi @JhoelForshav ,

 

Would it possible to change the format to DXF (2007 version)? Some parts are laser cut and our suppliers as for this format?


Regards,


Adam

0 Likes
Message 5 of 9

JhoelForshav
Mentor
Mentor

Hi @afinney8E9VH 

Getting the correct dxf-version (2007 in your case) was quite difficult. I found the best way was to save a .ini-file with from the dxf-translator and then referencing the file by code.

See iLogic code below and Screencast.

 

Rule0

' Get the DXF translator Add-In.
Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
Dim oDocument As DrawingDocument
oDocument = ThisDrawing.Document
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
Dim strIniFile As String
strIniFile = "C:\Users\hfljf\Desktop\DXF_2007\DXF_2007.ini" 'Path to .ini-file
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
Dim oPath As String = "C:\Users\hfljf\Desktop\DXF_2007"
For Each oSheet As Sheet In oDocument.Sheets
oSheet.Activate
oDataMedium.FileName = oPath & "\" & oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument.PropertySets _
.Item("Design Tracking Properties").Item("Part Number").Value & ".dxf"
DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Next

 

Rule1

Dim addins As ApplicationAddIns
addins = ThisApplication.ApplicationAddIns
Dim DXFAddIn As TranslatorAddIn = _
addins.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
DXFAddIn.Activate
transientObj = ThisApplication.TransientObjects

Context = transientObj.CreateTranslationContext
Context.Type = kUnspecifiedIOMechanism
Options = transientObj.CreateNameValueMap
SourceObject = ThisApplication.ActiveDocument
Call DXFAddIn.ShowSaveCopyAsOptions( _
SourceObject, Context, Options)

 

Hope it helps 🙂

 
 
 
0 Likes
Message 6 of 9

JhoelForshav
Mentor
Mentor

Hi @afinney8E9VH 

The only way to get the desired dxf-version I could find was to create a .ini-file and with the dxf-translator and then reference it by code.

See screencast and rules below.

Rule0

Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
Dim oDocument As DrawingDocument
oDocument = ThisDrawing.Document
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
Dim strIniFile As String
strIniFile = "C:\Users\hfljf\Desktop\DXF_2007\DXF_2007.ini" 'Path to .ini-file
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
Dim oPath As String = "C:\Users\hfljf\Desktop\DXF_2007"
For Each oSheet As Sheet In oDocument.Sheets
oSheet.Activate
oDataMedium.FileName = oPath & "\" & oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument.PropertySets _
.Item("Design Tracking Properties").Item("Part Number").Value & ".dxf"
DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Next

Rule1

Dim addins As ApplicationAddIns
addins = ThisApplication.ApplicationAddIns
Dim DXFAddIn As TranslatorAddIn = _
addins.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
DXFAddIn.Activate
transientObj = ThisApplication.TransientObjects

Context = transientObj.CreateTranslationContext
Context.Type = kUnspecifiedIOMechanism
Options = transientObj.CreateNameValueMap
SourceObject = ThisApplication.ActiveDocument
Call DXFAddIn.ShowSaveCopyAsOptions( _
SourceObject, Context, Options)

 

 
 
 
0 Likes
Message 7 of 9

JhoelForshav
Mentor
Mentor

Hi @afinney8E9VH 

The only way I could find to get the desired dxf-version was to save a .ini-file with the dxf-translator and then reference it by code.

 

I recorded a screencast to show you, but for some reason I can't include it in this post. It gets automatically reported as spam🙄

 

I'll try to get it included one more time. If it doesn't appear you can find it on my profile.

Export dxf from drawing to specific version

 

Here is the code used in the screencast:

 

Rule0

Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
Dim oDocument As DrawingDocument
oDocument = ThisDrawing.Document
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
Dim strIniFile As String
strIniFile = "C:\Users\hfljf\Desktop\DXF_2007\DXF_2007.ini" 'Path to .ini-file
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
Dim oPath As String = "C:\Users\hfljf\Desktop\DXF_2007"
For Each oSheet As Sheet In oDocument.Sheets
oSheet.Activate
oDataMedium.FileName = oPath & "\" & oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument.PropertySets _
.Item("Design Tracking Properties").Item("Part Number").Value & ".dxf"
DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Next

 

Rule1

Dim addins As ApplicationAddIns
addins = ThisApplication.ApplicationAddIns
Dim DXFAddIn As TranslatorAddIn = _
addins.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
DXFAddIn.Activate
transientObj = ThisApplication.TransientObjects

Context = transientObj.CreateTranslationContext
Context.Type = kUnspecifiedIOMechanism
Options = transientObj.CreateNameValueMap
SourceObject = ThisApplication.ActiveDocument
Call DXFAddIn.ShowSaveCopyAsOptions( _
SourceObject, Context, Options)

 

 

8227e8e8-2fce-42df-8294-feb011d9ab6b,640,620

0 Likes
Message 8 of 9

JhoelForshav
Mentor
Mentor
Accepted solution
Message 9 of 9

afinney8E9VH
Contributor
Contributor

Amazing. 


Thank you so much for taking the time to solve my problem.

 

Regards,


Adam