2022 Mass print from assembly using ilogic

2022 Mass print from assembly using ilogic

matthewZYUPT
Enthusiast Enthusiast
994 Views
5 Replies
Message 1 of 6

2022 Mass print from assembly using ilogic

matthewZYUPT
Enthusiast
Enthusiast

I noticed there is a post from 2018 discussing "Mass print from assembly using ilogic" (Solved: Mass print from assembly using ilogic - Autodesk Community - Inventor) and after copying and pasting there code into macro's I receive an error on "ThisApplication."  saying: "compile error: invalid outside procedure". any help would be greatly appreciated thank you!

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

WCrihfield
Mentor
Mentor

Hi @matthewZYUPT.  Just to clarify, in case you weren't aware, the solution in that post is written in iLogic, not VBA, so if you are trying to use it as a VBA macro, it would have to be translated a bit to make it work.  In VBA you have to use the term 'Set' in front of the line when assigning a value to a variable (but not for String, Double, Integer, etc), but you don't have to do this in iLogic (which uses VB.NET).  There are a lot of other little differences too.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

matthewZYUPT
Enthusiast
Enthusiast

Thank you for the clarification, I have copied and pasted the code into my ilogic and removed the "_" from line 42 however when running the code in a simple 3 part assembly (each part has a drawing (just one isometric view on a random sheet for tests)) but I receive and error "no IDW of the top level file found" looking into IDW's it appears that was the old way drawing formats where made so i ctrl+F and replaced all "idw"'s with "dwg" and now it works perfect! 
here is the code for a Ilogic rule batch print for inventor 2022 pro edition: 

' This is print all the associated dwg of an assembly to PDF. This is similar to the "Print all Ref dwg" macro functionality, but' it is now converted into a iLogic rule. This assumes that the dwg is teh same file name as the part or the assembly.'There are a few changes to the orginal Macro:' 1. Now have an option to print in color or B/W.' 2. The ilogic code also prints the top level assembly.' ----------------------------------------------------------------Kirk Arthur 8/30/2016

