MACRO LIBRARY

Export sheet metal flat pattern to specified location

Free public access. No account or subscription required.

Instructions

Preconditions
Sheet metal part is open.
Result
DWG is exported to chosen location.

Files

Export sheet metal flat pattern to specified location

Code

Module: mMain

'Saves out a sheet metal flat pattern with specified extension at _
    specified location.
    
'Preconditions: sheet metal part is open
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim strPath As String
    Dim strFileName As String
    Dim strExtension As String
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    'Verify that part is open
    If swModel.GetType <> swDocPART Then
        MsgBox "Please open a sheet metal part."
        End
    End If
    
    Set swPart = swModel
    
    'Verify that model is saved
    If swModel.GetPathName = Empty Then
        swApp.SendMsgToUser "File must be saved."
        End
    End If
    
    'Verify that model contains sheet metal
    Dim swFeat As SldWorks.Feature
    Dim blnFound As Boolean
    
    blnFound = False
    Set swFeat = swModel.FirstFeature
    Do While Not swFeat Is Nothing
        If swFeat.GetTypeName = "SheetMetal" Then
            blnFound = True
            Exit Do
        End If
        Set swFeat = swFeat.GetNextFeature
    Loop
    
    If blnFound = False Then
        swApp.SendMsgToUser "Not a sheet metal part."
        End
    End If
    
    'Get extension
    strExtension = InputBox("Enter a file extension like DWG or DXF.")
    If strExtension = Empty Then Exit Sub
    
    'Browse for folder
    strPath = BrowseFolder("Browse")
    
    'Get title
    strFileName = swModel.GetTitle
    
    'Remove extension, if present
    If InStr(strFileName, ".") <> 0 Then _
        strFileName = Left(strFileName, InStr(strFileName, ".") - 1)
    
    'Check if file already exists. If yes, prompt for a different file name.
    Dim fso As New FileSystemObject
    If fso.FileExists(strFileName) Then
        strFileName = InputBox("Please enter a new file name without the extension.")
        If strFileName = Empty Then Exit Sub
    End If
    
    'Save out flat pattern
    swPart.ExportFlatPatternView strPath & "\" & strFileName & "." & _
        strExtension, swExportFlatPatternOption_None
End Sub

Module: mBrowseFolder

#If VBA7 Then
    
Private Type BROWSEINFO
hOwner As LongPtr
pidlRoot As LongPtr
pszDisplayName As String
lpszTitle As String
ulFlags As LongPtr
lpfn As LongPtr
lParam As LongPtr
iImage As LongPtr
End Type
    
Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias _
            "SHGetPathFromIDListA" (ByVal pidl As LongPtr, _
            ByVal pszPath As String) As LongPtr
            
Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias _
            "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
            As LongPtr
            
Dim X As LongPtr, bi As BROWSEINFO, dwIList As LongPtr
    
#Else
    
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
    
Private Declare  Function SHGetPathFromIDList Lib "shell32.dll" Alias _
            "SHGetPathFromIDListA" (ByVal pidl As Long, _
            ByVal pszPath As String) As Long
            
Private Declare  Function SHBrowseForFolder Lib "shell32.dll" Alias _
            "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
            As Long
            
Dim X As Long, bi As BROWSEINFO, dwIList As Long
    
#End If
    
Private Const BIF_RETURNONLYFSDIRS = &H1
    
Public Function BrowseFolder(szDialogTitle As String) As String
    
Dim szPath As String, wPos As Integer
    
    With bi
        .hOwner = hWndAccessApp
        .lpszTitle = szDialogTitle
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With
    
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
    
    If X Then
        wPos = InStr(szPath, Chr(0))
        BrowseFolder = Left$(szPath, wPos - 1)
    Else
        BrowseFolder = vbNullString
    End If
End Function