6 Replies Latest reply on Jan 30, 2014 9:06 AM by Yosi Shani

    VBA CODE TO INSERT DesignTable TO DRAWING

    Yosi Shani

      Hey all

      Is their a way to insert DesignTable to drawing (opened drawing) with macro?

      I managed to insert BOM table and revision table with macro but not DesignTable.

      I will appreciate any help

       

      Thanks

      Yosi

        • Re: VBA CODE TO INSERT DesignTable TO DRAWING
          Artem Taturevych

          You cannot create Design Table in Drawing (same behaviour from User Interface). You can only propagate the Design Table from underlying part or assembly to drawing. Make sure that you selected the drawing view which is referencing the part or assembly with design table. Then call this method: ModelDoc2::InsertFamilyTableNew.

           

          Please read the remarks session here: http://help.solidworks.com/2014/English/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.imodeldoc2~insertfamilytablenew.html

            • Re: VBA CODE TO INSERT DesignTable TO DRAWING
              Yosi Shani

              Hi Artem

              I tried to use InsertFamilyTableNew. but got a message box about variable.

              I have a code that opens from drawing the design table (from part). I run this code when drawing is open

              what do I miss in my code to insert the design table in the drawing. can I insert the design table in a spesific point in drawing?

               

              attaching the part and drawing files I'm testing

              Thanks

               

              here is the code:

              ''''''''''''''''''''''''''''''''''''''''''''''''

              Option Explicit

              Sub main()

                    Dim swApp                 As SldWorks.SldWorks

                    Dim swModel               As SldWorks.ModelDoc

                    Dim swDesTable            As SldWorks.DesignTable
                  
                    Dim nErrors               As Long
                  
                    Dim nWarnings             As Long

                    Dim longstatus            As Long
                   
                    Dim myView                As SldWorks.View
                   
                    Dim designTableDoc        As SldWorks.ModelDoc2
                   
                    Dim viewModelName         As String
                   
                    Dim myDesignTable         As SldWorks.DesignTable
                  
                    Set swApp = CreateObject("SldWorks.Application")

                    Set swModel = swApp.ActiveDoc
                 
                    Set myView = swModel.GetFirstView
                 
                    viewModelName = myView.GetReferencedModelName()

                    Set designTableDoc = swApp.ActivateDoc2(viewModelName, True, longstatus)

                    Set myDesignTable = designTableDoc.GetDesignTable()
                 
                    Set myDesignTable1 = designTableDoc.InsertFamilyTableNew(myDesignTable)
                   
                    myDesignTable.Attach

               

              End Sub

              '''''''''''''''''''''''''''''''''''''''''''''''

                • Re: VBA CODE TO INSERT DesignTable TO DRAWING
                  Simon Turner

                  Option Explicit

                  Option Compare Text

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim myDoc As ModelDoc2

                   

                  Dim boolstatus As Boolean

                  Dim longstatus As Long, longwarnings As Long

                   

                  Sub main()

                      Dim myView As View

                      Dim myDraw As DrawingDoc

                      Dim myName As String

                   

                      Set swApp = Application.SldWorks

                      Set myDoc = swApp.ActiveDoc

                      Set myDraw = myDoc

                      Set myView = myDraw.GetFirstView

                      Set myView = myView.GetNextView 'Because the first view is the drawing sheet

                      If myView Is Nothing Then Exit Sub

                      myName = myView.Name

                   

                      boolstatus = myDoc.ActivateView(myName)

                      boolstatus = myDoc.Extension.SelectByID2(myName, "DRAWINGVIEW", 0#, 0#, 0#, False, 0, Nothing, 0)

                   

                      myDoc.InsertFamilyTableNew

                  End Sub

                    • Re: VBA CODE TO INSERT DesignTable TO DRAWING
                      Yosi Shani

                      Hi Simon

                      Thank you very much. it works great

                      can I insert the design table in a spesific point in drawing? (for example in the right corner)

                       

                      Yosi

                        • Re: VBA CODE TO INSERT DesignTable TO DRAWING
                          Simon Turner

                          You can alter the Boundary of the OLEObject:

                           

                           

                          Option Explicit

                          Option Compare Text

                           

                          Dim swApp As SldWorks.SldWorks

                          Dim myDoc As ModelDoc2

                           

                          Dim boolstatus As Boolean

                          Dim longstatus As Long, longwarnings As Long

                           

                          Sub main()

                              Dim myView As View

                              Dim myDraw As DrawingDoc

                              Dim myName As String

                           

                              Set swApp = Application.SldWorks

                              Set myDoc = swApp.ActiveDoc

                              Set myDraw = myDoc

                              Set myView = myDraw.GetFirstView

                              Set myView = myView.GetNextView 'Because the first view is the drawing sheet

                              If myView Is Nothing Then Exit Sub

                              myName = myView.Name

                           

                              boolstatus = myDoc.ActivateView(myName)

                              boolstatus = myDoc.Extension.SelectByID2(myName, "DRAWINGVIEW", 0#, 0#, 0#, False, 0, Nothing, 0)

                           

                              myDoc.InsertFamilyTableNew

                           

                              RepositionLastOLEObject

                           

                          End Sub

                           

                           

                          Sub RepositionLastOLEObject()

                              Dim myObj As SwOLEObject

                              Dim myBoundaries As Variant

                              Dim myOLEobjs As Variant

                              Dim myHeight As Double

                              Dim myWidth As Double

                              Dim x As Double

                              Dim y As Double

                           

                              myOLEobjs = myDoc.Extension.GetOLEObjects(swOleObjectOptions_e.swOleObjectOptions_GetAll)

                              If IsEmpty(myOLEobjs) Then Exit Sub

                              Set myObj = myOLEobjs(UBound(myOLEobjs))

                           

                              myBoundaries = myObj.Boundaries

                           

                              'Existing height & width

                              myHeight = myBoundaries(4) - myBoundaries(1)

                              myWidth = myBoundaries(3) - myBoundaries(0)

                           

                              'New position

                              x = 250 / 1000

                              y = 500 / 1000

                           

                              myBoundaries(0) = x

                              myBoundaries(1) = y

                              myBoundaries(3) = x + myWidth

                              myBoundaries(4) = y + myHeight

                           

                              myObj.Boundaries = myBoundaries

                          End Sub