Running rule on all open drawings

Running rule on all open drawings

sam_dobell
Participant Participant
734 Views
5 Replies
Message 1 of 6

Running rule on all open drawings

sam_dobell
Participant
Participant

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!

0 Likes
Accepted solutions (2)
735 Views
5 Replies
Replies (5)
Message 2 of 6

bradeneuropeArthur
Mentor
Mentor

Put the code in the on save event for all documents.

Than press save all!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
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:
My 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 !


 


EESignature

0 Likes
Message 3 of 6

JelteDeJong
Mentor
Mentor
Accepted solution

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.

EESignature


Blog: hjalte.nl - github.com

Message 4 of 6

sam_dobell
Participant
Participant

Thanks this worked perfectly!

Message 5 of 6

sam_dobell
Participant
Participant

Hi @JelteDeJong, sorry I have only just got around to properly testing this solution, and while it will generate separate folders, each with a pdf & dwg with the correct file names, the actual files themselves will all be of the active document when running the script. 

 

Eg. I have Part1.idw & Part2.idw open and run the script with Part1.idw as the active document.

It will create folders Part1 containing Part1.pdf &Part1.dwg & Part2 containing Part2.pdf &Part2.dwg.

Opening Part2.pdf or dwg shows the drawings for Part1.

 

Have testing this will multiple files and they will always export the active drawing to every file. 

 

I suspect it has something to do with the subroutines themselves but struggling to diagnose. 

 

Thanks

0 Likes
Message 6 of 6

sam_dobell
Participant
Participant
Accepted solution

I was able to solve this by tweaking the subroutines to remove  ThisDoc.Document

 

I have made the changes bold to highlight them 

 

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(Document, pdfFullFileName)

            Dim dwgFullFileName = System.IO.Path.Combine(subFolder.FullName, fileName + ".dwg")
            CreateDwg(Document, dwgFullFileName)

            Logger.Info("Created pdf and dwg from file: " + Document.FullFileName)
        Next
    End Sub

    Private Sub CreatePdf(document As Document, 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") = 1200
        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(document, context, options) Then
            pdfAddIn.SaveCopyAs(document, context, options, dataMediumPDF)
        End If
    End Sub

    Private Sub CreateDwg(document As Document, 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(document, context, options) Then
            dwgAddIn.SaveCopyAs(document, context, options, dataMediumDWG)
        End If
    End Sub

 

 

0 Likes