9 Replies Latest reply on Apr 30, 2018 4:29 PM by Alex Burnett

    Hiding sketches from the design tree for a particular selected view

    Abilash V.

      What is the best method to hide all the sketch of a selected drawing view from the model tree using API/Macros.I need not use the View->Hide all sketches.Rather to hide the sketches from drawing.This is such that i would revoke any sketch later on.

       

       

       

      Abilash

        • Re: Hiding sketches from the design tree for a particular selected view
          Micheal Johnson Selvaraj

          Hi,

           

          The best way to hide all the sketch from the drawing views to select the sketch visibility from the head up the toolbar.

           

          Hopes this help..!

           

          Regards,

          Micheal Johnson S

          • Re: Hiding sketches from the design tree for a particular selected view
            Alex Burnett

            I've written some code in my c# add-in but the logic you'll want to follow is here. This will need to be run on the part file since it traverses the feature tree.

             

            // This assumes you have a ModelDoc2 object already named "model"
            
            Feature curFeature = default(Feature);
            Feature subFeature = default(Feature);
            
            string featureTypeName = string.Empty;
            double start = DateTime.Now.TimeOfDay.TotalMilliseconds;
            
            curFeature = model.FirstFeature();
            
            while (!curFeature.isNull)
            {
                   featureTypeName = curFeature.GetTypeName2();
                   
                   if (curFeature.Visible == (int)swVisibilityState_e.swVisibilityStateShown)
                   {
                          switch (featureTypeName)
                          {
                                 case "RefPlane":
                                 case "RefAxis":
                                 case "CoordSys":
                                 case "RefPoint":
                                 case "ReferenceCurve":
                                       curFeature.Select(false);
                                       model.BlankRefGeom();  // Hides features listed above
                                       break;
            
                                 case "ProfileFeature":
                                 case "3DProfileFeature":
                                       curFeature.Select(false);
                                       model.BlankSketch();  // Hides features listed above
                                       break;
                                 default:
                                       break;
                          }
                   }
                   else if (curFeature.Visible == (int)swVisibilityState_e.swVisibilityStateHide)
                   {
                          switch (featureTypeName)
                          {
                                 case "OriginProfileFeature":
                                       curFeature.Select(false);
                                       model.UnblankSketch();  // Shows origin feature (as specified by our drafting standard)
                                       break;
                                 default:
                                       break;
                          }
                   }
            
                   subFeature = curFeature.GetFirstSubFeature();
                   while (!subFeature.isNull)
                   {
                          featureTypeName = subFeature.FeatureTypeName;
            
                          if (subFeature.Visible == (int)swVisibilityState_e.swVisibilityStateShown)
                          {
                                 switch (featureTypeName)
                                 {
                                       case "RefPlane":
                                       case "RefAxis":
                                       case "CoordSys":
                                       case "RefPoint":
                                       case "ReferenceCurve":
                                              subFeature.Select(false);
                                              model.BlankRefGeom();  // Hides features listed above
                                              break;
            
                                       case "ProfileFeature":
                                       case "3DProfileFeature":
                                              subFeature.Select(false);
                                              model.BlankSketch();  // Hides features listed above
                                              break;
                                       default:
                                              break;
                                 }
                          }
                          else if (subFeature.Visible == (int)swVisibilityState_e.swVisibilityStateHide)
                          {
            
                                 switch (featureTypeName)
                                 {
                                       case "OriginProfileFeature":
                                              subFeature.Select(false);
                                              model.UnblankSketch();  // Shows origin feature
                                              break;
                                       default:
                                              break;
                                 }
                          }
                          subFeature = subFeature.GetNextSubFeature();
                   }
            
                   // Get the next feature
                   curFeature = curFeature.GetNextFeature();
            }
            
            • Re: Hiding sketches from the design tree for a particular selected view
              Josh Brady

              There is a macro here:

               

              Sketch showing in drawing view

               

              Also, assuming that your sketches are still named "Sketch[something]" you can use the text filter on the design tree to show only the sketches.

                • Re: Hiding sketches from the design tree for a particular selected view
                  Abilash V.

                  That is dedicated to assembly document and not for drawing and view inside it.

                   

                  Abilash

                    • Re: Hiding sketches from the design tree for a particular selected view
                      Deepak Gupta

                      Look at the codes in this post (old but gold) and you can find the required codes for your need (of course you would need to modify) API - how to unhide all sketches of an assembly in drawing views

                        • Re: Hiding sketches from the design tree for a particular selected view
                          Alex Burnett

                          I was able to get sample code working based on all of the linked references.

                           

                          Edit: I just realized you want only the selected view but this should work for you if you modify the swView variable and how it gets set.

                           

                          Dim swApp As SldWorks.SldWorks
                          Dim swModel As SldWorks.ModelDoc2
                          Dim swRefModel As SldWorks.ModelDoc2
                          Dim swSheet As SldWorks.Sheet
                          Dim swView As SldWorks.View
                          Dim curFeature As Feature
                          Dim subFeature As Feature
                          Dim cfgName As String
                          Dim lerr As Long
                          Dim bstatus As Boolean
                          Dim strFullSketchName As String
                          Dim swDrwComp As DrawingComponent
                          
                          
                          Sub main()
                          
                          
                              Set swApp = Application.SldWorks
                              Set swModel = swApp.ActiveDoc
                              
                              If swModel.GetType <> swDocDRAWING Then
                                  Msg = "Only Allowed on Drawings" ' Define message
                                  Style = vbOKOnly ' OK Button only
                                  Title = "Error" ' Define title
                                  Call MsgBox(Msg, Style, Title) ' Display error message
                                  Exit Sub ' Exit this program
                              End If
                              
                              Set swSheet = swModel.GetCurrentSheet
                              Set swView = swModel.GetFirstView
                          
                          
                              While Not swView Is Nothing
                                  Debug.Print " "
                                  Debug.Print "View = " & swView.GetName2 & " [" & swView.Type & "]"
                                  Debug.Print "View = " & swView.Name
                                  Debug.Print "Ref = " & swView.GetReferencedModelName
                                  Set swDrawComp = swView.RootDrawingComponent
                                  
                                  Set swRefModel = swView.ReferencedDocument
                                  cfgName = swView.ReferencedConfiguration
                                  If Not swRefModel Is Nothing Then
                                      Set swRefModel = swApp.ActivateDoc2(swRefModel.GetPathName, True, lerr)
                                      bstatus = swRefModel.ShowConfiguration2(cfgName)
                                      
                                      Dim featureTypeName As String
                          
                          
                                      Set curFeature = swRefModel.FirstFeature
                          
                          
                                      While Not curFeature Is Nothing
                                          Debug.Print curFeature.Name
                                          featureTypeName = curFeature.GetTypeName2
                                          If curFeature.Visible = swVisibilityState_e.swVisibilityStateShown Then
                                              Select Case (featureTypeName)
                                                  'Case "RefPlane", "RefAxis", "CoordSys", "RefPoint", "ReferenceCurve"
                                                  '    curFeature.Select (False)
                                                  '    swModel.BlankRefGeom
                                                  Case "ProfileFeature", "3DProfileFeature"
                                                      strFullSketchName = getSketchFullName(swView.Name, swDrawComp.Name, curFeature.Name)
                                                      'swApp.ActivateDoc2 swModel.GetPathName, True, lerr
                                                      bstatus = swModel.Extension.SelectByID2(strFullSketchName, "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
                                                      If bstatus Then
                                                      
                                                          swModel.BlankSketch
                                                      End If
                                              End Select
                                          End If
                                          
                                          Set subFeature = curFeature.GetFirstSubFeature
                                          
                                          While Not subFeature Is Nothing
                                              Debug.Print "   " & subFeature.Name
                                              featureTypeName = subFeature.GetTypeName2
                                              If subFeature.Visible = swVisibilityState_e.swVisibilityStateShown Then
                                                  Select Case (featureTypeName)
                                                      'Case "RefPlane", "RefAxis", "CoordSys", "RefPoint", "ReferenceCurve"
                                                      '    subFeature.Select (False)
                                                      '    swModel.BlankRefGeom
                                                      Case "ProfileFeature", "3DProfileFeature"
                                                          strFullSketchName = getSketchFullName(swView.Name, swDrawComp.Name, subFeature.Name)
                                                          bstatus = swModel.Extension.SelectByID2(strFullSketchName, "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
                                                          If bstatus Then
                                                              swModel.BlankSketch
                                                          End If
                                                  End Select
                                              End If
                                              
                                              Set subFeature = subFeature.GetNextSubFeature
                                              
                                          Wend
                                          
                                          ' Get the next feature
                                          Set curFeature = curFeature.GetNextFeature
                                          
                                      Wend
                                  End If
                                  
                                  Set swView = swView.GetNextView
                              Wend
                          
                          
                          swApp.ActivateDoc2 swModel.GetPathName, True, lerr
                          End Sub
                          
                          
                          Function getSketchFullName(strViewName As String, strComponentName As String, strSketchName As String) As String
                              'this function construct the name of the sketch for selection in drawing
                              Dim vStr As Variant
                              Dim i As Long
                              Dim tmpStr As String
                              Dim StrParentName As String
                              
                              vStr = Split(strComponentName, "/")
                              tmpStr = strSketchName & "@" & vStr(0) & "@" & strViewName
                              For i = 0 To UBound(vStr) - 1
                              StrParentName = Left(vStr(i), InStrRev(vStr(i), "-") - 1)
                              tmpStr = tmpStr & "/" & vStr(i + 1) & "@" & StrParentName
                              Next i
                              getSketchFullName = tmpStr
                              Debug.Print getSketchFullName
                          End Function