Code Red > .NET

Rules on transactions etc.

(1/2) > >>

AlexFielder:
Hi all,

I've been seeing quite a good bit of success with the addin I mentioned starting to write back in August, but just lately I have a bunch of examples where I will place a blockreference in a drawing, and it just disappears moments later.

In and around the placement, a bunch of things happen- namely placing a dimension or two and depending upon the situation it may or may not draw a line or some piece of text (mainly for my own debugging purposes) and I wondered if someone has some general rules on transactions and use of the AutoCAD database in general.

Years ago on here I think it was Daniel or someone else who said something like "if you 'new' it, dispose of it or commit" or words to that effect.

One method I have which occasionally produces "invisible" blocks is this:


--- Code: ---public void InsertBlockReference(MasonryBracketSystemViewModel systemDefinition, Matrix3d transformMatrix, BlockReference blkRef)
        {
            Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                if (blkRef != null)
                {
                    blkRef.TransformBy(transformMatrix);

                    blkRef.SetDatabaseDefaults();
                    modelSpace.AppendEntity(blkRef);
                    tr.AddNewlyCreatedDBObject(blkRef, true);
                    var attrInfos = new Dictionary<string, AttrInfo>();

                    SetBlockRefAttributeValuesBasedOnParameters(systemDefinition, false, tr, blkRef, out attrInfos);

                    SetBlockRefDynPropsBasedOnParameters(systemDefinition, 0, blkRef);
                    blkRef.RecordGraphicsModified(true);

                    AddXRecordToBlockReference(blkRef, "systemName", systemDefinition.AssignedSystemName.SystemName);
                    AddXRecordToBlockReference(blkRef, "instanceNumber", systemDefinition.AssignedSystemName.InstanceNumber);
                    AddXRecordToBlockReference(blkRef, "level", systemDefinition.AssignedSystemName.Level);

                    SetXRecordsUsingViewModel(systemDefinition, blkRef);

                    modelSpace.UpdateAnonymousBlocks();
                    ed.Regen();
                    ed.UpdateScreen();
                    db.Regenmode = true;
                    db.UpdateExt(true);
                    tm.QueueForGraphicsFlush();
                    tr.Commit();
                }
                else
                {
                    throw new System.Exception("The block reference was null");
                }
            }
        }

--- End code ---

(db in this case is external to that method but I wonder whether it should in fact be local?):


--- Code: ---public static Database db; // = HostApplicationServices.WorkingDatabase;
        public static Document doc; // = Application.DocumentManager.MdiActiveDocument;
        public static Editor ed;

        public BlockHelpers()
        {
            uXSettings = new UXSettings();
            doc = CadApp.DocumentManager.MdiActiveDocument;
            db = doc.Database;
            ed = doc.Editor;
        }
--- End code ---

57gmc:
How about this thread?Fenton Webb's advice re "To dispose or not to dispose"

Bryco:
If you are adding the block to the blocktable from another dwg then the db is best to be local

kdub_nz:
Alex, I believe the Commit is in the incorrect place

Make it the last statement in the Using code block.
That may not be the cause of the irregularity but it will be conventional.

Some light reading
https://help.autodesk.com/view/OARX/2024/ENU/?guid=GUID-24B217B9-EC4D-4BE9-AC05-89265B3BE6B6

Jeff H:

--- Quote from: AlexFielder on February 06, 2024, 11:10:46 AM ---
(db in this case is external to that method but I wonder whether it should in fact be local?):



--- End quote ---
I usually make a DB property of a class with a CommandClass attribute since one is created for each document it used and lends itself to pointing to correct database when used.

Navigation

[0] Message Index

[#] Next page

Go to full version