TheSwamp
Code Red => .NET => Topic started by: TimBlanch on October 28, 2011, 02:28:30 PM
-
I have been playing with the block insert jig found on Kean Walmsey's blog, it works well.
http://through-the-interface.typepad.com/through_the_interface/2009/03/jigging-an-autocad-block-with-attributes-using-net.html (http://through-the-interface.typepad.com/through_the_interface/2009/03/jigging-an-autocad-block-with-attributes-using-net.html)
What I have been trying to add, with no luck, is the ability to set the rotation angle of the block when I place it. According to Kean it should be fairly easy but so far, and it probably is, for him.
Anybody want to give me a push in the right direction?
I would appreciate it.
Tim
-
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;
}
}
}
}
-
Thanks! Works great. Now at least I can compare it to my code and see where I was going wrong.
Tim
-
By my side, I use two classes derived from EntityJig, one for the insertion the other for the rotation.
Hi gile,
your double jig is pretty swell. I needed something similar for the insertion of an AEC MV block reference and ended up doing it with Transients instead.
Point3d ucsXDir = (Point3d)AcAp.GetSystemVariable("UCSXDIR");
double ucsRot = Vector3d.XAxis.GetAngleTo(ucsXDir.GetAsVector().TransformBy(mat), Vector3d.ZAxis);
Am I completely off that that's the same as this?
Point3d ucsRot = Vector3d.XAxis.GetAngleTo((ucs.CoordinateSystem3d.Xaxis.TransformBy(mat)), Vector3d.ZAxis);
-
You're absolutely right kaefer, it's more concise and elegant, thanks.
-
cool,just studying this today. thx.