Update BlockTableRecord without Transaction

Update BlockTableRecord without Transaction

CJModis
Advocate Advocate
884 Views
6 Replies
Message 1 of 7

Update BlockTableRecord without Transaction

CJModis
Advocate
Advocate

It possible to open BlockTableRecord for selected BlockReference, then DELETE all entities and ADD new entities without any of Transaction?

0 Likes
885 Views
6 Replies
Replies (6)
Message 2 of 7

kerry_w_brown
Advisor
Advisor

 

Have you tried it ?

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUI...

 

Regards,


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 3 of 7

CJModis
Advocate
Advocate

Yes. i try it. I can delete entities from BlockTableRecord, but i can not add new entities

0 Likes
Message 4 of 7

_gile
Consultant
Consultant

Hi,

 

This works for me:

 

        [CommandMethod("TEST")]
        public void Test()
        {
            var db = Application.DocumentManager.MdiActiveDocument.Database;
            using (var btr = (BlockTableRecord)SymbolUtilityServices.GetBlockModelSpaceId(db).Open(OpenMode.ForWrite))
            {
                foreach (ObjectId id in btr)
                {
                    if ((id.ObjectClass.Name == "AcDbLine"))
                    {
                        using (var line = (Line)id.Open(OpenMode.ForWrite))
                        {
                            double radius = line.Length / 2.0;
                            var center = line.GetPointAtDist(radius);
                            line.Erase();
                            using (var circle = new Circle(center, Vector3d.ZAxis, radius))
                            {
                                btr.AppendEntity(circle);
                            }
                        }
                    }
                }
            }
        }

Anyway, I do not know why you can not or do not want to use a transaction, but instead of a regular transaction, you can use an OpenCloseTransaction which is mainly a wrapper for the Open / Close process used in the upper code.

 

        [CommandMethod("TEST2")]
        public void Test2()
        {
            var db = Application.DocumentManager.MdiActiveDocument.Database;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var btr = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                foreach (ObjectId id in btr)
                {
                    if ((id.ObjectClass.Name == "AcDbLine"))
                    {
                        var line = (Line)tr.GetObject(id, OpenMode.ForWrite);
                        double radius = line.Length / 2.0;
                        var center = line.GetPointAtDist(radius);
                        line.Erase();
                        var circle = new Circle(center, Vector3d.ZAxis, radius);
                        btr.AppendEntity(circle);
                        tr.AddNewlyCreatedDBObject(circle, true);
                    }
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 7

ActivistInvestor
Mentor
Mentor

@CJModis wrote:

Yes. i try it. I can delete entities from BlockTableRecord, but i can not add new entities


You can delete entities from a BlockTableRecord without having to open it. It's the entities that you are deleting that you must open for write.

 

To add entities to a BlockTableRecord, you have to open it for write. If the BlockTableRecord is a record for an anonymous block, there's little point to deleting its contents. You should just create another new anonymous BlockTableRecord and populate it, then change the BlockTableRecord property of the BlockReference to reference the new BlockTableRecord. When the old one is no longer referenced by an BlockReferences, it will be discarded automatically.

 

About avoiding the use of Transactions, please see this previous post.

0 Likes
Message 6 of 7

CJModis
Advocate
Advocate

Activist_Investor написано:

To add entities to a BlockTableRecord, you have to open it for write. If the BlockTableRecord is a record for an anonymous block, there's little point to deleting its contents. You should just create another new anonymous BlockTableRecord and populate it, then change the BlockTableRecord property of the BlockReference to reference the new BlockTableRecord. When the old one is no longer referenced by an BlockReferences, it will be discarded automatically.


Are there any examples for this? exactly for anonymous blocks

0 Likes
Message 7 of 7

CJModis
Advocate
Advocate

CJModis написано:

Activist_Investor написано:

To add entities to a BlockTableRecord, you have to open it for write. If the BlockTableRecord is a record for an anonymous block, there's little point to deleting its contents. You should just create another new anonymous BlockTableRecord and populate it, then change the BlockTableRecord property of the BlockReference to reference the new BlockTableRecord. When the old one is no longer referenced by an BlockReferences, it will be discarded automatically.


Are there any examples for this? exactly for anonymous blocks


Don't need)) I translated it and understand what did you mean

0 Likes