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

Please, let me know how to change the block's x,y,z location.

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
TSYUK
242 Views, 5 Replies

Please, let me know how to change the block's x,y,z location.

Hello!

 

I will really appreciate if anybody let me know how to change x,y,z location of block.

I want to change the x,y,z location of a certain block to the point, (0,0,0).

 

 

the below .net c# code does not work.  There is no change.

 

 

Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;


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


             BlockTableRecord acBlkTblRec = new BlockTableRecord();
             acBlkTblRec = tr.GetObject(btl[BlockTableRecord.ModelSpace],
             OpenMode.ForWrite) as BlockTableRecord;


            foreach (var item in btl)
           {
                   var bk = (BlockTableRecord)tr.GetObject(item, OpenMode.ForRead);
                   if (bk.IsLayout)
                   {
                             foreach (var obj in bk)
                             {
                                       Entity ent = (Entity)tr.GetObject(obj, OpenMode.ForRead);


                                       if (ent != null && ent.GetType() == typeof(BlockReference))
                                       {
                                                BlockReference br = (BlockReference)tr.GetObject(obj, OpenMode.ForRead);

                                                if (br.IsDynamicBlock)
                                                {
                                                        ed.WriteMessage("\nDynamic Block Name: (" +                             ((BlockTableRecord)br.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name + ").");
                                                        blkname = ((BlockTableRecord)br.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name;

                                                 }
                                                else
                                                {
                                                         blkname = br.Name;
                                                 }


                                                BlockReference brt = (BlockReference)tr.GetObject(obj, OpenMode.ForWrite);
                                                brt.TransformBy(Matrix3d.Displacement(new Vector3d(0, 0, 0)));


                                          }
                                 }
                         }
                }
                tr.Commit();
         }


    }
    catch
    {
           ed.WriteMessage("\nSomething went wrong");
     }
     finally
    {
    }
    ed.WriteMessage("\n");

}

 

Tags (3)
5 REPLIES 5
Message 2 of 6
_gile
in reply to: TSYUK

Hi,

 

Try replacing:

brt.TransformBy(Matrix3d.Displacement(new Vector3d(0, 0, 0)));

with:

brt.TransformBy(Matrix3d.Displacement(brt.Position.GetVectorTo(Point3d.Origin)));

But, with your code this will affect all block references in all layouts (not only "a certain block").



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 6
_gile
in reply to: _gile

Please, use the "Insert/Edit code sample" button when you post code.

_gile_0-1704468104599.png

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 6
norman.yuan
in reply to: TSYUK

That is you pass i a 0 Vector to Matrix3d.Displacement(), meaning, the entity (BlockReference) is not moved to anywhere (e.g no move in 3 directions of X, Y, Z). What you should pass in is a Vector pointing from current block reference position to the point(0,0,0).

 

Also, there is no need to open the same entity 3 times in order to determine if the block reference is the target one.

 

The modified code in the for... loop of a Layout block would be like:

foreach (var obj in bk)
{
	if (obj.ObjectClass.DxfName.ToUpper()!="INSER") continue;
	
	var br = (BlockReference)tr.GetObject(obj, OpenMode.ForWrite);
	string blkName;
	if (br.IsDynamicBlock)
	{
		blkname = ((BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead)).Name;
	}
	else
	{
		blkname = br.Name;
	}
	if (blkName.ToUpper()=="XXXXXX")
	{
		br.TransformBy(Matrix3d.Displacement(br.Position.GetVectorTo(new Point3d(0, 0, 0))));
	}
}

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 6
TSYUK
in reply to: _gile

Great!!!

Now, it works.

 

 

Thank you very much.

Message 6 of 6
TSYUK
in reply to: norman.yuan

Ok!   I see.

 

Thanks much. 

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report