Problem when insert and move a block references

Problem when insert and move a block references

jamalkhah
Contributor Contributor
2,725 Views
17 Replies
Message 1 of 18

Problem when insert and move a block references

jamalkhah
Contributor
Contributor

Hi everyone

I want to insert a block reference to my drawing. Sometimes I have a problem with moving. When the block reference is inserted, the base point has moved away from the lines and lines will show in the wrong position.

photo_2024-04-07_01-13-28.jpg

 

The piece of code I used is:

            using (DocumentLock dlock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    #region bt
                    // Open the Block table for read
                    BlockTable bt;
                    bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    ObjectId blockId = ObjectId.Null;

                    if (bt.Has(blockName))
                        blockId = bt[blockName];
                    else
                        return;
                    #endregion

                    // Focus on drawing
                    Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();

                    using (BlockReference br = new BlockReference(Point3d.Origin, blockId))
                    {
                        #region Move BlockReference

                        Point3d p3d = Point3d.Origin;
                        Vector3d vector3D = p3d.GetVectorTo(insertPoint.Point3d());

                        br.TransformBy(Matrix3d.Displacement(vector3D));

                        #endregion
...
}

This problem only happens sometimes and this code works correctly most of the time. 

How can I fix it?

0 Likes
2,726 Views
17 Replies
Replies (17)
Message 2 of 18

ActivistInvestor
Mentor
Mentor

Why not just set the initial insertion point of the block reference to the point that you are trying to transform it to?

 

You are also missing something relevant. I do not know what this is:

 

insertPoint.Point3d()

 

0 Likes
Message 3 of 18

jamalkhah
Contributor
Contributor

1- I have tried this solution before, problem is the same.

2. insertPoint.Point3d() : Point3d() is a function that converts Point2d to Point3d.  

0 Likes
Message 4 of 18

jamalkhah
Contributor
Contributor

Update:

Recently, I found the reason for this problem in my tests. Before using this method, if I use the "Paste to original coordinates" option in AutoCAD when pasting something, the base point of this block reference will be moved by the distance of the copied item from the point (0, 0, 0). But this does not always happen, only sometimes!

 

What happens when using "Paste to original coordinates" that affects the result of my method?

0 Likes
Message 5 of 18

_gile
Consultant
Consultant

Hi,

It seems your issue is not due to the code you posted but to the way the block definition (BlockTableRecord) was created. Try to open the block in the block Editor and see the position of entities about the origin.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 18

jamalkhah
Contributor
Contributor

In the Block Editor, I have a Basepoint. When the code works properly, the Basepoint is in the right position, but when this problem occurs, the entities will move to another position.

 

Before, when I did not use basepoints, I used to draw the entities at the origin. When the problem arose, the entities were moved together from the origin.

0 Likes
Message 7 of 18

_gile
Consultant
Consultant

@jamalkhah wrote:

This problem only happens sometimes and this code works correctly most of the time. 


Isn't there a coordinate system issue?

Is the current UCS different from WCS when the problem happens and equal to WCS "most of the time"?

A good practice is to always transform coordinates of points got by Editor.GetPoint into WCS coordinates.

var promptPointResult = ed.GetPoint("\nSpecify insertion point: ");
if (promptPointResult.Status != PromptStatus.OK) return;
var insertionPoint = promptPointResult.Value.TransformBy(ed.CurrentUserCoordinateSystem);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 18

jamalkhah
Contributor
Contributor

Thanks, Gile

I will try this method but it doesn't seem to be the solution. The problem is that the block reference is manipulated after the execution of my code and the entities are moved!

0 Likes
Message 9 of 18

_gile
Consultant
Consultant

Sorry, I do not understand what you mean with: "the block reference is manipulated after the execution of my code and the entities are moved"



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 18

_gile
Consultant
Consultant

You should post a relevant example with a block insterted with your code at a simple point (e.g. 10, 10, 0) and another inserted directly (without code) which reflect what you expected..



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 18

jamalkhah
Contributor
Contributor

I explained it in my first post. Entities moved from their original position to another position (It can be seen in the block editor).

This move usually occurs after the use of the "Paste to original coordinate" option and the moving vector is equal to the size of the displacement vector of the copied entities from the origin.

For example, if I copy a polyline to my drawing to its original coordinates, and its original coordinates are (100, 200, 0), and after that, execute my code, the entities inside my block reference, will move by the vector (100, 200, 0).

 

Did I get my point across?

0 Likes
Message 12 of 18

_gile
Consultant
Consultant

So, my first reply (#5) was relevant.

When you create the block definition (BlockTableRecord), you have to displace all "copied entities" using a vector from the specified insertion point to origin.

You can see this topic with an example of custom command which mimics the native BLOCK command to create a block definition from selected entities and an insertion point.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 13 of 18

jamalkhah
Contributor
Contributor

@_gile wrote:

You should post a relevant example with a block insterted with your code at a simple point (e.g. 10, 10, 0) and another inserted directly (without code) which reflect what you expected..


What I expect:

1.png

 What happens after the code is executed:

2.png

In both images, the blue point is the block reference basepoint. In the second image, the code has placed the block reference in the correct place, but the entities have moved away from the base point.

0 Likes
Message 14 of 18

jamalkhah
Contributor
Contributor

@_gile wrote:

You should post a relevant example with a block insterted with your code at a simple point (e.g. 10, 10, 0) and another inserted directly (without code) which reflect what you expected..


Block editor - Before code execution:

 

1_.png

Block editor - Aftercode execution:

 

2_.png

 

0 Likes
Message 15 of 18

_gile
Consultant
Consultant

@jamalkhah wrote:

@_gile wrote:

You should post a relevant example with a block insterted with your code at a simple point (e.g. 10, 10, 0) and another inserted directly (without code) which reflect what you expected..


Block editor - Before code execution:

 

1_.png

Block editor - Aftercode execution:

 

2_.png

 


What you see in the block editor is the block definition (BlockTableRecord) and the code you posted just insert a block reference (BlockReference). So the changes in the block definition cannot be due to code you posted.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 16 of 18

ActivistInvestor
Mentor
Mentor

What you show is the result of redefining the block to have a different base point. That has nothing to do with code that inserts the block.

 

Unintended changes to the base point of a block could be the result of defining or redefining the block with a current UCS that is not the WCS. 

0 Likes
Message 17 of 18

jamalkhah
Contributor
Contributor

What you see in the block editor is the block definition (BlockTableRecord) and the code you posted just insert a block reference (BlockReference). So the changes in the block definition cannot be due to code you posted.

Could this be due to copying some entities from another source in the drawing?

 

0 Likes
Message 18 of 18

ActivistInvestor
Mentor
Mentor

Entities that are copied from another drawing will have the same coordinates as the original entities had, unless they were transformed after being copied.

0 Likes