Ilogic to print drawings that rescales to the drawing format size

Ilogic to print drawings that rescales to the drawing format size

matthew_mccartinLBLNC
Explorer Explorer
423 Views
3 Replies
Message 1 of 4

Ilogic to print drawings that rescales to the drawing format size

matthew_mccartinLBLNC
Explorer
Explorer

Trying to make an Ilogic that looks for the drawing of every individual part in an assembly and the assembly drawing even if the drawing is located outside of the main folder and prints the drawing at a different size depending on what format its on. So if the printer is set to "Adobe PDF" by default and the drawing it opens has a format labeled "A-SIZE" print to pdf in letter scale or 8.5 x 11 and if its "B-size" to be ansi B or 11 x 17 and so on up to "D-SIZE". I have a bunch of variations all either doing a fraction of what i need it to do all together. I want to be able to hit run, drawing open, drawing print to scale defined, then drawing closes and opens the next one to repeat. here's what I have so far that really only prints to PDF but for some strange reason it changes my default print to microsoft print to pdf :

Sub Main()
    ' Rev 1
    Try
        Dim oDoc As Document = ThisApplication.ActiveDocument
        Dim oDocName As String = oDoc.DisplayName
        Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(oDocName)
        Dim sDrawingName As String = sFileName & ".idw"

        ' Search for the drawing in all subfolders
        Dim allFiles As String() = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(oDoc.FullFileName), "*.idw", System.IO.SearchOption.AllDirectories)
        For Each file In allFiles
            If System.IO.Path.GetFileName(File).ToLower() = sDrawingName.ToLower() Then
                ' Open the found drawing file
                ThisApplication.Documents.Open(File, True)

                ' Check the sheet size
                Dim activeSheet As Sheet = ThisApplication.ActiveDocument.ActiveSheet
                Dim sheetSize As String = activeSheet.Size.ToString()

                ' Display the sheet size
                MsgBox("Sheet size: " & sheetSize, MsgBoxStyle.Information, "Sheet Size")

                ' Save the drawing as a PDF
                Dim pdfPath As String = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(File), sFileName & ".pdf")
                ThisApplication.ActiveDocument.SaveAs(pdfPath, True)

                ' Print the PDF using the default system printer
                Dim shellCommand As String = "rundll32.exe printui.dll,PrintUIEntry /y /n " & Chr(34) & "Microsoft Print to PDF" & Chr(34) & " " & Chr(34) & pdfPath & Chr(34)
                Shell(shellCommand, AppWinStyle.Hide, True)

                Exit Sub
            End If
        Next

        ' If still not found, show an error message
        MsgBox("Couldn't find the associated drawing.", MsgBoxStyle.Exclamation, "Error")
    Catch ex As Exception
        ' Handle any exceptions
        MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical, "Error")
    End Try
End Sub 

 I was thinking calling out SheetSize and for all 4 sizes just say If "A-SIZE.idw" then print to letter If "B-SIZE.idw" Print to 11 x 17 paper

0 Likes
424 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @matthew_mccartinLBLNC.  Welcome to the forum.  I am not sure where you got these two lines of code...

Dim shellCommand As String = "rundll32.exe printui.dll,PrintUIEntry /y /n " & Chr(34) & "Microsoft Print to PDF" & Chr(34) & " " & Chr(34) & pdfPath & Chr(34)
Shell(shellCommand, AppWinStyle.Hide, True)

, or why you are using them here, but there is a more native way to print drawings from Inventor by code, and without changing your system default printer.  In Inventor, there are two different objects used for printing (PrintManager & DrawingPrintManager).  The first one can be used to print models, while the second one is a derived type of the first one, that is further developed and customized for printing drawings, and has more options to work with.  You can get to this object from the DrawingDocument.PrintManager property.  It says it returns a regular PrintManager, but if you declare a DrawingPrintManager type variable ahead of time, and set its value with that property, you will have a real DrawingPrintManager.  You can then use that object, its properties, and its methods, to print the drawings, instead of those two odd looking lines of code you are currently using.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

