Hi there,
I am trying to create a rule that will take each open drawing file (.idw) and create a folder based on the part name and export a PDF & DWG within that folder.
I have created this rule that works well when run from the individual drawings, however I am trying to get it to run on all open drawings. I have another rule within the drawings that calls the rule below and contains the code to have in run on all open drawings, but I cant get it to run beyond the first drawing.
Eg. I will run the "Export" rule, which will call the "PDFDWG" rule on the first drawing and create the folder & files and then move to the next open drawing and then stop.
Code to create folder and export drawings (Rule Name = PDFDWG)
WorkspacePath = ThisDoc.WorkspacePath ' get the filename without extension oFileName = ThisDoc.FileName(False) oPath = ThisDoc.Path oFolder = oPath + "\" + oFileName '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 ' Get the PDF add-in Dim oPDFAddIn As ApplicationAddIn oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}") ' Get the DWG add-in Dim oDWGAddIn As ApplicationAddIn oDWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}") ' Create the translation context Dim oContext As TranslationContext oContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism ' Create the name-value map for options Dim oOptions As NameValueMap oOptions = ThisApplication.TransientObjects.CreateNameValueMap ' Set PDF export options oOptions.Value("Remove_Line_Weights") = 1 oOptions.Value("All_Color_AS_Black") = 0 oOptions.Value("Vector_Resolution") = 400 oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets ' Create the data medium for PDF Dim oDataMediumPDF As DataMedium oDataMediumPDF = ThisApplication.TransientObjects.CreateDataMedium oDataMediumPDF.FileName = oFolder & "\" & oFileName & ".pdf" ' Export the document to PDF If oPDFAddIn.HasSaveCopyAsOptions(ThisDoc.Document, oContext, oOptions) Then oPDFAddIn.SaveCopyAs(ThisDoc.Document, oContext, oOptions, oDataMediumPDF) End If ' Reset the options for DWG export oOptions = ThisApplication.TransientObjects.CreateNameValueMap ' Set DWG export options using an INI file Dim strIniFile As String strIniFile = ThisDoc.WorkspacePath + "\zInventorStuff\iLogic\DWGExport.ini" ' Create the name-value that specifies the ini file to use. oOptions.Value("Export_Acad_IniFile") = strIniFile ' Create the data medium for DWG Dim oDataMediumDWG As DataMedium oDataMediumDWG = ThisApplication.TransientObjects.CreateDataMedium oDataMediumDWG.FileName = oFolder & "\" & oFileName & ".dwg" ' Export the document to DWG If oDWGAddIn.HasSaveCopyAsOptions(ThisDoc.Document, oContext, oOptions) Then oDWGAddIn.SaveCopyAs(ThisDoc.Document, oContext, oOptions, oDataMediumDWG) End If
Code to run PDFDWG on all open drawings files (Rule Name = Export)
Dim oDoc As Inventor.Document For Each oDoc In ThisApplication.Documents.VisibleDocuments If oDoc.DocumentType = kDrawingDocumentObject Then oDoc.Activate iLogicVb.RunRule(oDoc, "PDFDWG") End If Next oDoc
I have gone through a few different versions of this code but havent been able to create anything that works beyond the first drawing. I have also tried to use external rules but keep getting an error with "Unable to cast COM object of type 'System.__ComObject' to class type 'System.String'."
Thanks in advance!
Solved! Go to Solution.
Solved by JelteDeJong. Go to Solution.
Put the code in the on save event for all documents.
Than press save all!
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
You are using this:
oFileName = ThisDoc.FileName(False)
ThisDoc will always return the document that contains the rule. That is no good if you want to target multiple documents.
You can try something like this:
Sub main()
For Each document As Document In ThisApplication.Documents.VisibleDocuments
If (document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject) Then
logger.Info("Skipped file: " + document.FullFileName)
Continue For
End If
Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(document.FullFileName)
Dim fileInfo As New System.IO.FileInfo(document.FullFileName)
Dim subFolder = fileInfo.Directory.CreateSubdirectory(fileName)
Dim pdfFullFileName = System.IO.Path.Combine(subFolder.FullName, fileName + ".pdf")
CreatePdf(pdfFullFileName)
Dim dwgFullFileName = System.IO.Path.Combine(subFolder.FullName, fileName + ".dwg")
CreateDwg(dwgFullFileName)
logger.Info("Created pdf and dwg from file: " + document.FullFileName)
Next
End Sub
Private Sub CreatePdf(fullFileName As String)
' Get the PDF add-in
Dim pdfAddIn As ApplicationAddIn
pdfAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
' Create the translation context
Dim context As TranslationContext
context = ThisApplication.TransientObjects.CreateTranslationContext
context.Type = IOMechanismEnum.kFileBrowseIOMechanism
' Create the name-value map for options
Dim options As NameValueMap
options = ThisApplication.TransientObjects.CreateNameValueMap
' Set PDF export options
options.Value("Remove_Line_Weights") = 1
options.Value("All_Color_AS_Black") = 0
options.Value("Vector_Resolution") = 400
options.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
' Create the data medium for PDF
Dim dataMediumPDF As DataMedium
dataMediumPDF = ThisApplication.TransientObjects.CreateDataMedium
dataMediumPDF.FileName = fullFileName
' Export the document to PDF
If pdfAddIn.HasSaveCopyAsOptions(ThisDoc.Document, context, options) Then
pdfAddIn.SaveCopyAs(ThisDoc.Document, context, options, dataMediumPDF)
End If
End Sub
Private Sub CreateDwg(fullFileName As String)
' Get the DWG add-in
Dim dwgAddIn As ApplicationAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
' Create the translation context
Dim context As TranslationContext
context = ThisApplication.TransientObjects.CreateTranslationContext
context.Type = IOMechanismEnum.kFileBrowseIOMechanism
' Reset the options for DWG export
Dim options = ThisApplication.TransientObjects.CreateNameValueMap
' Set DWG export options using an INI file
Dim workspacePath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
Dim strIniFile As String = System.IO.Path.Combine(workspacePath, "zInventorStuff\iLogic\DWGExport.ini")
' Create the name-value that specifies the ini file to use.
options.Value("Export_Acad_IniFile") = strIniFile
' Create the data medium for DWG
Dim dataMediumDWG As DataMedium
dataMediumDWG = ThisApplication.TransientObjects.CreateDataMedium
dataMediumDWG.FileName = fullFileName
' Export the document to DWG
If dwgAddIn.HasSaveCopyAsOptions(ThisDoc.Document, context, options) Then
dwgAddIn.SaveCopyAs(ThisDoc.Document, context, options, dataMediumDWG)
End If
End Sub
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com
Can't find what you're looking for? Ask the community or share your knowledge.