using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Colors; using acadApp = Autodesk.AutoCAD.ApplicationServices.Application; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.GraphicsInterface; [assembly: CommandClass(typeof(TesterCS.JigTest))] namespace TesterCS { public class JigTest : DrawJig { private string place; private Point3d basept; private Point3d cpt; private Entity ent; private JigBeam.SectionPlacement en; [CommandMethod("gt")] public void StartJig() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; try { basept = Point3d.Origin; PromptStringOptions pso = new PromptStringOptions("\nChoose Section Placement [TL/TC/TR/ML/MC/MR/BL/BC/BR] : "); pso.DefaultValue = "ML"; PromptResult pr = ed.GetString(pso); if (pr.Status == PromptStatus.OK) { place = pr.StringResult; } switch (place) { case "TL": en = JigBeam.SectionPlacement.TopLeft; break; case "TC": en = JigBeam.SectionPlacement.TopCentre; break; case "TR": en = JigBeam.SectionPlacement.TopRight; break; case "ML": en = JigBeam.SectionPlacement.MiddleLeft; break; case "MC": en = JigBeam.SectionPlacement.MiddleCentre; break; case "MR": en = JigBeam.SectionPlacement.MiddleRight; break; case "BL": en= JigBeam.SectionPlacement.BottomLeft; break; case "BC": en = JigBeam.SectionPlacement.BottomCentre; break; case "BR": en = JigBeam.SectionPlacement.BottomRight; break; default: break; } JigBeam jb = new JigBeam(); ent = jb.DrawSection(en, basept); ed.Drag(this); Autodesk.AutoCAD.DatabaseServices.Polyline pl = ent as Autodesk.AutoCAD.DatabaseServices.Polyline; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject (db.CurrentSpaceId, OpenMode.ForWrite); btr.AppendEntity(pl); tr.AddNewlyCreatedDBObject(pl, true); tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("\nError: {0}\nTrace: {1}", ex.Message, ex.StackTrace); } finally { ent.Dispose(); } } protected override SamplerStatus Sampler(JigPrompts prompts) { JigPromptPointOptions jppo = new JigPromptPointOptions("\nSpecify placement point: "); jppo.UserInputControls = UserInputControls.Accept3dCoordinates; PromptPointResult ppr = prompts.AcquirePoint(jppo); if (ppr.Status == PromptStatus.OK) { if (ppr.Value.IsEqualTo(cpt)) return SamplerStatus.NoChange; else { cpt = ppr.Value; Vector3d vect = basept.GetVectorTo(cpt); ent.TransformBy(Matrix3d.Displacement(vect)); basept = cpt; return SamplerStatus.OK; } } return SamplerStatus.Cancel; } protected override bool WorldDraw(WorldDraw draw) { draw.Geometry.Draw(ent); return true; } } } //----------------------------------------------------------------------------// using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Internal; using Autodesk.AutoCAD.Windows; using AcApp = Autodesk.AutoCAD.ApplicationServices.Application; [assembly: CommandClass(typeof(TesterCS.JigBeam))] namespace TesterCS { public class JigBeam { private double Bf = 0.15; private double tf = 0.15; private double tw = 0.15; private double r1 = 0.02; private double D = 0.1; private Polyline oPolyline; public enum SectionPlacement { TopLeft, TopCentre, TopRight, MiddleLeft, MiddleCentre, MiddleRight, BottomLeft, BottomCentre, BottomRight } public Polyline DrawSection(SectionPlacement Placement, Point3d InsertionPoint) { //convert the InsertionPoint to Point2d Point2d Inspt = new Point2d(InsertionPoint.X, InsertionPoint.Y); Point2d PtVertex = new Point2d(); oPolyline = new Polyline(16); Point3dCollection Pts = new Point3dCollection(); //Get top left based on Insertion Point being Middle centre switch (Placement) { case SectionPlacement.TopLeft: PtVertex = Inspt; break; case SectionPlacement.TopCentre: PtVertex = new Point2d(Inspt.X - (this.Bf / 2), Inspt.Y); break; case SectionPlacement.TopRight: PtVertex = new Point2d(Inspt.X - (this.Bf), Inspt.Y); break; case SectionPlacement.MiddleLeft: PtVertex = new Point2d(Inspt.X, Inspt.Y + (this.D / 2)); break; case SectionPlacement.MiddleCentre: PtVertex = new Point2d(Inspt.X - (this.Bf / 2), Inspt.Y + (this.D / 2)); break; case SectionPlacement.MiddleRight: PtVertex = new Point2d(Inspt.X - this.Bf, Inspt.Y + (this.D / 2)); break; case SectionPlacement.BottomLeft: PtVertex = new Point2d(Inspt.X, Inspt.Y + this.D); break; case SectionPlacement.BottomCentre: PtVertex = new Point2d(Inspt.X - (this.Bf / 2), Inspt.Y + this.D); break; case SectionPlacement.BottomRight: PtVertex = new Point2d(Inspt.X - this.Bf, Inspt.Y + this.D); break; } Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); oPolyline.AddVertexAt(0, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X + this.Bf, PtVertex.Y); oPolyline.AddVertexAt(1, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X, PtVertex.Y - this.tf); oPolyline.AddVertexAt(2, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X - (this.Bf / 2) + (this.tw / 2) + this.r1, PtVertex.Y); oPolyline.AddVertexAt(3, PtVertex, 0.414, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X - this.r1, PtVertex.Y - this.r1); oPolyline.AddVertexAt(4, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X, PtVertex.Y - this.D + (2 * (this.r1 + this.tf))); oPolyline.AddVertexAt(5, PtVertex, 0.414, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X + this.r1, PtVertex.Y - this.r1); oPolyline.AddVertexAt(6, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X + (this.Bf / 2) - (this.tw / 2) - r1, PtVertex.Y); oPolyline.AddVertexAt(7, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X, PtVertex.Y - this.tf); oPolyline.AddVertexAt(8, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X - this.Bf, PtVertex.Y); oPolyline.AddVertexAt(9, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X, PtVertex.Y + this.tf); oPolyline.AddVertexAt(10, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X + this.Bf / 2 - (this.tw / 2) - this.r1, PtVertex.Y); oPolyline.AddVertexAt(11, PtVertex, 0.414, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X + this.r1, PtVertex.Y + this.r1); oPolyline.AddVertexAt(12, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X, PtVertex.Y + this.D - (2 * (this.tf + this.r1))); oPolyline.AddVertexAt(13, PtVertex, 0.414, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X - this.r1, PtVertex.Y + this.r1); oPolyline.AddVertexAt(14, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X - (this.Bf / 2) + (this.tw / 2) + this.r1, PtVertex.Y); oPolyline.AddVertexAt(15, PtVertex, 0, 0, 0); Pts.Add(new Point3d(PtVertex.X, PtVertex.Y, 0)); PtVertex = new Point2d(PtVertex.X, PtVertex.Y + this.tf); oPolyline.AddVertexAt(16, PtVertex, 0, 0, 0); return oPolyline; } } }