How to Change Block Base Point

How to Change Block Base Point

dungthachbomay
Contributor Contributor
481 Views
4 Replies
Message 1 of 5

How to Change Block Base Point

dungthachbomay
Contributor
Contributor

Hi everybody !
I have a problem as follows:
- First, when the user types the command, the user will be asked to select all the objects the user wants to create into BlockReference
- Then we will pick 1 point as the set point for that BlockReference

 

+ I have created a Blockreference, but when the point is set, the objects inside that Block are not placed at that Point location, specifically as shown below.

aaaa.png

 

Any help is appreciated and thank you very much

0 Likes
482 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

 

In the block definition (BlockTableRecord), you have to displace all entities.

var xform Matrix3d.Displacement(pickedPoint.GetAsVector().Negate());
foreach (ObjectId id in btr)
{
    var entity = (Entity)tr.GetObject(id, OpenMode.ForWrite);
    entity.TransformBy(xform);
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

_gile
Consultant
Consultant

Here's a command example which mimics the -BLOCK command.

    // Mimics the native -BLOCK command
    [CommandMethod("CREATEBLOCK")]
    public void CreateBlock()
    {
        var doc = Application.DocumentManager.MdiActiveDocument;
        var db = doc.Database;
        var ed = doc.Editor;

        using (var tr = db.TransactionManager.StartTransaction())
        {
            var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

            string blockName;
            while (true)
            {
                var promptResult = ed.GetString("\nEnter block name: ");
                if (promptResult.Status != PromptStatus.OK) return;
                blockName = promptResult.StringResult;
                if (string.IsNullOrWhiteSpace(blockName)) return;
                if (!blockTable.Has(blockName)) break;
                ed.WriteMessage($"Block \"{blockName}\" already exists");
            }

            var promptPointResult = ed.GetPoint("\nSpecify the base point: ");
            if (promptPointResult.Status != PromptStatus.OK) return;

            var selection = ed.GetSelection();
            if (selection.Status != PromptStatus.OK) return;

            var blockDefinition = new BlockTableRecord();
            blockDefinition.Name = blockName;
            tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
            var blockId = blockTable.Add(blockDefinition);
            tr.AddNewlyCreatedDBObject(blockDefinition, true);
            var mapping = new IdMapping();
            var identifiers = new ObjectIdCollection(selection.Value.GetObjectIds());
            db.DeepCloneObjects(identifiers, blockId, mapping, false);
            var xform = Matrix3d.Displacement(
                promptPointResult.Value
                .TransformBy(ed.CurrentUserCoordinateSystem)
                .GetAsVector()
                .Negate());
            foreach (IdPair pair in mapping)
            {
                if (pair.IsCloned && pair.IsPrimary)
                {
                    var entity = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
                    entity.TransformBy(xform);
                }
            }
            tr.Commit();
        }
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 5

dungthachbomay
Contributor
Contributor

Hello !
Thank you for your help, Can you help me write a complete example? I'm just starting to learn so it's a bit difficult

0 Likes
Message 5 of 5

_gile
Consultant
Consultant

@dungthachbomay wrote:

Hello !
Thank you for your help, Can you help me write a complete example? I'm just starting to learn so it's a bit difficult


See reply #3



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes