Python SolidWorks 二次开发—SolidWorks打开新文件
Python SolidWorks 二次开发—SolidWorks打开新文件
文章目录
一、连接SolidWorks 二次开发
连接SolidWorks见>>Python SolidWorks 二次开发—Python如何连接SolidWorks
二、打开文件函数:OpenDoc6
使用OpenDoc6函数打开文件
1.OpenDoc6函数定义
函数原型如下:
Function OpenDoc6( _
ByVal FileName As System.String, _
ByVal Type As System.Integer, _
ByVal Options As System.Integer, _
ByVal Configuration As System.String, _
ByRef Errors As System.Integer, _
ByRef Warnings As System.Integer _
) As ModelDoc2
参数说明:
FileName:文件全路径名,包含扩展名,如果文件包含在已打开的装配体中,文件名可不包含路径
Type:打开文件的类型,包含以下选项
Member | Description |
---|---|
swDocASSEMBLY | 2 |
swDocDRAWING | 3 |
swDocIMPORTED_ASSEMBLY | 7; Multi-CAD |
swDocIMPORTED_PART | 6; Multi-CAD |
swDocLAYOUT | 5 |
swDocNONE | 0 |
swDocPART | 1 |
swDocSDM | 4 |
Options:文件的选项,包含如下选项
Member | Description |
---|---|
swOpenDocOptions_AutoMissingConfig | 32 or 0x20 = Obsolete; do not use The software automatically uses the last-used configuration of a model when it discovers missing configurations or component references as it silently opens drawings and assemblies. |
swOpenDocOptions_DontLoadHiddenComponents | 256 or 0x100 = By default, hidden components are loaded when you open an assembly document. Set swOpenDocOptions_DontLoadHiddenComponents to not load hidden components when opening an assembly document |
swOpenDocOptions_LoadExternalReferencesInMemory | 512 or 0x200 = Open external references in memory only |
swOpenDocOptions_LoadLightweight | 128 or 0x80 = Open assembly document as lightweight NOTE: The default for whether an assembly document is opened lightweight is based on a registry setting accessed via Tools, Options, Assemblies or with the user preference setting swAutoLoadPartsLightweight To override the default and specify a value with ISldWorks::OpenDoc6, set swOpenDocOptions_OverrideDefaultLoadLightweight. If set, then you can set swOpenDocOptions_LoadLightweight to open an assembly document as lightweight |
swOpenDocOptions_LoadModel | 16 or 0x10 = Load Detached model upon opening document (drawings only) |
swOpenDocOptions_OverrideDefaultLoadLightweight | 64 or 0x40 = Override default setting whether to open an assembly document as lightweight |
swOpenDocOptions_RapidDraft | 8 or 0x8 = Convert document to Detached format (drawings only) |
swOpenDocOptions_ReadOnly | 2 or 0x2 = Open document read only |
swOpenDocOptions_Silent | 1 or 0x1 = Open document silently |
swOpenDocOptions_ViewOnly 4 | or 0x4 = Open document in Large Design Review mode only (assemblies only) |
Configuration:打开文件的配置名称
Errors:打开文件出现错误的定义,包含如下选项
Member | Description |
---|---|
swAddinInteruptError | 1048576 or 0x100000: The user attempted to open a file, and then interrupted the open-file routine to open a different file |
swBasePartNotLoadedWarn | Obsolete; see swFileLoadWarning_e |
swDrawingANSIUpdateWarn | Obsolete; see swFileLoadWarning_e |
swDrawingSFSymbolConvertWarn | Obsolete; see swFileLoadWarning_e |
swDrawingsOnlyRapidDraftWarn | Obsolete; see swFileLoadWarning_e |
swFileAlreadyOpenWarn | Obsolete; see swFileLoadWarning_e |
swFileCriticalDataRepairError | 4194304 or 0x400000 = A document has critical data corruption |
swFileNotFoundError | 2 or 0x2 = Unable to locate the file; the file is not loaded or the referenced file (that is, component) is suppressed |
swFileRequiresRepairError | 2097152 or 0x200000 = A document has non-critical custom property data corruption |
swFileWithSameTitleAlreadyOpen | 65536 or 0x10000 = A document with the same name is already open |
swFutureVersion | 8192 or 0x2000 = The document was saved in a future version of SOLIDWORKS |
swGenericError | 1 or 0x1 = Another error was encountered |
swIdMatchError | Obsolete; see swFileLoadWarning_e |
swInvalidFileTypeError | 1024 or 0x400 = File type argument is not valid |
swLiquidMachineDoc | 131072 or 0x20000 = File encrypted by Liquid Machines |
swLowResourcesError | 262144 or 0x40000 = File is open and blocked because the system memory is low, or the number of GDI handles has exceeded the allowed maximum |
swNeedsRegenWarn | Obsolete; see swFileLoadWarning_e |
swNoDisplayData | 524288 or 0x80000 = File contains no display data |
swReadOnlyWarn | Obsolete; see swFileLoadWarning_e |
swSharingViolationWarn | Obsolete; see swFileLoadWarning_e |
swSheetScaleUpdateWarn | Obsolete; see swFileLoadWarning_e |
swViewMissingReferencedConfig | Obsolete; see swFileLoadWarning_e |
swViewOnlyRestrictions | Obsolete; see swFileLoadWarning_e |
Warnings:打开文件出现警告的定义,包含如下选项
Member | Description |
---|---|
swFileLoadWarning_AlreadyOpen | 128 or 0x80 = Warning appears because the document is already open. |
swFileLoadWarning_AutomaticRepair | 262144 or 0x40000 = Warning appears that non-critical data in the document was automatically repaired. |
swFileLoadWarning_BasePartNotLoaded | 64 or 0x40 = Warning appears because the document was defined in the context of another existing document that is not loaded. |
swFileLoadWarning_ComponentMissingReferencedConfig | 32768 or 0x8000 = Warning appears if document is opened silently and swOpenDocOptions_AutoMissingConfig is specified. |
swFileLoadWarning_CriticalDataRepair | 524288 or 0x80000 = Warning appears that critical data in the document was automatically repaired. |
swFileLoadWarning_DimensionsReferencedIncorrectlyToModels | 16384 or 0x4000 = Warning appears because some dimensions are referenced to the models incorrectly; these dimensions are highlighted in red in the drawing. |
swFileLoadWarning_DrawingANSIUpdate | 8 or 0x8 = Warning appears because radial dimension arrows now displayed outside when the dimension text is outside of the arc or circle. |
swFileLoadWarning_DrawingSFSymbolConvert | 2048 or 0x800Warning appears asking the user if he or she wants to convert this drawing’s surface finish symbols to the sizes specified in ANSI Y14.36M-1996 and ISO 1302-1978. |
swFileLoadWarning_DrawingsOnlyRapidDraft | 256 or 0x100 = Warning appears because the only RapidDraft format conversion that can take place is a drawing document that is not Detached. |
swFileLoadWarning_IdMismatch | 1 or 0x1 = Warning appears if the internal ID of the document does not match the internal ID saved with the referencing document. |
swFileLoadWarning_InvisibleDoc_LinkedDesignTableUpdateFail | 65536 or 0x10000 = Warning issued because an attempt has been made to open an invisible document with a linked design table that was modified externally, and the design table cannot be updated because the document cannot be displayed; you must make the document visible to open it and update the design table. |
swFileLoadWarning_MissingDesignTable | 131072 or 0x20000 = Warning appears because the design table is missing. |
swFileLoadWarning_ModelOutOfDate | 8192 or 0x2000 = Warning appears because some sheets contain drawing views that are out of date with their external models. |
swFileLoadWarning_NeedsRegen | 32 or 0x20 = Warning appears because the document needs to be rebuilt. |
swFileLoadWarning_ReadOnly | 2 or 0x2 = Warning appears because the document is read only. |
swFileLoadWarning_RevolveDimTolerance | 4096 or 0x1000 = Warning appears because some of the tolerances of the revolved feature dimensions were created in SOLIDWORKS 99 or earlier and are not synchronized with their corresponding dimensions in the sketch. |
swFileLoadWarning_SharingViolation | 4 or 0x4 = Warning appears if the document is being used by another user. |
swFileLoadWarning_SheetScaleUpdate | 16 or 0x10 = Warning appears because SOLIDWORKS now applies the scale of the sheet to the sketch entities on the sheet; which means that the sheet looks the same but dimension values are scaled. |
swFileLoadWarning_ViewMissingReferencedConfig | 1024 or 0x400 = Warning appears because a configuration that a drawing view is referencing no longer exists in the model (part or assembly); the active configuration is used. |
swFileLoadWarning_ViewOnlyRestrictions | 512 or 0x200 = Warning appears because the document is view only and a configuration other than the default configuration is set. |
返回值,返回ModelDoc2对象,可进行属性修改等相关操作
2.OpenDoc6函数的使用
执行以下代码可直接在SolidWorks中打开文件
import win32com.client
import pythoncom
def opendoc():
# SolidWorks年份版本
sldver=2018
# 建立com连接,如只有一个版本,可以只写"SldWorks.Application"
swApp=win32com.client.Dispatch(f'SldWorks.Application.{sldver-1992}')
# 提升API交互效率
swApp.CommandInProgress =True
# 显示SolidWorks界面
swApp.Visible =True
# 打开文件
Errors=win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
Warnings=win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
swApp.OpenDoc6(r"C:\...\滑轨.SLDPRT",1,1,"",Errors,Warnings)
if __name__ == '__main__':
swApp=opendoc()
2.参数Errors和Warnings的说明
参数Errors和Warnings的设定可参考>>用 Python 玩转 SolidWorks
里面有详细的设定说明