.NET

Reply
Contributor
kbrown
Posts: 18
Registered: ‎10-23-2012
Message 1 of 4 (395 Views)
Accepted Solution

Modified Event of BlockReference Causes Crash

395 Views, 3 Replies
12-11-2012 12:11 PM

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.

 

 

Contributor
kbrown
Posts: 18
Registered: ‎10-23-2012
Message 2 of 4 (391 Views)

Re: Modified Event of BlockReference Causes Crash

12-11-2012 12:29 PM 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. 

*Expert Elite*
Alfred.NESWADBA
Posts: 8,867
Registered: ‎06-29-2007
Message 3 of 4 (389 Views)

Re: Modified Event of BlockReference Causes Crash

12-11-2012 12:31 PM 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
-------------------------------------------------------------------------
Contributor
kbrown
Posts: 18
Registered: ‎10-23-2012
Message 4 of 4 (372 Views)

Re: Modified Event of BlockReference Causes Crash

12-11-2012 01:20 PM 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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community