entity position deviates from the specified position while copy BlockReference by blockId

entity position deviates from the specified position while copy BlockReference by blockId

lixiaohai1222
Explorer Explorer
793 Views
7 Replies
Message 1 of 8

entity position deviates from the specified position while copy BlockReference by blockId

lixiaohai1222
Explorer
Explorer
        static void CopyBlocks()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            var dynBlockNames = new List<string> { "block1", "block2", "block3" };

            for (int i = 0; i < dynBlockNames.Count; i++)
            {
                var basePoint = new Point3d(i, i, i);
                ObjectId blockId;
                BlockReference newBr;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                    newBr = new BlockReference(basePoint, bt["blockName"]);

                    BlockTableRecord modelSpace = trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;
                    modelSpace.AppendEntity(newBr);
                    blockId = newBr.ObjectId;
                    trans.Commit();
                }

                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    for (int j = 100; j < 1000; i++)
                    {
                        var tp = new Point3d(j, j, j);
                        ed.Command("_.COPY", blockId, "", basePoint, tp);
                    }

                    trans.Commit();
                }
            }
        }

something was wrong with the specified position "tp"

 

 

 

 

 

 

 

 

 

 

 

0 Likes
794 Views
7 Replies
Replies (7)
Message 2 of 8

ActivistInvestor
Mentor
Mentor

 

something was wrong with the specified position "tp"

What was wrong with the specified position?  You need to be more specific. What was the result verses what was expected result.

 

Running Object Snap perhaps ?

 

 

 

 

0 Likes
Message 3 of 8

lixiaohai1222
Explorer
Explorer

1

0 Likes
Message 4 of 8

lixiaohai1222
Explorer
Explorer

lixiaohai1222_0-1724743438469.jpeglixiaohai1222_1-1724743447488.jpeg

all the blocks specified by red arrow are also copied to wrong point

 

0 Likes
Message 5 of 8

_gile
Consultant
Consultant

Hi,

There's something weird with this line:

for (int j = 100; j < 1000; i++)

 You're incrementing 'i' and 'j' is never incremented.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 8

_gile
Consultant
Consultant

Also, as @ActivistInvestor said, maybe something due to some running object snap. If you absolutely want to use Editor.Command, you should temporarily disable object snaps:

ed.Command("_.COPY", blockId, "", "_none", basePoint, tp);

But, instead of calling the COPY command, you could simply insert the new block references as you do for the "base" ones.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 8

lixiaohai1222
Explorer
Explorer

thanks, but dynamic properties will be lost while simply insert the new block references. In addition, when using DotNetARX.BlockTools.SetDynBlockValue to modify the attribute value of the new block references, AutoCAD will leak memory.
issue: https://forums.autodesk.com/t5/net/net-plugin-causes-high-memory-consumption-leak-not-in-managed/td-...

0 Likes
Message 8 of 8

norman.yuan
Mentor
Mentor

Well, using Editor.Command()/using command "COPY" does not change the fact that excessive use of dynamic blocks would result in high memory consumption. If you insert the block references with its default dynamic properties with code, there would be no extra memory consumption. The increased memory is resulted in when the block reference's dynamic properties are changed (each change would lead AutoCAD to create a new BlockTableRecord and its block reference). If you have a very complicated block definition (say many entities in it and these entities could be manipulated with dozens of dynamic properties), the BlockTableRecord could be quite big in size of memory. So, each instance of the block reference with a unique dynamic property value would lead AutoCAD to create new pair of an anonymous BlockTableRecord (with similar big amount of memory required) and a block reference. You can see, if you create 100 block references of this block with each of them has unique dynamic property value, you would end up with 100 big sized anonymous BlockTablerecords plus 100 instances of BlockReferences (paired to each anonymous block definition). Therefore, using big-sized/complicated dynamic blocks excessively would cause heavy overhead for AutoCAD process.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes