10 Replies Latest reply on May 11, 2018 9:46 AM by Jacob Corder

    Drawing view - How can I get displayed components only?

    William Cruz

      I have a drawing with multiple views, the assembly that the drawing was created from has some components that are not shown via an extrude-cut feature, they are not hidden or suppressed in the feature tree but they are not visible in the mode view. When I use:

       

      swThisView.GetVisibleComponents();
      

       

      It returns everything, all the components even though they are not in the drawing view. This is how I get my drawing views:

       

              internal List<View> swGetAllDwgViews()
              {
                  List<View> lstSwViews = new List<View>();
                  try
                  {
                      object[] swAllDwgViews = (_swDwgDoc.GetCurrentSheet()).GetViews() as object[];
      
      
                      if (swAllDwgViews != null)
                      {
                          foreach (View swDwgView in swAllDwgViews)
                          {
                              lstSwViews.Add(swDwgView);
                          }
                      }
      
      
                      return lstSwViews;
                  }
                  catch (Exception ex)
                  {
                      return lstSwViews;
                  }
              }
      

       

      I've tried going directly to the RootComponent of the drawing view and processing it that way but I still get all the components, including those hidden by the extrude cut feature.

        • Re: Drawing view - How can I get displayed components only?
          Alex Burnett

          I don't know how much this helps but perhaps the workflow in this macro can be modified to work for you.

           

          This iterates through all views in a drawing and hides on the drawing level any sketches that are shown. It does require that the part or assembly file be open in order to do so.

           

          Rather than iterating through the feature tree to see what sketch is shown, you would have to iterate through the feature tree to see what bodies are shown.

           

          Hiding sketches from the design tree for a particular selected view

          • Re: Drawing view - How can I get displayed components only?
            Abilash V.

            'drView.GetVisibleComponents- should  directly help you.

            ' By the way the following macro helps to traverse selected drawing view and selects the sketches in that,May be u can modify it. if you want more help ,feel free to ask.

             

            'Link for macro .: https://abilashv.blogspot.in/2018/05/solidworks-macro-to-hideshow-sketch-in.html

             

            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(True, -1)

                                     

                                     '   prt.ClearSelection2 True

                                      '  swmodel.ForceRebuild3 False

                                        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

            MsgBox "COMPLETED", vbDefaultButton1

            End Sub

             

            'Regards

            'Abilash

            • Re: Drawing view - How can I get displayed components only?
              Jacob Corder

              getvisiblecomponents does not take into account displaystates. so if they are hidden in a displaystate

               

              so what you have to do is

              kick solidworks in the face,  and then proceed to do this

              for each component you need to get the view, then get the modeldoc, then get the displaystate from the view,  then get the displaystatesetting from the assembly.

               

              oh and you have to activate that display state in the assembly to perform the check to see if it is visible.

               

               

              pretty great huh......

              • Re: Drawing view - How can I get displayed components only?
                Jacob Corder

                ok. so i have created a few extensions that you can use.

                 

                i was wrong about activating the display state.   that was for checking if the component is exploded in a display state.

                 

                 

                 <Extension()>
                    Function GetVisibilityInDisplayState(ByVal SwComponent As Component2, ByVal DisplayStateName As String) As swComponentVisibilityState_e
                        If SwComponent Is Nothing Then Return swComponentVisibilityState_e.swComponentUnknown
                        Dim VisVals As Object = SwComponent.GetVisibilityInAsmDisplayStates(swDisplayStateOpts_e.swSpecifyDisplayState, New String() {DisplayStateName})
                        If VisVals Is Nothing Then Return swComponentVisibilityState_e.swComponentUnknown
                        If VisVals(0) = swComponentVisibilityState_e.swComponentVisible Then
                            Return swComponentVisibilityState_e.swComponentVisible
                        Else
                            Return swComponentVisibilityState_e.swComponentHidden
                        End If
                
                
                    End Function
                

                 

                 

                <Extension()>
                    Function IsVisibleInView(ByVal SwDrawingComponent As DrawingComponent) As swComponentVisibilityState_e
                        If SwDrawingComponent Is Nothing Then Return swComponentVisibilityState_e.swComponentUnknown
                        If SwDrawingComponent.Visible = False Then
                            Return swComponentVisibilityState_e.swComponentHidden
                        End If
                        Dim SwView As View = Nothing
                        Dim SwComp As Component2 = Nothing
                        Try
                            SwView = SwDrawingComponent.View
                            If SwView Is Nothing Then Return False
                            Dim DispStateName As String = SwView.DisplayState
                            If Len(Trim(DispStateName)) > 0 Then
                                SwComp = SwDrawingComponent.Component
                                Return SwComp.GetVisibilityInDisplayState(DispStateName)
                            Else
                                Return swComponentVisibilityState_e.swComponentVisible
                            End If
                
                
                        Catch ex As Exception
                        Finally
                            '  you should really release references to objects, marshal.releaseCOMObject(swview)
                             ' marshal.releaseCOMObject(SwComp)
                        End Try
                        Return swComponentVisibilityState_e.swComponentUnknown
                    End Function
                
                  • Re: Drawing view - How can I get displayed components only?
                    William Cruz

                    I get two exceptions and SW force-closes when I run the below code. I code in c# but it's a direct translation except all I need is a Bool as a return value - something I noticed in your code is that SwComp is never assigned a value before sending it off to "GetVisibilityInDisplayState" , So I get the swDwgComp.Component and use that.

                     

                    The exceptions occur when calling "GetVisibilityInAsmDisplayStates"

                    Exception thrown: 'System.AccessViolationException'

                    Exception thrown: 'System.Reflection.TargetInvocationException'

                     

                        internal static bool IsVisibleInView(this DrawingComponent swDwgComp)
                        {
                            View swView = swDwgComp.View;
                    
                            try
                            {
                                if (!swDwgComp.Visible)
                                {
                                    return false;
                                }
                    
                                if (swView != null)
                                {
                                    string strDispStName = swView.DisplayState;
                    
                                    if (!string.IsNullOrEmpty(strDispStName))
                                    {
                                        return (swDwgComp.Component).GetVisibilityInDisplayState(strDispStName);
                                    }
                                    else
                                    {
                                        return true;
                                    }
                                }
                                else
                                {
                                    return false;
                                }
                            }
                            catch (Exception ex)
                            {
                                /*handle error*/
                                return false;
                            }
                            finally
                            {
                                Marshal.ReleaseComObject(swView);
                            }
                        }
                    
                        internal static bool GetVisibilityInDisplayState(this Component2 swComp, string strDispStName)
                        {
                            try
                            {
                                object[] AsmDisplayStates = swComp.GetVisibilityInAsmDisplayStates(
                                    (int)swDisplayStateOpts_e.swSpecifyDisplayState,
                                    new object[1] { strDispStName });
                    
                                if (AsmDisplayStates != null)
                                {
                                    if ((swComponentVisibilityState_e)AsmDisplayStates[0] ==
                                        swComponentVisibilityState_e.swComponentVisible)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                }
                                else
                                {
                                    return false;
                                }
                            }
                            catch (Exception ex)
                            {
                                /*handle errors*/
                                return false;
                            }
                            finally
                            {
                            }
                    }