matthew_mccartinLBLNC
Explorer
Explorer

Gotta be honest I'm take portions of other people's Ilogic and pasting them together till I get a rough Idea of what i need then using edge copilot to translate as I'm less familiar with inventor ILogic. With a bit of experimenting, I've managed to get a pretty good basis with this new code I have. I still need to retry this one again but last time I printed using this code all the drawings where printed AND adjusted to their format size, so D-size printed well, C-size printed well but B and A I think I just need to find a way to say "Change orientation of these from portrait to landscape" because they printed fine, but they weren't centered and they had a bunch of left over space on one end. If I can figure out the orientation issue the only other problem I have and have been working more directly on today is making it so when you hit run on the code it only runs on the current representation that is being viewed and does NOT open any drawing marked as "Reference" or suppressed or not "active" but it looks like its slowly coming together over time. I've also manged to tell it to NOT look in any folder mentioning "old versions" in its name. Here is the revised Ilogic I've been working on : 

Public Class PrintOptions
    Public PrinterName As String
    Public NumberOfCopies As Integer
    Public AllColorsAsBlack As Boolean
    ' Add any other properties you need for printing
End Class

Sub Main()
    OpenAllDrawingsAndPrint()
End Sub

Sub OpenAllDrawingsAndPrint()
    Dim oAss As AssemblyDocument = ThisApplication.ActiveDocument
    Dim assemblyDirectory As String = System.IO.Path.GetDirectoryName(oAss.FullFileName)
    Dim drawingPaths As New List(Of String)
    Dim searchPatterns As String() = {"*.idw", "*.dwg"}

    For Each searchPattern As String In searchPatterns
        Dim idwFiles As System.IO.FileInfo() = New System.IO.DirectoryInfo(assemblyDirectory).GetFiles(searchPattern, System.IO.SearchOption.AllDirectories)
        For Each idwFile As System.IO.FileInfo In idwFiles
            If Not idwFile.FullName.Contains("OldVersions") Then
                Dim oDoc As Document = ThisApplication.Documents.Open(idwFile.FullName)
                If Not IsPartSuppressedOrReference(oDoc) Then
                    AdjustPrintSettings()
                End If
                oDoc.Close(True)
            End If
        Next
    Next
End Sub

Function IsPartSuppressedOrReference(doc As Document) As Boolean
    If TypeOf doc Is PartDocument Then
        Dim oPart As PartDocument = CType(doc, PartDocument)
        Return oPart.ComponentDefinition.Suppressed OrElse oPart.ComponentDefinition.ReferenceComponents.Count > 0
    End If
    Return False
End Function

Sub AdjustPrintSettings()
    Dim oPrintMgr As Object
    Dim activeSheetHeight As Double
    Dim activeSheetWidth As Double

    oPrintMgr = ThisApplication.ActiveDocument.PrintManager
    activeSheetHeight = ThisApplication.ActiveDocument.ActiveSheet.Height
    activeSheetWidth = ThisApplication.ActiveDocument.ActiveSheet.Width

    oPrintMgr.ColorMode = kPrintGrayScale
    oPrintMgr.NumberOfCopies = 1
    oPrintMgr.Orientation = kPortraitOrientation
    oPrintMgr.PaperSize = kPaperSizeCustom
    oPrintMgr.PaperHeight = activeSheetHeight
    oPrintMgr.PaperWidth = activeSheetWidth

    oPrintMgr.SubmitPrint

    Debug.Print("Print output sheet size set to " & activeSheetWidth & " x " & activeSheetHeight & " inches.")
End Sub

 

0 Likes
Message 4 of 4

SELF UPDATE :

