Author Topic: A Hook for dbconnect labels  (Read 852 times)

0 Members and 1 Guest are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1839
A Hook for dbconnect labels
« on: April 25, 2018, 04:50:27 PM »
Hi, I hope someone knows the hook to find when someone has added a freestanding label or attached label fro the dbconnect command. It doesn't show uo in begin command so I am not quite sure where to start

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: A Hook for dbconnect labels
« Reply #1 on: April 25, 2018, 05:28:45 PM »
I fired up autocad and turned on a bunch of events in MgdDbg and went through adding a label using dbconnect
Notice the new dictionary creation for 1CE and 1CF, these are in the NOD and called CONLDEFDictionary (1CE) which contains CablesLink1 (1CF) which has some xdata which holds the query
then later just before asking me for label location it creates dictionary 1D0 and 1D1, again these are in the NOD and called CONLABELDictionary (1D0) which contains CablesLabel1

The short story: doesn't seem to be a direct database event for catching this command.

I think if you implement an overrule on type ImpDBObject that you may be able to catch when these are used
Might be helpful to start out in your dictionary, and then knowing what dictionaries contain the label def you could catch the creation of the new MTEXT, which has xdata pointing to the label definition.

Code: [Select]
Customization file loaded successfully. Customization Group: ACAD
Customization file loaded successfully. Customization Group: CUSTOM
Customization file loaded successfully. Customization Group: MODELDOC
Customization file loaded successfully. Customization Group: CONTENTEXPLORER
Customization file loaded successfully. Customization Group: SKETCHUPIMPORTSKP
Customization file loaded successfully. Customization Group: EXPRESS
Regenerating model.

Loading MgdDbg...
AutoCAD menu utilities loaded.
Command: _RIBBON

Command: COMMANDLINE

Command: properties

Command:
Command:
Command: _MgdDbgEvents

Application Events Turned On ...

Database Events Turned On ...

Database Object Events Turned On ...

Dynamic Linker Events Turned On ...

Command: dbc
[App Event] : System Var Changing       : DCTMAINdbc
[App Event] : System Var Changed        : DCTMAINdbc
[App Event] : System Var Changing       : DCTMAINdbc
[App Event] : System Var Changed        : DCTMAINdbc
[App Event] : System Var Changing       : DCTCUSTdbc
[App Event] : System Var Changed        : DCTCUSTdbc
[App Event] : System Var Changing       : DCTCUSTdbc
[App Event] : System Var Changed        : DCTCUSTDBCONNECT

