11 Replies Latest reply on Nov 9, 2016 3:32 AM by Deepak Gupta

    Insert Design Table in Drawing

    Di Anne

      Hi SW Community, Keith Rice, Deepak Gupta

       

      Does anyone know how to insert design table in 2D Drawing?

      or does SolidWorks API have the function on how to insert design table in 2D drawing?

       

      Thank you in advance.

       

      Regards,

      DiAnne

        • Re: Insert Design Table in Drawing
          Deepak Gupta

          Not sure on API but here is ho you can insert DT manually in drawing. You need to select the view first.

           

          • Re: Insert Design Table in Drawing
            Nilesh Patel

            Hi Di,

             

            As Deepak explained, you need to select the view to insert the design table. Also, you can only insert the design table if  the part or assembly in the view has a design table.

             

            Cannot find any API to insert Design Table in a drawing.

             

            Get the IModelDoc2 object from the view and use IModelDoc2.GetDesignTable to get the design table. Then you could get the data from Design Table and copy them into a General Table. Use IDrawingDoc.InsertTableAnnotation2 method to insert General Table.

             

            Unfortunately, data in the General table won't be para-metrically linked to Design Table.

             

            I will give it a try and let you know.

             

            Regards,

              • Re: Insert Design Table in Drawing
                Di Anne

                Hi Nilesh,

                 

                I can only get the design table and delete it but

                i can't find API function on how to insert it.

                 

                Thanks in advance.

                 

                Regards,

                DiAnne

                  • Re: Insert Design Table in Drawing
                    Nilesh Patel

                    Hi Di,

                     

                    Try following codes. As I explained earlier, it gets the data from design table from part or assembly (referenced in the drawing view) and copies then back to General Table . The data are not para-metrically linked. The drawing view must be selected before running the macro. Not sure if this is what you want.

                     

                    Option Explicit
                    
                    
                    Dim swApp As SldWorks.SldWorks
                    Dim swModel As SldWorks.ModelDoc2
                    Dim swViewModel As SldWorks.ModelDoc2
                    Dim swDraw As SldWorks.DrawingDoc
                    Dim swSelMgr As SldWorks.SelectionMgr
                    Dim swView As SldWorks.View
                    Dim swDesignTable As SldWorks.DesignTable
                    Dim swTableAnn As SldWorks.TableAnnotation
                    Dim docName As String
                    Dim i As Long
                    Dim j As Long
                    Dim totalRow As Integer
                    Dim totalColumn As Integer
                    Dim lErrors As Long
                    Dim lWarnings As Long
                    
                    
                    Sub main()
                    
                    
                        Set swApp = Application.SldWorks
                        Set swModel = swApp.ActiveDoc
                        
                        If swModel Is Nothing Then
                            swApp.SendMsgToUser2 "No document is open. Open a drawing document.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                            Exit Sub
                        End If
                    
                    
                        If swModel.GetType <> swDocumentTypes_e.swDocDRAWING Then
                            swApp.SendMsgToUser2 "Active document is not a drawing document. Open a drawing document.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                            Exit Sub
                        Else
                            Set swDraw = swModel
                        End If
                        
                        Set swSelMgr = swModel.SelectionManager
                        
                        Set swView = swSelMgr.GetSelectedObjectsDrawingView2(1, -1)
                        
                        If swView Is Nothing Then
                            swApp.SendMsgToUser2 "No drawing view is selected. Select a drawing view.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                            Exit Sub
                        Else
                            Set swViewModel = swView.ReferencedDocument
                            docName = swViewModel.GetPathName
                            docName = Right(docName, Len(docName) - InStrRev(docName, "\"))
                            
                        End If
                        
                        If swViewModel.Extension.HasDesignTable = False Then
                            swApp.SendMsgToUser2 "Referenced document '" & docName & "' in the drawing view '" & swView.Name & "' does not contain a design table.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                            Exit Sub
                        Else
                            Set swDesignTable = swViewModel.GetDesignTable
                            swApp.ActivateDoc3 swViewModel.GetPathName, False, swRebuildOnActivation_e.swDontRebuildActiveDoc, lErrors
                            If swDesignTable.Attach = False Then
                                swApp.SendMsgToUser2 "Error: Failed to attach design table.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                                Exit Sub
                            End If
                        End If
                    
                    
                        totalRow = swDesignTable.GetTotalRowCount
                        totalColumn = swDesignTable.GetTotalColumnCount
                        
                        Set swTableAnn = swDraw.InsertTableAnnotation2(True, 0, 0, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, "", totalRow + 1, totalColumn + 1)
                        
                        For i = 0 To totalRow
                            For j = 0 To totalColumn
                                swTableAnn.Text(i, j) = swDesignTable.GetEntryValue(i, j)
                            Next j
                        Next i
                        
                        swDesignTable.Detach
                        
                        swApp.CloseDoc (swViewModel.GetPathName)
                        swModel.ClearSelection2 True
                        swModel.ViewZoomtofit2
                        swModel.EditRebuild3
                        
                        If swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, lErrors, lWarnings) = False Then
                            swApp.SendMsgToUser2 "Error: Failed to save active drawing document.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                        End If
                        
                    End Sub
                    

                     

                    Regards,

                      • Re: Insert Design Table in Drawing
                        Di Anne

                        Hi Nilesh,

                         

                        I needed the design table from part model to be inserted in the drawing

                        so that if I'm going to update the data in the design table in the part model

                        it automatically updates the design table in the drawing.

                         

                        Anyways thanks for this work around you made but I already found out

                        how to insert the design table from part model.

                         

                        I used this function InsertFamilyTableNew.

                         

                        Thank you so much.

                         

                        Regards,

                        DiAnne

                  • Re: Insert Design Table in Drawing
                    Di Anne

                    Inserting Design Table in Drawing.

                     

                    2017 SOLIDWORKS API Help - InsertFamilyTableNew Method (IModelDoc2)   

                     

                    swDoc.Extension.SelectByID2("DrawingView1", "DRAWINGVIEW", 0, 0, 0, false, 0, null, 0);
                    swDoC.InsertFamilyTableNew();
                    
                    • Re: Insert Design Table in Drawing
                      Peter Brinkhuis

                      The macro recorder doesn't record anything while inserting a design table, unfortunately. I couldn't find anything in the API help either.

                       

                      I did notice however that the table gets treated like an OLE object. Maybe you can use InsertObjectFromFile to insert the excel document?

                      2015 SOLIDWORKS API Help - InsertObjectFromFile Method (IModelDocExtension)