C# Solidworks二次开发:选择管理器相关的API介绍

今天在讲述主要内容之前,先说一个不太相关的问题。

我之前在其他文章中看到有一些朋友在问为什么获取到的点位数据需要乘以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,还是比较常用的。

本篇文章就介绍这些,我们下篇文章再见。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵桑さん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值