.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Delete BlockTableRecord aka purge

26 REPLIES 26
Reply
Message 1 of 27
Ed.Jobe
1709 Views, 26 Replies

Delete BlockTableRecord aka purge

How do you delete/purge a block table record? The code I have below deletes a specific block and attempts to purge the block def. The ent gets erased ok, but the block table gets messed up.
[code]Public Sub RemoveStamp()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = New Database(False, False)

Dim t As Transaction = doc.TransactionManager.StartTransaction
Try
' delete block insertion
Dim bref As BlockReference = CType(t.GetObject(brefID, OpenMode.ForWrite), BlockReference)
' delete block definition
Dim bt As BlockTable = CType(t.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite), BlockTable)
Dim btr As BlockTableRecord = CType(t.GetObject(btrID, OpenMode.ForWrite), BlockTableRecord)
Try
bref.Erase()
btr.Erase()
Finally
CType(bref, IDisposable).Dispose()
CType(btr, IDisposable).Dispose()
End Try
t.Commit()
t.Dispose()
Finally
CType(t, IDisposable).Dispose()
End Try
End Sub[/code]

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

26 REPLIES 26
Message 21 of 27
Ed.Jobe
in reply to: Ed.Jobe

But I've come so far Master Yoda...

What I don't understand is.. In a command that calls this sub, I insert a block, delete the ref and then the def. The next time I run the command, the same sequence occurs. shouldn't the block def be there because I inserted the block again.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 22 of 27
Mikko
in reply to: Ed.Jobe

Here's what I've used... seems to work for me.

Public Function PurgeBlock(ByVal BlockName As String) As Boolean
Using db As Database = HostApplicationServices.WorkingDatabase
Using t As Transaction = db.TransactionManager.StartTransaction()
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Try
Dim tv() As TypedValue = {New TypedValue(DxfCode.Start, "INSERT"), New TypedValue(DxfCode.BlockName, BlockName)}
Dim sf As SelectionFilter = New SelectionFilter(tv)
Dim psr As PromptSelectionResult = ed.SelectAll(sf)
If Not psr.Status = PromptStatus.OK Then
Exit Try
End If
Dim ss As SelectionSet = psr.Value
Dim idarray As ObjectId() = ss.GetObjectIds()
Dim id As ObjectId
For Each id In idarray
Dim e As Entity = t.GetObject(id, OpenMode.ForWrite, True)
e.Erase()
Next id
Dim bt As BlockTable = t.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim idColl As ObjectIdCollection = New ObjectIdCollection()
For Each id In bt
idColl.Add(id)
Next
db.Purge(idColl)
For Each id In idColl
Dim btr As BlockTableRecord = t.GetObject(id, OpenMode.ForWrite)
btr.Erase(True)
Next
t.Commit()
Return True
Catch ex As Exception
Return False
End Try
End Using
End Using
End Function
Message 23 of 27
Anonymous
in reply to: Ed.Jobe

Hi Ed. The def is still there, its just erased.

See the ObjectARX docs for AcDbDatabase::purge().

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5565347@discussion.autodesk.com...
But I've come so far Master Yoda...

What I don't understand is.. In a command that calls this sub, I insert a block, delete the ref and then the def. The next time I run the command, the same sequence occurs. shouldn't the block def be there because I inserted the block again.
Message 24 of 27
Ed.Jobe
in reply to: Ed.Jobe

OK, so I can insert a block that has been erased, sort of like using (handent id) to recall an ent that was erased? When I insert the block, I just use the file name because the block is stored with the project. So the second time, its not fully restoring the bt, just using whats there?

Since Purge() doesn't actually remove the btr, it seems it only helps me avoid an error when the btr is referenced. I know for certain that there are no references. But my problem occurs when the record has been purged once and is marked as erased.

...I did some more testing. On the second pass, I reinsert the block again (this is part of a plot stamp program where I need to remove the block after plotting), and then delete it after plotting. Purge still returns the block id, even though an attempt to get the btr returns an eWasErased error. Then I tried using the overload for getting the btr that allows you to get a record that was erased, but then the block doesn't get erased. Help says that the arx Purge function has an overload that will skip erased records, but the managed method does not. I'm going around in circles here. Any other ideas? I think what I need is to be able to tell if the btr is marked as erased and not try to erase it again. Purge doesn't tell me that, so how can I find out?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 25 of 27
Anonymous
in reply to: Ed.Jobe

Padawan, have you taken a REALLY close look at the ObjectId structure?

Tony, I don't have '8 here to test, but is the......undesired behaviour...of the default indexer on SymbolTables still present?

Cheers,
Glenn.
Message 26 of 27
Anonymous
in reply to: Ed.Jobe

Glenn,
It does for layers. Haven't tested other tables but I'd assumed they were
the same.



wrote in message news:5568487@discussion.autodesk.com...
Padawan, have you taken a REALLY close look at the ObjectId structure?

Tony, I don't have '8 here to test, but is the......undesired behaviour...of
the default indexer on SymbolTables still present?

Cheers,
Glenn.
Message 27 of 27
Ed.Jobe
in reply to: Ed.Jobe

I will review the help docs on that subject. thx

I also found that the PlotMgr.DocumentEnded event that calls this sub is firing twice, which is not helping matters.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost