Transformed BlockReferences (AutoCAD .net API)

So you’ve inserted a block reference into the model space. Very good. And you’ve decided to rotate it and move it around. Again: very good. Now you wish to programmatically place more block references, similarly rotated, but perhaps spaced some distance apart. How are you going to do this.

	// Assume we get the block reference like this:
	BlockReference br = tr.GetObject(id, OpenMode.ForRead) as BlockReference;

	// now in order to get the coordinate system which would transform the 
// block definition to the block reference’s current position in the model 
// space we could do so like this:

CoordinateSystem3d ucs = br.BlockTransform.CoordinateSystem3d;

// and using that, we could transform any block references that are inserted
// so that they are aligned with the originally placed block reference

You can also use the coordinate system obtained above, to get a new transformation plane:

	Vector3d xAxisAcad = br.BlockTransform.CoordinateSystem3d.Xaxis;
       Vector3d yAxisAcad = br.BlockTransform.CoordinateSystem3d.Yaxis;

xAxis = new Vector(xAxisAcad.X, xAxisAcad.Y, xAxisAcad.Z);
       yAxis = new Vector(yAxisAcad.X, yAxisAcad.Y, yAxisAcad.Z);
	

	// set a new transformation plane:
	new Model().GetWorkPlaneHandler().SetCurrentTransformationPlane(new TransformationPlane(insertionPoint, xAxis, yAxis));

This can be particularly useful if you are placing strangely oriented block references from one drawing to another, and you want to maintain the same orientation.

Hope you found it helpful!

Written on June 13, 2017