5 Replies Latest reply on Oct 26, 2018 12:41 PM by Fifi Riri

    API MoveOrCopy() General Table

    Dennis Finn

      Hello all!

       

      I am exploring the ability to copy and paste tables on drawings.

       

      I am able to write a c# program that uses the MoveOrCopy function with the following:

      ...

      swModel = (ModelDoc2)swApp.ActiveDoc;

      boolstatus = swModel.Extension.SelectByID2("DetailItem40@Sheet1", "ANNOTATIONTABLES", 7.8e-03, 2.2e-02, 0, false, 0, null, 0); //x and y values don't seem to matter until you've pasted the table, then the values matter.

      swModel.Extension.MoveOrCopy(true, 1, false, 0, 0, 0, 0, -.0508, 0);

      ...

      Now this works great but the name "DetailItem40@Sheet1" will be different for each drawing I will be working with.  Also I don't see anyway to find the DetailItem# without manually creating a macro to find the number...?!??  Instead, I'd like to reference the table by name.

       

      What I've come up with is:

       

      boolstatus = swModel.Extension.SelectByID2("General Table1", "GENERALTABLEFEAT", 0, 0, 0, false, 0, null, 0);

       

      This will select the table in the drawing, however, the MoveOrCopy command does nothing.  It seems that switching the type of entity from "ANNOTATIONTABLES" to "GENERALTABLEFEAT" somehow makes MoveOrCopy() not work but I am struggling to see why this is the case.

       

      I'm hoping someone here can point me in the right direction.

       

      Thanks!

        • Re: API MoveOrCopy() General Table
          Fifi Riri

          Hello,

          One problem with GENERALTABLEFEAT would be that the name also increment.

          You could try this, it will find the number of the first general table and copy it.

           

          Option Explicit

          Dim swApp       As SldWorks.SldWorks

          Dim swDraw      As SldWorks.DrawingDoc

          Dim swView      As SldWorks.View

          Dim swAnn       As SldWorks.Annotation

          Dim swTable     As SldWorks.TableAnnotation

          Dim boolstatus  As Boolean

          Sub main()

          Set swApp = CreateObject("SldWorks.Application")

          Set swDraw = swApp.ActiveDoc

          Set swView = swDraw.GetFirstView

          Set swTable = swView.GetFirstTableAnnotation

          Do While Not swTable Is Nothing

              If swTable.Type = 0 Then ' only consider general tables

                  Set swAnn = swTable.GetAnnotation

                  Debug.Print swAnn.GetName & "@" & swView.Name

                  swDraw.ClearSelection2 True

                  boolstatus = swDraw.Extension.SelectByID2(swAnn.GetName & "@" & swView.Name, "ANNOTATIONTABLES", 0, 0, 0, False, 0, Nothing, 0)

                  boolstatus = swDraw.Extension.MoveOrCopy(True, 1, False, 0, 0, 0, 0, -0.0508, 0)

                  Exit Do ' exit after first table found

              Else

                  Set swTable = swTable.GetNext

              End If

          Loop

          End Sub

            • Re: API MoveOrCopy() General Table
              Dennis Finn

              Thank you!

               

              I'll do something like this and filter for the proper table name.

                • Re: API MoveOrCopy() General Table
                  Fifi Riri

                  Updated with Array (to prevent infinite loop), and Select2 instead of SelectByID2

                   

                  Option Explicit

                  Dim swApp       As SldWorks.SldWorks

                  Dim swDraw      As SldWorks.DrawingDoc

                  Dim swView      As SldWorks.View

                  Dim swAnn       As SldWorks.Annotation

                  Dim swTables    As Variant

                  Dim swTable     As Variant

                  Dim swTableAnn  As SldWorks.TableAnnotation

                  Dim boolstatus  As Boolean

                  Sub main()

                  Set swApp = CreateObject("SldWorks.Application")

                  Set swDraw = swApp.ActiveDoc

                  Set swView = swDraw.GetFirstView

                  swTables = swView.GetTableAnnotations

                  For Each swTable In swTables

                      Set swTableAnn = swTable

                      If swTableAnn.Type = 0 Then ' only consider general tables

                          Set swAnn = swTableAnn.GetAnnotation

                          Debug.Print swAnn.GetName & "@" & swView.Name

                          boolstatus = swAnn.Select2(False, 0)

                          boolstatus = swDraw.Extension.MoveOrCopy(True, 1, False, 0, 0, 0, 0, -0.0508, 0)

                          Exit For  ' exit after first table found

                      End If

                  Next

                  swDraw.ClearSelection2 True

                  End Sub

              • Re: API MoveOrCopy() General Table
                Josh Brady

                Dennis Finn wrote:

                 

                This will select the table in the drawing, however, the MoveOrCopy command does nothing. It seems that switching the type of entity from "ANNOTATIONTABLES" to "GENERALTABLEFEAT" somehow makes MoveOrCopy() not work but I am struggling to see why this is the case.

                 

                 

                I haven't tested Fifi's code, but it looks like it's probably the way to go...

                This is just some additional background on why this is the case...

                 

                There are a couple of different SolidWorks "things" associated with tables.  You've got the feature, which is sorta represented by the icon in the feature tree, and the actual annotation itself, which is the graphical representation on the drawing sheet.  When you click on the table on the sheet, the feature icon highlights in the tree, and vice versa.  But you're actually selecting different "things" that have different capabilities.  Your "GENERALTABLEFEAT" selection actually picks the feature in the tree, not the annotation.  Features can't "MoveOrCopy", but Annotations can.