Chuyển đến nội dung
Diễn đàn CADViet
gia_bach

Tạo 1 ứng dụng AutoCAD .NET với MS Visual C#

Các bài được khuyến nghị

Chào các Anh. Các Anh có thể chỉ giúp em viết 1 đoạn code. Tùy biên 2 đoạn thẳng giao nhau không ạ.  VD Đoạn AB = 4m, BC =5m. em muốn thay đổi các thông số này 1 cách linh hoạt AB= m, BC=n. Và sau khi đưa ra man hình thì có luôn dim trong 2 đoạn thẳng được không ạ/ Em cảm ơn

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Bạn tham khảo code này, tuy nó không xuất ra kết quả như bạn yêu cầu nhưng nó có tất cả các hàm để viết.

AddLineDimTextCircle.thumb.png.60bac83459980aea502eeda7e308cc4f.png

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace Gia_Bach
{
    public class Class1
    {
        static double _deltaX = 4000;
        static double _deltaY = 5000;
        const double pi = System.Math.PI;
        const double dimBaseLine = 350;
        const double txtHeight = 175;


        [CommandMethod("test")]
        public static void AddLineDimTextCircle()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                Point3d insPt = new Point3d(6000, 6000, 0);

                Point3d ptX = Polar(insPt, 0, _deltaX);                

                // line by X
                Line line = new Line(insPt, ptX);
                line.ColorIndex = 3;
                AddToDataBase(db, line);

                // dim
                Point3d dimLinePt = Polar(insPt, pi / 2, dimBaseLine);
                AlignedDimension dim = new AlignedDimension(insPt, ptX, dimLinePt, "", db.Dimstyle);
                dim.ColorIndex = 4;
                AddToDataBase(db, dim);

                // text
                Point3d midPt = MidPoint(insPt, ptX);
                Point3d txtInsPt = Polar(midPt, pi / -2, txtHeight * 1.5);
                DBText txt = new DBText();
                txt.Height = txtHeight;
                txt.TextString = "B";
                txt.Position = txtInsPt;
                txt.HorizontalMode = TextHorizontalMode.TextCenter;
                txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                txt.AlignmentPoint = txtInsPt;
                AddToDataBase(db, txt);

                // cicrle
                Circle cir = new Circle(txtInsPt, txt.Normal, txtHeight * 1.0);
                cir.ColorIndex = 1;
                AddToDataBase(db, cir);


                //Point3d ptY = Polar(insPt, pi / -4, _deltaY);
                //// line by Y
                //line = new Line(insPt, ptY);
                //line.ColorIndex = 3;
                //AddToDataBase(db, line);

                tr.Commit();
            }
        }

        public static Point3d Polar(Point3d org, double angle, double distance)
        {
            return new Point3d(
                org.X + (distance * Math.Cos(angle)),
                org.Y + (distance * Math.Sin(angle)),
                org.Z);
        }

        static public Point3d MidPoint(Point3d P1, Point3d P2)
        {
            double x, y, z;
            x = (P1.X + P2.X) * 0.5;
            y = (P1.Y + P2.Y) * 0.5;
            z = (P1.Z + P2.Z) * 0.5;
            return new Point3d(x, y, z);
        }

        public static void AddToDataBase(Database db, Entity ent)
        {
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(ent);
                tr.AddNewlyCreatedDBObject(ent, true);

                tr.Commit();
            }
        }

    }
}

 

  • Like 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác
10 phút trước, gia_bach đã nói:

Bạn tham khảo code này, tuy nó không xuất ra kết quả như bạn yêu cầu nhưng nó có tất cả các hàm để viết.

AddLineDimTextCircle.thumb.png.60bac83459980aea502eeda7e308cc4f.png

 


