Python SolidWorks 二次开发---SolidWorks保存文件

Python SolidWorks 二次开发—SolidWorks保存文件

Python SolidWorks 二次开发—SolidWorks保存文件



一、连接SolidWorks 二次开发

连接SolidWorks见>>Python SolidWorks 二次开发—Python如何连接SolidWorks

二、保存文件函数:Save3

使用Save3函数进行文件的保存

1.Save3函数定义

函数原型如下:

Function Save3( _
   ByVal Options As System.Integer, _
   ByRef Errors As System.Integer, _
   ByRef Warnings As System.Integer _
) As System.Boolean

参数说明:
Options:保存文件时的选项,包含以下选项

MemberDescription
swSaveAsOptions_AvoidRebuildOnSave8 or 0x8
swSaveAsOptions_Copy2 or 0x2
swSaveAsOptions_DetachedDrawing128 or 0x80; Not a valid option for IPartDoc::SaveToFile2
swSaveAsOptions_IgnoreBiography256 or 0x100; Prune a SOLIDWORKS file’s revision history to just the current file name
swSaveAsOptions_OverrideSaveEmodel32 or 0x20; Saves eDrawings-related information into a section of the file being saved; specifying this setting overrides the Tools, Options, System Options, General, Save eDrawings data in SOLIDWORKS document setting; not a valid option for IPartDoc::SaveToFile2
swSaveAsOptions_SaveEmodelDataObsolete.
swSaveAsOptions_SaveReferenced4 or 0x4; Supports parts, assemblies, and drawings; this setting indicates to save all components (sub-assemblies and parts) in both assemblies and drawings; if a part has an external reference, then this setting indicates to save the external reference
swSaveAsOptions_Silent1 or 0x1
swSaveAsOptions_UpdateInactiveViews16 or 0x10; Not a valid option for IPartDoc::SaveToFile2; this setting is only applicable for a drawing that has one or more sheets; this setting updates the views on inactive sheets

Errors:保存文件出现错误的定义,包含如下选项

MemberDescription
swFileLockError16 or 0x10
swFileNameContainsAtSign8 or 0x8 = File name cannot contain the at symbol (@)
swFileNameEmpty4 or 0x4 = File name cannot be empty
swFileSaveAsBadEDrawingsVersion1024 or 0x400
swFileSaveAsDoNotOverwrite128 or 0x80 = Do not overwrite an existing file
swFileSaveAsInvalidFileExtension256 or 0x100 = File name extension does not match the SOLIDWORKS document type
swFileSaveAsNameExceedsMaxPathLength2048 or 0x800 = File name cannot exceed 255 characters
swFileSaveAsNoSelection512 or 0x200 = Save the selected bodies in a part document. Valid option for IPartDoc::SaveToFile2; however, not a valid option for IModelDocExtension::SaveAs
swFileSaveAsNotSupported4096 or 0x1000 = Save As operation:is not supported was executed is such a way that the resulting file might not be complete, possibly because SOLIDWORKS is hidden; if the error persists after setting SOLIDWORKS to visible and re-attempting the Save As operation, contact SOLIDWORKS API support.
swFileSaveFormatNotAvailable32 or 0x20 = Save As file type is not valid
swFileSaveRequiresSavingReferences8192 or 0x2000 = Saving an assembly with renamed components requires saving the references
swFileSaveWithRebuildErrorObsolete = See swFileSaveWarning_e
swGenericSaveError1 or 0x1
swReadOnlySaveError2 or 0x2

Warnings:保存文件出现警告的定义,包含如下选项

MemberDescription
swFileSaveWarning_AnimatorCameraViews128 or 0x80
swFileSaveWarning_AnimatorFeatureEdits16 or 0x10
swFileSaveWarning_AnimatorLightEdits64 or 0x40
swFileSaveWarning_AnimatorNeedToSolve8 or 0x8
swFileSaveWarning_AnimatorSectionViews256 or 0x100
swFileSaveWarning_EdrwingsBadSelection32 or 0x20
swFileSaveWarning_MissingOLEObjects512 or 0x200
swFileSaveWarning_NeedsRebuild2 or 0x2
swFileSaveWarning_OpenedViewOnly1024 or 0x400
swFileSaveWarning_RebuildError1 or 0x1
swFileSaveWarning_ViewsNeedUpdate4 or 0x4
swFileSaveWarning_XmlInvalid2048 or 0x800

返回值,返回布尔类型,保存成功放回true,失败返回falese

2.Save3函数的使用

执行以下代码可直接在SolidWorks中保存当前激活的文件

import win32com.client
from swconst import constants
import pythoncom

def savefile():
    # SolidWorks年份版本
    sldver=2018
    # 建立com连接,如只有一个版本,可以只写"SldWorks.Application"
    swApp=win32com.client.Dispatch(f'SldWorks.Application.{sldver-1992}')
    # 提升API交互效率
    swApp.CommandInProgress =True
    # 显示SolidWorks界面
    swApp.Visible =True
    # 获取当前激活文档对象
    swModel = swApp.ActiveDoc
    #错误和警告
    Errors=win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
    Warnings=win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
    # constants.swSaveAsOptions_Silent也可以直接按照选项说明写整数:1
    boolstatus = swModel.Save3(constants.swSaveAsOptions_Silent, Errors, Warnings)
    if boolstatus:
        print('文件保存成功')
    else:
        print(f'文件保存失败,出现如下错误:{Errors}')
        print(f'文件保存失败,出现如下警告:{Warnings}')


if __name__ == '__main__':
    savefile()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值