Batch export PDF and STEP with custom iProperties as name

Batch export PDF and STEP with custom iProperties as name

Anonymous
Not applicable
861 Views
2 Replies
Message 1 of 3

Batch export PDF and STEP with custom iProperties as name

Anonymous
Not applicable

Hi,

 

I'm trying to make a rule that would export a PDF and STEP files for all open drawings with a custom iProperties value as name for the files.

Please note that I have no idea how all this works, I just copy pasted together the code from various post from the internet.

 

My current code practically exports all the files with the same name.

 

 

I guess it's always checking the iProperties for the file that is currently on screen, not for each part when it goes through them, but I can't figure out how to make that happen.

 

Here is my code, any help would be much appreciated.

 

SyntaxEditor Code Snippet

Dim oDoc As Inventor.Document
For Each oDoc In ThisApplication.Documents.VisibleDocuments
     If (oDoc.documenttype = kDrawingDocumentObject) Then 'Found a drawing
     'find the postion of the last backslash in the path
    oFNamePos = InStrRev(oDoc.fullfileName, "\", -1)   
    'get the file name with the file extension
    oName = Right(oDoc.fullfileName, Len(oDoc.fullfileName) - oFNamePos)
    'get the path of the folder containing the file
    oPath = Left(oDoc.fullfileName, Len(oDoc.fullfileName) - Len(oName))
     
    If iProperties.Value("Project", "Part Number") <> "" Then
        oShortName = iProperties.Value("Project", "Part Number")
    Else
        oShortName = ThisDoc.FileName(False)
    End If  
       
    'get PDF target folder path
    oFolder = "C:\InventorExport\" & "\Batch"
        
    '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
    
'**********************Create PDF**********************    

    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(oDataMedium, oContext, oOptions) Then
    'oOptions.Value("All_Color_AS_Black") = 0
    'oOptions.Value("Remove_Line_Weights") = 0
    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 & "\" & oShortName & ".pdf"
    
    
    'Publish document
    oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)


'**********************Create STEP**********************'define the model referenced by the drawing
Dim oModelDoc = ThisDoc.ModelDocument
' Get the STEP translator Add-In.
Dim oSTEPTranslator As TranslatorAddIn
oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim oSTEPContext As TranslationContext
oSTEPContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oSTEPOptions As NameValueMap
oSTEPOptions = ThisApplication.TransientObjects.CreateNameValueMap

If oSTEPTranslator.HasSaveCopyAsOptions(oModelDoc, oSTEPContext, oSTEPOptions) Then
    ' Set application protocol.
    ' 2 = AP 203 - Configuration Controlled Design
    ' 3 = AP 214 - Automotive Design
    oSTEPOptions.Value("ApplicationProtocolType") = 3
    ' Other options...
    'oSTEPOptions.Value("Author") = ""
    'oSTEPOptions.Value("Authorization") = ""
    'oSTEPOptions.Value("Description") = ""
    'oSTEPOptions.Value("Organization") = ""
    oSTEPContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    Dim oData As DataMedium
    oData = ThisApplication.TransientObjects.CreateDataMedium
    
    oData.FileName = oFolder & "\" & oShortName & ".stp"
    
     'Publish document    
    oSTEPTranslator.SaveCopyAs(oModelDoc, oSTEPContext, oSTEPOptions, oData)
End If

End If
      
Next oDoc

'Show message box
MessageBox.Show("PDF(s) and STEP(s) exported to: " & oFolder , "iLogic") 
'--------------------------------------------------------------------------------

Exit Sub

 

0 Likes
Accepted solutions (1)
862 Views
2 Replies
Replies (2)
Message 2 of 3

MechMachineMan
Advisor
Advisor
Accepted solution

Try This:

 

Sub Main()
	Dim oDoc As Inventor.Document

	For Each oDoc In ThisApplication.Documents.VisibleDocuments
		If Not (oDoc.documenttype = kDrawingDocumentObject) Then Continue For
			'oSourceFolder = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
	
		oPN = oDoc.PropertySets("Design Tracking Properties")("Part Number").Value
	
		If oPN <> "" Then
			oShortName = oPN
		Else
			oShortName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
		End If  
		
		'get PDF target folder path
		oFolder = "C:\InventorExport\" & "Batch"
			
		'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
		
		Call CreatePDF(oDoc, oFolder & "\" & oShortName)
		Call CreateSTEP(oDoc.Sheets(1).DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument, oFolder & "\" & oShortName)
		
		Process.Start(oFolder)
		'MessageBox.Show("PDF(s) and STEP(s) exported to: " & oFolder , "iLogic") 
	Next oDoc

End Sub

Sub CreatePDF(oModelDoc As Document, oOutputName As String)   
    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(oDataMedium, oContext, oOptions) Then
		'oOptions.Value("All_Color_AS_Black") = 0
		'oOptions.Value("Remove_Line_Weights") = 0
		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 = oOutputName & ".pdf"
    
    'Publish document
    oPDFAddIn.SaveCopyAs(oModeldoc, oContext, oOptions, oDataMedium)

End Sub

Sub CreateSTEP(oModelDoc As Document, oOutputName As String)

	Dim oSTEPTranslator As TranslatorAddIn
	oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	
	Dim oSTEPContext As TranslationContext
	oSTEPContext = ThisApplication.TransientObjects.CreateTranslationContext
	
	Dim oSTEPOptions As NameValueMap
	oSTEPOptions = ThisApplication.TransientObjects.CreateNameValueMap
	
	If oSTEPTranslator.HasSaveCopyAsOptions(oModelDoc, oSTEPContext, oSTEPOptions) Then
		' Set application protocol.
		' 2 = AP 203 - Configuration Controlled Design
		' 3 = AP 214 - Automotive Design
		oSTEPOptions.Value("ApplicationProtocolType") = 3
		' Other options...
		'oSTEPOptions.Value("Author") = ""
		'oSTEPOptions.Value("Authorization") = ""
		'oSTEPOptions.Value("Description") = ""
		'oSTEPOptions.Value("Organization") = ""
		oSTEPContext.Type = kFileBrowseIOMechanism
		
		Dim oData As DataMedium
		oData = ThisApplication.TransientObjects.CreateDataMedium
		oData.FileName = oOutputName & ".stp"
		
		'Publish document    
		oSTEPTranslator.SaveCopyAs(oModelDoc, oSTEPContext, oSTEPOptions, oData)
	End If

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 3

Anonymous
Not applicable

Wow. Works like a charm.

Very much appreciated!

0 Likes