今天在講述主要內容之前,先說一個不太相關的問題。
我之前在其他文章中看到有一些朋友在問為什么獲取到的點位數據需要乘以1000進行單位轉換,其實原因是這樣的,在所有使用的API中如果沒有特殊說明,所有的長度單位都是米,角度單位都是弧度,順手解答一下那位朋友的疑問。
下面開始今天文章的主題:
1、介紹第一個和選擇管理器相關的API:GetSelectedObjectCount2 Method (ISelectionMgr)
這個API的解釋為:獲取所選對象的數目。
2、第二個API為:GetSelectedObject6 Method (ISelectionMgr)
這個API的解釋為:獲取選擇對象,這個我平時用的非常多。
下面是使用的例子:
??public?void?Main(){ModelDoc2?swModel?=?default(ModelDoc2);ModelDocExtension?swModelDocExt?=?default(ModelDocExtension);FeatureManager?swFeatureManager?=?default(FeatureManager);MidSurface3?swMidSurfaceFeature?=?default(MidSurface3);Feature?swFeature?=?default(Feature);SelectionMgr?swSelectionMgr?=?default(SelectionMgr);Face2?swFace?=?default(Face2);bool?status?=?false;int?errors?=?0;int?warnings?=?0;string?fileName?=?null;int?count?=?0;object[]?faces?=?null;int?i?=?0;fileName?=?"C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\samples\\tutorial\\api\\box.sldprt";swModel?=?(ModelDoc2)swApp.OpenDoc6(fileName,?(int)swDocumentTypes_e.swDocPART,?(int)swOpenDocOptions_e.swOpenDocOptions_Silent,?"",?ref?errors,?ref?warnings);swModelDocExt?=?(ModelDocExtension)swModel.Extension;status?=?swModelDocExt.SelectByID2("",?"FACE",?-0.0533080255641494,?0.0299999999999727,?0.0131069871973182,?true,?0,?null,?0);status?=?swModelDocExt.SelectByID2("",?"FACE",?-0.0370905424398416,?0,?0.0289438729892595,?true,?0,?null,?0);swFeatureManager?=?(FeatureManager)swModel.FeatureManager;swFeatureManager.InsertMidSurface(null,?null,?0.0,?false);status?=?swModelDocExt.SelectByID2("Surface-MidSurface1",?"REFSURFACE",?0,?0,?0,?false,?0,?null,?0);swSelectionMgr?=?(SelectionMgr)swModel.SelectionManager;swFeature?=?(Feature)swSelectionMgr.GetSelectedObject6(1,?-1);swMidSurfaceFeature?=?(MidSurface3)swFeature.GetSpecificFeature2();count?=?swMidSurfaceFeature.GetFaceCount();Debug.Print("Number?of?faces?for?midsurface?feature:?"?+?count);faces?=?(object[])swMidSurfaceFeature.GetFaces();for?(i?=?faces.GetLowerBound(0);?i?<=?faces.GetUpperBound(0);?i++){swFace?=?(Face2)faces[i];Debug.Print("Area?of?face?"?+?i?+?"?of?midsurface?feature:?"?+?swFace.GetArea());}}///?<summary>///??The?SldWorks?swApp?variable?is?pre-assigned?for?you.///?</summary>public?SldWorks?swApp;
3、第三個API為:GetSelectedObjectType3 Method (ISelectionMgr)
這個API的解釋為:獲取選擇對象的類型。
下面介紹一個使用的例子:
public void Main()
????{
????????ModelDoc2 swModel = default(ModelDoc2);
????????bool boolstatus = false;
????????SelectionMgr SelMgr = default(SelectionMgr);
????????TableAnnotation theTableAnnotation = default(TableAnnotation);
????????int SelObjType = 0;
????????int TableAnnotationType = 0;
????????swModel = (ModelDoc2)swApp.ActiveDoc;
????????SelMgr = (SelectionMgr)swModel.SelectionManager;
????????SelObjType = SelMgr.GetSelectedObjectType3(1, -1);
????????if (SelObjType != (int)swSelectType_e.swSelANNOTATIONTABLES)
????????{
????????????MessageBox.Show("Select a BOM table in the drawing before running this example.");
????????????return;
????????}
????????theTableAnnotation = (TableAnnotation)SelMgr.GetSelectedObject6(1, -1);
????????TableAnnotationType = theTableAnnotation.Type;
????????if (TableAnnotationType != (int)swTableAnnotationType_e.swTableAnnotation_BillOfMaterials)
????????{
????????????MessageBox.Show("Select a BOM table in the drawing before running this example.");
????????????return;
????????}
????????Debug.Print("Table before inserting a column...");
????????// Display table before inserting a column
????????DisplayTableColumnProps(theTableAnnotation);
????????// Insert new column
????????boolstatus = theTableAnnotation.InsertColumn2((int)swTableItemInsertPosition_e.swTableItemInsertPosition_Last, 0, "New Column", (int)swInsertTableColumnWidthStyle_e.swInsertColumn_DefaultWidth);
????????boolstatus = theTableAnnotation.SetColumnType2(theTableAnnotation.ColumnCount - 1, (int)swTableColumnTypes_e.swBomTableColumnType_PartNumber, true);
????????Debug.Print(" ");
????????Debug.Print("Table after inserting a column...");
????????// Display table after inserting a column
????????DisplayTableColumnProps(theTableAnnotation);
????}
????public void DisplayTableColumnProps(TableAnnotation theTableAnnotation)
????{
????????int ColCount = 0;
????????int i = 0;
????????string iString = null;
????????int ColType = 0;
????????string ColTypeString = null;
????????string ColTitle = null;
????????Debug.Print("Col# " + "Type ?" + "Title");
????????ColCount = theTableAnnotation.ColumnCount;
????????for (i = 0; i <= ColCount - 1; i++)
????????{
????????????ColType = theTableAnnotation.GetColumnType2(i, true);
????????????ColTypeString = System.Convert.ToString(ColType);
????????????ColTitle = theTableAnnotation.GetColumnTitle2(i, true);
????????????iString = System.Convert.ToString(i);
????????????Debug.Print(iString + " ???" + ColTypeString + " ?" + ColTitle);
????????}
????}
????/// <summary>
????/// The SldWorks swApp variable is pre-assigned for you.
????/// </summary>
????public SldWorks swApp;
今天介紹的這三個API都是選擇管理器相關的API,還是比較常用的。
本篇文章就介紹這些,我們下篇文章再見。