vbacoordinate-systemssolidworksstep

How to save Solidworks file as step 214 and also include accessory coordinate system using VBA?


Currently I have a working script that saves a Solidworks part as step file. However, when I open this file, it has no color. I already read about needing to save as 214 step. However, I cannot find it. If I record a macro the "save as 214 step" and the "include coordinate system options" are not visible. How can I implement these userpreferences? I tried some code I found online, but I find it hard to implement it correctly.

A small part of my code, basically what it does: In an already openend Solidworks assembly, it saves a particular part and saves it with the extention "step".

Sub SaveFiles()

'Declare variables
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Dim Step As Long
Dim PathInit, PathCut  As String
Dim instance As ISldWorks

    'Use opened file as active document
    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc
    
    Set SelMgr = Part.SelectionManager
    
    'Open and Save
    Set Part = swApp.OpenDoc6("filenameold.SLDPRT", 1, 0, "", longstatus, longwarnings)

    'Assign correct STEP type DOES NOT WORK
    Set SelMgr = Part.SelectionManager
    Step = swApp.SetUserPreferenceIntegerValue(swStepAP, 214)

    Part.Extension.SetUserPreferenceString swFileSaveAsCoordinateSystem, 0, "Coordinate System1" 'DOES NOT WORK
    SavePart = Part.SaveAs3("filenamenew.STEP", 0, 2)
End sub

How and where do I define that my step file should be 214 and what coordinate system to include? If anything is unclear, or I need to share more code, please let me know. Thank you in advance.


Solution

  • The code bellow will open a part file that has a coordinate system named Coordinate System Test

    Since SaveAs3 Method (IModelDocExtension) will be used to export to step, the file is activated and after that we get the active document, as documented in Remarks here. Note that this save method is not the same that you have in your code. You are using the now obsolete SaveAs3 Method (IModelDoc2). Strangely enough the SolidWorks VBA recorder also uses this obsolete method...

    Then all the relevant user preferences on the the system options are defined. If you need the textures to go with the step file you need to assure that the option is checked.

    And after that the file is saved with the already referenced method.

    Tested with one part that has a face colored red and a coordinate system at 50,50,50 and it worked just fine.

    Option Explicit
    
        Dim swApp                   As SldWorks.SldWorks
        Dim swModel                 As SldWorks.ModelDoc2
        Dim swModelActivated        As SldWorks.ModelDoc2
        Dim swModelToExport         As SldWorks.ModelDoc2
        Dim strModelName            As String
        Dim nStatus                 As Long
        Dim nErrors                 As Long
        Dim nWarnings               As Long
        
    Sub SaveToStep()
    
        'Declare variables
    
        Dim OutputCoordSys As String
        Dim FileToExport As String
        Dim StepFile As String
    
        Set swApp = Application.SldWorks
    
        FileToExport = "C:\temp\ExportTestPart.SLDPRT"
        
        OutputCoordSys = "Coordinate System Test"
        
        StepFile = Replace(FileToExport, ".SLDPRT", ".STEP")
        
        'Open
        Set swModel = swApp.OpenDoc6(FileToExport, 1, 0, "", nStatus, nWarnings)
            
        'Activate the model
        strModelName = swModel.GetTitle
        
        Set swModelActivated = swApp.ActivateDoc3(strModelName, False, swRebuildOnActivation_e.swUserDecision, nErrors)
        
        'Get the active model
        Set swModelToExport = swApp.ActiveDoc
        
        'Assign correct STEP
        swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swStepAP, 214
        
        'Set Export appearences option to true
        swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swStepExportAppearances, True
    
        'Assign Output coordinate system to use
        swApp.SetUserPreferenceStringValue swUserPreferenceStringValue_e.swExportOutputCoordinateSystem, OutputCoordSys
        
        'Save the file as step
        swModelToExport.Extension.SaveAs3 StepFile, 0, 1, Nothing, Nothing, nErrors, nWarnings
        
    End Sub
    

    I'm not sure if I understood your comments on the OP, but not having textures on the step does not tell us that it is a 203 or a 214 step. If you have textures you can be sure that it is a 214 step file, but not the other way around.

    What you can do is open the file with a text editor and check the header. These are the first four lines on the file created with the code I've posted:

    ISO-10303-21;
    HEADER;
    FILE_DESCRIPTION (( 'STEP AP214' ),
    '1' );