Author Topic: 2D Isometric Rectangle Jig  (Read 1189 times)

0 Members and 1 Guest are viewing this topic.

jthrasher

  • Guest
2D Isometric Rectangle Jig
« on: September 11, 2009, 01:56:45 pm »
I have messed around with jigs a little bit and managed to create one that will place text that is obliqued
and rotated per the snapstyl and snapisopair settings.

Could somebody point me in the right direction for creating a jig that will draw a rectangle that obeys the system variables snapstyl and snapisopair?  I do a lot of 2d Iso's and this sort of command would be really handy.  Could a transformation matrix handle this sort of thing?

Any ideas for an approach to this would be awesome.

jthrasher

  • Guest
Re: 2D Isometric Rectangle Jig
« Reply #1 on: September 12, 2009, 06:26:56 am »
I have messed around with jigs a little bit and managed to create one that will place text that is obliqued
and rotated per the snapstyl and snapisopair settings.

Could somebody point me in the right direction for creating a jig that will draw a rectangle that obeys the system variables snapstyl and snapisopair?  I do a lot of 2d Iso's and this sort of command would be really handy.  Could a transformation matrix handle this sort of thing?

Any ideas for an approach to this would be awesome.

I stumbled onto this cool little lisp routine written by Hadi Sharghi @ http://esoftworks.net/ called Angular-Select.  This gives me some ideas for an approach,  I'll let everyone know how its coming along once I get back to the office next week and can get started on it.

nullptr

  • Bricscad
  • Needs a day job
  • Posts: 6583
  • AKA Daniel
Re: 2D Isometric Rectangle Jig
« Reply #2 on: September 12, 2009, 07:16:03 am »

Sorry I can't be of much help, just wanted to say welcome to TheSwamp  :-)

jthrasher

  • Guest
Re: 2D Isometric Rectangle Jig
« Reply #3 on: September 12, 2009, 07:41:24 am »
Thanks Daniel.  I've been lurking around here for about a year now, have learned a lot from this site and finally decided to start participating.  Took an introductory online class about c# back in December and have been eagerly exploring the .Net API ever since.  :-)

Bryco

  • Water Moccasin
  • Posts: 1708
Re: 2D Isometric Rectangle Jig
« Reply #4 on: September 13, 2009, 02:14:37 pm »
Here's a start

Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(Iso.IsoRectangle))]

namespace Iso
{
    class IsoRectangle
    {
        [CommandMethod("ir")]
        public static void Rectangle()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            PromptPointOptions ppo = new PromptPointOptions
                ("\nSpecify first corner point: ");
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return;
            Point3d corner1 = ppr.Value;
            RectangleJig Recjig = new RectangleJig(corner1);
            if (ed.Drag(Recjig).Status == PromptStatus.Cancel) return;

            Polyline pline = Recjig.pline;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject
                    (db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                btr.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }
            return;
        }


        public class RectangleJig : EntityJig
        {
            private Point2d P1, P2, P3, P4;
            private Point3d corner1, corner2;   
            private PromptPointResult pres;
            public Polyline pline;
            private double tan30 = Math.Tan((30.0 / 180.0) * Math.PI);


            public RectangleJig(Point3d _corner1)
                : base(new Polyline(3))
            {
                corner1 = _corner1;
                pline = (Polyline)Entity;
                pline.SetDatabaseDefaults();
                P1 = new Point2d(corner1.X, corner1.Y);
                pline.AddVertexAt(0, P1, 0, 0, 0);
                pline.AddVertexAt(1, P1, 0, 0, 0);
                pline.AddVertexAt(2, P1, 0, 0, 0);
                pline.AddVertexAt(3, P1, 0, 0, 0);
                pline.Closed = true;
            }

            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                JigPromptPointOptions jigPointOpts = new JigPromptPointOptions
                    ("\nSpecify other corner point");

