Author Topic: Set block rotation angle with jig  (Read 3766 times)

0 Members and 1 Guest are viewing this topic.

TimBlanch

  • Guest
Set block rotation angle with jig
« 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
 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

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Set block rotation angle with jig
« Reply #1 on: October 28, 2011, 03:35:20 PM »
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).

Code: [Select]
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;
            }
        }
    }
}
Speaking English as a French Frog

TimBlanch

  • Guest
Re: Set block rotation angle with jig
« Reply #2 on: October 30, 2011, 02:26:13 PM »
Thanks! Works great. Now at least I can compare it to my code and see where I was going wrong.
Tim

kaefer

  • Guest
Re: Set block rotation angle with jig
« Reply #3 on: October 31, 2011, 05:22:30 AM »
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.

Quote
Code: [Select]
                    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?
Code: [Select]
                    Point3d ucsRot = Vector3d.XAxis.GetAngleTo((ucs.CoordinateSystem3d.Xaxis.TransformBy(mat)), Vector3d.ZAxis);

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Set block rotation angle with jig
« Reply #4 on: October 31, 2011, 07:27:51 AM »
You're absolutely right kaefer, it's more concise and elegant, thanks.
Speaking English as a French Frog

cean

  • Guest
Re: Set block rotation angle with jig
« Reply #5 on: February 24, 2012, 04:26:32 AM »
cool,just studying this today. thx.