You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何SolidWorks的Extension.SaveAs3无法保存指定新坐标系的STEP文件?

解决SolidWorks VBA导出STEP时指定坐标系不生效的问题

我经常收到坐标系不一致的STEP文件,因此需要通过VBA将SolidWorks零件的主坐标系转换为指定坐标系后导出STEP。在Stack Overflow找到一段VBA代码,但测试时发现:虽然代码中修改的偏好设置已生效(通过断点验证),但生成的STEP文件仍使用旧坐标系;而手动操作保存时,STEP文件能正确应用指定坐标系。

原代码如下:

Sub SaveToStep()
    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
    'Declare variables

    Dim OutputCoordSys As String
    Dim FileToExport As String
    Dim StepFile As String

    Set swApp = Application.SldWorks

    FileToExport = "C:\Users\mypath\mypart.SLDPRT"
    
    OutputCoordSys = "Koordinatensystem1"
    
    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 

问题原因

原代码使用swApp.SetUserPreferenceStringValue设置的是全局用户偏好,但SolidWorks导出STEP时,文档自身的导出设置优先级更高。手动保存时,系统会读取当前文档的坐标系选择,而代码中未针对文档设置导出坐标系,导致全局设置被文档默认值覆盖。

解决方案

使用ModelDoc2.SetSaveAsCoordinateSystem方法,直接为当前要导出的文档指定目标坐标系,替代全局偏好设置。

修改后的代码如下:

Sub SaveToStep()
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc2
    Dim nStatus                 As Long
    Dim nErrors                 As Long
    Dim nWarnings               As Long

    Dim OutputCoordSys As String
    Dim FileToExport As String
    Dim StepFile As String

    Set swApp = Application.SldWorks

    FileToExport = "C:\Users\mypath\mypart.SLDPRT"
    OutputCoordSys = "Koordinatensystem1"
    StepFile = Replace(FileToExport, ".SLDPRT", ".STEP")
    
    '打开文档
    Set swModel = swApp.OpenDoc6(FileToExport, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", nStatus, nWarnings)
    
    '设置STEP版本和外观导出(全局偏好可保留)
    swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swStepAP, 214
    swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swStepExportAppearances, True
    
    '关键修改:针对当前文档指定导出坐标系
    swModel.SetSaveAsCoordinateSystem OutputCoordSys
    
    '导出为STEP
    swModel.Extension.SaveAs3 StepFile, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, Nothing, nErrors, nWarnings
    
    '关闭文档(可选,根据需求添加)
    swApp.CloseDoc swModel.GetPathName
End Sub

修改说明

  1. 移除了不必要的文档激活步骤,直接使用打开后的swModel对象操作,简化流程
  2. 用swModel.SetSaveAsCoordinateSystem OutputCoordSys替代全局偏好设置,直接为当前文档绑定导出坐标系,确保优先级
  3. 优化文档打开参数,使用枚举常量替代数字,提升代码可读性
  4. 可选添加文档关闭步骤,避免占用SolidWorks资源

内容的提问来源于stack exchange,提问作者user28659248

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:53:22