[Dynamic Linker Event] : Module Loading            : c:\program files\autodesk\autocad 2014\condlg.arx
[Dynamic Linker Event] : Module Loading            : ase.arx
[Dynamic Linker Event] : Module Loading            : acmted2.arx
[Dynamic Linker Event] : Module Loading            : acmted2.crx
[Dynamic Linker Event] : Module Loaded             : ase.arx
[Dynamic Linker Event] : Module Loaded             : acmted2.crx
[Dynamic Linker Event] : Module Loaded             : acmted2.arx
[Dynamic Linker Event] : Module Loaded             : c:\program files\autodesk\autocad 2014\condlg.arx
Command:
Command:
[DB Event : 728361680] : Database ConstructedDBC
DBCONNECT
Command:
[DB Event : 694855296] : Object Opened For Modify  : <RegAppTable, 9>
[DB Event : 694855296] : Object Opened For Modify  : <RegAppTableRecord, 1CD>
[DB Event : 694855296] : Object Appended           : <RegAppTableRecord, 1CD>
[DB Event : 694855296] : Object Modified           : <RegAppTableRecord, 1CD>
[DB Event : 694855296] : Object Opened For Modify  : <DBDictionary, C>
[DB Event : 694855296] : Object Opened For Modify  : <DBDictionary, 1CE>
[DB Event : 694855296] : Object Appended           : <DBDictionary, 1CE>
[DB Event : 694855296] : Object Modified           : <DBDictionary, 1CE>
[DB Event : 694855296] : Object Opened For Modify  : <DBDictionary, 1CE>
[DB Event : 694855296] : Object Opened For Modify  : <ImpDBObject, 1CF>
[DB Event : 694855296] : Object Appended           : <ImpDBObject, 1CF>
[DB Event : 694855296] : Object Modified           : <ImpDBObject, 1CF>
[DB Event : 694855296] : Object Modified           : <DBDictionary, 1CE>
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUPREC
[App Event] : System Var Changing       : AUPREC
[DB Event : 694855296] : System Var Changed        : AUPREC
[App Event] : System Var Changed        : AUPREC
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : System Var Will Change    : AUNITS
[App Event] : System Var Changing       : AUNITS
[DB Event : 694855296] : System Var Changed        : AUNITS
[App Event] : System Var Changed        : AUNITS
[DB Event : 694855296] : Object Opened For Modify  : <DBDictionary, C>
[DB Event : 694855296] : Object Opened For Modify  : <DBDictionary, 1D0>
[DB Event : 694855296] : Object Appended           : <DBDictionary, 1D0>
[DB Event : 694855296] : Object Modified           : <DBDictionary, 1D0>
[DB Event : 694855296] : Object Opened For Modify  : <DBDictionary, 1D0>
[DB Event : 694855296] : Object Opened For Modify  : <ImpDBObject, 1D1>
[DB Event : 694855296] : Object Appended           : <ImpDBObject, 1D1>
[DB Event : 694855296] : Object Modified           : <ImpDBObject, 1D1>
[DB Event : 694855296] : Object Modified           : <DBDictionary, 1D0>Specify point for label:
[App Event] : System Var Changing       : VIEWBACKSTATUS
[App Event] : System Var Changed        : VIEWBACKSTATUS
[DB Event : 694855296] : Object Opened For Modify  : <BlockTableRecord, 1F>
[DB Event : 694855296] : Object Opened For Modify  : <MText, 1D2>
[DB Event : 694855296] : Object Appended           : <MText, 1D2>
[DB Event : 694855296] : Object Modified           : <MText, 1D2>
[DB Event : 694855296] : Object Modified           : <BlockTableRecord, 1F>
[DB Event : 694855296] : Object Modified           : <MText, 1D2>
[DB Event : 694855296] : Object Opened For Modify  : <MText, 1D2>
[DB Event : 694855296] : Object Modified           : <MText, 1D2>1 Record(s) linked with 1 Label(s)

Command:
[DB Event : 694855296] : Object Opened For Modify  : <ImpSymbolTable, 1C9>
[DB Event : 694855296] : Object Appended           : <ImpSymbolTableRecord, 1D3>
[DB Event : 694855296] : Object Modified           : <ImpSymbolTableRecord, 1D3>
[DB Event : 694855296] : Object Opened For Modify  : <ImpSymbolTableRecord, 1D3>
[DB Event : 694855296] : Object Modified           : <ImpSymbolTableRecord, 1D3>

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: A Hook for dbconnect labels
« Reply #2 on: April 25, 2018, 05:37:39 PM »
Also if it helps, everything is referenced by the registered app id DCO15 (in my case, I suspect this is coded to the version of dbconnect). This includes the ImpDBObjects in the dictionaries and any mtext labels

Bryco

  • Water Moccasin
  • Posts: 1839
Re: A Hook for dbconnect labels
« Reply #3 on: April 25, 2018, 05:44:54 PM »
Thanks for the reply Will,
I just found   https://forums.autodesk.com/t5/net/connect-to-sql-database-from-c-plugin/td-p/7295765
where Norman mentioned
Code: [Select]
But if you indeed want to connect to AutoCAD built-in DBConnect, then you need to add refernce to CAO 10. I hope that has something

Bryco

  • Water Moccasin
  • Posts: 1839
Re: A Hook for dbconnect labels
« Reply #4 on: April 26, 2018, 08:16:16 PM »
static void db_ObjectAppended(object sender, ObjectEventArgs e)

