ObjectOpenedForModify event

ObjectOpenedForModify event

Anonymous
Not applicable
827 Views
3 Replies
Message 1 of 4

ObjectOpenedForModify event

Anonymous
Not applicable

Hi,

 

I am using AutoCAD (Map) 2010.

 

Previously I was using the .Net Framework 3.0 and the ObjectOpenedForModify was called upon modifying  entities.

After changing to .Net Framework 3.5 the ObjectOpenedForModify is not called anymore.

This is not entirely true, it is called once, only first time.

 

Also for ImpliedSelectionChanged, the event is not fired for 3.5

 

Anyone has the same problems?

Is there a solution (besides using 3.0)?

 

Thanks,

Gertwin

 

n.b. I want to use Linq, that is why I changed to 3.5

 

 

0 Likes
828 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

I do not have access to Acad2010, but I tried with Acad2009 and Acad2011 (both are AcadMap) with .NET3.5 (VS2008). The event fires as expected. This is the code (I added some comments regarding the event with relevant lines of code):

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(ObjectOpenedEvent.MyCommand))]

namespace ObjectOpenedEvent
{
    public class MyCommand 
    {
        [CommandMethod("MyCommand")]
        public static void RunThisMethod()
        {
            Document dwg = Autodesk.AutoCAD.ApplicationServices.
                Application.DocumentManager.MdiActiveDocument;

            Editor ed = dwg.Editor;

            dwg.Database.ObjectOpenedForModify+=
                new ObjectEventHandler(Database_ObjectOpenedForModify);

            bool go = true;
            while (go)
            {
                PromptEntityOptions opt = 
                    new PromptEntityOptions("\nPick an entity:");
                PromptEntityResult res = ed.GetEntity(opt);

                if (res.Status == PromptStatus.OK)
                {
                    ObjectId entId = res.ObjectId;

                    ModifyObject(dwg.Database, entId);
                }
                else
                {
                    go = false;
                }
            }

            dwg.Database.ObjectOpenedForModify -= Database_ObjectOpenedForModify;

            ed.WriteMessage("\nMyCommand executed.");
        }

        private static void Database_ObjectOpenedForModify(object sender, ObjectEventArgs e)
        {
            Document dwg = Autodesk.AutoCAD.ApplicationServices.
                Application.DocumentManager.MdiActiveDocument;

            dwg.Editor.WriteMessage(
                "\nObjectOpenedForModify event being handled on this object: {0}", 
                e.DBObject.ObjectId.ToString());
        }

        private static void ModifyObject(Database db, ObjectId entId)
        {
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                Entity ent = (Entity)tran.GetObject(entId, OpenMode.ForRead);

                //This line does not trigger the envent
                ent.UpgradeOpen();

                //The event only triggered when the first line of code
                //that modified the entity is to be executed.
                //That is, event fires first that the ColorIndex is changed
                ent.ColorIndex = 1;

                //The event only fires once per entity being opened.
                //Thus this line does not trigger the event
                ent.Layer = "Layer1";

                ent.DowngradeOpen();

                tran.Commit();
            }
        }
    }
}

 

Here is the Acad's text window, after picking 3 entities (the code prints text inside the event handler, so we know the event fires):

 

Command: mycommand
Pick an entity:
ObjectOpenedForModify event being handled on this object: (2129323136)
Pick an entity:
ObjectOpenedForModify event being handled on this object: (2129323144)
Pick an entity:
ObjectOpenedForModify event being handled on this object: (2129323152)
Pick an entity: *Cancel*
MyCommand executed.

 

I did not try Document.ImpliedSelectionChanged event, though.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

Anonymous
Not applicable

Hi Norman,

 

Thanks for your reply.

Your code works as expected, so I have to review my own code.

 

Regards,

Gertwin

0 Likes
Message 4 of 4

Anonymous
Not applicable

FYI,

 

An exception was  thrown from within the event handler.

This causes the event not to fire anymore.

 

This line was the cause of the exception:

 

foreach (KeyValuePair<string, AttributeClass> key_value_pair in attributes.OrderBy(key => key.Value))

 

I needed to implement the IComparable interface in AttributeClass.

After that it works as expected.

 

Gertwin

0 Likes