6 Replies Latest reply on Mar 9, 2016 3:52 PM by Chris Vaught

    How to get the display state name of the model in a drawing view

    Adam Hoffman

      I'm looping through some views in a drawing. The drawing contains an assembly, one configuration throughout the entire drawing, but I have different display states in each view. How can I obtain the name of the display state currently being applied in each view?

        • Re: How to get the display state name of the model in a drawing view
          Simon Turner

          I haven't tried this myself, but give it a go:

          From the View object, call RootDrawingComponent to get a DrawingComponent object.

          From that, call Component to get a Component2 object.

          From that, call ReferencedDisplayState to get the name of the display state.

           

            • Re: How to get the display state name of the model in a drawing view
              Adam Hoffman

              I tried going through the drawing component like you suggested and tried getting it from the assembly doc itself by using View.ReferencedDocument. In both cases getting the root component and checking the Comp.ReferencedDisplayState. It keeps returning an empty string. Any other ideas?

               

              Code for ReferencedDocument Method:

               

               

              Dim swApp     As SldWorks.SldWorks
              Dim swModel   As SldWorks.ModelDoc2
              Dim swDraw    As SldWorks.DrawingDoc
              Dim swView    As SldWorks.View

               

              Sub main()

               

              Set swApp = _
              Application.SldWorks

               

              If swApp.GetDocumentCount() = 0 Then Exit Sub

               

              Set swModel = swApp.ActiveDoc

               

              If Not swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then Exit Sub

               

              Set swDraw = swModel

              Set swView = swDraw.GetFirstView

              Set swView = swView.GetNextView

               

              Do While Not swView Is Nothing

               

                 Dim swDrawModel As SldWorks.ModelDoc2
                
                 Set swDrawModel = swView.ReferencedDocument
                
                 If swDrawModel.GetType() = swDocumentTypes_e.swDocASSEMBLY Then
                   
                    Dim swConFig          As SldWorks.Configuration
                    Dim swComp            As SldWorks.Component2
                    Dim sCurrentDisplay   As String
                   
                    Set swConFig = swDrawModel.GetConfigurationByName(swView.ReferencedConfiguration)
                   
                    If Not swConFig Is Nothing Then
                      
                       swDrawModel.ShowConfiguration2 swConFig.Name
                      
                       Set swComp = swConFig.GetRootComponent3(True)
                   
                       sCurrentDisplay = swComp.ReferencedDisplayState
                   
                       MsgBox ("The Name Of The View Is: " + swView.Name + vbNewLine + "The Referenced Display State Name Is: " + sCurrentDisplay)
                   
                    End If
                
                 End If
                
                 Set swView = swView.GetNextView
                
              Loop

               

              MsgBox ("Macro Complete")

               

              End Sub

               

               

               

               

               

              Code for DrawingComponent method:

               

              Dim swApp     As SldWorks.SldWorks
              Dim swModel   As SldWorks.ModelDoc2
              Dim swDraw    As SldWorks.DrawingDoc
              Dim swView    As SldWorks.View

               

              Sub main()

               

              Set swApp = _
              Application.SldWorks

               

              If swApp.GetDocumentCount() = 0 Then Exit Sub

               

              Set swModel = swApp.ActiveDoc

               

              If Not swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then Exit Sub

               

              Set swDraw = swModel

              Set swView = swDraw.GetFirstView

              Set swView = swView.GetNextView

               

              Do While Not swView Is Nothing

               

                 Dim swDrawComp        As SldWorks.DrawingComponent
                 Dim swComp            As SldWorks.Component2
                 Dim sCurrentDisplay   As String
                
                 Set swDrawComp = swView.RootDrawingComponent
                
                 Set swComp = swDrawComp.Component
                
                 If Not swComp Is Nothing Then
                
                    sCurrentDisplay = swComp.ReferencedDisplayState
                   
                    MsgBox ("The Name Of The View Is: " + swView.Name + vbNewLine + "The Referenced Display State Name Is: " + sCurrentDisplay)
                   
                 End If

               

                 Set swView = swView.GetNextView


              Loop

               

              MsgBox ("Macro Complete")

               

              End Sub

            • Re: How to get the display state name of the model in a drawing view
              Ivana Kolin

              if you find a solution , please let me know . I'm looking for it for years.

               

              display state and drawing

              • Re: How to get the display state name of the model in a drawing view
                Chris Vaught

                I came across this thread when I was trying to solve the same issue recently. Below is a method that I am currently attempting to use. I'm sure there are edge cases where this hack won't work but it is accomplishing what I need for now. It isn't ideal but it appears to be working if you are using display states to control whether components are hidden or shown. Hopefully the api is eventually updated to support directly retrieving the referenced display state from the drawing view just like you can get the reference configuration.

                 

                1. Get the drawing view of interest from the drawing
                2. Get the referenced configuration from the view
                3. Get the visible components for the view using view.GetVisibleComponents()
                4. Open the assembly and make the configuration obtained from the view active
                5. Get the list of display states using configuration.GetDisplayStates()
                6. Apply the first display state using configuration.ApplyDisplayState(name)
                7. Get all of the components for the assembly doc using aslyDoc.getComponents(false)
                8. Iterate through each component checking the visiblity of each.
                9. Record the name of each visible component
                10. If the list of components matches the visible components recorded in the drawing view then you have found the matching display state. If not then repeat steps 6-9 until a match is found.

                 

                Unfortunately this is slightly more complex for assemblies containing sub assemblies. Basically you need to modify the list you obtain from the view.GetVisibleComponents in step 3 to match the naming convention of components returned in step 7 otherwise the two lists will never match. To do this, add the sub assembly component names to your list by splitting the component names returned. If you look at the results returned for both while debugging it is pretty straight forward to get it to match.