Python SolidWorks 二次开发---SolidWorks打开新文件

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:打开文件的类型,包含以下选项

MemberDescription
swDocASSEMBLY2
swDocDRAWING3
swDocIMPORTED_ASSEMBLY7; Multi-CAD
swDocIMPORTED_PART6; Multi-CAD
swDocLAYOUT5
swDocNONE0
swDocPART1
swDocSDM4

Options:文件的选项,包含如下选项

MemberDescription
swOpenDocOptions_AutoMissingConfig32 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_DontLoadHiddenComponents256 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_LoadExternalReferencesInMemory512 or 0x200 = Open external references in memory only
swOpenDocOptions_LoadLightweight128 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_LoadModel16 or 0x10 = Load Detached model upon opening document (drawings only)
swOpenDocOptions_OverrideDefaultLoadLightweight64 or 0x40 = Override default setting whether to open an assembly document as lightweight
swOpenDocOptions_RapidDraft8 or 0x8 = Convert document to Detached format (drawings only)
swOpenDocOptions_ReadOnly2 or 0x2 = Open document read only
swOpenDocOptions_Silent1 or 0x1 = Open document silently
swOpenDocOptions_ViewOnly 4or 0x4 = Open document in Large Design Review mode only (assemblies only)

Configuration:打开文件的配置名称

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

MemberDescription
swAddinInteruptError1048576 or 0x100000: The user attempted to open a file, and then interrupted the open-file routine to open a different file
swBasePartNotLoadedWarnObsolete; see swFileLoadWarning_e
swDrawingANSIUpdateWarnObsolete; see swFileLoadWarning_e
swDrawingSFSymbolConvertWarnObsolete; see swFileLoadWarning_e
swDrawingsOnlyRapidDraftWarnObsolete; see swFileLoadWarning_e
swFileAlreadyOpenWarnObsolete; see swFileLoadWarning_e
swFileCriticalDataRepairError4194304 or 0x400000 = A document has critical data corruption
swFileNotFoundError2 or 0x2 = Unable to locate the file; the file is not loaded or the referenced file (that is, component) is suppressed
swFileRequiresRepairError2097152 or 0x200000 = A document has non-critical custom property data corruption
swFileWithSameTitleAlreadyOpen65536 or 0x10000 = A document with the same name is already open
swFutureVersion8192 or 0x2000 = The document was saved in a future version of SOLIDWORKS
swGenericError1 or 0x1 = Another error was encountered
swIdMatchErrorObsolete; see swFileLoadWarning_e
swInvalidFileTypeError1024 or 0x400 = File type argument is not valid
swLiquidMachineDoc131072 or 0x20000 = File encrypted by Liquid Machines
swLowResourcesError262144 or 0x40000 = File is open and blocked because the system memory is low, or the number of GDI handles has exceeded the allowed maximum
swNeedsRegenWarnObsolete; see swFileLoadWarning_e
swNoDisplayData524288 or 0x80000 = File contains no display data
swReadOnlyWarnObsolete; see swFileLoadWarning_e
swSharingViolationWarnObsolete; see swFileLoadWarning_e
swSheetScaleUpdateWarnObsolete; see swFileLoadWarning_e
swViewMissingReferencedConfigObsolete; see swFileLoadWarning_e
swViewOnlyRestrictionsObsolete; see swFileLoadWarning_e

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

MemberDescription
swFileLoadWarning_AlreadyOpen128 or 0x80 = Warning appears because the document is already open.
swFileLoadWarning_AutomaticRepair262144 or 0x40000 = Warning appears that non-critical data in the document was automatically repaired.
swFileLoadWarning_BasePartNotLoaded64 or 0x40 = Warning appears because the document was defined in the context of another existing document that is not loaded.
swFileLoadWarning_ComponentMissingReferencedConfig32768 or 0x8000 = Warning appears if document is opened silently and swOpenDocOptions_AutoMissingConfig is specified.
swFileLoadWarning_CriticalDataRepair524288 or 0x80000 = Warning appears that critical data in the document was automatically repaired.
swFileLoadWarning_DimensionsReferencedIncorrectlyToModels16384 or 0x4000 = Warning appears because some dimensions are referenced to the models incorrectly; these dimensions are highlighted in red in the drawing.
swFileLoadWarning_DrawingANSIUpdate8 or 0x8 = Warning appears because radial dimension arrows now displayed outside when the dimension text is outside of the arc or circle.
swFileLoadWarning_DrawingSFSymbolConvert2048 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_DrawingsOnlyRapidDraft256 or 0x100 = Warning appears because the only RapidDraft format conversion that can take place is a drawing document that is not Detached.
swFileLoadWarning_IdMismatch1 or 0x1 = Warning appears if the internal ID of the document does not match the internal ID saved with the referencing document.
swFileLoadWarning_InvisibleDoc_LinkedDesignTableUpdateFail65536 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_MissingDesignTable131072 or 0x20000 = Warning appears because the design table is missing.
swFileLoadWarning_ModelOutOfDate8192 or 0x2000 = Warning appears because some sheets contain drawing views that are out of date with their external models.
swFileLoadWarning_NeedsRegen32 or 0x20 = Warning appears because the document needs to be rebuilt.
swFileLoadWarning_ReadOnly2 or 0x2 = Warning appears because the document is read only.
swFileLoadWarning_RevolveDimTolerance4096 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_SharingViolation4 or 0x4 = Warning appears if the document is being used by another user.
swFileLoadWarning_SheetScaleUpdate16 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_ViewMissingReferencedConfig1024 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_ViewOnlyRestrictions512 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

里面有详细的设定说明

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值