this lets me find the mtext but it is in a read state at that point  (probably while it adds xdata) so I cant open it for write.
Also I get "Object reference not set to an instance of an object."  on using (Transaction tr = db.TransactionManager.StartTransaction())
If I use  using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())        error is       eHadMultipleReaders

pretty confusing

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: A Hook for dbconnect labels
« Reply #5 on: April 28, 2018, 12:02:59 PM »
Consider using an overrule pattern on the closed method of the object to make the changes after autocad hands off but still while it's open for write.

You can use whatever logic in any other method (like ObjectAppended) to flag the items you want to control.

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: A Hook for dbconnect labels
« Reply #6 on: May 01, 2018, 04:08:24 PM »
I was super curious about this and took a moment to experiment. I found the Closed overrule to be quite ineffective. It never fires with a condition of IsNewObject with the xdata filter applied.
Code - C#: [Select]
  1. [CommandMethod("watchlabel")]
  2.        public void watchLabels()
  3.        {
  4.            var orule = new MTextWatcher();
  5.            orule.SetXDataFilter("DCO15");
  6.            Overrule.AddOverrule(RXClass.GetClass(typeof(MText)), orule, true);
  7.            Overrule.Overruling = true;
  8.        }
  9.    public class MTextWatcher : ObjectOverrule
  10.    {
  11.        public MTextWatcher()
  12.        { ed = Application.DocumentManager.MdiActiveDocument.Editor; }
  13.        private Editor ed;
  14.        public override void Close(DBObject dbObject)
  15.        {
  16.            if (dbObject.IsNewObject)
  17.            {
  18.                ed.WriteMessage("\nGot new object");
  19.            }
  20.            else if (dbObject.IsModified)
  21.            {
  22.                ed.WriteMessage("\nGot modified object");
  23.            }
  24.            base.Close(dbObject);
  25.        }
  26.    }

I instead looked at this under the view of events and created a simple Mealy machine to capture the case of a new label. This is a decent start but needs a bit more refinement on the contents of the xdata, as the event is captured twice in this configuration.

Code - C#: [Select]
  1.        [CommandMethod("CatchDbConnect")]
  2.        public void catchDbConnect()
  3.        {
  4.            Application.DocumentManager.MdiActiveDocument.Database.ObjectOpenedForModify += Database_ObjectOpenedForModify;
  5.        }
  6.        RXClass blocktablerecord = RXClass.GetClass(typeof(BlockTableRecord));
  7.        RXClass mtext = RXClass.GetClass(typeof(MText));
  8.        ObjectId blocktablerecordId = ObjectId.Null;
  9.        private void Database_ObjectOpenedForModify(object sender, ObjectEventArgs e)
  10.        {
  11.            var dbObject = e.DBObject;
  12.            var rxClass = dbObject.GetRXClass();
  13.            if ( rxClass == blocktablerecord)
  14.            {
  15.                blocktablerecordId = e.DBObject.ObjectId;
  16.                e.DBObject.ObjectClosed += DBObject_BlockTableRecordClosed;
  17.            }
  18.            else if (rxClass == mtext && dbObject.OwnerId == blocktablerecordId)
  19.            {
  20.                dbObject.ModifiedXData += DbObject_ModifiedMtextXdata;
  21.            }
  22.        }
  23.  
  24.        private void DbObject_ModifiedMtextXdata(object sender, EventArgs e)
  25.        {
  26.            ((DBObject)sender).ObjectClosed -= DbObject_ModifiedMtextXdata;
  27.            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nModified Mtext xdata, new label?!?");
  28.  
  29.        }

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: A Hook for dbconnect labels
« Reply #7 on: May 07, 2018, 02:26:40 PM »
Curiosity ensued.. I enjoy banging around low level logic once in awhile  :idiot2:

