Select custom table by name and delete

Select custom table by name and delete

shallN6L5D
Enthusiast Enthusiast
303 Views
5 Replies
Message 1 of 6

Select custom table by name and delete

shallN6L5D
Enthusiast
Enthusiast

How can I select and delete two custom tables from a drawing using iLogic?

 

A drawing may or may not contain these two custom tables, but when it does, they will always have the same Title ( oTable.Title property ) - let's say "Table1" and "Table2" - in a For loop such as 

For Each oTable As CustomTable In ThisApplication.ActiveDocument.ActiveSheet.CustomTables

 

For the purpose of coding it should be simpler to delete these tables and recreate them using the same code that originally created them from model properties and parameters; instead of modifying cell values. The new tables may have a different number of rows. When the model is updated, running this iLogic rule will update some tabulated manufacturing data by deleting the old tables in the drawing and making new tables.

 

The problem I have is that my For loop above which steps through all the custom tables in a drawing will find and delete the tables, but then the iLogic ends - it doesn't exit the loop nicely and carry on through the code. I suspect it runs into trouble after the tables are deleted, and there are no more custom tables to loop through.

 

Is there an alternative way to select these tables by Title and delete them? Perhaps without using a For loop?

 

I have tried counting the custom tables and looping based on the count, but this doesn't seem to work either. The internet seems to think a loop is the best way to select the tables, but I'm not convinced.

0 Likes
Accepted solutions (1)
304 Views
5 Replies
Replies (5)
Message 2 of 6

A.Acheson
Mentor
Mentor

Hi @shallN6L5D 

 

We would need to see the other code your trying to work with. It sounds like a sequencing problem. Can you share the code for context? If your having trouble sequencing I always like to run in a  seperate rule so you have it working in isolation then bring in your complicated code and patch in the variables needed.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 6

shallN6L5D
Enthusiast
Enthusiast

Hi @A.Acheson here are the two functions within my code. I have peppered it with traces so that I can see where it gets up to. Rather than send the full script, here is the problem section of code.

'Function to open and switch to an existing drawing
'If drawing is open, switch to it (make it the active document)
'If not; Open existing drawing and switch to it (make it the active document)
Private Function OpenExistingDrawing()
	Dim CheckForExistingDrawing As Boolean
	Dim oPart As Document = ThisApplication.ActiveDocument
	Dim oDrawDoc As Document
	Dim FilePath As String = oPart.FullFileName
	Dim FileFolder As String = System.IO.Path.GetDirectoryName(FilePath)
	Dim itemName As String = ThisDoc.FileName(False) 'without extension
    Dim drawingName As String = itemName & ".dwg"
	Dim drawingPath As String = System.IO.Path.Combine(FileFolder, drawingName)
    ' Loop through all open documents
    For Each doc As Document In ThisApplication.Documents
        ' Check if it's a drawing document with the expected name
        If doc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
            If doc.DisplayName = drawingName Then
                CheckForExistingDrawing = True
				doc.Activate()
				DeleteTable()
				Exit Function
            End If
        End If
    Next
	If CheckForExistingDrawing = False Then
		'open and activate the existing drawing
		oDrawDoc = ThisApplication.Documents.Open(drawingPath)
		oDrawDoc.Activate()
		DeleteTable()
	Else
		MessageBox.Show("Could not open the drawing")
	End If
	
End Function

'Function to delete the YBC table etc from an existing drawing which is currently active
Private Function DeleteTable()
'Get the active drawing document
    Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
	For Each oTable As CustomTable In oDrawDoc.ActiveSheet.CustomTables
        'Check if the table's name matches the ones you want
		If oTable.Title = "Pipe YBC" Then
            'Delete the table
            oTable.Delete()
			Logger.trace("deleted YBC table")
		End If
		If oTable.Title = "PIPE INFORMATION" Then
			'Delete the table
            oTable.Delete()
			Logger.trace("deleted pipe info table")
        End If
    Next
	Logger.trace("old tables deleted")

End Function

 

The first function works fine, and the second function (called by the first) works up to the point where it deletes the two tables. Confusingly it still prints the "old tables deleted" trace then jumps straight to the trace message in my Catch statement at the end of the main sub, without completing the rest of the main sub.

 

0 Likes
Message 4 of 6

cidhelp
Advocate
Advocate
Accepted solution

Hello @shallN6L5D ,

 

iterating a collection, you cannot delete elements from this collection.

You can iterate the collection and store the objects to delete in an ObjectCollection. After that iterate through the ObjectCollection and delete the elements.

Try changing your DeleteTable-Function to:

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oTabsToDelete As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim TabNamesToDelete As New ArrayList ({"Pipe YBC", "PIPE INFORMATION" })

For Each oTable As CustomTable In oDrawDoc.ActiveSheet.CustomTables
	If TabNamesToDelete.Contains(oTable.Title) Then
                oTabsToDelete.Add(oTable)
		Logger.Trace("found: " & oTable.Title)
	End If
Next
For Each oTab As CustomTable In oTabsToDelete
	TabName = oTab.Title
	oTab.Delete
	Logger.Trace("deleted: " & TabName)
Next 

 

0 Likes
Message 5 of 6

shallN6L5D
Enthusiast
Enthusiast

@cidhelp Thanks for this advice. I think I previously tried collecting the tables into another collection and then deleting from there, but that didn't work either. 

I'll try as you've suggested above, in case my methodology was wrong.

0 Likes
Message 6 of 6

shallN6L5D
Enthusiast
Enthusiast

Confirming the above code worked thanks. It ran so quickly that I had to modify the tables before I ran the Rule, to confirm they had been deleted and recreated.

0 Likes