using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace Gia_Bach
{
    public class Class1
    {
        static double _deltaX = 4000;
        static double _deltaY = 5000;
        const double pi = System.Math.PI;
        const double dimBaseLine = 350;
        const double txtHeight = 175;


        [CommandMethod("test")]
        public static void AddLineDimTextCircle()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                Point3d insPt = new Point3d(6000, 6000, 0);

                Point3d ptX = Polar(insPt, 0, _deltaX);                

                // line by X
                Line line = new Line(insPt, ptX);
                line.ColorIndex = 3;
                AddToDataBase(db, line);

                // dim
                Point3d dimLinePt = Polar(insPt, pi / 2, dimBaseLine);
                AlignedDimension dim = new AlignedDimension(insPt, ptX, dimLinePt, "", db.Dimstyle);
                dim.ColorIndex = 4;
                AddToDataBase(db, dim);

                // text
                Point3d midPt = MidPoint(insPt, ptX);
                Point3d txtInsPt = Polar(midPt, pi / -2, txtHeight * 1.5);
                DBText txt = new DBText();
                txt.Height = txtHeight;
                txt.TextString = "B";
                txt.Position = txtInsPt;
                txt.HorizontalMode = TextHorizontalMode.TextCenter;
                txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                txt.AlignmentPoint = txtInsPt;
                AddToDataBase(db, txt);

                // cicrle
                Circle cir = new Circle(txtInsPt, txt.Normal, txtHeight * 1.0);
                cir.ColorIndex = 1;
                AddToDataBase(db, cir);


                //Point3d ptY = Polar(insPt, pi / -4, _deltaY);
                //// line by Y
                //line = new Line(insPt, ptY);
                //line.ColorIndex = 3;
                //AddToDataBase(db, line);

                tr.Commit();
            }
        }

        public static Point3d Polar(Point3d org, double angle, double distance)
        {
            return new Point3d(
                org.X + (distance * Math.Cos(angle)),
                org.Y + (distance * Math.Sin(angle)),
                org.Z);
        }

        static public Point3d MidPoint(Point3d P1, Point3d P2)
        {
            double x, y, z;
            x = (P1.X + P2.X) * 0.5;
            y = (P1.Y + P2.Y) * 0.5;
            z = (P1.Z + P2.Z) * 0.5;
            return new Point3d(x, y, z);
        }

        public static void AddToDataBase(Database db, Entity ent)
        {
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(ent);
                tr.AddNewlyCreatedDBObject(ent, true);

                tr.Commit();
            }
        }

    }
}

 

Thank Anh. nhưng vẫn chưa đủ để giải quyến ván đề đoạn thằng tùy biến của em ạ. (Chưa thông số). Em mò hòa mà không được mới nhờ mọi người giúp đỡ :((

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác
10 giờ trước, cuongtk2 đã nói:

@gia_bachDùng thừa Transaction vòng ngoài rồi

Không có cũng được, nhưng không thừa đâu!

 

Bổ sung hàm getdouble (nhập số thực) và getpoint (chọn điểm):

 

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

namespace Gia_Bach
{
    public class Class1
    {
        static double _deltaX = 4000;
        static double _deltaY = 5000;
        const double pi = System.Math.PI;
        const double dimBaseLine = 350;
        const double txtHeight = 175;


        [CommandMethod("test")]
        public static void AddLineDimTextCircle()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                PromptDoubleOptions pdo = new PromptDoubleOptions("\nNhập chiều dài phương ngang: ");
                pdo.DefaultValue = _deltaX;
                PromptDoubleResult res = ed.GetDouble(pdo);
                if (res.Status != PromptStatus.OK)
                    return;
                _deltaX = res.Value;


                PromptPointResult ppr= ed.GetPoint("\nĐiểm chèn:");
                if (ppr.Status != PromptStatus.OK)
                    return;
                Point3d insPt = ppr.Value;


                // horizontal
                Point3d ptX = Polar(insPt, 0, _deltaX);
                
                // line  
                Line line = new Line(insPt, ptX);
                line.ColorIndex = 3;
                AddToDataBase(db, line);

                // dim
                Point3d dimLinePt = Polar(insPt, pi / 2, dimBaseLine);
                AlignedDimension dim = new AlignedDimension(insPt, ptX, dimLinePt, "", db.Dimstyle);
                dim.ColorIndex = 4;
                AddToDataBase(db, dim);

                // text
                Point3d midPt = MidPoint(insPt, ptX);
                Point3d txtInsPt = Polar(midPt, pi / -2, txtHeight * 1.5);
                DBText txt = new DBText();
                txt.Height = txtHeight;
                txt.TextString = "B";
                txt.Position = txtInsPt;
                txt.HorizontalMode = TextHorizontalMode.TextCenter;
                txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                txt.AlignmentPoint = txtInsPt;
                AddToDataBase(db, txt);

                // cicrle
                Circle cir = new Circle(txtInsPt, txt.Normal, txtHeight * 1.0);
                cir.ColorIndex = 1;
                AddToDataBase(db, cir);


                //Point3d ptY = Polar(insPt, pi / -4, _deltaY);
                //// line by Y
                //line = new Line(insPt, ptY);
                //line.ColorIndex = 3;
                //AddToDataBase(db, line);

                tr.Commit();
            }
        }

        public static Point3d Polar(Point3d org, double angle, double distance)
        {
            return new Point3d(
                org.X + (distance * Math.Cos(angle)),
                org.Y + (distance * Math.Sin(angle)),
                org.Z);
        }

        static public Point3d MidPoint(Point3d P1, Point3d P2)
        {
            double x, y, z;
            x = (P1.X + P2.X) * 0.5;
            y = (P1.Y + P2.Y) * 0.5;
            z = (P1.Z + P2.Z) * 0.5;
            return new Point3d(x, y, z);
        }

        public static void AddToDataBase(Database db, Entity ent)
        {
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(ent);
                tr.AddNewlyCreatedDBObject(ent, true);

                tr.Commit();
            }
        }

    }
}

 

  • Like 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Build successed rồi anh ạ.