Here's a class that seems to do what you're after:
Code - C#: [Select]
  1.    public class DBConnectLabelEvents
  2.    {
  3.        public DBConnectLabelEvents(Database database, string dbcAppname)
  4.        {
  5.            database.ObjectOpenedForModify += Database_ObjectOpenedForModify;
  6.            database.ObjectAppended += DbConnectEvents_ObjectAppended;
  7.            DBCAppname = dbcAppname;
  8.        }
  9.        public event EventHandler<DBConnectLabelEventArgs> LabelAdded;
  10.        protected virtual void OnLabelAdded(DBConnectLabelEventArgs e)
  11.        {
  12.            if (LabelAdded != null)
  13.                LabelAdded(this, e);
  14.        }
  15.        private string DBCAppname;
  16.        private static RXClass BlockTableRecordRXClass = RXClass.GetClass(typeof(BlockTableRecord));
  17.        private static RXClass MTextRXClass = RXClass.GetClass(typeof(MText));
  18.        private void Database_ObjectOpenedForModify(object sender, ObjectEventArgs e)
  19.        {
  20.            var dbObject = e.DBObject;
  21.            if (dbObject.GetRXClass() == BlockTableRecordRXClass)
  22.            {
  23.                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nBlock Table Record Opened");
  24.                dbObject.ObjectClosed += DBObject_BlockTableRecordClosed;
  25.            }
  26.        }
  27.        private void DbConnectEvents_ObjectAppended(object sender, ObjectEventArgs e)
  28.        {
  29.            var dbObject = e.DBObject;
  30.            if (dbObject.GetRXClass() == MTextRXClass)
  31.            {
  32.                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nAppended MText");
  33.                dbObject.ModifiedXData += DbObject_ModifiedMtextXdata;
  34.            }
  35.        }
  36.        private void DbObject_ModifiedMtextXdata(object sender, EventArgs e)
  37.        {
  38.            var dbObject = (DBObject)sender;
  39.            dbObject.ModifiedXData -= DbObject_ModifiedMtextXdata;
  40.            var xdata = dbObject.GetXDataForApplication(DBCAppname);
  41.            if (xdata == null) return;
  42.            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nFiring Label Added");
  43.            OnLabelAdded(new DBConnectLabelEventArgs(dbObject));
  44.        }
  45.        private void DBObject_BlockTableRecordClosed(object sender, ObjectClosedEventArgs e)
  46.        {
  47.            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nBlock Table Record Closed");
  48.            var BlockTableRecord = (DBObject)sender;
  49.            BlockTableRecord.ObjectClosed -= DBObject_BlockTableRecordClosed;
  50.        }
  51.    }
it requires a modified event args class
Code - C#: [Select]
  1.    public class DBConnectLabelEventArgs :EventArgs
  2.    {
  3.        public DBConnectLabelEventArgs(DBObject dBObject)
  4.        {
  5.            DBObject = dBObject;
  6.        }
  7.        public DBObject DBObject { get; internal set; }
  8.    }
And can be used like:
Code - C#: [Select]
  1.        [CommandMethod("CatchDbConnect")]
  2.        public void catchDbConnect()
  3.        {
  4.            var catcher = new DBConnectLabelEvents(Application.DocumentManager.MdiActiveDocument.Database, "DCO15");
  5.            catcher.LabelAdded += Catcher_LabelAdded;
  6.        }
  7.  
  8.        private void Catcher_LabelAdded(object sender, DBConnectLabelEventArgs e)
  9.        {
  10.            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  11.            ed.WriteMessage("\nInserted label: {0}", e.DBObject.Handle);
  12.        }

Bryco

  • Water Moccasin
  • Posts: 1839
Re: A Hook for dbconnect labels
« Reply #8 on: May 18, 2018, 06:47:51 PM »
My apologies for not replying Will, this looks really good. I hope to be able to play with it next week.
It seems like you can do more in 10 mins than I can do in a week,  very impressive.  2 thumbs up

Bryco

  • Water Moccasin
  • Posts: 1839
Re: A Hook for dbconnect labels
« Reply #9 on: May 23, 2018, 05:33:42 PM »
It works thanks Will but I keep getting
this error, the condlg.arx is loaded in cad. But I cannot find the pbd

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: A Hook for dbconnect labels
« Reply #10 on: May 24, 2018, 04:21:10 PM »
I doubt that you'll find a pdb included with any of the core .arx extensions...  I'm guessing this is an issue in debugging the code, I loathe debugging within acad...

