You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SolidWorks C#无法提取TypeName为Reference的特征下的项目

解决SolidWorks装配体中提取零件下指定参考草图的问题

需求

使用C#打开SolidWorks装配体(swDocumentTypes_e.swDocASSEMBLY类型),遍历所有特征,获取每个零件下名称格式为ISO/XXX的草图。

当前代码

public void openFile(string skeletonFilePath)
{
    object[] Features = null;
    int i = 0;
    string FeatType = null;
    string FeatTypeName = null;

    if (string.IsNullOrWhiteSpace(skeletonFilePath)) { return; }
    ModelDoc2 model = _sldWorks.OpenDoc("C:PATH/fileName.SLDASM", (int)swDocumentTypes_e.swDocASSEMBLY);
    Feature swFeat = default(Feature);
    SelectionMgr swSelMgr = default(SelectionMgr);
    swSelMgr = (SelectionMgr)model.SelectionManager;
    swFeat = (Feature)model.FirstFeature();

    while ((swFeat != null))
    {
        FeatType = swFeat.Name;
        FeatTypeName = swFeat.GetTypeName2();
        if ((FeatTypeName == "Reference"))
        {
            Debug.Print(" Name of feature: " + swFeat.Name);
            Debug.Print(" Type of feature: " + swFeat.GetTypeName2());
        }
        swFeat = (Feature)swFeat.GetNextFeature();
    }
}

遇到的问题

遍历到TypeName为Reference的特征时,尝试提取该特征对应的草图内容出现异常,已尝试以下方法但未解决:

  • swFeat.GetDefinition():触发空引用异常
  • swFeat.GetSpecificFeature2():返回动态类型,无法确定转换目标类
  • var childs = (Object[])swFeatSupport.GetChildren():仅获取到零件的两个约束,未找到目标草图

解决方案

1. 核心问题说明

你当前遍历的是装配体的顶层特征,而目标ISO/XXX草图属于装配体中零部件的内部特征,并非装配体层面的Reference特征。需要先定位到装配体中的零部件,再进入对应零件文档内部查找草图。

2. 修正后的代码

public void openFile(string skeletonFilePath)
{
    if (string.IsNullOrWhiteSpace(skeletonFilePath)) { return; }
    
    // 打开目标装配体
    ModelDoc2 assyModel = _sldWorks.OpenDoc(skeletonFilePath, (int)swDocumentTypes_e.swDocASSEMBLY);
    if (assyModel == null) return;
    
    AssemblyDoc assyDoc = (AssemblyDoc)assyModel;
    
    // 获取装配体中所有零部件(包含子装配体零件)
    object[] components = (object[])assyDoc.GetComponents(true);
    
    foreach (Component2 comp in components)
    {
        // 获取零部件对应的零件文档
        ModelDoc2 partModel = (ModelDoc2)comp.GetModelDoc2();
        if (partModel == null) continue;
        
        // 遍历零件内部的所有特征
        Feature swFeat = (Feature)partModel.FirstFeature();
        while (swFeat != null)
        {
            string featName = swFeat.Name;
            string featTypeName = swFeat.GetTypeName2();
            
            // 筛选名称以ISO/开头的草图特征
            if (featTypeName == "Sketch" && featName.StartsWith("ISO/"))
            {
                Debug.Print($"零件[{comp.Name}]找到目标草图: {featName}");
                
                // 转换为Sketch类操作草图内容
                Sketch targetSketch = (Sketch)swFeat.GetSpecificFeature2();
                if (targetSketch != null)
                {
                    // 示例:获取草图中的所有线段
                    object[] sketchSegments = (object[])targetSketch.GetSketchSegments();
                    // 此处添加你的草图处理逻辑
                }
            }
            
            swFeat = (Feature)swFeat.GetNextFeature();
        }
    }
}

3. 关键说明

  • 装配体中的Reference特征是外部参考标记,并非零件内部的草图,无需针对该类型特征做处理
  • 通过Component2.GetModelDoc2()获取零部件对应的零件文档,才能访问其内部的草图特征
  • 草图的特征类型名为Sketch,可通过名称前缀ISO/精准筛选目标

内容的提问来源于stack exchange,提问作者ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 21:35:24