batch flat pattern
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi
I used google AI to write a ilogic code that creates dxf files for all open sheet metal parts.
what i want it to do is use my flat pattern dxf export options, add my job number to the end of the file name, and decide which side to use as the front ( which ever side has the most IV_BEND or bend lines (front) lines).
code below, any help appreciated
' 1. DEFINE YOUR INI PATH
Dim sIniFile As String = "C:\Users\design.BRISELEC\Documents\Drawing Standards and Parameters\FlatPatternConfiguration.ini"
If Not System.IO.File.Exists(sIniFile) Then
MessageBox.Show("Configuration file not found at:" & vbLf & sIniFile, "Error")
Return
End If
' 2. PROMPT FOR JOB NUMBER ONCE
Dim oJobNum As String = InputBox("Enter Job Number for ALL open parts:", "Batch Export", "")
If String.IsNullOrEmpty(oJobNum) Then Return
' 3. CAPTURE OPEN DOCUMENTS
Dim oDocsToProcess As New List(Of Document)
For Each oDoc As Document In ThisApplication.Documents.VisibleDocuments
oDocsToProcess.Add(oDoc)
Next
' 4. PROCESS LOOP
For Each oDoc As Document In oDocsToProcess
If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
oDoc.Activate() ' Prevents export errors in 2024
Dim oPartDoc As PartDocument = oDoc
If oPartDoc.ComponentDefinition.Type = ObjectTypeEnum.kSheetMetalComponentDefinitionObject Then
Dim oCompDef As SheetMetalComponentDefinition = oPartDoc.ComponentDefinition
' A. REFRESH FLAT PATTERN
If oCompDef.HasFlatPattern Then
Try : oCompDef.FlatPattern.Delete() : Catch : End Try
End If
Try : oCompDef.Unfold() : Catch : Continue For : End Try
' B. OPTIMIZE BEND DIRECTION (Favor Front/Up Bends)
Dim oFlatPattern As FlatPattern = oCompDef.FlatPattern
Dim upCount As Integer = 0
Dim downCount As Integer = 0
For Each oResult As FlatBendResult In oFlatPattern.FlatBendResults
If oResult.IsDirectionUp Then upCount += 1 Else downCount += 1
Next
If downCount > upCount Then
oFlatPattern.FlatPatternOrientations.ActiveFlatPatternOrientation.FlipBaseFace = Not oFlatPattern.FlatPatternOrientations.ActiveFlatPatternOrientation.FlipBaseFace
End If
' C. DEFINE FILENAME
Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)
Dim oFilePath As String = System.IO.Path.GetDirectoryName(oPartDoc.FullFileName)
Dim sFullDXFName As String = oFilePath & "\" & oFileName & " - " & oJobNum & ".dxf"
' D. ROBUST EXPORT STRING (Forces valid version and loads INI)
' Using AcadVersion=2018 is standard for 2024. Use R12 if your CNC is very old.
Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2018&ConfigFile=" & sIniFile
Try
' This method is the most direct way to bypass "not valid" errors
oCompDef.DataIO.WriteDataToFile(sOut, sFullDXFName)
oPartDoc.Save()
oPartDoc.Close(True)
Catch ex As Exception
' Skips read-only or locked files
End Try
End If
End If
Next
MessageBox.Show("Batch processing complete.", "Success")