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
:保存文件时的选项,包含以下选项
Member | Description |
---|---|
swSaveAsOptions_AvoidRebuildOnSave | 8 or 0x8 |
swSaveAsOptions_Copy | 2 or 0x2 |
swSaveAsOptions_DetachedDrawing | 128 or 0x80; Not a valid option for IPartDoc::SaveToFile2 |
swSaveAsOptions_IgnoreBiography | 256 or 0x100; Prune a SOLIDWORKS file’s revision history to just the current file name |
swSaveAsOptions_OverrideSaveEmodel | 32 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_SaveEmodelData | Obsolete. |
swSaveAsOptions_SaveReferenced | 4 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_Silent | 1 or 0x1 |
swSaveAsOptions_UpdateInactiveViews | 16 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
:保存文件出现错误的定义,包含如下选项
Member | Description |
---|---|
swFileLockError | 16 or 0x10 |
swFileNameContainsAtSign | 8 or 0x8 = File name cannot contain the at symbol (@) |
swFileNameEmpty | 4 or 0x4 = File name cannot be empty |
swFileSaveAsBadEDrawingsVersion | 1024 or 0x400 |
swFileSaveAsDoNotOverwrite | 128 or 0x80 = Do not overwrite an existing file |
swFileSaveAsInvalidFileExtension | 256 or 0x100 = File name extension does not match the SOLIDWORKS document type |
swFileSaveAsNameExceedsMaxPathLength | 2048 or 0x800 = File name cannot exceed 255 characters |
swFileSaveAsNoSelection | 512 or 0x200 = Save the selected bodies in a part document. Valid option for IPartDoc::SaveToFile2; however, not a valid option for IModelDocExtension::SaveAs |
swFileSaveAsNotSupported | 4096 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. |
swFileSaveFormatNotAvailable | 32 or 0x20 = Save As file type is not valid |
swFileSaveRequiresSavingReferences | 8192 or 0x2000 = Saving an assembly with renamed components requires saving the references |
swFileSaveWithRebuildError | Obsolete = See swFileSaveWarning_e |
swGenericSaveError | 1 or 0x1 |
swReadOnlySaveError | 2 or 0x2 |
Warnings
:保存文件出现警告的定义,包含如下选项
Member | Description |
---|---|
swFileSaveWarning_AnimatorCameraViews | 128 or 0x80 |
swFileSaveWarning_AnimatorFeatureEdits | 16 or 0x10 |
swFileSaveWarning_AnimatorLightEdits | 64 or 0x40 |
swFileSaveWarning_AnimatorNeedToSolve | 8 or 0x8 |
swFileSaveWarning_AnimatorSectionViews | 256 or 0x100 |
swFileSaveWarning_EdrwingsBadSelection | 32 or 0x20 |
swFileSaveWarning_MissingOLEObjects | 512 or 0x200 |
swFileSaveWarning_NeedsRebuild | 2 or 0x2 |
swFileSaveWarning_OpenedViewOnly | 1024 or 0x400 |
swFileSaveWarning_RebuildError | 1 or 0x1 |
swFileSaveWarning_ViewsNeedUpdate | 4 or 0x4 |
swFileSaveWarning_XmlInvalid | 2048 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()
版权声明:本文为Bluma原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。