工程圖中爲了便於輸出不同顏色的的對象信息,如顏色和線型,有時候會在工程圖中建立各種各樣的圖層。
此文章的目標就是圖層的管理。
前提條件:打開一張工程圖
實現目標:遍歷顯示當前圖紙中圖層的信息,根據需要建立自己需要的圖層。
關於圖層,英文名是Layer:
我們先看一下Solidworks Api幫助中的信息:發現裏面有例子,所以我們就可以直接參考了
如下圖,我們當前打開的圖紙中,有四個圖層
下面先看一下獲取這些圖層的信息
直接上代碼:
ISldWorks swApp = Utility.ConnectToSolidWorks();
var swModel = (ModelDoc2)swApp.ActiveDoc;
var swLayerMgr = (LayerMgr)swModel.GetLayerManager();
//獲取當前圖層數量
var layCount = swLayerMgr.GetCount();
var layerList = (String[])swLayerMgr.GetLayerList();
foreach (var lay in layerList)
{
var currentLayer = swLayerMgr.GetLayer(lay);
if (currentLayer != null)
{
var currentName = currentLayer.Name;
//顏色的Ref值
var currentColor = currentLayer.Color;
var currentDesc = currentLayer.Description;
//swLineStyles_e 對應的值
var currentStype = Enum.GetName(typeof(swLineStyles_e), currentLayer.Style);
var currentWidth = currentLayer.Width;
int refcolor = currentColor;
int blue = refcolor >> 16 & 255;
int green = refcolor >> 8 & 255;
int red = refcolor & 255;
int colorARGB = 255 << 24 | (int)red << 16 | (int)green << 8 | (int)blue;
//得到對應的RGB值
Color ARGB = Color.FromArgb(colorARGB); //得到結果
Debug.Print($"圖層名稱:{currentName}");
Debug.Print($"圖層顏色:R {ARGB.R},G {ARGB.G} ,B {ARGB.B}");
Debug.Print($"圖層描述:{currentDesc}");
Debug.Print($"圖層線型:{currentStype}");
Debug.Print($"-------------------------------------");
}
}
運行一下,結果如下圖:
可以看到Layer0的 顏色 R 255 G 0 B 0 就是紅色。
接下來看看如何增加一個圖層。
比如說我要增加一個紫色的圖層。
//下面來建圖層。
var swDrawing = (DrawingDoc)swModel;
// var colorString = "Purple";
Color color = Color.Purple; //System.Drawing.ColorTranslator.FromHtml(colorString); 如果是字符串可以通過這轉
//給定的
int colorInt = color.ToArgb();
int red2 = colorInt >> 16 & 255;
int green2 = colorInt >> 8 & 255;
int blue2 = colorInt & 255;
int refcolor2 = (int)blue2 << 16 | (int)green2 << 8 | (int)red2;
var bRes = swDrawing.CreateLayer2("NewPurple", "New Purple Layout ", (int)refcolor2, (int)swLineStyles_e.swLineCONTINUOUS, (int)swLineWeights_e.swLW_NORMAL, true, true);
if (bRes == true)
{
Debug.Print($"圖層已經創建");
}
運行之後,圖層已經創建完成,這樣我們就可以再進行別的操作了。
老樣子,代碼自取。希望口味喜歡。
圖層的一些刪除操作就比較簡單了,我就不演示了,它也沒有幾個方法。