主要参考Solidworks Macros via Python
绿色版的SolidWorks 2016: 三维机械制图 SolidWorks Premium 2016 完整绿色版
试用了一下,该绿色版有两点毛病:
第一点,直接运行win32com.client.Dispatch("SldWorks.Application")
会提示“无效的类字符串”错误,补全如下注册表内容可解决(自己修改程序路径):
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\SldWorks.Application]
@="SldWorks Application"
[HKEY_CLASSES_ROOT\SldWorks.Application\CLSID]
@="{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}"
[HKEY_CLASSES_ROOT\SldWorks.Application.24]
@="SldWorks 2016 Application"
"AllowSilentDefaultTakeOver"=""
[HKEY_CLASSES_ROOT\SldWorks.Application.24\CLSID]
@="{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}"
[HKEY_CLASSES_ROOT\SldWorks.Application.24\shell]
[HKEY_CLASSES_ROOT\SldWorks.Application.24\shell\open]
[HKEY_CLASSES_ROOT\SldWorks.Application.24\shell\open\command]
@="E:\\Programs\\SolidWorks Corp\\SOLIDWORKS\\SLDWORKS.exe \"%1\""
[HKEY_CLASSES_ROOT\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}]
@="SldWorks 2016 Application"
"AppID"="{83A33D30-27C5-11CE-BFD4-00400513BB57}"
[HKEY_CLASSES_ROOT\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\InprocHandler32]
@="ole32.dll"
[HKEY_CLASSES_ROOT\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\LocalServer32]
@="E:\\Programs\\SolidWorks Corp\\SOLIDWORKS\\SLDWORKS.exe"
[HKEY_CLASSES_ROOT\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\ProgID]
@="SldWorks.Application.24"
[HKEY_CLASSES_ROOT\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\TypeLib]
@="{83A33D31-27C5-11CE-BFD4-00400513BB57}"
[HKEY_CLASSES_ROOT\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\VersionIndependentProgID]
@="SldWorks.Application"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}]
@="SldWorks 2016 Application"
"AppID"="{83A33D30-27C5-11CE-BFD4-00400513BB57}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\InprocHandler32]
@="ole32.dll"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\LocalServer32]
@="E:\\Programs\\SolidWorks Corp\\SOLIDWORKS\\SLDWORKS.exe"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\ProgID]
@="SldWorks.Application.24"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\TypeLib]
@="{83A33D31-27C5-11CE-BFD4-00400513BB57}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}\VersionIndependentProgID]
@="SldWorks.Application"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SldWorks.Application]
@="SldWorks Application"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SldWorks.Application\CLSID]
@="{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SldWorks.Application.24]
@="SldWorks 2016 Application"
"AllowSilentDefaultTakeOver"=""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SldWorks.Application.24\CLSID]
@="{F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SldWorks.Application.24\shell]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SldWorks.Application.24\shell\open]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SldWorks.Application.24\shell\open\command]
@="E:\\Programs\\SolidWorks Corp\\SOLIDWORKS\\SLDWORKS.exe \"%1\""
第二点毛病就是,无法录制和编辑宏,会导致程序奔溃(不影响python3的API调用),暂未解决!
先开启SolidWorks,在运行下面的测试代码:
import win32com.client
import pythoncom
swYearLastDigit = 6
sw = win32com.client.Dispatch("SldWorks.Application.%d" % (20+(swYearLastDigit-2))) # e.g. 20 is SW2012, 24 is SW2016
model = sw.ActiveDoc
modelExt = model.Extension
selMgr = model.SelectionManager
featureMgr = model.FeatureManager
sketchMgr = model.SketchManager
eqMgr = model.GetEquationMgr
print("Equation 1 is: " + eqMgr.Equation(1))
eqMgr.Equation(1, "\"myVar\" = 42")
print("Equation 1 is now: " + eqMgr.Equation(1))
END!
网友评论