SolidWorks二次开发 遍历树节点

125 篇文章 72 订阅
54 篇文章 15 订阅

各位Solidworks二次开发小伙伴,趁着年还没过玩,祝大家新年快乐,掐指一算 , 明天就是元宵节了。
各位是不是应该送我点什么?
在这里插入图片描述
说错了,是我送来送点东西给大家。

在这里插入图片描述
最近在看api帮助时发现了一个知识点。
Solidworks 的特征树,也是可以当然TreeView节点Nodes来获取的。
**并且还可以和Solidworks中的对象进行转换,就类似于Tag对象。

在这里插入图片描述
这样我们就可以在节点上进行展开的收缩,虽然之前有人问过怎么全部收缩节点,那时候是用执行solidworks的命令来解决的。

因为官方有个例子,我就列一下,贴个代码,大家可以自己去试试。

Expand or Collapse FeatureManager Design Tree Nodes Example (C#)
This example shows how to traverse, expand, and collapse the nodes of a FeatureManager design tree.

Get Objects in Selection Set Example (C#)

This example shows how to get the objects in a selection set.

关键的对象有
TreeControlItem

我们来测试一下代码:
在这里插入图片描述

执行后在VS的输出栏会显示 :
在这里插入图片描述

 /// <summary>
        /// 遍历的层级
        /// </summary>
        private int traverseLevel;

        /// <summary>
        /// 展开此项
        /// </summary>
        private bool expandThis;

        /// <summary>
        /// 遍历节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetFeatureNodes_Click(object sender, EventArgs e)
        {
            var swApp = PStandAlone.GetSolidWorks();

            var swModel = (ModelDoc2)swApp.ActiveDoc;

            FeatureManager featureMgr = default(FeatureManager);
            TreeControlItem rootNode = default(TreeControlItem);

            featureMgr = swModel.FeatureManager;

            //通过特征树获取根节点
            rootNode = featureMgr.GetFeatureTreeRootItem2((int)swFeatMgrPane_e.swFeatMgrPaneBottom);

            int i = 0;

            for (i = 0; i <= 1; i++)
            {
                if ((rootNode != null))
                {
                    Debug.Print("");
                    traverseLevel = 0;
                    traverse_node(rootNode);
                }

                expandThis = false;

                if (i == 0)
                {
                    MessageBox.Show(" OK to collapse all nodes?");
                }
            }
        }

        /// <summary>
        /// 遍历节点
        /// </summary>
        /// <param name="node"></param>
        private void traverse_node(TreeControlItem node)
        {
            TreeControlItem childNode = default(TreeControlItem);
            Feature featureNode = default(Feature);
            Component2 componentNode = default(Component2);
            int nodeObjectType = 0;
            object nodeObject = null;
            string restOfString = "";
            string indent = "";
            int i = 0;
            bool displayNodeInfo = false;
            string compName = null;
            int suppr = 0;
            string supprString = "";
            int vis = 0;
            string visString = "";
            string notfloatingString = "";
            bool notfloating = false;
            object componentDoc = null;
            string docString = "";
            string refConfigName = "";

            displayNodeInfo = false;

            nodeObjectType = node.ObjectType; //节点对象类型
            nodeObject = node.Object; //节点对象   目前只支持 Feature 和 Component对象转换

            switch (nodeObjectType)
            {
                case (int)swTreeControlItemType_e.swFeatureManagerItem_Feature:

                    displayNodeInfo = true;
                    if ((nodeObject != null))
                    {
                        featureNode = (Feature)nodeObject;
                        restOfString = "[FEATURE: " + featureNode.Name + "]";
                    }
                    else
                    {
                        restOfString = "[FEATURE: object Null?!]";
                    }

                    break;

                case (int)swTreeControlItemType_e.swFeatureManagerItem_Component:

                    displayNodeInfo = true;

                    if ((nodeObject != null))
                    {
                        componentNode = (Component2)nodeObject;
                        compName = componentNode.Name2;

                        if ((string.IsNullOrEmpty(compName)))
                        {
                            compName = "???";
                        }
                        //组件节点状态
                        suppr = componentNode.GetSuppression();

                        switch ((suppr))
                        {
                            case (int)swComponentSuppressionState_e.swComponentFullyResolved:
                                supprString = "Resolved";

                                break;

                            case (int)swComponentSuppressionState_e.swComponentLightweight:
                                supprString = "Lightweight";

                                break;

                            case (int)swComponentSuppressionState_e.swComponentSuppressed:
                                supprString = "Suppressed";

                                break;
                        }
                        //组件节点可见
                        vis = componentNode.Visible;

                        switch ((vis))
                        {
                            case (int)swComponentVisibilityState_e.swComponentHidden:
                                visString = "Hidden";

                                break;

                            case (int)swComponentVisibilityState_e.swComponentVisible:
                                visString = "Visible";

                                break;
                        }
                        //组件节点完全定义?
                        notfloating = componentNode.IsFixed();

                        if (notfloating == false)
                        {
                            notfloatingString = "Floating";
                        }
                        else
                        {
                            notfloatingString = "Fixed";
                        }

                        componentDoc = componentNode.GetModelDoc2();

                        if (componentDoc == null)
                        {
                            docString = "NotLoaded";
                        }
                        else
                        {
                            docString = "Loaded";
                        }
                        //组件节点配置
                        refConfigName = componentNode.ReferencedConfiguration;

                        if ((string.IsNullOrEmpty(refConfigName)))
                        {
                            refConfigName = "???";
                        }

                        restOfString = "[COMPONENT: " + compName + " " + docString + " " + supprString + " " + visString + " " + refConfigName + "]";
                    }
                    else
                    {
                        restOfString = "[COMPONENT: object Null?!]";
                    }

                    break;

                default:

                    displayNodeInfo = true;

                    if ((nodeObject != null))
                    {
                        restOfString = "[object type not handled]";
                    }
                    else
                    {
                        restOfString = "[object Null?!]";
                    }

                    break;
            }

            for (i = 1; i <= traverseLevel; i++)
            {
                indent = indent + "  ";
            }

            if ((displayNodeInfo))
            {
                Debug.Print(indent + node.Text + " : " + restOfString);
            }

            // Expand the node
            node.Expanded = expandThis;
            traverseLevel = traverseLevel + 1;
            childNode = node.GetFirstChild();

            while ((childNode != null))
            {
                Debug.Print(indent + "Node is expanded: " + childNode.Expanded);
                traverse_node(childNode);
                childNode = childNode.GetNext();
            }

            traverseLevel = traverseLevel - 1;
        }

好了,本文到此结束 。

源代码还是老地方,大家可以拉取更新:

https://gitee.com/painezeng/CSharpAndSolidWorks

https://github.com/painezeng/CSharpAndSolidWorks

如果哪里写的不好,请注意,请保持沉默。
在这里插入图片描述
哈哈

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
你可以使用 SolidWorks API 的二次开发来获取模型的最顶层节点的 Component2。首先,你需要遍整个模型,找到最顶层的组件节点。以下是一个示例代码: ```C# using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; using System; namespace SolidWorksAPI { class Program { static void Main(string[] args) { // 创建 SolidWorks 应用程序对象 SldWorks swApp = new SldWorks(); // 打开 SolidWorks 模型 ModelDoc2 swModel = swApp.OpenDoc("C:\\Path\\To\\Your\\Model.sldprt", (int)swDocumentTypes_e.swDocPART); // 获取模型的特征管理器 FeatureManager swFeatMgr = swModel.FeatureManager; // 获取模型的顶层特征 Feature swTopFeat = swFeatMgr.GetFirstFeature(); // 遍模型,找到最顶层的组件节点 while (swTopFeat != null) { if (swTopFeat.GetTypeName2() == "Reference") { // 获取组件的 Component2 接口 Component2 swComponent = (Component2)swTopFeat.GetSpecificFeature2(); Console.WriteLine("顶层组件名称: " + swComponent.Name); break; } swTopFeat = swTopFeat.GetNextFeature(); } // 关闭 SolidWorks 模型 swApp.CloseDoc("Model.sldprt"); // 退出 SolidWorks 应用程序 swApp.ExitApp(); } } } ``` 请注意,你需要确保已经正确安装了 SolidWorks 开发环境并添加了对应的 COM 引用。此外,你需要将代码中的文件路径替换为你的实际文件路径。这段代码将打开一个 SolidWorks 零件文件,遍模型并输出顶层组件的名称。 希望对你有帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Paine Zeng

如果对有帮助,请我喝咖啡吧

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

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

打赏作者

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

抵扣说明:

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

余额充值