Message 1 of 15
ObjectModified Event Handler
Not applicable
04-22-2009
09:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am writing a C# program to monitor the blocks that are inserted on a specific layer (PIDSYM) and that have an attribute Tag attached called "DEVICE_NAME". I need to know when a given blocks attribute values have been modified. I have added a couple of Event Handlers to DocumentManager_DocumentActivated. The ObjectAppended event works fine, but when I put the ObjectModified event handler in the code below in it goes into an infinite loop and displays the MessageBox statement over and over. I have to kill AutoCAD to get out of the loop.
Here is the code (The attachment contains the code as well in case the {code} tags don't take):
{code}
private void DocumentManager_DocumentActivated(object sender, Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs e)
{
//Get a reference to the current document's database
Database activeDatabase = AcadApp.DocumentManager.MdiActiveDocument.Database;
//Add the event handler
activeDatabase.ObjectModified += new ObjectEventHandler(OnObjectModified);
}
private void OnObjectModified(object sender, ObjectEventArgs e)
{
//If the object is already open for write then some other
//process has it so quit until it is done
if (e.DBObject.IsWriteEnabled) return;
//Is the object modified a block
if (e.DBObject is BlockReference)
{
//Set up a reference to the current drawing's database
Database db = AcadApp.DocumentManager.MdiActiveDocument.Database;
//Set up a transaction envelope and START it
using (Transaction tm = db.TransactionManager.StartTransaction())
{
try
{
//Create a reference to the block object passed in
BlockReference blkRef = e.DBObject as BlockReference;
//Open it for reading
blkRef.UpgradeOpen();
//Is it on the PIDSYM layer, if not ignore it
if (blkRef.Layer.Equals("PIDSYM"))
{
//Get the attributes off of the block reference
Autodesk.AutoCAD.DatabaseServices.AttributeCollection blockAttrs = blkRef.AttributeCollection;
//Only want the blocks that have DEVICE_NAME as the first tag
//Set up a reference to the first objectID in the list and open it for reading
AttributeReference attFirst = (AttributeReference)tm.GetObject(blockAttrs[0], OpenMode.ForRead);
//Check the attribute reference tag
if (attFirst.Tag.Equals("DEVICE_NAME"))
{
//Need a var to put the block data into
String attributeData = "";
//Cycle through the attribute objectIDs
foreach (ObjectId attrID in blockAttrs)
{
//Create a reference to the attribute and open it for reading
AttributeReference attRefers = (AttributeReference)tm.GetObject(attrID, OpenMode.ForRead);
//Append the data fields together
attributeData += attRefers.TextString + "|";
} //End foreach attrID
MessageBox.Show("Modified Block: " + attributeData);
} //End If attFirst
} //End If blkRef
}
catch (System.Exception ex)
{
MessageBox.Show("Error in OnObjectModified:: " + ex.ToString());
}
} //End Using Transaction tm
} //End If e.DBObject
} //End Method OnObjectModified
{code}
Here is the code (The attachment contains the code as well in case the {code} tags don't take):
{code}
private void DocumentManager_DocumentActivated(object sender, Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs e)
{
//Get a reference to the current document's database
Database activeDatabase = AcadApp.DocumentManager.MdiActiveDocument.Database;
//Add the event handler
activeDatabase.ObjectModified += new ObjectEventHandler(OnObjectModified);
}
private void OnObjectModified(object sender, ObjectEventArgs e)
{
//If the object is already open for write then some other
//process has it so quit until it is done
if (e.DBObject.IsWriteEnabled) return;
//Is the object modified a block
if (e.DBObject is BlockReference)
{
//Set up a reference to the current drawing's database
Database db = AcadApp.DocumentManager.MdiActiveDocument.Database;
//Set up a transaction envelope and START it
using (Transaction tm = db.TransactionManager.StartTransaction())
{
try
{
//Create a reference to the block object passed in
BlockReference blkRef = e.DBObject as BlockReference;
//Open it for reading
blkRef.UpgradeOpen();
//Is it on the PIDSYM layer, if not ignore it
if (blkRef.Layer.Equals("PIDSYM"))
{
//Get the attributes off of the block reference
Autodesk.AutoCAD.DatabaseServices.AttributeCollection blockAttrs = blkRef.AttributeCollection;
//Only want the blocks that have DEVICE_NAME as the first tag
//Set up a reference to the first objectID in the list and open it for reading
AttributeReference attFirst = (AttributeReference)tm.GetObject(blockAttrs[0], OpenMode.ForRead);
//Check the attribute reference tag
if (attFirst.Tag.Equals("DEVICE_NAME"))
{
//Need a var to put the block data into
String attributeData = "";
//Cycle through the attribute objectIDs
foreach (ObjectId attrID in blockAttrs)
{
//Create a reference to the attribute and open it for reading
AttributeReference attRefers = (AttributeReference)tm.GetObject(attrID, OpenMode.ForRead);
//Append the data fields together
attributeData += attRefers.TextString + "|";
} //End foreach attrID
MessageBox.Show("Modified Block: " + attributeData);
} //End If attFirst
} //End If blkRef
}
catch (System.Exception ex)
{
MessageBox.Show("Error in OnObjectModified:: " + ex.ToString());
}
} //End Using Transaction tm
} //End If e.DBObject
} //End Method OnObjectModified
{code}