'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'get user input
RUsure = MessageBox.Show ( _
"This will create a PDF file for all of the assembly components that have" _
& vbLf & "drawings files. This rule expects that the drawing file shares the same" _
& vbLf & " name and location as the component." _
& vbLf & "" _
& vbLf & "Make sure the file extensions are set to Visible in Windows Explorer." _
& vbLf & "" _
& vbLf & "This could take a while. Are you sure you want to do this?" _
& vbLf & "" _
& vbLf & "", "iLogic - Batch Output PDFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
Return
Else
End If

'- - - - - - - - - - - - -PDF setup - - - - - - - - - - - -

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MsgBox ("This is NOT an assembly document!")
Exit Sub
End If

Dim oPrintMgr As PrintManager
oPrintMgr = ThisApplication.ActiveDocument.PrintManager
If MsgBox("Using Printer " & oPrintMgr.Printer & ", Best Fit, 11x17 paper, Landscape Mode." _
& vbLf & " " _
& vbLf & " Do you want to continue?" ,vbYesNo + vbQuestion,"System Printer Settings ") = vbNo Then
Exit Sub
End If

Dim oDrgPrintMgr As DrawingPrintManager
'MsgBox ("Using Printer " & oPrintMgr.Printer & ", Best Fit, 11x17 paper, Landscape Mode.")
dirPath = Left(oAsmDoc.FullFileName, Len(oAsmDoc.FullFileName) - Len(oAsmDoc.DisplayName))

' ----Determine if the prints should be in color or B/W ------
oColorAsBlack="False"
RUsure = MessageBox.Show ( "Do you want to print this in COLOR?"& vbLf & "", "Color or B/W Prints",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
oColorAsBlack="True"
'Return
Else
End If

Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
numFiles = 0
''End If

'get PDF target folder path
oFolder = oPath & "\" & oAsmName & " PDF Files"

'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

'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -'look at the files referenced by the assembly
For Each oRefDoc In oRefDocs
dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "dwg"
If (System.IO.File.Exists(dwgPathName)) Then
numFiles = numFiles + 2
'If numFiles = 10 Then Exit Sub
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.Documents.Open(dwgPathName, True)
oDrawDoc.Activate
oDrawDoc = ThisApplication.ActiveDocument
oDrgPrintMgr = oDrawDoc.PrintManager
oDrgPrintMgr.AllColorsAsBlack = oColorAsBlack
oDrgPrintMgr.ScaleMode = kPrintBestFitScale
oPrintMgr = ThisApplication.ActiveDocument.PrintManager

' Printer setup, default printer
'oPrintMgr.ColorMode = kPrintDefaultColorMode ' Set to default. Uses printer settings
oPrintMgr.PrintRange = kPrintAllSheets 'Prints all sheets in the dwg.
oPrintMgr.NumberOfCopies = 1 ' Set to print one copies.
oPrintMgr.Orientation = kLandscapeOrientation ' Set to print using landscape orientation.
oPrintMgr.PaperSize = kPaperSizeTabloid 'Set the paper size.
oPrintMgr.SubmitPrint ' Submit the print.
oDrawDoc.Close (True)
End If

Next
'MsgBox ("There are " & numFiles & " sent to printer.")

'- - - - - - - - - - - - -

'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -

oAsmDrawing = ThisDoc.ChangeExtension(".dwg")
If (System.IO.File.Exists(oAsmDrawing)) Then
oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)

'write out the PDF for the Top Level Assembly Drawing file

oAsmDrawDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
oAsmDrawDoc.Activate
oDrgPrintMgr = oAsmDrawDoc.PrintManager
oDrgPrintMgr.AllColorsAsBlack = oColorAsBlack
oDrgPrintMgr.ScaleMode = kPrintBestFitScale
oPrintMgr = ThisApplication.ActiveDocument.PrintManager
' Printer setup, default printer
'oPrintMgr.ColorMode = kPrintDefaultColorMode ' Set to default. Uses printer settings
oPrintMgr.PrintRange = kPrintAllSheets 'Prints all sheets in the dwg.
oPrintMgr.NumberOfCopies = 1 ' Set to print one copies.
oPrintMgr.Orientation = kLandscapeOrientation ' Set to print using landscape orientation.
oPrintMgr.PaperSize = kPaperSizeTabloid 'Set the paper size.
oPrintMgr.SubmitPrint ' Submit the print.
oAsmDrawDoc.Close (True)
numFiles = numFiles + 1

Else
MessageBox.Show (" No DWG of the Top level file found!" _
& vbLf & " " _
& vbLf & "There were " & numFiles & " files sent to the printer."," Job Complete ")
Return
End If

'- - - - - - - - - - - - -

MessageBox.Show ("There were " & numFiles & " files sent to the printer."," Job Complete ")

'MessageBox.Show("New Files Created in: " & vbLf & dwgpathname, "iLogic")'open the folder where the new ffiles are saved'Shell("explorer.exe " & oFolder,vbNormalFocus)

0 Likes
Message 4 of 6

matthewZYUPT
Enthusiast
Enthusiast

Last question: is there a way I can make this rule run off of a button in my ribbon without having to change the code? just looking for a quick option so I can click the button and it will update the specific rule set?

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Yes.

Step 1:  In order to run an iLogic rule from a button in your ribbon, you first need to create a VBA macro that will run that rule.

Step 2:  Then you can add a button for that macro into your ribbon using the Customize tool > Ribbon tab > select 'Macros' from the drop-down list under 'Choose commands from', then select where to put it by selecting something in the drop-down list under 'Choose tab to add custom panel to'.  Then drag the macro from left side panel, over to the right panel.

 

Here is one example of a VBA macro created specifically to run an external rule.

Sub RunRule()
    'get the iLogic Add-in, and its Automation object
    'so we can use the 'RunRule' type methods defined within it
    Dim oInv As Inventor.Application
    Set oInv = ThisApplication
    Dim oiLogic As Inventor.ApplicationAddIn
    Dim oAuto As Object
    For Each oiLogic In oInv.ApplicationAddIns
        If VBA.Strings.InStr(oiLogic.DisplayName, "iLogic") > 0 Then
            If Not oiLogic.Activated Then oiLogic.Activate
            Set oAuto = oiLogic.Automation
            Exit For
        End If
    Next
    
    'specify target document for external rule to effect
    Dim oDoc As Inventor.Document
    Set oDoc = oInv.ActiveDocument
    
    'specify the name of the rule
    Dim oRuleName As String
    oRuleName = "All Text & Boolean Params to iProps"
    
    'call the method that will run the rule
    Call oAuto.RunExternalRule(oDoc, oRuleName)
    
    'To send data to the iLogic rule, first create a NameValueMap,
    'create some name/value pairs within it,
    'then supply that in place of the 'Arguments' input variable in the RunExternalRuleWithArguments() method
    'Dim oArgs As Inventor.NameValueMap
    'Call oArgs.Add("TargetDocument", oDoc)
    'Call oAuto.RunExternalRuleWithArguments(oDoc, oRuleName, oArgs)
    'the iLogic rule will need to be set-up to retrieve that NameValueMap using the RuleArguments interface.
    'once the iLogic rule has retrieved it, it can get/set values or pairs within it
    'once the iLogic rule finishes, it will return control to this VBA macro,
    'then the macro can simply retrieve any resulting values from the NameValueMap it created earlier
End Sub

There are also other ways of adding buttons to ribbons, but the process mentioned above is the quickest & simplest way.  You can also create your own custom button icon/image if needed, but that starts to get more complicated.

Here are some other resources you might be interested in:

https://knowledge.autodesk.com/support/inventor-products/learn-explore/caas/CloudHelp/cloudhelp/2015... 

https://www.cadlinecommunity.co.uk/hc/en-us/articles/115000859309-Inventor-Run-iLogic-Rules-from-you... 

https://help.autodesk.com/view/INVLT/2015/ENU/?guid=GUID-0BEC2C2C-D1A7-45E3-81C5-FE9327AAFC10 

https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/add-button-for-vba-macro-to-custom-ribb... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

matthewZYUPT
Enthusiast
Enthusiast

Thank you for all your help. Only issue is "Call oAuto.RunExternalRule(oDoc, oRuleName)" 

0 Likes