2

I'm trying to programmatically rotate the purple block in AutoCAD shown here by 90 degrees so that it lines up with the orange block.

enter image description here

The purple block's base point is the lower left hand corner. Using AutoCAD's built in rotate function gives me the result that I want shown here:

enter image description here

But when I try to rotate it programmatically with this function

public static BlockReference RotateBlockWithAttributes(ObjectId passedIdOfBlockToRotate)
{
    Transaction tr = _database.TransactionManager.StartTransaction();
    DocumentLock docLock = _activeDocument.LockDocument();

    using (tr)
    using (docLock)
    {
        BlockReference blockToRotate = tr.GetObject(passedIdOfBlockToRotate, OpenMode.ForWrite) as BlockReference;
        blockToRotate.TransformBy(Matrix3d.Rotation(Math.PI / 2, blockToRotate.Normal, blockToRotate.Position));
        tr.Commit();
        return blockToRotate;
    }
}

I get this result

enter image description here

And have no idea why...

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • 1
    The point is: Around what is the rectangle supposed to rotate? Around one of its corners, around its center, the coordinate origin, something else? – Olivier Jacot-Descombes Oct 08 '15 at 21:23
  • The purple block's base point is the lower left hand corner. That is the point its supposed to rotate around – Nick Gilbert Oct 08 '15 at 21:32
  • The height and width of the rectangle are not equal. So you have to move the object to the left half the distance of the difference between the height and width of the rectangle. You are rotating around the center of the rectangle. Changing the rotation around the corner will not solve the issue, it would move the object up or down which is not what you want. – jdweng Oct 08 '15 at 21:37
  • Works for me. Are you sure that the normal is 0, 0, 1 (Z axis)? – Maxence Oct 12 '15 at 12:10

1 Answers1

1

I believe you should change the BlockReference.Rotation property instead.

From the help file:

Accesses the rotation value (in radians) of the block reference. The rotation value is relative to the X axis of a coordinate system that is parallel to the OCS of the block reference, but has its origin at the position point of the block reference. The rotation axis is the Z axis of this coordinate system with positive rotations going counterclockwise when looking down the Z axis towards the origin.

Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44