Em đã gõ lệnh CADVIET1 và đã chạy thành công. :lol:

Nhưng gõ lệnh CADVIET2 thì sau khi Chọn điểm đặt Text thì nó không có ra kết quả chi cả. Anh có thể xem lại chổ này 1 chút được không? :lol:

 

Em đã bắt đầu chào "AUTOCAD .NET API" và "HELLO WORLD" rồi anh ạ. Hy vọng sẽ tạo ra những ứng dụng thật sự hiệu quả.

Em chỉ có thắc mắc chổ ý thứ 3 ở bài viết trên của anh 1 tý ạ: Khi đã mở file class1.cs , vì nó báo lỗi, nên phải sửa lại những chổ bị lỗi rồi phải Build Solution lại nhưng em chẳng thấy menu Build ở đâu? để Build Solution và tạo ra file .dll. Anh có thể nói rõ hơn ý thứ 3 ở bài viết trên của anh được không? Em đã sửa lỗi rồi mà chẳng thấy menu Build đâu anh ạ??| day rut nhuaCảm ơn anh rất nhiều :lol:

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác
Vào lúc 30/8/2021 tại 09:38, cuongtk2 đã nói:

Viết theo kiểu đề phòng thiếu hả?

 

41 phút trước, betion đã nói:

Build successed rồi anh ạ.

Em đã gõ lệnh CADVIET1 và đã chạy thành công. :lol:

Nhưng gõ lệnh CADVIET2 thì sau khi Chọn điểm đặt Text thì nó không có ra kết quả chi cả. Anh có thể xem lại chổ này 1 chút được không? :lol:

 

Em đã bắt đầu chào "AUTOCAD .NET API" và "HELLO WORLD" rồi anh ạ. Hy vọng sẽ tạo ra những ứng dụng thật sự hiệu quả.

Em chỉ có thắc mắc chổ ý thứ 3 ở bài viết trên của anh 1 tý ạ: Khi đã mở file class1.cs , vì nó báo lỗi, nên phải sửa lại những chổ bị lỗi rồi phải Build Solution lại nhưng em chẳng thấy menu Build ở đâu? để Build Solution và tạo ra file .dll. Anh có thể nói rõ hơn ý thứ 3 ở bài viết trên của anh được không? Em đã sửa lỗi rồi mà chẳng thấy menu Build đâu anh ạ??| day rut nhuaCảm ơn anh rất nhiều :lol:

Bác "dây rút nhựa" betion nên chờ version "Viết theo kiểu không đề phòng thiếu hả?" của bác @cuongtk2 thì mới có cơ sở đế so sánh với version "Viết theo kiểu đề phòng thiếu hả?" của bác gia-bach nhé !

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Tạo một tài khoản hoặc đăng nhập để nhận xét

Bạn cần phải là một thành viên để lại một bình luận

Tạo tài khoản

Đăng ký một tài khoản mới trong cộng đồng của chúng tôi. Điều đó dễ mà.

Đăng ký tài khoản mới

Đăng nhập

Bạn có sẵn sàng để tạo một tài khoản ? Đăng nhập tại đây.

Đăng nhập ngay

×