訪問(wèn)項(xiàng)目 Github倉(cāng)庫(kù)
使用Nuget:
PM> Install-Package Du.SolidWorks -Version 0.1.1
1.如何打開(kāi)一個(gè)文檔
SolidWorks Api 在SldWorks接口中為我們提供了很多OpenDoc方法來(lái)打開(kāi)一個(gè)SolidWorks文件或者SolidWorks兼容的文件,如下
我使用的為2016版的幫助文檔,文檔中指示OpenDoc - OpenDoc4 已經(jīng)是Obsolete狀態(tài),表示新版本中已經(jīng)不在推薦使用,在Api演進(jìn)過(guò)程中已經(jīng)是過(guò)時(shí)Api.
使用 OpenDoc6打開(kāi)文檔
1.1打開(kāi)文檔
這里使用c#的擴(kuò)展方法封裝了一個(gè)靜默打開(kāi)文檔的方法,這里隱藏了OpenDoc6的很多參數(shù),只使用了一個(gè)路徑字符串和一個(gè)用來(lái)控制文檔是否可見(jiàn)的bool值.
/// <summary>
/// 靜默模式打開(kāi)隱藏文件
/// </summary>
/// <param name="app"><see cref="ISldWorks"/></param>
/// <param name="filePath">文件路徑</param>
/// <param name="Hidden">是否隱藏 默認(rèn)隱藏</param>
/// <returns></returns>
public static ModelDoc2 OpenInvisibleDocClient(this SldWorks app, string filePath, bool Hidden = true)
{
int Errors = -1, Warning = -1;
var type = app.FileType(filePath);
try
{
if (Hidden) app.DocumentVisible(false, (int)type);
ModelDoc2 doc = app.OpenDoc6(filePath, type.SWToInt(), swOpenDocOptions_e.swOpenDocOptions_Silent.SWToInt(),
"", ref Errors, ref Warning) as ModelDoc2;
if (doc == null)
{
throw new Exception(string.Format("errors:{0},warings{1}",
Errors.CastObj<swFileLoadError_e>().ToString(),
Warning.CastObj<swFileLoadWarning_e>().ToString()));
}
return doc;
}
catch (Exception)
{
throw;
}
finally
{
app.DocumentVisible(true, (int)type);
}
}
下面根據(jù)文件路徑判斷SolidWorks文檔類型的方法
/// <summary>
/// 判斷文件類型
/// </summary>
/// <param name="app"><see cref="ISldWorks"/></param>
/// <param name="filePath">文件全路徑</param>
/// <returns></returns>
public static swDocumentTypes_e FileType(this SldWorks app, string filePath)
{
string extension = Path.GetExtension(filePath).ToLower();
swDocumentTypes_e type = swDocumentTypes_e.swDocPART;
switch (extension)
{
case ".sldprt":
type = swDocumentTypes_e.swDocPART;
break;
case ".sldasm":
type = swDocumentTypes_e.swDocASSEMBLY;
break;
case ".slddrw":
type = swDocumentTypes_e.swDocDRAWING;
break;
default:
throw new FileFormatException("不支持的文件類型:" + extension);
}
return type;
}
1.2.只讀打開(kāi)
除了靜默打開(kāi),我們有時(shí)候會(huì)在后臺(tái)打開(kāi)一個(gè)文件作為數(shù)據(jù)庫(kù)使用,像草圖輪廓庫(kù),特征數(shù)據(jù)庫(kù)等.這時(shí)我們可以使用只讀模式打開(kāi).下面的方法封裝了OpenDoc7 來(lái)實(shí)現(xiàn)一個(gè)只讀打開(kāi).
/// <summary>
/// 以不可見(jiàn)模式打開(kāi)文檔 Open a document invisibly. It will not be shown to the user but you will be
/// able to interact with it through the API as if it is loaded.
/// </summary>
/// <param name="sldWorks"></param>
/// <param name="toolFile"></param>
/// <param name="visible">是否對(duì)用戶可見(jiàn)</param>
/// <param name="type"><see cref="swDocumentTypes_e"/> SolidWorks 文件類型 ,默認(rèn)為零件</param>
/// <returns></returns>
public static ModelDoc2 OpenInvisibleReadOnly(this ISldWorks sldWorks, string toolFile, bool visible = false, swDocumentTypes_e type = swDocumentTypes_e.swDocPART)
{
try
{
if (!visible)
sldWorks.DocumentVisible(false, (int)type);
var spec = (IDocumentSpecification)sldWorks.GetOpenDocSpec(toolFile);
if (!visible)
{
spec.Silent = true;
spec.ReadOnly = true;
}
var doc = sldWorks.OpenDoc7(spec);
doc.Visible = visible;
return doc;
}
finally
{
if (!visible)
sldWorks.DocumentVisible
(true,
(int)
type);
}
}
1.3.遍歷打開(kāi)的文檔
SolidWorks提供了 GetDocuments 方法來(lái)獲取所有打開(kāi)的文檔,其中包括在內(nèi)存中打開(kāi)并對(duì)用戶不可見(jiàn)的文檔,這立封裝了一個(gè)方法,來(lái)遍歷所有的文檔并執(zhí)行同一操作.
/// <summary>
/// 對(duì)內(nèi)存中所有文檔進(jìn)行操作
/// </summary>
/// <param name="swApp"></param>
/// <param name="action"></param>
public static void UsingOpenDoc(this SldWorks swApp, Action<IModelDoc2> action)
{
var docs = (swApp.GetDocuments() as Object[]).Cast<IModelDoc2>();
if (docs == null)
{
return;
}
foreach (var item in docs)
{
action?.Invoke(item);
}
}
1.4.判斷文檔是否已經(jīng)打開(kāi)
有時(shí)在打開(kāi)前我們需要判斷是否已經(jīng)打開(kāi)此文檔,雖然對(duì)已經(jīng)打開(kāi)的文檔再次打開(kāi)SolidWorks也會(huì)返回到ModelDoc2對(duì)象,但有時(shí)判斷一下還是很有必要.下面的方法便是對(duì)上面遍歷所有文檔的再次封裝來(lái)實(shí)現(xiàn)判斷此文檔是否已經(jīng)被打開(kāi).實(shí)現(xiàn)如下:
/// <summary>
/// 判斷此文檔是否打開(kāi)
/// </summary>
/// <param name="app"></param>
/// <param name="DocFilePathName"></param>
/// <returns></returns>
public static bool HasDocOpened(this SldWorks app, string DocFilePathName)
{
bool exist = false;
app.UsingOpenDoc(d =>
{
if (d.GetPathName() == DocFilePathName)
{
exist = true;
}
}
);
return exist;
}
1.5.對(duì)當(dāng)前打開(kāi)的活動(dòng)文檔執(zhí)行動(dòng)作
對(duì)當(dāng)前活動(dòng)文檔執(zhí)行某些操作是相當(dāng)普遍的做法,下面的方法使用了一個(gè) Action類型的參數(shù)來(lái)對(duì)活動(dòng)文檔快速調(diào)用.適合一些對(duì)活動(dòng)文檔的快速操作.
/// <summary>
/// 對(duì)活動(dòng)文檔進(jìn)行操作
/// </summary>
/// <param name="swApp"></param>
/// <param name="action"></param>
public static void UsingActiveDoc(this SldWorks swApp, Action<IModelDoc2> action)
{
if (swApp.ActiveDoc != null)
{
action?.Invoke((IModelDoc2)swApp.ActiveDoc);
}
}
2.新建SolidWorks零件或者裝配體
2.1.調(diào)用默認(rèn)模板創(chuàng)建零件或者裝配體
在對(duì)SolidWorks開(kāi)發(fā)中,新建一個(gè)零件或者裝配體是再經(jīng)常不過(guò)的高頻操作,下面的方法封裝了一個(gè)創(chuàng)建文檔的操作,并針對(duì)中文環(huán)境下SolidWork默認(rèn)模板文件命名變換的情況做了判斷處理.
/// <summary>
/// 以默認(rèn)設(shè)置的模板創(chuàng)建文檔
/// </summary>
/// <param name="sldWorks"><see cref="ISldWorks"/></param>
/// <param name="type"><see cref="swDocumentTypes_e"/></param>
/// <returns></returns>
public static IModelDoc2 CreateDocument(this ISldWorks sldWorks, swDocumentTypes_e type)
{
string templatePath = string.Empty;
switch (type)
{
case swDocumentTypes_e.swDocPART:
templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
break;
case swDocumentTypes_e.swDocASSEMBLY:
templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateAssembly);
break;
case swDocumentTypes_e.swDocDRAWING:
templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateDrawing);
break;
default:
break;
}
if (!File.Exists(templatePath))
{
templatePath =
Path.GetDirectoryName(templatePath) + "\\" +
Path.GetFileName(templatePath).
Replace("零件", "gb_part").
Replace("裝配體", "gb_assembly").Replace("工程圖", "gb_a1");
}
if (!File.Exists(templatePath))
{
throw new FileNotFoundException("無(wú)法找到SolidWorks文件--" + templatePath);
}
return sldWorks.CreateDocument(templatePath);
}
2.2.通過(guò)模板新建文檔
上面我們通過(guò)獲取SolidWorks默認(rèn)設(shè)置的模板來(lái)新建一個(gè)零件或者裝配體,下面我們通過(guò)一個(gè)給定的模板路徑來(lái)新建一個(gè)零件或者裝配體。
/// <summary>
/// 通過(guò)模板創(chuàng)建新零件或者新項(xiàng)目
/// </summary>
/// <param name="app"></param>
/// <param name="TemFilePath">*.asmdot 或者 *.prtdot</param>
/// <returns></returns>
public static ModelDoc2 NewPartOrAssembly(this SldWorks app, string TemFilePath)
{
return app.NewDocument(TemFilePath, (int)swDwgPaperSizes_e.swDwgPaperA4size, 100, 100) as ModelDoc2;
}
3.修改SolidWorks設(shè)置
獲取SolidWorks設(shè)置選項(xiàng)的基本方法在ISldWorks接口下,如下
修改SolidWorks設(shè)置選項(xiàng)的方法也在ISldWorks接口下掀宋,大概與獲取相對(duì)應(yīng)昙读,如下:
3.1 先獲取設(shè)置項(xiàng)驶悟,執(zhí)行完需要的代碼后焊夸,將選項(xiàng)設(shè)置為默認(rèn)狀態(tài)
下面的代碼演示了如何在某種狀態(tài)下執(zhí)行我們的業(yè)務(wù)代碼苛败,然后將設(shè)置選項(xiàng)還原到默認(rèn)狀態(tài)水孩,以達(dá)到不隨意改變用戶設(shè)置的要求镰矿。
/// <summary>
/// 設(shè)置一個(gè)選項(xiàng)后執(zhí)行動(dòng)作,執(zhí)行完后設(shè)置回系統(tǒng)默認(rèn)值
/// </summary>
/// <param name="app">ISldwork interface</param>
/// <param name="toggleSetting">需要設(shè)置的枚舉值</param>
/// <param name="value">值</param>
/// <param name="action">執(zhí)行的動(dòng)作</param>
public static void WithToggleState(this ISldWorks app, swUserPreferenceToggle_e toggleSetting, bool value, Action action)
{
try
{
bool fileLockState = app.GetUserPreferenceToggle(toggleSetting.SWToInt());
app.SetUserPreferenceToggle(swUserPreferenceToggle_e.swLockRecentDocumentsList.SWToInt(), true);
action?.Invoke();
}
catch (Exception)
{
throw;
}
finally
{
app.SetUserPreferenceToggle(toggleSetting.SWToInt(), value);
}
}
下一篇: