Toegang tot onderdeelgegevens via de SolidWorks 2021 API

Hallo
Ik werk aan de solidwork API en probeer toegang te krijgen tot de informatie in een deel. Ik werk aan stukken op lengte, samengesteld uit extrusie, en soms materiaalverwijdering.
Ik ben erin geslaagd om de afmetingen van de extrusie te herstellen met mijn code, maar er zijn schetsbladeren aanwezig, en ik zou ze graag willen kunnen uitsluiten.

Dus probeerde ik de bijbehorende schets voor de extrusie te vinden, zodat ik de gegevens kon lezen. Het lukte me om de naam van de schets te vinden, maar ik had geen toegang tot de gegevens. Regelmatig krijg ik de foutmelding " Eigenschap of methode niet afgehandeld door dit object ". Ik moet iets missen in de manipulatie van objecten.
Ik heb de SketchManager gezien, maar ik begrijp niet hoe ik hem moet gebruiken.

Hier is mijn 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"

Einde Sub

En mijn code om de bovenliggende relaties van de functie te vinden:
Sub ListFeatureParents()
Dim swApp als SldWorks.SldWorks
Dim swModel als SldWorks.ModelDoc2
Dim swFeature als SldWorks.Feature
Dim swParentFeature als SldWorks.Feature
Dim vParents als variant
Dim i als geheel getal

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

Einde Sub

Hallo

Voor de foutmelding heeft het waarschijnlijk te maken met lege variabelen vanwege een gebrek aan controle over de inhoud hiervan in de code.
Voor SkecthManager moet u kijken naar de helpcode Get Sketch Entities Example (VBA) - 2021 - SOLIDWORKS API Help
Om te helpen en te testen heb je een bestand nodig dat overeenkomt met wat je wilt doen.

2 likes

0579794.SLDPRT (71.8 KB)
Hier is het deel waarop ik mijn tests doe, ik zal naar het voorbeeld kijken en proberen me aan te passen aan mijn behoeften.

Het doel is om de diepte van de buis, de dikte, de afmetingen en de snijhoek ten opzichte van een marker te kunnen vaststellen.