Accessing Part Data via the SolidWorks 2021 API

Hello
I'm working on the solidwork API and trying to access the information in a part. I work on pieces to length, composed of extrusion, and sometimes material removal.
I managed to recover the dimensions of the extrusion with my code, however sketch leaves are present, and I would like to be able to exclude them.

So I tried to find the corresponding sketch for the extrusion so that I could read the data. I managed to find the name of the sketch but I couldn't access the data. I regularly get the error message " Property or method not handled by this object ". I must be missing something in the manipulation of objects.
I've seen the SketchManager around, but I don't understand how to use it.

Here's my code:

Sub GetExtrusionDimensions()

' Initialize SolidWorks application and get the active document
Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim Feature As Feature
Dim ExtrudeFeat As ExtrudeFeatureData
Dim DisplayDim As DisplayDimension
Dim DimVal As Dimension
Dim MsgStr As String

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

' Check if the active document is a part file
If Part Is Nothing Or Part.GetType <> swDocPART Then
    MsgBox "Veuillez ouvrir une pièce avant d'exécuter ce script.", vbCritical
    Exit Sub
End If

' Start with the first feature in the part
Set Feature = Part.FirstFeature
MsgStr = "Dimensions des extrusions :" & vbNewLine & vbNewLine

' Loop through each feature in the feature tree
Do While Not Feature Is Nothing

    ' Check if the feature is an extrusion
    If Feature.GetTypeName2 = "Extrusion" Then
        ' Access extrusion-specific data
        Set ExtrudeFeat = Feature.GetDefinition

        If Not ExtrudeFeat Is Nothing Then
            MsgStr = MsgStr & "Fonction : " & Feature.Name & vbNewLine

            ' Access display dimensions
            Set DisplayDim = Feature.GetFirstDisplayDimension
            Do While Not DisplayDim Is Nothing
                ' Get the linked dimension object
                Set DimVal = DisplayDim.GetDimension
                If Not DimVal Is Nothing Then
                    MsgStr = MsgStr & "  Dimension : " & DimVal.FullName & " = " & DimVal.Value & " mm" & vbNewLine
                End If
                ' Move to the next display dimension
                Set DisplayDim = Feature.GetNextDisplayDimension(DisplayDim)
            Loop
            
            MsgStr = MsgStr & vbNewLine
        End If
    End If

    ' Move to the next feature in the tree
    Set Feature = Feature.GetNextFeature

Loop

' Display the extracted information
MsgBox MsgStr, vbInformation, "Résultats"

End Sub

And my code to find the parent relationships of the Feature:
Sub ListFeatureParents()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeature As SldWorks.Feature
Dim swParentFeature As SldWorks.Feature
Dim vParents As Variant
Dim i As Integer

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

If swModel Is Nothing Then
    MsgBox "Aucun document actif. Veuillez ouvrir une pièce.", vbExclamation
    Exit Sub
End If

If swModel.GetType <> swDocPART Then
    MsgBox "Ce script fonctionne uniquement avec des pièces.", vbExclamation
    Exit Sub
End If

' Boucle à travers toutes les fonctions
Set swFeature = swModel.FirstFeature
Do While Not swFeature Is Nothing
    ' Afficher le nom et le type de la fonction
    Debug.Print "Feature: " & swFeature.Name & " (" & swFeature.GetTypeName2 & ")"
    
    ' Récupérer les parents de la fonction
    vParents = swFeature.GetParents
    If Not IsEmpty(vParents) Then
        For i = LBound(vParents) To UBound(vParents)
            Set swParentFeature = vParents(i)
            If Not swParentFeature.Name = "face" Or swParentFeature.Name = "Origine" Then
            Debug.Print "  -> Parent Feature: " & swParentFeature.Name & " (" & swParentFeature.GetTypeName2 & ")"
            End If
        Next i
    Else
        Debug.Print "  -> No Parent Features"
    End If
    
    ' Passer à la fonction suivante
    Set swFeature = swFeature.GetNextFeature
Loop

End Sub

Hello

For the error message, it's probably related to empty variables due to lack of control over the content of these in the code.
For SkecthManager, you need to look at the help code Get Sketch Entities Example (VBA) - 2021 - SOLIDWORKS API Help
To help and test would need a file corresponding to what you want to do.

2 Likes

0579794.SLDPRT (71.8 KB)
Here is the part on which I am doing my tests, I will look at the example and try to adapt to my needs.

The goal is to be able to establish the depth of the tube, its thickness, its dimensions, and the cutting angle in relation to a marker.