Edit Insertion Point of Existing Block

Edit Insertion Point of Existing Block

mcoH3VZV
Advocate Advocate
1,239 Views
3 Replies
Message 1 of 4

Edit Insertion Point of Existing Block

mcoH3VZV
Advocate
Advocate

In the attached drawing file, I have two blocks. One is a blue rectangle that has its insertion point at 0,0. The other block has its insertion point at a lesser X and Y coordinate below 0,0. 

 

Is there any way to edit the block in place so that the insertion point is right at 0,0? Would it be best to explode and re-block, or is there a way to do this easily?

 

I am using C# .NET

 

Thanks,

0 Likes
Accepted solutions (1)
1,240 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant

Hi,

you have to compute the vector from (0, 0) to the block position, the open the block definition (BlockTableRecord) and displace all the entities it contains using this vector.

If you also want the block reference to stay at the same place, you have to dispace it with the negated vector.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

Here's a little example.

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using AcCoreAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace MoveInsertionPointSample
{
    public class Commands
    {
        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = AcCoreAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var peo = new PromptEntityOptions("\nSelect block reference: ");
            peo.SetRejectMessage("\nSelected object is not a block reference.");
            peo.AddAllowedClass(typeof(BlockReference), true);
            var per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK)
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var br = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
                var dp = br.Position.GetAsVector();
                var xform = Matrix3d.Displacement(dp);
                var btr = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
                foreach (ObjectId id in btr)
                {
                    var ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                    ent.TransformBy(xform);
                }
                br.TransformBy(Matrix3d.Displacement(dp.Negate()));
                tr.Commit();
            }
        }
    }
}

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4

mcoH3VZV
Advocate
Advocate

Excellent as always, Thank you.

0 Likes