×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Save as using VB

Save as using VB

Save as using VB

(OP)
Ok, I am trying to save out some solidworks drawing sheets with the maping on using VB.  I can get the file to save out as the sheet name but the mapping never happens (aka: everything is on the same layer).  Does anyone spot something in my code that is preventing this?



Public Enum swSaveAsVersion_e
    swSaveAsCurrentVersion = 0  '  default
    swSaveAsFormatProE = 2      '  save SolidWorks part as Pro/E format .prt/.asm extension (not as SolidWorks.prt/.asm)
End Enum

Public Enum swSaveAsOptions_e
    swSaveAsOptions_Silent = &H1            '  Save document silently or not
    swSaveAsOptions_Copy = &H2              '  Save document as a copy or not
    swSaveAsOptions_SaveReferenced = &H4    '  Save referenced documents or not (drawings and parts only)
End Enum

Public Enum swFileSaveError_e
    swGenericSaveError = &H1
    swReadOnlySaveError = &H2
    swFileNameEmpty = &H4                       '  The filename cannot be empty
    swFileNameContainsAtSign = &H8              '  The filename cannot contain an the at-sign character (@)
    swFileLockError = &H10
    swFileSaveFormatNotAvailable = &H20         '  The Save As file type is not valid
    swFileSaveAsDoNotOverwrite = &H80           '  The user chose not to overwrite an existing file
    swFileSaveAsInvalidFileExtension = &H100    '  The file extension differs from the SolidWorks document type
End Enum

Public Enum swFileSaveWarning_e
    swFileSaveWarning_RebuildError = &H1    '  The file was saved with a rebuild error
End Enum

Public Enum swDxfFormat_e
    swDxfFormat_R12 = 0
    swDxfFormat_R13 = 1
    swDxfFormat_R14 = 2
    swDxfFormat_R2000 = 3
End Enum

Public Enum swArrowDirection_e
    swINSIDE = 0
    swOUTSIDE = 1
    swSMART = 2
End Enum

Public Enum swUserPreferenceToggle_e
    swDxfMapping = 8
    swDXFDontShowMap = 21
End Enum

Public Enum swUserPreferenceIntegerValue_e
    swDxfVersion = 0
    swDxfOutputFonts = 1
    swDxfMappingFileIndex = 2
    swDxfOutputLineStyles = 135
    swDxfOutputNoScale = 136
End Enum

Public Enum swUserPreferenceDoubleValue_e
    swDxfOutputScaleFactor = 79
End Enum

Public Enum swUserPreferenceStringListValue_e
    swDxfMappingFiles = 0
End Enum

Sub main()
    Dim swApp                       As Object
    Dim swModel                     As Object
    Dim swDraw                      As Object
    Dim vSheetName                  As Variant
    Dim nErrors                     As Long
    Dim nWarnings                   As Long
    Dim nRetval                     As Long
    Dim bShowMap                    As Boolean
    Dim nNumSheet                   As Long
    Dim i                           As Long
    Dim bRet                        As Boolean
    
    Set swApp = CreateObject("sldworks.application")
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel

    ' Current settings
    Debug.Print "DxfMapping             = " & swApp.GetUserPreferenceToggle(swDxfMapping)
    Debug.Print "DXFDontShowMap         = " & swApp.GetUserPreferenceToggle(swDXFDontShowMap)
    Debug.Print "DxfVersion             = " & swApp.GetUserPreferenceIntegerValue(swDxfVersion)
    Debug.Print "DxfOutputFonts         = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputFonts)
    Debug.Print "DxfMappingFileIndex    = " & swApp.GetUserPreferenceIntegerValue(swDxfMappingFileIndex)
    Debug.Print "DxfOutputLineStyles    = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputLineStyles)
    Debug.Print "DxfOutputNoScale       = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputNoScale)
    Debug.Print "DxfOutputScaleFactor   = " & swApp.GetUserPreferenceDoubleValue(swDxfOutputScaleFactor)
    Debug.Print "DxfMappingFiles        = " & swApp.GetUserPreferenceStringListValue(swDxfMappingFiles)
    Debug.Print ""
    
    ' Turn off showing of map
    swApp.SetUserPreferenceToggle swDXFDontShowMap, False
    
    vSheetName = swDraw.GetSheetNames
    For i = 0 To UBound(vSheetName)
        bRet = swDraw.ActivateSheet(vSheetName(i))
        bRet = swModel.SaveAs4(vSheetName(i) & ".dxf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
        Debug.Assert bRet
    Next i
    
    ' Switch back to first sheet
    bRet = swDraw.ActivateSheet(vSheetName(0))
    
    ' Restore old setting
    swApp.SetUserPreferenceToggle swDXFDontShowMap, bShowMap
    
End Sub

RE: Save as using VB

maybe you should add
swApp.SetUserPreferenceToggle swDxfMapping, True

before:
swApp.SetUserPreferenceToggle swDXFDontShowMap, False

I've done a lot of saveAs-macro's. But my company doesn;t use layers so I dont really have experience with it.

Bouke Brouwers
P4 2,8 2Gb RAM, Quadro FX 500
SW2005 SP2.0

RE: Save as using VB

(OP)
I tried that.  It still doesn't work.  It still saves as a drawing but with everything on one layer.

RE: Save as using VB

Are you sure the format you are saving to supports layers?  Have you successfully saved with mapped layers directly from SW?

RE: Save as using VB

(OP)
Yes, I have successfully saved with mapped layers directly from SW in both .dxf and .dwg formats.

To do this I have a drawing open.
Go to File Save As.
Change file type to .dxf or .dwg.
Click the options button if the mapping isn't already enabled and enable it (if mapping is aready enabled you can skip this step).
And hit the save button.
Because I have a map file already set up I just hit ok when the "Solidworks To DXF/DWG Mapping" screen pops up.
Then there is a file saved, with layers.

Now if I could only do this with a macro.

RE: Save as using VB

swDXFDontShowMap should be set to True - as in, it's True that you don't want to show the mapping.

Also add:
swApp.SetUserPreferenceToggle swDxfMapping, True

To force custom mapping on.

RE: Save as using VB

(OP)
Still doesn't work engAlright.  I am starting to think it can't be done.  Has anyone been able to do this?

RE: Save as using VB

I tried your code with swDXFDontShowMap set to True and it worked fine.  

When you do the "Save As" to DXf, are you sure that the "Custom Map to Solidworks" option box is selected, and that it's pointing to the correct location for the mapping file?

RE: Save as using VB

(OP)
In the "Custom Map Solidworks to DXF" option there is a checkmark beside enable and the "Map file:" location is set to the correct file.  I also changed swDXFDontShowMap to true.  But the DXF file it creates is only one layer.  If I do the save as dxf file manualy it will work with the layers.

RE: Save as using VB

(OP)
Hey Guys,

Thanks for your help.  I submited a help request through Solidworks apisupport division.  The problem it seems was I was using Solidworks 2005 SP0.1.  Once we upgraded to Solidworks 2005 SP02 the program worked.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources