获取当前正在编辑草图特征的父级特征以及获取装饰螺纹线

获取当前正在编辑草图特征的父级特征以及获取装饰螺纹线

在这里插入图片描述

功能需求描述

在使用SolidWorks Macro 调用HoleWizard5 Method (IFeatureManager)进行打孔时,如果同时添加了多个孔但是装饰螺纹线只显示一个,通过一下方案可以找到当前正在编辑的父级特征然后更新未显示的装饰螺纹线。

本方法的目标是通过 SolidWorks API 实现以下功能:

  1. 获取当前正在编辑的草图特征。
  2. 通过草图特征找到其父级特征。
  3. 在父特征中查找装饰螺纹线特征(Cosmetic Thread),并提取其相关参数。

技术点

  1. 如何获取当前正在编辑状态的草图特征

在 SolidWorks 中,通过 SketchManager.ActiveSketch 可以获取当前正在编辑的草图特征。该属性返回一个 Sketch 对象,代表当前编辑的草图。如果没有草图在编辑状态,则该属性返回 null

Sketch swSketch = swSketchManager.ActiveSketch;
  1. 如何获取指定特征的父级特征
    在 SolidWorks 中,通过 Feature.GetOwnerFeature() 方法可以获取指定特征的父级特征。该方法返回一个 Feature 对象,代表该特征的父级特征。通过这种方式,我们可以追溯到草图特征的父级特征,并进一步获取装饰螺纹线等子特征。
Feature swFeatParent = (Feature)swFeat.GetOwnerFeature();

完整代码

/// <summary>
/// 获取装饰螺纹线参数
/// </summary>
/// <param name="swApp"></param>
public static void GetCosmeticThreadFeature(SldWorks swApp)
{
    
    
    var swModel = (ModelDoc2)swApp.ActiveDoc;
    var swFeatureManager = swModel.FeatureManager;

    // 获取当前草图管理器
    SketchManager swSketchManager = swModel.SketchManager;

    // 检查是否正在编辑草图
    if (swSketchManager.ActiveSketch != null)
    {
    
    
        // 获取正在编辑的草图
        Sketch swSketch = swSketchManager.ActiveSketch;
        Feature swFeat = (Feature)swSketch;
        Feature swFeatParent = (Feature)swFeat.GetOwnerFeature();

        var swSubFeat = (Feature)swFeatParent.GetFirstSubFeature();
        while ((swSubFeat != null))
        {
    
    

            switch (swSubFeat.GetTypeName())
            {
    
    
                case "CosmeticThread":

                    Debug.Print($"    {
      
      swSubFeat.Name}   [{
      
      swSubFeat.GetTypeName()}]");

                    判断是不是第一个CosmeticThreadFeature
                    //if (swSubFeat.Name.Contains("firstCosmeticThreadFeature"))
                    //{
    
    
                    //    // 第一个CosmeticThreadFeature的相关处理
                    //}

                    // 读取 CosmeticThread 的相关参数
                    var swCosThread = (CosmeticThreadFeatureData)swSubFeat.GetDefinition();

                    Debug.Print("      ApplyThread      = " + swCosThread.ApplyThread);
                    Debug.Print("      BlindDepth       = " + swCosThread.BlindDepth * 1000.0 + " mm");
                    Debug.Print("      Diameter         = " + swCosThread.Diameter * 1000.0 + " mm");
                    Debug.Print("      DiameterType     = " + swCosThread.DiameterType);
                    Debug.Print("      ThreadCallout    = " + swCosThread.ThreadCallout);
                    Debug.Print("      ConfigurationOption as defined in swCosmeticConfigOptions_e = " + swCosThread.ConfigurationOption);
                    Debug.Print("      EndCondition as defined in swCosmeticEndConditions_e = " + swCosThread.EndCondition);
                    Debug.Print("      Size = " + swCosThread.Size);
                    Debug.Print("      Standard as defined in swCosmeticStandardType_e = " + swCosThread.Standard);
                    Debug.Print("      StandardType = " + swCosThread.StandardType);
                    Debug.Print("");
                    break;
            }

            swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
        }

        Console.WriteLine("当前正在编辑的草图名称: " + swFeat.Name);
    }
    else
    {
    
    
        Console.WriteLine("当前没有正在编辑的草图。");
    }
}

