Database.AddDBObject vs. TransactionManager.AddNewlyCreatedDBObject

Database.AddDBObject vs. TransactionManager.AddNewlyCreatedDBObject

kelidesign
Enthusiast Enthusiast
848 Views
5 Replies
Message 1 of 6

Database.AddDBObject vs. TransactionManager.AddNewlyCreatedDBObject

kelidesign
Enthusiast
Enthusiast

Is there any difference in usage between Database.AddDBObject and TransactionManager.AddNewlyCreatedDBObject? I recently made a mistake (if there are two identically defined methods, one is a native method and the other is an extended method, no prompt will be given...), I am considering writing a demo to analyze and verify, and submit it to Microsoft, think about design rationality).

 

Because I had never used AddDBObject and had no impression of it, I accidentally wrote an extension method with the same name and the same parameter list, causing an error in the method I wanted to execute.

 

Share my method:

    public static void AddDBObject(this Database db, DBObject obj)
    {
        if (!db.IsValid())
            throw new InvalidDataException(nameof(db));

        if (!obj.IsValid())
            throw new InvalidDataException(nameof(obj));

        db.GetTopTransaction().AddNewlyCreatedDBObject(obj, true);
    }
0 Likes
Accepted solutions (1)
849 Views
5 Replies
Replies (5)
Message 2 of 6

ActivistInvestor
Mentor
Mentor

AddDBObject() adds an object to the Database. 

 

AddNewlyCreatedDBObject() adds an object to the transaction.

 

One has nothing to do with the other.

 

You rarely need to call AddDBObject() because adding a DBObject to an owner (e.g., BlockTableRecord.AppendEntity) also adds it to the Database.

Message 3 of 6

kelidesign
Enthusiast
Enthusiast
I still can't intuitively feel the difference and when to use it. It would be great if I could explain a specific example. Thanks.
0 Likes
Message 4 of 6

daniel_cadext
Advisor
Advisor
Accepted solution

AddDBObject appends an object to the database, but it has no owner, orphaned objects may not be saved. So, you should avoid AddDBObject unless you have a specific reason to use it.

 

BlockTableRecord.AppendEntity appends an object to the database AND applies an owner, which in this case, is the BlockTableRecord, such as modelspace or paperspace

 

AddNewlyCreatedDBObject is a transaction, this for more for memory management, undo redo

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 5 of 6

ActivistInvestor
Mentor
Mentor

I would suggest finding a better language translator.

 

 

0 Likes
Message 6 of 6

kelidesign
Enthusiast
Enthusiast
Sorry, it may have something to do with the simplicity of my expression. I will pay attention to it, thank you.
0 Likes