一、添加必要的引用:
using SolidWorks.Interop.sldworks;using SolidWorks.Interop.swconst;
二、连接方式:
private ISldWorks _swApp;public ISldWorks SwApp{get{if (_swApp == null){try{_swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application"); //默认获取当前电脑SolidWoks第一个版本实例}catch (Exception){try //目的:为解决多版本并存,无法同时获取各个版本SolidWoks实例而导致连接错误的问题{_swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application.29"); // SolidWoks版本号为25,SolidWoks版本号为26,SolidWoks版本号为29}catch (Exception){try{_swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application.25");}catch (Exception){throw;}}}finally{if (_swApp == null){MessageBox.Show("连接SolidWorks失败,请尝试重启SolidWorks软件或电脑后重新打开软件");System.Environment.Exit(0);}}}return _swApp;}set => _swApp = value;}
本文参考:/zengqh0314/article/details/100690000
如果觉得《Solidworks二次开发-连接solidworks》对你有帮助,请点赞、收藏,并留下你的观点哦!