5 Replies Latest reply on Sep 5, 2017 8:06 AM by Deepak Gupta

    Do something to cutlist 'FirstFeature' - VBA

    Raymond Yau

      Hi,

       

      I'm having some trouble getting the logic right on a macro I have. The macro currently performs a function to each and every item in the cutlist. Something like:

       

      Set swFeat = swModel.FirstFeature
      
      Do While Not swFeat is Nothing
      //set some variables
           If swFeat.GetTypeName = "CutListFolder" Then
                //Do something i.e. get cutlist properties
           End If
      set swFeat = swFeat.GetNextFeature
      loop
      
      
      

       

      But what if I wanted to perform an action only to the first item on the cutlist before the Do While loop? I tried to perform a function only to the first item on the list by adding the function outside the loop but for some reason, it doesn't get/select the first item on the list.

       

      Set swFeat = swModel.FirstFeature
      
      If Not swFeat Is Nothing Then
      //Do something i.e add new property to first cutlist item 
      //Debug.print new property
      End If
      
      Do While Not swFeat is Nothing
      //set other variables
           If swFeat.GetTypeName = "CutListFolder" Then
                //Do something i.e. get all cutlist properties
           End If
      set swFeat = swFeat.GetNextFeature
      loop
      
      

       

      Sorry if its a bit vague, I'll update this with more info if it's not clear enough.

        • Re: Do something to cutlist 'FirstFeature' - VBA
          Nilesh Patel

          Hi Raymond,

           

          You will have to move following lines if you want to access the first item in the cut list folder

          1. //Do something i.e add new property to first cutlist item  
          2. //Debug.print new property 

          After this line:

               If swFeat.GetTypeName = "CutListFolder" Then

           

          Remove following lines:

          1. If Not swFeat Is Nothing Then 
          2. //Do something i.e add new property to first cutlist item  
          3. //Debug.print new property 
          4. End If

          The first feature in the part or assembly document is 'Favourites" or "History" folder.

           

          You will also have to make sure that when the 'Do While' loop is run second time, you skip codes for the first item in the cut list folder. Could you attach your part?

           

          Regards,

          • Re: Do something to cutlist 'FirstFeature' - VBA
            Deepak Gupta

            Try these

             

            Option Explicit

            Dim swApp           As SldWorks.SldWorks

            Dim swModel         As ModelDoc2

            Dim swFeat          As SldWorks.Feature

            Dim strValue(1)     As String

             

            sub Main()

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

               

                Set swFeat = swModel.FirstFeature

                 Do While Not swFeat Is Nothing

                   

                    If swFeat Is Nothing Then

                        Exit Do

                    End If

                  

                    If swFeat.GetTypeName2 = "CutListFolder" Then

                        swFeat.CustomPropertyManager.Get4 "DESCRIPTION", False, strValue(0), strValue(1)

                        Debug.Print strValue(1)

                        'Add you codes here

                        Exit Do

                    End If

              

                    Set swFeat = swFeat.GetNextFeature

                Loop

             

            End Sub

            And please upload files to this forums directly. Check #18 of Forum Posting on how to attach files while replying to a post.