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

Modified Event of BlockReference Causes Crash

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
kbrown
1543 Views, 3 Replies

Modified Event of BlockReference Causes Crash

I have a group of classes that essentially implement the Model View Controller pattern. The view is a custom control contained in a PaletteSet. The Model is a class that wraps the ObjectID of a given block. I've used these classes to create a replacement for the Properties tool in AutoCAD (2011). Basically the UI can be used to edit the properties of a Dynamic Block. But I apply my own rules and behaviors to the UI.

 

I had no problems until I added support for the View to be updated when the block is modified (say by dragging to increase a length property). To do this I handle the Modified event of the block. After the block is modified, some rules are evaluated. If the rules result in changes to the block's properties, these changes are applied. This last part is where I'm having trouble. 

 

When the "Rule Evaluation" code is invoked because of changes to the view (WinForms Controls), I am able to update the block's Properties using the following code without issue (Sorry, it's VB.NET. My employers choice, not mine):

 

Public Shared Function SetParameter(ByVal BlockID As ObjectId, ByVal ParameterName As String, ByVal Value As Double) As Boolean
Using dl As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument(DocumentLockMode.ProtectedAutoWrite, Nothing, Nothing, True)
Using myTrans As Transaction = BlockID.Database.TransactionManager.StartTransaction
Dim myBRef As BlockReference = BlockID.GetObject(OpenMode.ForWrite)
For Each myDynamProp As DynamicBlockReferenceProperty In myBRef.DynamicBlockReferencePropertyCollection
If myDynamProp.PropertyName.Equals(ParameterName, StringComparison.OrdinalIgnoreCase) Then
myDynamProp.Value = Value
myTrans.Commit()
Return True
End If
Next
Return False
End Using
End Using
End Function

 

However, when this same code is invoked as a result of the BlockReference Modified event, AutoCAD completely crashes the momment the transaction.Commit() method is called. I've attempted to catch the exception causing the crash and role back the transaction, but AutoCAD simply exits and displays the error (Error handler re-entered. Exiting now.) and no exception is caught. Note that I do not use the BlockReference instance passed to the Modified event handler. Instead I store an ObjectID instance elsewhere which is consistently used to read/write the block. 

 

Is there something I must do before commiting a transaction when using the Modified event of BlockReference? I checked, and the same "Main Thread" is executing whether the code is invoked from a UI event or the Modified event. I tried calling the Close method, which is attributed as Obsolete, on the BlockReference object before executing the transaction without any success.

 

Thanks for any help. I'm a bit new to the AutoCAD API.

 

 

3 REPLIES 3
Message 2 of 4
kbrown
in reply to: kbrown

To make things clearer and easier to read, I've attached the code for the "Model" class mentioned in the opening post. After the class definition, I've provided the code for one of the methods called to update a block property value. When the transaction.Commit() method is called, AutoCAD crashes as previously described. 

Message 3 of 4
Alfred.NESWADBA
in reply to: kbrown

Hi,

 

>> However, when this same code is invoked as a result of the BlockReference Modified event,

>> AutoCAD completely crashes the momment the transaction.Commit() method is called

Within the event AutoCAD has the blockreference currently open (and locked for it's own modifications), so you can't commit any additional changes while the entity is "in-change" state.

 

I would do in that case

within the object-modified event

  • store the objectid of the modified object in a temporary global variable
  • create a new eventhandler for "AutoCAD gets quiescent"

within the eventhandler "got Quiescent"

  • do your entity modifications as in this state, AutoCAD has finished it's modifications

 

HTH, - alfred -

 

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 4 of 4
kbrown
in reply to: Alfred.NESWADBA

Thank you Alfred. 

 

To fix the issue, I added a handler for the Editor.EnteringQuiencentState event. I also added a local boolean variable 'BlockHasModification' that I set to true when the Modified event is raised. In the EnteringQuiencentState event handler, I check the BlockHasModification variable. If it is True, I first set it to false, and then perform further transactions if needed on the given block. So far AutoCAD has stopped crashing. I'm attaching the updated code for anyone that stumbles on this thread.

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