save only sheet metal pdf

save only sheet metal pdf

engineeringSEZWD
Contributor Contributor
102 Views
1 Reply
Message 1 of 2

save only sheet metal pdf

engineeringSEZWD
Contributor
Contributor

Hello

i am trying to elaborate an ilogic rule on the 2D drawing that go through  all my 40 pages of sheet drawings. here in the company we create only one 2d Drawing file and we add many pages with assemply, parts frames.

so the idea of this ilogic is go through all pages, and when find a page that has a sheet metal active in the page, export only that page with the name of the sheet.

however the ilogic rule does almos everything perfect

It does find only the sheet with sheet metal, 

it does save the pdf with the correct name of the sheet with sheet metal
It does save current page, although  the current page is not the page with the sheet metal active, it saving the current page that I ran the macro. where usually is the main assembly when I open the drawing.

 

 

thank you for the help

 

' Get the active document (must be a drawing)
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument

If oDoc.DocumentType <> kDrawingDocumentObject Then
MessageBox.Show("Open a Drawing (IDW/DWG) before running this script!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If

' Get PDF Add-in
Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

' Get the file path of the current drawing
Dim drawingFolder As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

' Loop through all sheets in the drawing
Dim exportedCount As Integer = 0

For Each oSheet As Sheet In oDoc.Sheets
' Check if the sheet has at least one drawing view
If oSheet.DrawingViews.Count = 0 Then Continue For ' Skip empty sheets

' Get the referenced document from the first view on the sheet
Dim oReferencedDoc As Document
Try
oReferencedDoc = oSheet.DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
Catch
Continue For ' Skip sheets if reference retrieval fails
End Try

' Ensure a valid referenced document exists
If oReferencedDoc Is Nothing Then Continue For

' Check if the referenced model is a Sheet Metal part
Dim isSheetMetal As Boolean = False

If oReferencedDoc.DocumentType = kPartDocumentObject Then
Dim oPartDoc As PartDocument = oReferencedDoc

' Check if ComponentDefinition is actually Sheet Metal
If TypeOf oPartDoc.ComponentDefinition Is SheetMetalComponentDefinition Then
isSheetMetal = True
End If
End If

' If it's not a Sheet Metal part, skip this sheet
If Not isSheetMetal Then Continue For

' Get the sheet name and clean it up (remove everything after ":")
Dim sheetName As String = oSheet.Name
If sheetName.Contains(":") Then
sheetName = sheetName.Split(":")(0).Trim()
End If

' Define the PDF file path
Dim pdfPath As String = System.IO.Path.Combine(drawingFolder, sheetName & ".pdf")

' Set up export options
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism

Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
oOptions.Value("Sheet_Range") = "Sheet" ' Ensures only the current sheet is exported
oOptions.Value("Custom_Sheet_Range") = oSheet.Name ' Specify the exact sheet name

Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
oDataMedium.FileName = pdfPath

' Export the sheet to PDF
If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
exportedCount += 1
End If
Next ' <-- Make sure this matches the 'For Each' loop

' Show final message
If exportedCount > 0 Then
MessageBox.Show(exportedCount & " Sheet Metal sheets exported as PDF!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("No Sheet Metal sheets found to export!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

0 Likes
103 Views
1 Reply
Reply (1)
Message 2 of 2

a.brusamolino
Enthusiast
Enthusiast

Hi @engineeringSEZWD 

 

I think the problem is here:

oOptions.Value("Sheet_Range") = "Sheet"

"Sheet" is not a recognized value for Sheet_Range option. Try the following steps:

  • Add "oSheet.Activate" after "If Not isSheetMetal Then Continue For" to make oSheet the actual current sheet
If Not isSheetMetal Then Continue For
oSheet.Activate
  • Change the Sheet_Range value to kPrintCurrentSheet and put it in the last If statement
If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
   oOptions.Value("Sheet_Range") = kPrintCurrentSheet
   oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
   exportedCount += 1
End If

 

Let me know if it helps!

0 Likes