I put a little bit more work and trial and error and I believe I have the code set to open all drawings inside an assembly including the assembly drawing itself even if the drawing is in a sub directory (But making sure not to look into directories named "OldVerions") but to not open or print drawings of parts marked as "Suppressed" unfortunately It still grabs parts marked as "Reference" but I think I can live with this because from an engineering perspective having documents that are reference can help with assembling and not often do I see an assembly made up of a majority of "Reference parts". The only problem I'm having now is that I've been mostly testing with PDF just to save paper but moving onto actually printing I am having some formatting issues. Printing on our plotter D-size and C-size comes out perfect (maybe tight boarders but still pretty perfect for Ilogic code) when it comes to B-size and A-Size it prints but there's a large space on one end of both of them so I need to find a way to either adjust length of the print or its orientation possibly? I've been playing a lot with the code in just sections really and patching it together with hot glue so if I have time tomorrow I can try and play with my two parts that are A and B size and then see if the code still works with other sizes. On our plotter seems to print pretty good but our network printer for the department gets mad and requests I tell it the exact size format of paper I want to use. Here is my current code I will be titling "Matts Batch Printer Rev 2" :

Imports System.Runtime.InteropServices

Public Class PrintOptions
Public PrinterName As String
Public NumberOfCopies As Integer
Public AllColorsAsBlack As Boolean
End Class

Sub Main()
Try
' Get the application object
Dim invApp As Inventor.Application = Marshal.GetActiveObject("Inventor.Application")

' Get the active document (the assembly)
Dim assemblyDoc As AssemblyDocument = invApp.ActiveDocument

' Specify the directory where the drawings are located
Dim drawingsDirectory As String = "Z:\Matt McCartin\Matthew McCartin\passion projects\Department\auto printer"

' Get the assembly drawing file name
Dim assemblyDrawingFileName As String = System.IO.Path.GetFileNameWithoutExtension(assemblyDoc.FullFileName) & ".idw"

' Handle the assembly drawing
HandleDrawing(invApp, drawingsDirectory, assemblyDrawingFileName)

' Iterate through all parts in the assembly
For Each oOccurrence As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences
' Check if the part is active
If oOccurrence.Suppressed = False Then
' Get the part document
Dim partDoc As PartDocument = oOccurrence.Definition.Document

' Get the drawing file name
Dim drawingFileName As String = System.IO.Path.GetFileNameWithoutExtension(partDoc.FullFileName) & ".idw"

' Handle the part drawing
HandleDrawing(invApp, drawingsDirectory, drawingFileName)
End If
Next
Catch ex As Exception
' Display the error message
MsgBox("An error occurred: " & ex.Message)
End Try
End Sub

Sub HandleDrawing(invApp As Inventor.Application, drawingsDirectory As String, drawingFileName As String)
' Get all files in the directory and subdirectories that match the drawing file name
Dim files As String() = System.IO.Directory.GetFiles(drawingsDirectory, drawingFileName, System.IO.SearchOption.AllDirectories)

' Iterate through all matching files
For Each File As String In files
' Check if the directory is not labeled "OldVersions"
If Not File.Contains("\OldVersions\") Then
' Open the drawing
Dim drawingDoc As DrawingDocument = invApp.Documents.Open(File)

' Print the drawing
Dim printOptions As New PrintOptions
printOptions.PrinterName = "YourPrinterName"
printOptions.NumberOfCopies = 1
printOptions.AllColorsAsBlack = True

' Adjust print settings
Dim oPrintMgr As Object
Dim activeSheetHeight As Double
Dim activeSheetWidth As Double

oPrintMgr = drawingDoc.PrintManager
activeSheetHeight = drawingDoc.ActiveSheet.Height
activeSheetWidth = drawingDoc.ActiveSheet.Width

oPrintMgr.ColorMode = kPrintGrayScale
oPrintMgr.NumberOfCopies = 1
oPrintMgr.Orientation = kPortraitOrientation
oPrintMgr.PaperSize = kPaperSizeCustom
oPrintMgr.PaperHeight = activeSheetHeight
oPrintMgr.PaperWidth = activeSheetWidth

Try
oPrintMgr.SubmitPrint()
MsgBox("Printing " & File & " using printer " & printOptions.PrinterName & "...")
Catch ex As Exception
MsgBox("Error while printing " & File & ": " & ex.Message)
End Try

' Close the drawing
drawingDoc.Close(True)
End If
Next
End Sub

0 Likes