Example #1
0
            public static bool Jig(Point3d pBase, MText mtext, bool autoLine, double lineLength, bool autoRotateText, double textRotation)
            {
                Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                Autodesk.AutoCAD.DatabaseServices.Database    db  = doc.Database;

                CoordinateJig jigger = new CoordinateJig(mtext, pBase, autoLine, lineLength, autoRotateText, textRotation);

                PromptResult res = doc.Editor.Drag(jigger);

                jigger.EraseLine();

                if (res.Status == PromptStatus.OK)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
Example #2
0
        public void Coord()
        {
            if (!CheckLicense.Check())
            {
                return;
            }

            Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Autodesk.AutoCAD.DatabaseServices.Database    db  = doc.Database;

            if (!init)
            {
                if (!ShowSettings())
                {
                    return;
                }
            }

            bool flag = true;

            ObjectId textStyleId = ObjectId.Null;

            using (Transaction tr = db.TransactionManager.StartTransaction())
                using (TextStyleTable tt = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead))
                {
                    if (tt.Has(TextStyleName))
                    {
                        textStyleId = tt[TextStyleName];
                    }
                    tr.Commit();
                }
            Matrix3d ucs2wcs = AcadUtility.AcadGraphics.UcsToWcs;

            while (flag)
            {
                PromptPointResult pointRes = null;
                if (AutoNumbering)
                {
                    PromptPointOptions pointOpts = new PromptPointOptions("\n" + CurrentNumber.ToString() + ". Koordinat yeri: [Reset/Liste/Seç]", "Reset List Select");
                    pointRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(pointOpts);
                }
                else
                {
                    PromptPointOptions pointOpts = new PromptPointOptions("\nKoordinat yeri: [Reset/Seç]", "Reset Select");
                    pointRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(pointOpts);
                }

                if (pointRes.Status == PromptStatus.Cancel)
                {
                    return;
                }
                else if (pointRes.Status == PromptStatus.Keyword && pointRes.StringResult == "Reset")
                {
                    Reset();
                    return;
                }
                else if (pointRes.Status == PromptStatus.Keyword && pointRes.StringResult == "List")
                {
                    PromptPointOptions listOpts = new PromptPointOptions("\nListe yeri: ");
                    PromptPointResult  listRes  = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(listOpts);
                    ShowList(listRes.Value);
                    return;
                }
                else if (pointRes.Status == PromptStatus.Keyword && pointRes.StringResult == "Select")
                {
                    using (SelectObjectsForm form = new SelectObjectsForm())
                    {
                        if (Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form) == System.Windows.Forms.DialogResult.OK)
                        {
                            // Select objects
                            List <TypedValue> tvs = new List <TypedValue>();
                            switch (form.SelectObjects)
                            {
                            case SelectObjectsForm.SelectCoordinateObjects.Polyline:
                                tvs.Add(new TypedValue((int)DxfCode.Operator, "<OR"));
                                tvs.Add(new TypedValue((int)DxfCode.Start, "LWPOLYLINE"));
                                tvs.Add(new TypedValue((int)DxfCode.Start, "POLYLINE"));
                                tvs.Add(new TypedValue((int)DxfCode.Operator, "OR>"));
                                break;

                            case SelectObjectsForm.SelectCoordinateObjects.Circle:
                                tvs.Add(new TypedValue((int)DxfCode.Operator, "<OR"));
                                tvs.Add(new TypedValue((int)DxfCode.Start, "CIRCLE"));
                                tvs.Add(new TypedValue((int)DxfCode.Start, "ARC"));
                                tvs.Add(new TypedValue((int)DxfCode.Start, "ELLIPSE"));
                                tvs.Add(new TypedValue((int)DxfCode.Operator, "OR>"));
                                break;

                            case SelectObjectsForm.SelectCoordinateObjects.Block:
                                tvs.Add(new TypedValue((int)DxfCode.Start, "INSERT"));
                                break;

                            case SelectObjectsForm.SelectCoordinateObjects.Point:
                                tvs.Add(new TypedValue((int)DxfCode.Start, "POINT"));
                                break;

                            case SelectObjectsForm.SelectCoordinateObjects.Line:
                                tvs.Add(new TypedValue((int)DxfCode.Start, "LINE"));
                                break;
                            }
                            SelectionFilter filter = new SelectionFilter(tvs.ToArray());

                            PromptSelectionResult selRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetSelection(filter);
                            if (selRes.Status == PromptStatus.OK)
                            {
                                using (Transaction tr = db.TransactionManager.StartTransaction())
                                    using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))
                                    {
                                        TextPositioner positioner = new TextPositioner(ucs2wcs, TextRotation * Math.PI / 180, LineLength);

                                        // Read object coordinates
                                        List <TextPositioner.SelectedObjectCoordinate> objectPoints = new List <TextPositioner.SelectedObjectCoordinate>();
                                        foreach (ObjectId id in selRes.Value.GetObjectIds())
                                        {
                                            if (id.ObjectClass == Autodesk.AutoCAD.Runtime.RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)))
                                            {
                                                Autodesk.AutoCAD.DatabaseServices.Polyline obj = tr.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Polyline;

                                                positioner.ClearPoints();
                                                for (int i = 0; i < obj.NumberOfVertices; i++)
                                                {
                                                    positioner.AddPoint(obj.GetPoint3dAt(i));
                                                }
                                                objectPoints.AddRange(positioner.GetPositions());
                                            }
                                            else if (id.ObjectClass == Autodesk.AutoCAD.Runtime.RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Circle)))
                                            {
                                                Circle obj = tr.GetObject(id, OpenMode.ForRead) as Circle;
                                                objectPoints.Add(positioner.GetPosition(obj.Center));
                                            }
                                            else if (id.ObjectClass == Autodesk.AutoCAD.Runtime.RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Arc)))
                                            {
                                                Arc obj = tr.GetObject(id, OpenMode.ForRead) as Arc;
                                                objectPoints.Add(positioner.GetPosition(obj.Center));
                                            }
                                            else if (id.ObjectClass == Autodesk.AutoCAD.Runtime.RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Ellipse)))
                                            {
                                                Ellipse obj = tr.GetObject(id, OpenMode.ForRead) as Ellipse;
                                                objectPoints.Add(positioner.GetPosition(obj.Center));
                                            }
                                            else if (id.ObjectClass == Autodesk.AutoCAD.Runtime.RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.BlockReference)))
                                            {
                                                BlockReference obj = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
                                                objectPoints.Add(positioner.GetPosition(obj.Position));
                                            }
                                            else if (id.ObjectClass == Autodesk.AutoCAD.Runtime.RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.DBPoint)))
                                            {
                                                DBPoint obj = tr.GetObject(id, OpenMode.ForRead) as DBPoint;
                                                objectPoints.Add(positioner.GetPosition(obj.Position));
                                            }
                                            else if (id.ObjectClass == Autodesk.AutoCAD.Runtime.RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Line)))
                                            {
                                                Line obj = tr.GetObject(id, OpenMode.ForRead) as Line;
                                                positioner.ClearPoints();
                                                positioner.AddPoint(obj.StartPoint);
                                                positioner.AddPoint(obj.EndPoint);
                                                objectPoints.AddRange(positioner.GetPositions());
                                            }
                                        }
                                        // Sort coordinates
                                        objectPoints.Sort((p1, p2) =>
                                        {
                                            switch (form.Ordering)
                                            {
                                            case SelectObjectsForm.CoordinateOrdering.IncreasingX:
                                                return(p1.BasePoint.X < p2.BasePoint.X ? -1 : 1);

                                            case SelectObjectsForm.CoordinateOrdering.IncreasingY:
                                                return(p1.BasePoint.Y < p2.BasePoint.Y ? -1 : 1);

                                            case SelectObjectsForm.CoordinateOrdering.DecreasingX:
                                                return(p1.BasePoint.X > p2.BasePoint.X ? -1 : 1);

                                            case SelectObjectsForm.CoordinateOrdering.DecreasingY:
                                                return(p1.BasePoint.Y > p2.BasePoint.Y ? -1 : 1);

                                            default:
                                                return(0);
                                            }
                                        });
                                        // Write coordinates
                                        foreach (TextPositioner.SelectedObjectCoordinate coord in objectPoints)
                                        {
                                            MText mtext = CreateText(textStyleId, coord.TextPoint);
                                            btr.AppendEntity(mtext);
                                            tr.AddNewlyCreatedDBObject(mtext, true);

                                            // Rotate text
                                            if (coord.TextToLeft)
                                            {
                                                mtext.Attachment = (AutoNumbering ? AttachmentPoint.BottomRight : AttachmentPoint.MiddleRight);
                                            }
                                            else
                                            {
                                                mtext.Attachment = (AutoNumbering ? AttachmentPoint.BottomLeft : AttachmentPoint.MiddleLeft);
                                            }

                                            Line line = new Line();
                                            line.StartPoint = coord.BasePoint;
                                            line.EndPoint   = coord.TextPoint;
                                            btr.AppendEntity(line);
                                            tr.AddNewlyCreatedDBObject(line, true);

                                            points.Add(new CoordPoint(CurrentNumber, coord.BasePoint));
                                            if (AutoNumbering)
                                            {
                                                CurrentNumber = CurrentNumber + 1;
                                            }
                                        }

                                        tr.Commit();
                                    }
                            }
                        }
                    }
                    return;
                }
                else
                {
                    Point3d pCoord = pointRes.Value.TransformBy(ucs2wcs);

                    using (Transaction tr = db.TransactionManager.StartTransaction())
                        using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))
                        {
                            MText mtext = CreateText(textStyleId, pCoord);

                            btr.AppendEntity(mtext);
                            tr.AddNewlyCreatedDBObject(mtext, true);

                            if (CoordinateJig.Jig(pointRes.Value, mtext, AutoLine, LineLength, AutoRotateText, TextRotation))
                            {
                                points.Add(new CoordPoint(CurrentNumber, pCoord));
                                if (AutoNumbering)
                                {
                                    CurrentNumber = CurrentNumber + 1;
                                }

                                Line line = new Line();
                                line.StartPoint = pCoord;
                                line.EndPoint   = mtext.Location;

                                btr.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                tr.Commit();
                            }
                            else
                            {
                                mtext.Dispose();
                                tr.Abort();
                                return;
                            }
                        }
                }
            }
        }