.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Change Block Base Point

4 REPLIES 4
Reply
Message 1 of 5
leanhtien_hutech
317 Views, 4 Replies

How to Change Block Base Point

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

4 REPLIES 4
Message 2 of 5
_gile
in reply to: leanhtien_hutech

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
in reply to: _gile

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

Message 4 of 5
leanhtien_hutech
in reply to: _gile

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

Message 5 of 5
_gile
in reply to: leanhtien_hutech


@leanhtien_hutech 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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report