As part of a larger Macro, I need to scan drawing files for general tables with specific names and delete them. I've tried several methods from examples found here or from CodeStack, but can't get them to work. Either nothing happens or I get the message "None of the selected entities could be deleted". Here's the code that shows several methods I have tried. Any advice is appreciated.
I'm not sure of the best way to paste code, so here's my attempt
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim BoolStatus As Boolean
Dim swFeat As SldWorks.Feature
Dim swSubFeat As SldWorks.Feature
Dim TARGET_TABLE_NAME As String
TARGET_TABLE_NAME = "General Table3" ' This is the name of the General Table to delete
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeat = swModel.FirstFeature
' Step through the feature tree, looking for a GENERAL TABLE with a name that matches TARGET_TABLE_NAME
Do Until swFeat Is Nothing
Set swSubFeat = swFeat.GetFirstSubFeature
Do Until swSubFeat Is Nothing
If swSubFeat.GetTypeName = "GeneralTableFeature" Then
If swSubFeat.Name = TARGET_TABLE_NAME Then
Debug.Print "Bingo"
BoolStatus = swModel.Extension.SelectByID2(swSubFeat.Name, swSubFeat.GetTypeName, 0, 0, 0, False, 0, Nothing, 0)
'Delete attempt #1
Call swModel.Extension.SelectByID2(swSubFeat.Name, swSubFeat.GetTypeName, 0, 0, 0, False, 0, Nothing, 0)
'Delete attempt #2
Call swModel.Extension.SelectByID2(swSubFeat.Name, swSubFeat.GetTypeName, 0, 0, 0, True, 0, Nothing, 0)
'Delete attempt #3
swModel.EditDelete
BoolStatus = swModel.DeleteSelection(False)
'Delete attempt #4
swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed
BoolStatus = swModel.DeleteSelection(False)
End If
End If
Set swSubFeat = swSubFeat.GetNextSubFeature
Loop
Set swFeat = swFeat.GetNextFeature
Loop
End Sub
SolidworksDrawings And Detailing
Hi Mark,
try that:
vSheets = swDraw.GetViews
For i = 0 To UBound(vSheets)
vViews = vSheets(i)
For j = 0 To UBound(vViews)
vTableAnnots = vViews(j).GetTableAnnotations
If Not IsEmpty(vTableAnnots) Then
For k = 0 To UBound(vTableAnnots)
If vTableAnnots(k).Type = 0 Then
Set swTable = vTableAnnots(k)
Set swGenTabFeat = swTable.GeneralTableFeature
Set swFeat = swGenTabFeat.GetFeature
swFeat.Select2 False, -1
bRet = swModel.Extension.DeleteSelection2(2)
End If
Next
End If
Next
Next
Kind regards
Ronny
Hi,
check out .. that should get you closer
https://www.codestack.net/solidworks-api/document/tables/find-tables-by-type/
https://forum.solidworks.com/thread/225065
http://help.solidworks.com/2018/English/api/sldworksapi/Get_Annotations_Example_VB.htm
https://ww3.cad.de/foren/ubb/Forum2/HTML/029396.shtml
SW API Help should help you with that 2019 SOLIDWORKS API Help - swSelectType_e Enumeration
Thank you Deepak. I was able to get my original code to work with your help.
That's crazy. I was looking for a feature of the type "GeneralTableFeature" instead of type "GENERALTABLEFEAT" because "GeneralTableFeature" is what is reported in the immediate window. Can you point me to a reference where I can ready more about this phenomenon?
Ronny, I figured out how to get your code to only look on the current sheet. Thanks for your help.
Replace
with
Thank you, Ronny. It took me a while to figure out the variable declarations, but I was able to get your code to work. Now to figure out how to make it delete tables by name.