                jigPointOpts.UseBasePoint = true;
                jigPointOpts.BasePoint = corner1;
                jigPointOpts.UserInputControls = (UserInputControls.Accept3dCoordinates)
                    | UserInputControls.NullResponseAccepted;
                pres = prompts.AcquirePoint(jigPointOpts);
                Point3d endPointTemp = pres.Value;

                if (endPointTemp != corner2)
                {
                    corner2 = endPointTemp;
                }
                else
                    return SamplerStatus.NoChange;

                if (pres.Status == PromptStatus.Cancel)
                    return SamplerStatus.Cancel;
                else
                    return SamplerStatus.OK;
            }



            protected override bool Update()
            {         
                P3 = new Point2d(corner2.X, corner2.Y);
                double y = tan30 * (corner2.X - corner1.X);
                P2 = new Point2d(corner2.X, corner1.Y + y);
                P4 = new Point2d(corner1.X, corner2.Y - y);
                pline.SetPointAt(1, P2);
                pline.SetPointAt(2, P3);
                pline.SetPointAt(3, P4);

                return true;
            } 

        }// end class RectangleJig

    }   //End class IsoRectangle
   

jthrasher

  • Guest
Re: 2D Isometric Rectangle Jig
« Reply #5 on: September 14, 2009, 04:33:17 pm »
Thanks Bryco!  Couldnt have done this without your help. :-D

Here's what I managed to get done, probably not the most elegent solution, but it works for me... :lol:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;


using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(Iso.IsoRectangle))]


namespace Iso
{
   


    class IsoRectangle
    {


        [CommandMethod("ir")]
        public void Rectangle()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            PromptPointOptions ppo = new PromptPointOptions
                ("\nSpecify first corner point: ");
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return;
            Point3d corner1 = ppr.Value;
            RectangleJig Recjig = new RectangleJig(corner1);
            if (ed.Drag(Recjig).Status == PromptStatus.Cancel) return;

            Polyline pline = Recjig.pline;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject
                    (db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                btr.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }
            return;
        }




        public class RectangleJig : EntityJig
        {
            private Point2d P1, P2, P3, P4;
            private Point3d corner1, corner2;
            private PromptPointResult pres;
            public Polyline pline;
            private double tan30 = Math.Tan((30.0 / 180.0) * Math.PI);
            private double degAng2Use1;
            private double degAng2Use2;




            public RectangleJig(Point3d _corner1)
                : base(new Polyline(3))
            {
                corner1 = _corner1;
                pline = (Polyline)Entity;
                pline.SetDatabaseDefaults();
                P1 = new Point2d(corner1.X, corner1.Y);
                pline.AddVertexAt(0, P1, 0, 0, 0);
                pline.AddVertexAt(1, P1, 0, 0, 0);
                pline.AddVertexAt(2, P1, 0, 0, 0);
                pline.AddVertexAt(3, P1, 0, 0, 0);
                pline.Closed = true;
            }

            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                JigPromptPointOptions jigPointOpts = new JigPromptPointOptions
                    ("\nSpecify other corner point");

                jigPointOpts.UseBasePoint = true;
                jigPointOpts.BasePoint = corner1;
                jigPointOpts.UserInputControls = (UserInputControls.Accept3dCoordinates)
                    | UserInputControls.NullResponseAccepted;
                pres = prompts.AcquirePoint(jigPointOpts);
                Point3d endPointTemp = pres.Value;

                if (endPointTemp != corner2)
                {
                    corner2 = endPointTemp;
                }
                else
                    return SamplerStatus.NoChange;

                if (pres.Status == PromptStatus.Cancel)
                    return SamplerStatus.Cancel;
                else
                    return SamplerStatus.OK;
            }

