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");
}
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
Solved by _gile. Go to Solution.
Solved by _gile. Go to Solution.
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").
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
Can't find what you're looking for? Ask the community or share your knowledge.