AnsweredAssumed Answered

Get first component name in assembly

Question asked by Aaron Heathcote on Jul 12, 2016
Latest reply on May 31, 2018 by Artem Taturevych

I am trying to get name of the first component/assembly in an assembly. I am new to writing in vb but slowly picking it up.

Its important that the name I get is the first part or assembly rather than the name of the whole assembly

 

Here is my current file structure that would be typical for my work. As you can see what I am trying to get is 222222-1110

This number may be different for every job.

File Structure.png

and I have achieved this using this code:

 

Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics




Partial Class SolidWorksMacro


    Public Sub main()


        Dim swModel As ModelDoc2                                'ModelDoc2 allows access to SOLIDWORKS parts, assemblies and drawing documents 
        Dim swModelDocExt As ModelDocExtension                  'I think this looks at the document type


        swModel = swApp.ActiveDoc                               'sets the current document to the active document
        swModelDocExt = swModel.Extension                       'get the current document extension e.g. .sldasm


        Dim swFeature As Feature
        Dim featureName As String
        swFeature = swModel.FirstFeature
        featureName = swFeature.Name 'Retrieves "Comments"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Favorites"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "History"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Selection Sets"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Sensors"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Tables"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Live Section PLanes"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Design Binder"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Annotations"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Lights, Camera and Scenes
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Equations"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Front Plane"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Top Plane"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Right Plane"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "Origin"
        swFeature = swFeature.GetNextFeature
        featureName = swFeature.Name 'Retrieves "222222-1110-1" THIS IS WHAT I WANTED :)


        MsgBox(featureName)




    End Sub


    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks




End Class

 

Now as I said I'm new, so i was wondering if this is the best way to do this? seems long winded. I've been on the api help for about 4 hours now and this is the only way i could get the result I wanted.

 

Once I have worked this out I would then like to get the name of the folder that first part is in but will come to it when I suss out the first problem.

 

Any help I would be truly grateful.

 

PS, ignore all the comments in the code, I'm new to api so I comment to help me remember what everything is doing

Outcomes