1 Reply Latest reply on Dec 27, 2014 11:00 AM by Yong Ning

    How to get view name from BOM?

    Yong Ning

      Hope get view name from BOM.

      but follow code → get sheet name from BOM.

       

       

      0.jpg

      Folloe code, can get sheet name,but don't get view name

      method is SwAnn.Owner

       

       

      Sub ll()

        Dim SwApp As SldWorks.SldWorks, SwDraw As DrawingDoc

          Set SwApp = Application.SldWorks

          Set SwDraw = SwApp.ActiveDoc

        Dim SwSelMgr As SelectionMgr, SwView As View, SwSheet As Sheet

          Set SwSelMgr = SwDraw.SelectionManager

        Dim SwBomFeat As BomFeature

          Set SwBomFeat = SwSelMgr.GetSelectedObject5(1)

          Debug.Print SwBomFeat.GetFeature.Name

        Dim SwTabAnn As TableAnnotation, SwAnn As Annotation

          Set SwTabAnn = SwBomFeat.GetTableAnnotations(0)

          Set SwAnn = SwTabAnn.GetAnnotation

          Debug.Print SwAnn.GetName, SwAnn.OwnerType

          Debug.Print SwTabAnn.Title

       

          Set SwSheet = SwAnn.Owner

          Debug.Print SwSheet.GetName

          Debug.Print

       

       

      End Sub

       

       

      code from

      SolidWorks Forums: Get Sheet Name that BOM is on

      https://forum.solidworks.com/message/436330#436330

      SolidWorks Forums: Macro to delete BOM

      https://forum.solidworks.com/message/415225#415225

        • Re: How to get view name from BOM?
          Yong Ning

          SolidWorks Forums: Get Sheet Name that BOM is on

          https://forum.solidworks.com/message/436330#436330

          2012-8-27  Artem Taturevych  tips

           

          How to get →

          SwAnn.OwnerType = swAnnotationOwner_e.swAnnotationOwner_DrawingView  ????

           

           

              If SwAnn.OwnerType = swAnnotationOwner_e.swAnnotationOwner_DrawingSheet Then

                  Set SwSheet = SwAnn.Owner

              ElseIf SwAnn.OwnerType = swAnnotationOwner_e.swAnnotationOwner_DrawingView Then

                  Dim SwView As SldWorks.View

                  Set SwView = SwAnn.Owner

                  Set SwSheet = SwView.Sheet

              End If

           

          '------------------------------------------------------------------------------------

          'Created by Artem Taturevych (Intercad, Australia)

          'http://intercad.com.au/

          '------------------------------------------------------------------------------------

          'Disclamer: The API examples are provided as is and should be used as reference only.

          'In no event shall Intercad be liable for any types of damages whatsoever

          '(including without limitation, damages from the loss of use, data, profits, or business)

          'arising out of the uses of this information, applications, or services.

          '------------------------------------------------------------------------------------

           

           

          Const TABLE_NAME As String = "Bill of Materials1"

           

           

          Dim SwApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swFeat As SldWorks.Feature

           

           

          Sub main()

           

           

              Set SwApp = Application.SldWorks

              Set swModel = SwApp.ActiveDoc

              Set SwSelMgr = swModel.SelectionManager

              Set swFeat = swModel.FeatureByName(TABLE_NAME)

           

              If swFeat Is Nothing Then

                  MsgBox "Specified feature does not exist"

                  End

              End If

           

              Dim SwBomFeat As SldWorks.BomFeature

           

              Set SwBomFeat = swFeat.GetSpecificFeature2

           

              Dim swTableAnn As SldWorks.TableAnnotation

              Set swTableAnn = SwBomFeat.GetTableAnnotations()(0)

           

              Dim SwSheet As SldWorks.Sheet

           

              Set SwSheet = GetTableSheet(swTableAnn)

           

              If Not SwSheet Is Nothing Then

                  Debug.Print SwSheet.GetName

              Else

                  MsgBox "Error"

              End If

           

          End Sub

           

           

          Function GetTableSheet(swTableAnn As SldWorks.TableAnnotation) As Sheet

           

              Dim SwSheet As SldWorks.Sheet

              Dim SwAnn As SldWorks.Annotation

              Set SwAnn = swTableAnn.GetAnnotation()

           

              If SwAnn.OwnerType = swAnnotationOwner_e.swAnnotationOwner_DrawingSheet Then

                  Set SwSheet = SwAnn.Owner

              ElseIf SwAnn.OwnerType = swAnnotationOwner_e.swAnnotationOwner_DrawingView Then

                  Dim SwView As SldWorks.View

                  Set SwView = SwAnn.Owner

                  Set SwSheet = SwView.Sheet

              End If

           

              Set GetTableSheet = SwSheet

           

          End Function

           

          ***************

          Dim swApp As SldWorks.SldWorks

           

          Private Const strApp As String = "Link2BOM v1.00"

           

           

           

          Public Sub subMain()

           

              Dim iCnt, iTotal As Integer

              Dim retVal As Variant

              Dim sBOMname As String

              Dim vSheetNames As Variant

              Dim swView As SldWorks.View

              Dim swSheetView As SldWorks.View

              Dim swModel As SldWorks.ModelDoc2

              Dim swDrawing As SldWorks.DrawingDoc

              Dim swFeat As SldWorks.Feature

              Dim swBOM As SldWorks.Feature

           

           

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              ' No document loaded error handler

              If swModel Is Nothing Then

                  MsgBox "No document loaded !", vbCritical

                  End

              ElseIf Not swModel.GetType = 3 Then 'When no Drawing

                  MsgBox "This macro is only for a drawing !", vbCritical

                  End

              End If

              ''

              Set swDrawing = swModel

              Set swFeat = swModel.FirstFeature

           

              'Get BOM name

              Do While Not swFeat Is Nothing

                  If "BomFeat" = swFeat.GetTypeName Then

                      Set SwBomFeat = swFeat.GetSpecificFeature2

                      Set swBOM = SwBomFeat.GetFeature

                      sBOMname = swBOM.Name

                      Exit Do

                  End If

                  Set swFeat = swFeat.GetNextFeature

              Loop

           

              vSheetNames = swDrawing.GetSheetNames

              iTotal = UBound(vSheetNames) + 1

              iCnt = 0

           

              Do While iCnt < iTotal

                  'Set view linked to BOM

                  retVal = swDrawing.ActivateSheet(vSheetNames(iCnt))

                  Set swSheetView = swDrawing.GetFirstView

                  Set swView = swSheetView.GetNextView

                  Do While Not swView Is Nothing

                      retVal = swView.SetKeepLinkedToBOM(True, sBOMname)

                      Debug.Print swView.Name

                      Set swView = swView.GetNextView

                  Loop

                  iCnt = iCnt + 1

              Loop

              retVal = swDrawing.ActivateSheet(vSheetNames(0))

              swApp.SendMsgToUser2 "       Linking is complete...       ", swMbInformation, swMbOk

           

              Set swApp = Nothing

              Set swModel = Nothing

              Set swDrawing = Nothing

              Set swFeat = Nothing

              Set SwBomFeat = Nothing

              Set swBOM = Nothing

              Set swSheetView = Nothing

              Set swView = Nothing

              End

           

          End Sub