Bryco

  • Water Moccasin
  • Posts: 1839
Re: A Hook for dbconnect labels
« Reply #11 on: May 24, 2018, 06:49:48 PM »
I may be doomed anyway as the mtext is openfornotify so its hard to change its layer and size.
Even opening up the database is a nightmare

WILL HATCH

  • Bull Frog
  • Posts: 448
Re: A Hook for dbconnect labels
« Reply #12 on: May 25, 2018, 03:58:33 PM »
Have a go at replacing the Catcher_LabelAdded function I provided before with this sort of pattern for when it's open for notify:
Code - C#: [Select]
  1.        private void Catcher_LabelAdded(object sender, DBConnectLabelEventArgs e)
  2.        {
  3.            bool doWeModify = true;
  4.            if (e.DBObject.IsNotifyEnabled)
  5.            {
  6.                Application.ShowAlertDialog("is notify enabled!");
  7.                e.DBObject.UpgradeFromNotify();
  8.            }
  9.            try
  10.            {
  11.                e.DBObject.Erase();
  12.                var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  13.                ed.WriteMessage("\nInserted label: {0}", e.DBObject.Handle);
  14.            }
  15.            finally
  16.            {
  17.                if (e.DBObject.IsNotifyEnabled)
  18.                    e.DBObject.DowngradeToNotify(doWeModify);
  19.            }
  20.        }

I tested it with this bit of code and it seems to work fine:
Code - C#: [Select]
  1.        [CommandMethod("testopennotify")]
  2.        public void testopenfornotify()
  3.        {
  4.            var doc = Application.DocumentManager.MdiActiveDocument;
  5.            var ed = doc.Editor;
  6.            var db = doc.Database;
  7.            var pr = ed.GetEntity("\nSelect Entity");
  8.            if (pr.Status != PromptStatus.OK) return;
  9.            using (var tr = db.TransactionManager.StartTransaction())
  10.            {
  11.                var obj = tr.GetObject(pr.ObjectId, OpenMode.ForNotify);
  12.                Catcher_LabelAdded(obj, new DBConnectLabelEventArgs(obj));
  13.                tr.Commit();
  14.            }
  15.        }
or if you're really desperate, consider implementing a close overrule that holds a dictionary of ObjectId and the changes you wish to make, then when an object is closing, check if the Id is in the dictionary.
You could even get the dictionary to store some actions, then just call the action and delete the dictionary entry before closing. In case lambdas are a little over your head, here's an example where I've got a list of actions, I add an action to the list then execute and remove it.
Code - C#: [Select]
  1.        private void Catcher_LabelAdded(object sender, DBConnectLabelEventArgs e)
  2.        {
  3.            bool doWeModify = true;
  4.            if (e.DBObject.IsNotifyEnabled)
  5.            {
  6.                Application.ShowAlertDialog("is notify enabled!");
  7.                e.DBObject.UpgradeFromNotify();
  8.            }
  9.            try
  10.            {
  11.                actions.Add(() => {
  12.                    e.DBObject.Erase();
  13.                  var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  14.                    ed.WriteMessage("\nInserted label: {0}", e.DBObject.Handle);
  15.                });
  16.                for (int i = actions.Count - 1; i >= 0; i--)
  17.                {
  18.                    actions[i].Invoke();
  19.                    actions.RemoveAt(i);
  20.                }
  21.            }
  22.            finally
  23.            {
  24.                if (e.DBObject.IsNotifyEnabled)
  25.                    e.DBObject.DowngradeToNotify(doWeModify);
  26.            }
  27.        }

Bryco

  • Water Moccasin
  • Posts: 1839
Re: A Hook for dbconnect labels
« Reply #13 on: May 29, 2018, 09:01:57 PM »
testopennotify works fine on a selected labe but whe using CatchDbConnect
the mtext is  not .IsNotifyEnabled but is .IsNotifying and wont allow me to downgrade or upgrade.
So perhaps I will have to let the drafter manually select the mtext later.