代码解析

  1. 获取当前活动的 SolidWorks 文档
    首先,我们获取当前正在使用的 SolidWorks 实例和文档对象。此步骤确保我们能够在当前文档中操作特征。
var swModel = (ModelDoc2)swApp.ActiveDoc;
swModel 是 ModelDoc2 类型的对象,代表当前活动的模型文档。
  1. 获取草图管理器
    接下来,我们获取草图管理器(SketchManager),它用于管理当前文档中的所有草图特征。通过它,我们可以检查当前是否有草图处于编辑状态。
SketchManager swSketchManager = swModel.SketchManager;
  1. 检查是否正在编辑草图
    如果当前有草图处于编辑状态,则继续执行。通过 swSketchManager.ActiveSketch 可以获取当前正在编辑的草图对象。若无草图处于编辑状态,函数会输出提示信息。
if (swSketchManager.ActiveSketch != null)
{
    
    
    // 获取正在编辑的草图
    Sketch swSketch = swSketchManager.ActiveSketch;
    Feature swFeat = (Feature)swSketch;
    Feature swFeatParent = (Feature)swFeat.GetOwnerFeature();
}
  1. 获取父级特征
    通过草图特征获取其父级特征。Feature.GetOwnerFeature() 方法返回该特征的父级特征对象。此步骤对于查找装饰螺纹线特征(Cosmetic Thread)至关重要,因为螺纹线通常是草图的父特征。
Feature swFeatParent = (Feature)swFeat.GetOwnerFeature();
  1. 遍历子特征
    获取父特征后,遍历其所有子特征,寻找类型为 CosmeticThread 的装饰螺纹线特征。如果找到该类型的子特征,则读取其相关的参数。
var swSubFeat = (Feature)swFeatParent.GetFirstSubFeature();
while ((swSubFeat != null))
{
    
    
    switch (swSubFeat.GetTypeName())
    {
    
    
        case "CosmeticThread":
            // 处理 CosmeticThread 参数
            break;
    }

    swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
}
   ``
6. **读取螺纹参数**
通过 CosmeticThreadFeatureData 获取装饰螺纹线的相关参数。常见的参数包括螺纹直径、盲深度、螺纹标注、尺寸等。

```csharp
var swCosThread = (CosmeticThreadFeatureData)swSubFeat.GetDefinition();

Debug.Print("      ApplyThread      = " + swCosThread.ApplyThread);
Debug.Print("      BlindDepth       = " + swCosThread.BlindDepth * 1000.0 + " mm");
Debug.Print("      Diameter         = " + swCosThread.Diameter * 1000.0 + " mm");
Debug.Print("      DiameterType     = " + swCosThread.DiameterType);
Debug.Print("      ThreadCallout    = " + swCosThread.ThreadCallout);
Debug.Print("      ConfigurationOption as defined in swCosmeticConfigOptions_e = " + swCosThread.ConfigurationOption);
Debug.Print("      EndCondition as defined in swCosmeticEndConditions_e = " + swCosThread.EndCondition);
Debug.Print("      Size = " + swCosThread.Size);
Debug.Print("      Standard as defined in swCosmeticStandardType_e = " + swCosThread.Standard);
Debug.Print("      StandardType = " + swCosThread.StandardType);

后续

刚把这个文章写完,一个优秀的群友给出了更好的解决方案:

代码如下:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Sub main升级装饰线()

Set swApp = Application.SldWorks

Dim allowUpgrade As Boolean
allowUpgrade = swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swEnableAllowCosmeticThreadsUpgrade)

try:
On Error GoTo catch

Set swModel = swApp.ActiveDoc

If Not swModel Is Nothing Then

swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swEnableAllowCosmeticThreadsUpgrade, True

If False = swModel.Extension.UpgradeLegacyCThreads() Then
Debug.Print "Thread is not upgraded"
End If

Else
Err.Raise vbError, "", "Please open document"
End If

GoTo finally

catch:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally:

swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swEnableAllowCosmeticThreadsUpgrade, allowUpgrade

End Sub

问题根源分析:

说到底就是"允许升级装饰螺纹线"的设置选项未打开。哈哈哈!!!

所以在论坛或群聊中提问题的时候,一定要把问题的相关使用场景描述清楚,以便于快速解决问题!

在这里插入图片描述