            private bool IsometricSnapIsOn()
            {
                if (Application.GetSystemVariable("Snapstyl").ToString() == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }

            private String GetIsoPlane()
            {
                String result = "Left";

                if (Application.GetSystemVariable("Snapisopair").ToString() == "1")
                {
                    result = "Top";
                }
                else if (Application.GetSystemVariable("Snapisopair").ToString() == "2")
                {
                    result = "Right";
                }
                return result;
            }

            private Point2d PolarPoint(Point2d basepoint, double angle, double distance)
            {
                return new Point2d(
                basepoint.X + (distance * Math.Cos(angle)),
                basepoint.Y + (distance * Math.Sin(angle))
                );
            }

            private Point2d ImaginaryIntersect(Point2d line1_pt1, Point2d line1_pt2, Point2d line2_pt1, Point2d line2_pt2)
            {
                Line2d line1 = new Line2d(line1_pt1, line1_pt2);
                Line2d line2 = new Line2d(line2_pt1, line2_pt2);

                double line1Ang = line1.Direction.Angle;
                double line2Ang = line2.Direction.Angle;

                double line1ConAng = line1Ang + Degrees_Radians_Conversion(180, false);
                double line2ConAng = line2Ang + Degrees_Radians_Conversion(180, false);

                Point2d RayLine1_pt1 = PolarPoint(line1_pt1, line1Ang, 10000);
                Point2d RayLine1_pt2 = PolarPoint(line1_pt1, line1ConAng, 10000);
                Line2d RayLine1 = new Line2d(RayLine1_pt1, RayLine1_pt2);

                Point2d RayLine2_pt1 = PolarPoint(line2_pt1, line2Ang, 10000);
                Point2d RayLine2_pt2 = PolarPoint(line2_pt1, line2ConAng, 10000);
                Line2d RayLine2 = new Line2d(RayLine2_pt1, RayLine2_pt2);

                Point2d[] col = RayLine1.IntersectWith(RayLine2);

                return col[0];

            }

            private Double Degrees_Radians_Conversion(Double Angle, bool inputIsRadians)
            {
                if (inputIsRadians)
                {
                    Angle = (180 * (Angle / Math.PI));
                }
                else
                {
                    Angle = (Math.PI * (Angle / 180));
                }
                return Angle;
            }


            protected override bool Update()
            {
                if (!IsometricSnapIsOn())
                {
                    degAng2Use1 = 0;
                    degAng2Use2 = 90;
                }
                else if (GetIsoPlane() == "Right")
                {
                    degAng2Use1 = 30;
                    degAng2Use2 = 90;
                }
                else if (GetIsoPlane() == "Left")
                {
                    {
                        degAng2Use1 = 330;
                        degAng2Use2 = 90;
                    }
                }
                else
                {
                    {
                        degAng2Use1 = 30;
                        degAng2Use2 = 330;
                    }
                }

                double Ang2Use1 = Degrees_Radians_Conversion(degAng2Use1, false);
                double ConAng2Use1 = Ang2Use1 + Degrees_Radians_Conversion(180, false);

                double Ang2Use2 = Degrees_Radians_Conversion(degAng2Use2, false);
                double ConAng2Use2 = Ang2Use2 + Degrees_Radians_Conversion(180, false);



                P3 = new Point2d(corner2.X, corner2.Y);
                //double y = tan30 * (corner2.X - corner1.X);
                P2 = ImaginaryIntersect(P1, PolarPoint(P1, Ang2Use1, 1), P3, PolarPoint(P3, Ang2Use2, 1));
                P4 = ImaginaryIntersect(P1, PolarPoint(P1, ConAng2Use2, 1), P3, PolarPoint(P3, ConAng2Use1, 1));
                pline.SetPointAt(1, P2);
                pline.SetPointAt(2, P3);
                pline.SetPointAt(3, P4);

                return true;
            }

        }// end class RectangleJig

    }   //End class IsoRectangle
}

Bryco

  • Water Moccasin
  • Posts: 1708
Re: 2D Isometric Rectangle Jig
« Reply #6 on: September 14, 2009, 04:38:48 pm »
Glad to help. What I wrote wouldn't work in 3d but I doubt that would ever be a problem.