Thứ Năm, 9 tháng 5, 2024

AutoCAD dotNet Bài 1.1 Lấy tọa độ người dùng | Editor GetPoint GetLine UCS WCS | AutoCAD dotNet C#

Khi mới làm quen với AutoCAD dotNet, người lập trình sẽ lạ lẫm với các hàm GetUserInput và các khái niệm PromptResult hay PromptOptions


Phải sử dụng như thế nào cho đúng?
Thông tin thêm: 👉👉👉

Khởi tạo Project mới trong VS

Để sử dụng, hãy tạo mới 1 Solution và 1 Project mới có tên GetUserInput_by_AJS



Thêm code c#



1 Thêm class EditorExtensions.cs

(Copy nội dung sau)
Code:
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.Diagnostics;

namespace GetUserInput_by_AJS
{
    public static class EditorExtensions
    {
        public static PromptStatus GetResult = PromptStatus.None;

        public static Point3d GetPointWCS(this Editor ed, string msg = "\nSpecify a point", bool allowNone = true)
        {
            var ppo = new PromptPointOptions(msg);
            ppo.AllowNone = allowNone;

            var psr = ed.GetPoint(ppo);
            GetResult = psr.Status;

            //Example by www.lisp.vn
            return psr.Status == PromptStatus.OK ? psr.Value.ToWCS() : Point3d.Origin;
        }

        public static ObjectId ToDatabase(this Entity e)
        {
            ObjectId retId = ObjectId.Null;
            var db = HostApplicationServices.WorkingDatabase;
            using (var tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                retId = btr.AppendEntity(e);
                tr.AddNewlyCreatedDBObject(e, true);
                tr.Commit();
            }
            //Example by www.lisp.vn
            return retId;
        }

        public static Line GetLine(this Editor ed)
        {
            var ppo = new PromptPointOptions("\nSpecify first point");
            ppo.AllowNone = true;
            var ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return null;

            var p_UCS = ppr.Value;

            var p1 = p_UCS.ToWCS();

            ppo.Message = "\nSpecify second point";
            ppo.BasePoint = p1.ToUCS();
            ppo.UseBasePoint = true;

            ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return null;

            var p2 = ppr.Value.ToWCS();

            //Example by www.lisp.vn
            return new Line(p1, p2);
        }

        public static Point3d ToWCS(this Point3d pt)
        {
            //Example by www.lisp.vn
            return pt.TransformBy(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem);
        }

        public static Point3d ToUCS(this Point3d pt)
        {
            //Example by www.lisp.vn
            return pt.TransformBy(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem.Inverse());
        }

        public static void Princ(string msg)
            => Debug.Print(msg);
    }
}




2 Thêm hoặc chỉnh sửa myCommands.cs

(Copy nội dung sau)
Code:
// (C) Copyright 2024 by
//
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(GetUserInput_by_AJS.MyCommands))]

namespace GetUserInput_by_AJS
{
    public partial class MyCommands
    {
        [CommandMethod("GetPointWCS", CommandFlags.Modal)]
        public void GetPointWCS()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            if (doc != null)
            {
                Editor ed = doc.Editor;

                var pt = ed.GetPointWCS();
                if (EditorExtensions.GetResult == PromptStatus.OK)
                {
                    Circle cc = new Circle(pt, Autodesk.AutoCAD.Geometry.Vector3d.ZAxis, 10);
                    cc.ToDatabase();
                }
            }
            //Example by www.lisp.vn
        }

        [CommandMethod("Get2Point", CommandFlags.Modal)]
        public void Get2Point_WCS()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed;
            if (doc != null)
            {
                ed = doc.Editor;
                Line ln = ed.GetLine();
                if (ln != null)
                {
                    ln.ToDatabase();
                }
                return;
            }
            //Example by www.lisp.vn
        }
    }
}


Build và chạy trong AutoCAD

Dùng lệnh Netload để tải tệp tin GetUserInput_by_AJS.dll vừa build.



Chạy các lệnh GetPointWCS và Get2Point

  • Lệnh GetPointWCS có chức năng Lấy tọa độ điểm từ người dùng và vẽ 1 Circle có bán kính = 10 tại vị trí vừa chọn

  • Lệnh Get2Point có chức năng lấy tọa độ 2 điểm từ người dùng và vẽ 1 Line từ 2 điểm vừa chọn


Tham khảo GitHub



---------------------------------------------------------------------------------------------------------

Ứng dụng được phát triển bởi đội ngũ AutoLISP Thật là đơn giản

    

Mọi thông tin xin liên hệ Fanpage AutoLISP Thật là đơn giản!
Cảm ơn bạn đã theo dõi!

Không có nhận xét nào:

Đăng nhận xét

Text bị lệch khi xuất file hoặc jig | Wrong location of text from exporting | AutoCAD dotNet

Ứng dụng được phát triển bởi đội ngũ AutoLISP Thật là đơn giản Thông tin thêm: 👉👉👉