0 Replies Latest reply on Aug 19, 2016 11:35 AM by John Lindskog

    API - Change the Configuration and Visibility of a Drawing View or BOM table.

    John Lindskog

      If you have a Macro that creates new configurations of a part or assembly, you may want a subroutine that can change the configuration of a drawing view or BOM Table in a drawing. This Macro does just that.

       

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

      Sub ChangeDrawingView(DrawingViewName As String, VisibleOrNot As Boolean, Optional Configuration As String) 'Written by John D. Lindskog

      'This subroutine changes the visibility and configuration of a drawing view or drawing bill of materials table. If the optional

      'configuration argument is ommitted or the VisibleOrNot Boolean is false or the configuration is not available then the

      'configuration is not changed.  This subroutine understandably does not change the configuration of Detail or Section Views.

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

       

       

      Dim swDrawingDoc As SldWorks.DrawingDoc

      Dim swSheet As SldWorks.Sheet

      Dim swView As SldWorks.View

      Dim swConfigMgr As SldWorks.ConfigurationManager

      Dim swConfiguration As SldWorks.Configuration

      Dim swBomFeature As SldWorks.BomFeature

      Dim swBomTableAnnotation As SldWorks.BomTableAnnotation

      Dim swTableAnnotation As SldWorks.TableAnnotation

      Dim swAnn As SldWorks.Annotation

      Dim swSelMgr As SldWorks.SelectionMgr

       

       

      Set swDrawingDoc = swModel

      If swDrawingDoc Is Nothing Then                                             'If there is a problem then send a message

          MsgBox "The Drawing Doc object was not connected to the drawing ModelDoc2 object.", vbOKOnly, _

          "Object was not found by ChangeDrawingView function call."

          Exit Sub                                                                'to the user and end the subroutine.

      End If

       

       

      Set swSelMgr = swModel.SelectionManager                                     'Connect to the Selection Manager.

      If swSelMgr Is Nothing Then                                                 'If there is a problem then send a message

          MsgBox "The Selection Manager object was not connected to the drawing ModelDoc2 object.", vbOKOnly, _

          "Object was not found by ChangeDrawingView function call."

          Exit Sub                                                                'to the user and end the subroutine.

      End If

       

       

      swModel.Extension.SelectByID2 DrawingViewName, "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0

      Set swView = swSelMgr.GetSelectedObject6(1, -1)                             'Set the View object to the selected object

      If swView Is Nothing Then

          swModel.Extension.SelectByID2 DrawingViewName, "BOMFEATURE", 0, 0, 0, False, 0, Nothing, 0

          Set swBomFeature = swSelMgr.GetSelectedObject6(1, -1)                   'Set the BOM object to the selected object

          If swBomFeature Is Nothing Then

              MsgBox "Error selecting the object: " & DrawingViewName & ", please check name.", vbOKOnly, _

              "Object was not found by ChangeDrawingView function call."

              Exit Sub

          End If

      Else

          Dim swViewSheet As SldWorks.Sheet                                       'Check to make sure the drawing sheet that the view

          Set swViewSheet = swView.Sheet                                          'is on is the active sheet.  If not, then change

          Set swSheet = swDrawingDoc.GetCurrentSheet                              'the active sheet and reacquire the view object.

          If Not StrComp(swSheet.GetName, swViewSheet.GetName, vbTextCompare) = 0 Then

              swDrawingDoc.ActivateSheet (swViewSheet.GetName)

              swModel.Extension.SelectByID2 DrawingViewName, "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0

              Set swView = swSelMgr.GetSelectedObject6(1, -1)

              If swView Is Nothing Then

                  MsgBox "Error selecting the drawing view object " & DrawingViewName & ", please check name.", vbOKOnly, _

                  "Object was not found by ChangeDrawingView function call."

                  Exit Sub

              End If

          End If

      End If

       

       

      If swSelMgr.GetSelectedObjectType3(1, -1) = swSelectType_e.swSelDRAWINGVIEWS Then

          swView.SetVisible VisibleOrNot, False                                   'Set the View visibility

          If Configuration = "" Then                                              'If configuration empty then

              swModel.EditRebuild3                                                'we are finished, redraw the window and

              Exit Sub                                                            'exit this subroutine.

          End If

          If swView.Type = SwConst.swDrawingViewTypes_e.swDrawingDetailView Then

              MsgBox "Error Changing Configuration of " & DrawingViewName, vbOKOnly, _

              "Detail View configurations cannot be changed by ChangeDrawingView function call."

              Exit Sub

          ElseIf swView.Type = SwConst.swDrawingViewTypes_e.swDrawingSectionView Then

              MsgBox "Error Changing Configuration of " & DrawingViewName, vbOKOnly, _

              "Section Views configurations cannot be changed by ChangeDrawingView function call."

              Exit Sub

          End If

          If Not StrComp(Configuration, swView.ReferencedConfiguration, vbTextCompare) = 0 Then

              Dim swModelTempObject As SldWorks.ModelDoc2                         'Check if the view is already set to the desired

              Set swModelTempObject = swView.ReferencedDocument                   'configuration.  If not then change it.

              If Not swModelTempObject Is Nothing Then

                  Set swConfiguration = swModelTempObject.GetConfigurationByName(Configuration)

                  If Not swConfiguration Is Nothing Then

                      swView.ReferencedConfiguration = Configuration              'Change the View configuration.

                      swModel.EditRebuild3                                        'This function must be called to see config changes

                  Else

                      MsgBox "Error Changing Configuration of " & DrawingViewName, vbOKOnly, _

                      "ChangeDrawingView function call could not obtain Configuration object."

                      Exit Sub                                                    'If configuration is not available then send the

                  End If                                                          'user a message and exit this subroutine.

              Else

                  MsgBox "Error Changing Configuration of " & DrawingViewName, vbOKOnly, _

                  "ChangeDrawingView function call could not obtain View Part or Assembly object"

                  Exit Sub

              End If

          End If

      ElseIf swSelMgr.GetSelectedObjectType3(1, -1) = swSelectType_e.swSelBOMFEATURES Then

          Dim TableAnnotationsArray As Variant

          TableAnnotationsArray = swBomFeature.GetTableAnnotations                'Get TableAnnotationsArray from BomFeature object

          Set swBomTableAnnotation = TableAnnotationsArray(0)                     'Get BomTableAnnotation object from that array

          Set swTableAnnotation = swBomTableAnnotation                            'Get TableAnnotation object from that object

          Set swAnn = swTableAnnotation.GetAnnotation                             'Get Annotation object from that object

          If VisibleOrNot = True Then

              swAnn.Visible = swAnnotationVisibilityState_e.swAnnotationVisible   'Use the Annotation object to set the

              swModel.EditRebuild3                                                'Visibility of the BOM

          Else

              swAnn.Visible = swAnnotationVisibilityState_e.swAnnotationHidden

          End If

          If VisibleOrNot = False Or Configuration = "" Then

              swModel.EditRebuild3                                                'If BOM is hidden or configuration empty then

              Exit Sub                                                            'we are finished, exit the subroutine

          End If

          Dim ConfigurationCount As Integer

          ConfigurationCount = swBomFeature.GetConfigurationCount(False)

          Dim ConfigurationVisible As Variant

          Dim ConfigurationNames As Variant

          ConfigurationNames = swBomFeature.GetConfigurations(False, ConfigurationVisible)

          Dim count As Integer

          For count = 0 To ConfigurationCount - 1

              If StrComp(Configuration, ConfigurationNames(count), vbTextCompare) = 0 Then

                  swBomFeature.Configuration = Configuration                      'Set BOM Configuration

                  swModel.EditRebuild3

                  Exit For

              ElseIf count = ConfigurationCount - 1 Then                          'If the configuration is not available then

                  MsgBox "Cannot change configuration of " & DrawingViewName & " because configuration " & Configuration & _

                  " was not available.", vbOKOnly, "ChangeDrawingView function call could not change Configuration"

              End If                                                              'send the user a message.

          Next count

      Else                                                                        'If anything other than a drawing view or BOM is

          MsgBox "Error Changing: " & DrawingViewName, vbOKOnly, "Object Type not supported by ChangeDrawingView function" & _

                 " call.  Only Drawing Views and BOM Tables are supported."

      End If                                                                      'passed to the function call then do nothing.

       

       

      Set swDrawingDoc = Nothing

      Set swSheet = Nothing

      Set swView = Nothing

      Set swConfigMgr = Nothing

      Set swConfiguration = Nothing

      Set swBomFeature = Nothing

      Set swBomTableAnnotation = Nothing

      Set swTableAnnotation = Nothing

      Set swAnn = Nothing

      Set swSelMgr = Nothing

       

       

      End Sub