Macro vb.net, Browse Features

Hi all

I need a little help with one of my macros! I usually manage to manage but not this time.
I would like to browse all the features of a part or assembly

In vba, with a macro integrated into SolidWorks, I get there with the code below.
But I would like to be able to do it in Vb.net without having to open Solidworks.
I have a bug on the last line. I have the impression that you can't access swFeat without opening SolidWorks?

If someone can help me, that would be cool!

Thank you

VBA Code:

Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc

If Not swModel Is Nothing Then

   Dim swFeat As SldWorks.Feature
   Set swFeat = swModel.FirstFeature

   While Not swFeat Is Nothing
       Debug.Print "    " & swFeat.Name & " [" & swFeat.GetTypeName & "]"
        Set swFeat = swFeat.GetNextFeature
    Wend

End If

Vb.net Code:

       Dim sDocFileName = "W:\...."

        Dim swClassFact As SwDMClassFactory
        Dim swDocMgr As SwDMApplication
        Dim swDoc As SwDMDocument17
        Dim nDocType As Integer
        Dim nRetVal As Integer
        Dim swCfgMgr As SwDMConfigurationMgr

        If InStr(LCase(sDocFileName), "sldasm") > 0 Then
            nDocType = SwDmDocumentType.swDmDocumentAssembly
        Else
            nDocType = SwDmDocumentType.swDmDocumentUnknown
            Exit Sub
        End If

        If (nDocType = SwDmDocumentType.swDmDocumentAssembly) Then
            swClassFact = CreateObject("SwDocumentMgr.SwDMClassFactory") 'si bug :Frameworks cible = .NET Framework 4
            swDocMgr = swClassFact.GetApplication(sLicenseKey)
            swDoc = swDocMgr.GetDocument(sDocFileName, nDocType, True, nRetVal)
            swCfgMgr = swDoc.ConfigurationManager
            Select Case nRetVal
                Case 0
                    Debug.Print("File successfully opened : ")
            End Select
        End If

        Debug.Print("File = " & swDoc.FullName)
        Debug.Print("Active Configuration Name = " & swCfgMgr.GetActiveConfigurationName)


        Dim swSelMgr As SelectionMgr
        Dim swFeat As Feature

        swSelMgr = swDoc.SelectionManager
        swFeat = swCfgMgr.GetSelectedObject6(1, -1)

Hello;

To be able to view your features, it is essential that the Solidworks document is open.
… it would be easier with your entire macro...
The type of language chosen is no exception to this rule.
By the way, VBA macros are not "Integrated" into Solidworks, the Solidworks macro editor only avoids the manual addition of Libraries (References) and Add-ins to another editor (the Excel editor for example). an API.
(SOLIDWORKS API - 2021 - SOLIDWORKS Help).

Kind regards.

Thank you for your answer!

I still find it blizzard that you can't read the functions,
knowing that you can retrieve the list of all components, configs, custom properties.
Well, too bad.
PS: I didn't want to put all my code because it is way too long and I don't think it would have been much use especially if I ask for something impossible to do :slight_smile:

Hello
Nothing weird about this :wink:
What can be read in VBA or Vb.Net without opening the document is more related to extensions of possibility through Windows Explorer than pure SW.
For the rest, to browse a tree is a bit like listing the cells of an Excel table via macro, without opening it, impossible.

2 Likes