List Open Drawings using iLogic

List Open Drawings using iLogic

andrew_canfield
Collaborator Collaborator
1,480 Views
9 Replies
Message 1 of 10

List Open Drawings using iLogic

andrew_canfield
Collaborator
Collaborator

How can all the current open drawings be listed using iLogic?

 

The list will be used to save & save as pdf all open drawings (already have a rule to save as pdf).

 

Regards

 

Andrew

0 Likes
Accepted solutions (1)
1,481 Views
9 Replies
Replies (9)
Message 2 of 10

Curtis_Waguespack
Consultant
Consultant

Hi @andrew_canfield 

 

You can look at the "ThisApplication.Documents" collection to see all open documents, and then filter by document type, as shown in this example, which adds them to a list

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim oDoc As Document
For Each oDoc In ThisApplication.Documents
	If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
		'print code there
		MessageBox.Show("Printing:  " & vbLf & oDoc.FullFileName, "iLogic")

	End If
Next


EESignature

0 Likes
Message 3 of 10

andrew_canfield
Collaborator
Collaborator

Hello Curtis & thanks for the reply - the code works but my explanation has hit a problem.

I already have code to create a pdf on the desktop & thought (wrongly) 'run rule' instead of "Printing" would print the file described by the text:batch pdf.JPG

 

if there are 3 drawings open only one is printed - the active document (i think).

Guessing I need to transfer the file path information from your code into the print pdf code - something like:

 

OpenBatch.png

 

 

Here is all the code - will try to merge them

 

Dim oDoc As Document
For Each oDoc In ThisApplication.Documents
	If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
		'print code there
		MessageBox.Show("Printing:  " & vbLf & oDoc.FullFileName, "iLogic")

	End If
Next


'pass info from above into rule below
'the rule below will print a pdf to a desktop folder with defer updates on




Dim oDoc As Document'
oDoc = ThisApplication.ActiveDocument'
'oValue = oDoc.DrawingSettings.DeferUpdates ()'

oDoc.DrawingSettings.DeferUpdates = "True"'

ThisDoc.Save

Dim oFolder As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) & "\PDF"
If Not System.IO.Directory.Exists(oFolder) Then 
    System.IO.Directory.CreateDirectory(oFolder)
End If

oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oRevNum = iProperties.Value("project", "revision number")
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 


If oPDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 1
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 


 'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf" 


'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) 
'------end of iLogic-------

 

Regards

 

Andrew

0 Likes
Message 4 of 10

Curtis_Waguespack
Consultant
Consultant
Accepted solution

 Hi @andrew_canfield ,

 

Maybe something like this.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub main
	Dim oDoc As Document
	
	'count open drawings 
	i = 0
	For Each oDoc In ThisApplication.Documents
		If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			i+=1
		End If
	Next		
	
	'Get user input
	RUsure = MessageBox.Show ( _
	i & " drawings about to print to PDF." _
	& vbLf & "This could take a while, do you want to continue?", "iLogic", _
	MessageBoxButtons.YesNo, MessageBoxIcon.Question)
	
	If RUsure = vbNo Then
	Return
	Else
	End If

	For Each oDoc In ThisApplication.Documents
		If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			Call PrintPDF(oDoc)
		End If
	Next		
	
	If oDoc is ThisApplication.ActiveDocument Then
	oDoc.DrawingSettings.DeferUpdates = "True"
	oDoc.Save
	End If
	
	MessageBox.Show("PDFs complete", "iLogic")
	
	Try
		Process.Start(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) & "\PDF")
	Catch
	End Try

End Sub

Sub PrintPDF(oDoc As Document)
	
	
Dim oFolder As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) & "\PDF"
If Not System.IO.Directory.Exists(oFolder) Then 
    System.IO.Directory.CreateDirectory(oFolder)
End If

oFileName = Left(oDoc.DisplayName,Len(oDoc.DisplayName)-4)

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium 


If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 1
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 


 'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf" 


'Publish document
oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium) 
End Sub

EESignature

Message 5 of 10

claudio.ibarra
Advocate
Advocate

What would you add to make the resulting PDF filenames contain the drawing revisions at the end, before the ".pdf"?

0 Likes
Message 6 of 10

Curtis_Waguespack
Consultant
Consultant

Hi @claudio.ibarra ,

 

This should do it.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub main
	Dim oDoc As Document
	
	'count open drawings 
	i = 0
	For Each oDoc In ThisApplication.Documents
		If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			i+=1
		End If
	Next		
	
	'Get user input
	RUsure = MessageBox.Show ( _
	i & " drawings about to print to PDF." _
	& vbLf & "This could take a while, do you want to continue?", "iLogic", _
	MessageBoxButtons.YesNo, MessageBoxIcon.Question)
	
	If RUsure = vbNo Then
	Return
	Else
	End If

	For Each oDoc In ThisApplication.Documents
		If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			Call PrintPDF(oDoc)
		End If
	Next		
	
	If oDoc is ThisApplication.ActiveDocument Then
	oDoc.DrawingSettings.DeferUpdates = "True"
	oDoc.Save
	End If
	
	MessageBox.Show("PDFs complete", "iLogic")
	
	Try
		Process.Start(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) & "\PDF")
	Catch
	End Try

End Sub

Sub PrintPDF(oDoc As Document)
	
	
Dim oFolder As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) & "\PDF"
If Not System.IO.Directory.Exists(oFolder) Then 
    System.IO.Directory.CreateDirectory(oFolder)
End If

oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) -4)
oRevNum = oDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").value
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium 


If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 1
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 


 'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & "-Rev" &  oRevNum & ".pdf" 


'Publish document
oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium) 
End Sub

EESignature

Message 7 of 10

andrew_canfield
Collaborator
Collaborator

Many Thanks

0 Likes
Message 8 of 10

andrew_canfield
Collaborator
Collaborator

Added a slight tweak (around line 51) - some users have file extensions visible & other don't so....

 

FileName1 = Left(oDoc.DisplayName, Len(oDoc.DisplayName))
FileName3 = Right (FileName1, 4)
FileName2 = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)
FileName4 = Right (FileName2, 4)

If FileName3 <> ".idw" Then oFileName = FileName1
If FileName4 <> ".idw" Then oFileName = FileName2


'oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName))

it may be crude but the returned pdf's don't include ".idw"  (still needs some testing)

  🙂

0 Likes
Message 9 of 10

andrew_canfield
Collaborator
Collaborator

amendment- thankfully the drawing numbers have a fixed length...

 

FileName1 = Left(oDoc.DisplayName, Len(oDoc.DisplayName))
StrLength = FileName1.Length()
If StrLength = 21 Then  oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)
If StrLength = 17 Then  oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) )

 

0 Likes
Message 10 of 10

creggo
Enthusiast
Enthusiast

Any way to take the revision number from model document instead of drawing document?

 

Running as external rule so might not be possible? 🤔

0 Likes