18 Replies Latest reply on May 17, 2018 4:17 AM by Abilash V.

    Unable to select a sketch feature by selecting a view on the drawing

    Abilash V.

      I am been trying to select  all the sketch in drawing and trying to show them all. but the selection never works.

       

      Dim swApp As SldWorks.SldWorks
      Dim swmodel As SldWorks.ModelDoc2
      Dim selmgr As SldWorks.SelectionMgr
      Dim swSelData As SldWorks.SelectData
      Dim swview As SldWorks.View
      Dim swdraw As SldWorks.ModelDoc2
      Dim swfeat As SldWorks.Feature
      Dim swfeatmgr As SldWorks.FeatureManager
      Dim vfeat As Variant
      Dim swassem As SldWorks.AssemblyDoc
      Dim swcomp As SldWorks.Component2
      Dim vcomp As Variant
      Sub main()
      Set swApp = Application.SldWorks
      Set swmodel = swApp.ActiveDoc
      Set swSelMgr = swmodel.SelectionManager
      Set swview = swSelMgr.GetSelectedObject6(1, -1)
      Set swSelData = swSelMgr.CreateSelectData
      'Set swdraw = swview.RootDrawingComponent
      Debug.Print swview.GetName2
      Set swdraw = swview.ReferencedDocument
      Set swfeatmgr = swdraw.FeatureManager
      Debug.Print swdraw.GetTitle
      vfeat = swfeatmgr.GetFeatures(False)
      'For i = 0 To UBound(vfeat)
      'Set swfeat = vfeat(i)
      'Debug.Print swfeat.Name
      'Next
      Set swassem = swdraw
      ' swApp.ActivateDoc swdraw.GetTitle
      Debug.Print swassem.GetComponentCount(False)
      vcomp = swassem.GetComponents(False)
      For i = 0 To UBound(vcomp)
      Set swcomp = vcomp(i)
      Set swfeat = swcomp.FirstFeature
        While Not swfeat Is Nothing
      Debug.Print swfeat.GetTypeName2
      swmodel.ClearSelection2 True
      If swfeat.GetTypeName2 = "ProfileFeature" Then
      Dim bool As Boolean
      bool = swfeat.Select2(False, 0)
      swmodel.BlankSketch
      Debug.Print swfeat.Visible
      End If
      'swdraw.ClearSelection2 True
        Set swfeat = swfeat.GetNextFeature
      Wend
       
      Next
      End Sub

       

      Thanks

      kindly help

      Artem Taturevich  Christian Chu @

        • Re: Unable to select a sketch feature by selecting a view on the drawing
          Dave Paul

          Just for clarification, you want to open the assembly from the drawing and show all the sketches.  Is this correct?

          • Re: Unable to select a sketch feature by selecting a view on the drawing
            Josh Brady

            Well... For one thing you're never gonna show a sketch with the command BlankSketch.  You would need UnblankSketch.

            • Re: Unable to select a sketch feature by selecting a view on the drawing
              Simon Turner

              I have achieved this in the past by using SelectByID2 and specifying the name of the sketch as it appears in the drawing.

              Record a macro of yourself doing that to find the structure of the name to use. It will be something like "Sketch1@Assem-1@DrawingView1/ComponentName-1@Assem"

              So the code to show the sketch would be something like this:

               

                                  boolStatus = Draw.Extension.SelectByID2("Sketch1@Assem-1@DrawingView1/ComponentName-1@Assem", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

                                  If boolStatus Then Draw.UnBlankSketch()

              • Re: Unable to select a sketch feature by selecting a view on the drawing
                Abilash V.

                The following code id getting the error:

                Dim swApp As SldWorks.SldWorks

                Dim swmodel As SldWorks.ModelDoc2

                Dim bool As Boolean

                Dim swfeatmgr As SldWorks.FeatureManager

                Dim rootnode As SldWorks.TreeControlItem

                Dim swnode As SldWorks.TreeControlItem

                Dim swchildnode As SldWorks.TreeControlItem

                Dim swfirstnode As TreeControlItem

                Dim swfinalnode As SldWorks.TreeControlItem

                Dim fnalfeature As SldWorks.TreeControlItem

                Dim fnalfeature1 As SldWorks.TreeControlItem

                Dim longstat As Long, war As Long

                Dim swselmgr As SldWorks.SelectionMgr

                Dim swfeat As SldWorks.Feature

                Dim swview As SldWorks.View

                Dim prt As SldWorks.ModelDoc2

                Sub main()

                Set swApp = Application.SldWorks

                Set swmodel = swApp.ActiveDoc

                Set prt = swApp.ActiveDoc

                Set swfeatmgr = swmodel.FeatureManager

                Set rootnode = swfeatmgr.GetFeatureTreeRootItem2(0)

                Set swselmgr = swmodel.SelectionManager

                Set swview = swselmgr.GetSelectedObject6(1, -1)

                Debug.Print rootnode.Text

                    If Not rootnode Is Nothing Then

                    Set swnode = rootnode.GetFirstChild

                    Debug.Print swnode.Text

                        While Not swnode Is Nothing

                        Set swfirstnode = swnode.GetFirstChild

                            While Not swfirstnode Is Nothing

                            Debug.Print swfirstnode.Text

                            Set swchildnode = swfirstnode.GetFirstChild

                             If swview.Name = swfirstnode.Text Then

                                While Not swchildnode Is Nothing

                                Debug.Print swchildnode.Text

                                '*****************************************if loop

                            

                                Set swfinalnode = swchildnode.GetFirstChild

                                    While Not swfinalnode Is Nothing

                                    Debug.Print swfinalnode.Text

                                    Set fnalfeature = swfinalnode.GetFirstChild

                                        While Not fnalfeature Is Nothing

                                        Debug.Print fnalfeature.Text

                                        Set fnalfeature1 = fnalfeature.GetFirstChild

                                            While Not fnalfeature1 Is Nothing

                                            Debug.Print fnalfeature1.Text

                                            Set swfeat = fnalfeature1.Object

                                            If swfeat.GetTypeName2 = "ProfileFeature" Then

                                            bool = swfeat.Select2(False, -1)

                                            prt.BlankSketch

                                         '   prt.ClearSelection2 True

                                          '  swmodel.ForceRebuild3 False

                                            End If

                                            Set fnalfeature1 = fnalfeature1.GetNext

                                            Wend

                                        Set fnalfeature = fnalfeature.GetNext

                                        Wend

                                     Set swfinalnode = swfinalnode.GetNext

                                    Wend

                                 

                                '*******************

                                Set swchildnode = swchildnode.GetNext

                                Wend

                                End If

                            Set swfirstnode = swfirstnode.GetNext

                            Wend

                        Set swnode = swnode.GetNext

                        Wend

                    End If

                 

                 

                End Sub

                Capture.PNG

                'This is the error I am getting.

                 

                Alin Vargatu Christian Chu , Deepak Gupta KINDLY HELP

                  • Re: Unable to select a sketch feature by selecting a view on the drawing
                    Abilash V.

                    The following code can help:

                    Dim swApp As SldWorks.SldWorks

                    Dim swmodel As SldWorks.ModelDoc2

                    Dim bool As Boolean

                    Dim swfeatmgr As SldWorks.FeatureManager

                    Dim rootnode As SldWorks.TreeControlItem

                    Dim swnode As SldWorks.TreeControlItem

                    Dim swchildnode As SldWorks.TreeControlItem

                    Dim swfirstnode As TreeControlItem

                    Dim swfinalnode As SldWorks.TreeControlItem

                    Dim fnalfeature As SldWorks.TreeControlItem

                    Dim fnalfeature1 As SldWorks.TreeControlItem

                    Dim longstat As Long, war As Long

                    Dim swselmgr As SldWorks.SelectionMgr

                    Dim swFeat As SldWorks.Feature

                    Dim swview As SldWorks.View

                    Dim prt As SldWorks.ModelDoc2

                     

                     

                    Sub main()

                     

                     

                    Set swApp = Application.SldWorks

                    Set swmodel = swApp.ActiveDoc

                    Set prt = swApp.ActiveDoc

                    Set swfeatmgr = swmodel.FeatureManager

                    Set rootnode = swfeatmgr.GetFeatureTreeRootItem2(0)

                    Set swselmgr = swmodel.SelectionManager

                    Set swview = swselmgr.GetSelectedObject6(1, -1)

                    Dim swFeatsColl As Collection

                    Set swFeatsColl = New Collection

                     

                     

                    Debug.Print rootnode.Text

                        If Not rootnode Is Nothing Then

                        Set swnode = rootnode.GetFirstChild

                        Debug.Print swnode.Text

                            While Not swnode Is Nothing

                            Set swfirstnode = swnode.GetFirstChild

                                While Not swfirstnode Is Nothing

                                Debug.Print swfirstnode.Text

                                Set swchildnode = swfirstnode.GetFirstChild

                                 If swview.Name = swfirstnode.Text Then

                                    While Not swchildnode Is Nothing

                                    Debug.Print swchildnode.Text

                                    '*****************************************if loop

                                  

                                    Set swfinalnode = swchildnode.GetFirstChild

                                        While Not swfinalnode Is Nothing

                                        Debug.Print swfinalnode.Text

                                        Set fnalfeature = swfinalnode.GetFirstChild

                                            While Not fnalfeature Is Nothing

                                            Debug.Print fnalfeature.Text

                                            Set fnalfeature1 = fnalfeature.GetFirstChild

                                                While Not fnalfeature1 Is Nothing

                                                Debug.Print fnalfeature1.Text

                                                Set swFeat = fnalfeature1.Object

                                                If swFeat.GetTypeName2 = "ProfileFeature" Then

                                                bool = swFeat.Select2(True, -1)

                    '                            prt.BlankSketch

                    'prt.ForceRebuild3 False

                                             '   prt.ClearSelection2 True

                                              '  swmodel.ForceRebuild3 False

                                              swFeatsColl.Add swFeat

                                             

                                             

                                                End If

                                                swApp.Visible = True

                                                Set fnalfeature1 = fnalfeature1.GetNext

                                                Wend

                                            Set fnalfeature = fnalfeature.GetNext

                                            Wend

                                         Set swfinalnode = swfinalnode.GetNext

                                        Wend

                                       

                                    '*******************

                                    Set swchildnode = swchildnode.GetNext

                                    Wend

                                    End If

                                Set swfirstnode = swfirstnode.GetNext

                                Wend

                            Set swnode = swnode.GetNext

                            Wend

                        End If

                    ' prt.BlankSketch

                    'prt.ForceRebuild3 False

                      For i = 1 To swFeatsColl.Count

                            Dim swFeat1 As SldWorks.Feature

                            Set swFeat1 = swFeatsColl.Item(i)

                          bool = swFeat1.Select2(False, -1)

                    prt.BlankSketch

                    prt.ForceRebuild3 False

                        Next

                    End Sub

                     

                     

                     

                    Please find the link to download the macro.

                    http://abilashv.blogspot.in/2018/05/solidworks-macro-to-hideshow-sketch-in.html

                     

                    Artem Taturevich thanks for the insight on treecontrolitem.

                    • Re: Unable to select a sketch feature by selecting a view on the drawing
                      Deepak Gupta

                      Is the macro working for you except that error? For me it doesn't work at all and error out on this line

                       

                       

                      Can you attach a sample file to test/debug this macro?