Hi,
By my side, I use two classes derived from EntityJig, one for the insertion the other for the rotation.
Here's a little sample (works whatever the current coordinate system).
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace InsertAndRotateBlock
{
public class Commands
{
[CommandMethod("Test")]
public void Test()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptStringOptions pso = new PromptStringOptions("\nEnter the bloc name: ");
pso.AllowSpaces = false;
PromptResult pr = ed.GetString(pso);
if (pr.Status != PromptStatus.OK) return;
string blockName = pr.StringResult;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(blockName))
{
AcAp.ShowAlertDialog(string.Format("Can't find the '{0}' block."));
return;
}
using (BlockReference br = new BlockReference(Point3d.Origin, bt[blockName]))
{
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
br.TransformBy(ucs);
Vector3d ucsNormal = ucs.CoordinateSystem3d.Zaxis;
Matrix3d mat = Matrix3d.WorldToPlane(new Plane(Point3d.Origin, ucsNormal));
Point3d ucsXDir = (Point3d)AcAp.GetSystemVariable("UCSXDIR");
double ucsRot = Vector3d.XAxis.GetAngleTo(ucsXDir.GetAsVector().TransformBy(mat), Vector3d.ZAxis);
InsertBlockJig insJig = new InsertBlockJig(br);
pr = ed.Drag(insJig);
if (pr.Status == PromptStatus.OK)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
RotateBlockJig rotJig = new RotateBlockJig(br, ucsRot);
pr = ed.Drag(rotJig);
if (pr.Status != PromptStatus.OK)
{
br.Rotation = 0.0;
}
else
{
rotJig.UpdateRotation();
}
}
tr.Commit();
}
}
}
class InsertBlockJig : EntityJig
{
Point3d _dragPt;
BlockReference _br;
public InsertBlockJig(BlockReference br)
: base(br)
{
_br = br;
_dragPt = br.Position;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
string msg = "\nSpecify insertion point:";
JigPromptPointOptions jppo = new JigPromptPointOptions(msg);
jppo.UserInputControls =
(UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted);
PromptPointResult ppr = prompts.AcquirePoint(jppo);
if (_dragPt == ppr.Value)
{
return SamplerStatus.NoChange;
}
else
{
_dragPt = ppr.Value;
}
return SamplerStatus.OK;
}
protected override bool Update()
{
_br.Position = _dragPt;
return true;
}
}
class RotateBlockJig : EntityJig
{
private BlockReference _br;
private double _rot, _ucsRot;
public RotateBlockJig(BlockReference br, double ucsRot)
: base(br)
{
_br = br;
_ucsRot = ucsRot;
}
protected override bool Update()
{
_br.Rotation = _rot + _ucsRot;
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptAngleOptions jpao = new JigPromptAngleOptions("\nSpecify the rotation: ");
jpao.DefaultValue = 0.0;
jpao.UseBasePoint = true;
jpao.BasePoint = _br.Position;
jpao.Cursor = CursorType.RubberBand;
jpao.UserInputControls = (
UserInputControls.Accept3dCoordinates |
UserInputControls.NullResponseAccepted);
PromptDoubleResult pdr = prompts.AcquireAngle(jpao);
if (_rot == pdr.Value)
{
return SamplerStatus.NoChange;
}
else
{
_rot = pdr.Value;
return SamplerStatus.OK;
}
}
// update for 'default'
internal void UpdateRotation()
{
_br.Rotation = _rot + _ucsRot;
}
}
}
}