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ị

Cám ơn Tue_NV đã chỉ điểm vị trí để tham khảo hàm RPN_Cal .

Gia_Bach xem lại giúp, mình kg vào link này download here đc.

Gia_Bach có thể biên dịch nó thành dll để sử dụng chung trong windows được kg

Gửi lại mã nguồn C# và dll hàm Lisp download here

  • Vote tăng 2

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

Cám ơn Gia_Bach, mình đã dơn đc nhưng sd có chút trục trặc, nhờ bạn hướng dẫn giúp.

Mình mở cad2006, đánh lệnh netload sau đó chọn file RPN_Calculator.dll.

Xong rồi thử và kq như sau:

Command: (RPN_cal "3^2 +abs(-1)") ; error: no function definition: RPN_CAL

Có thể mình sd kg đúng.

 

Bạn cho mình hỏi thêm, nếu gọi hàm theo kiểu liên kết động có đc kg và hàm RPN_CAL có dạng đấy đủ như thế nào (ý mình muốn hỏi kiều của hàm và các đối số của nó). Mính muốn gọi nó trong Delphi và VC++. Cám ơn bạ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

Cám ơn Gia_Bach, mình đã dơn đc nhưng sd có chút trục trặc, nhờ bạn hướng dẫn giúp.

Mình mở cad2006, đánh lệnh netload sau đó chọn file RPN_Calculator.dll.

Xong rồi thử và kq như sau:

Command: (RPN_cal "3^2 +abs(-1)") ; error: no function definition: RPN_CAL

Có thể mình sd kg đúng.

 

Bạn cho mình hỏi thêm, nếu gọi hàm theo kiểu liên kết động có đc kg và hàm RPN_CAL có dạng đấy đủ như thế nào (ý mình muốn hỏi kiều của hàm và các đối số của nó). Mính muốn gọi nó trong Delphi và VC++. Cám ơn bạn

- Có thể Cad2006 chưa hỗ trợ tốt Netload.

 

Các thông tin về đối số của hàm ReversePolishNotation bạn xem trong code đã đính kèm. (file ReversePolishNotation.cs)

Có 2 hàm cần chú ý : ParseEvaluate

 

Tham khảo cách dùng cho VB (nhớ add reference cho dll của ReversePolishNotation)

dll của ReversePolishNotation http://www.cadviet.c...ishnotation.zip

<LispFunction("RPN_CalVB")> _
Public Sub RPN_CalVB(ByVal rbArgs As ResultBuffer)
   	If Not rbArgs = Nothing Then
           Dim args As TypedValue() = rbArgs.AsArray
       	Dim bieuthuc As String = args(0).Value.ToString()
       	Dim res As Double = 0.0
       	Dim rpn As MathText.ReversePolishNotation = New MathText.ReversePolishNotation()
       	Try
           	rpn.Parse(bieuthuc)
           	res = rpn.Evaluate()
       	Catch ex As Exception
           	Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("System error: " + ex.Message)
       	End Try
       	Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbLf & "Kết quả: " & res)
   	End If
End Sub

  • Vote tăng 2

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

Cám ơn gia_bach.

đúng là cad2006 có vđ. mình thử trên cad2008 đã chạy tốt.

Còn việc đọc code thì mình đã đọc, tuy nhiên kg phải là tín đồ của C# nên chẳng hiểu gì. Nó khác nhiều so với lập trình trên Delphi nên ... sau này hãy tính. giờ tạm dùng hàm RPN_cal thôi

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 cho mình hỏi thêm, nếu gọi hàm theo kiểu liên kết động có đc kg và hàm RPN_CAL có dạng đấy đủ như thế nào (ý mình muốn hỏi kiều của hàm và các đối số của nó). Mính muốn gọi nó trong Delphi và VC++. Cám ơn bạn

Ví dụ gọi hàm theo kiểu liên kết động trong VC++ (sử dụng thư viện ReversePolishNotation.dll)

1. Add reference cho dll của ReversePolishNotation) file ReversePolishNotation.dll

2. Khai báo biến và tính toán:

String^ bieuthuc; // biếu thức cần tính toán

double result; // kết quả

MathText::ReversePolishNotation^ rpn = gcnew MathText::ReversePolishNotation(); // Khởi tạo hàm tính biểu thức

 

bieuthuc = "3^2 +abs(-1)"; // gán giá trị cho biểu thức

rpn->Parse(bieuthuc); //

result = rpn->Evaluate(); // gán kết quả vào biến result

  • Vote tăng 1
  • Vote giảm 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

bây giờ nếu e muốn hiện các form trong autocad thì code như thế nào ạ?

ví dụ e tạo form1 với các nút điều khiển trên đó (button, textbox,...), sau đó e dùng lệnh netload trong cad(ví dụ là lệnh mycommand)

và e muốn Cad hiện lên form1. nhưng e không biết đoạn code đó như thế nào, mong các bác giúp đỡ

e dung C# VB2008, autocad2010

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ây giờ nếu e muốn hiện các form trong autocad thì code như thế nào ạ?

ví dụ e tạo form1 với các nút điều khiển trên đó (button, textbox,...), sau đó e dùng lệnh netload trong cad(ví dụ là lệnh mycommand)

và e muốn Cad hiện lên form1. nhưng e không biết đoạn code đó như thế nào, mong các bác giúp đỡ

e dung C# VB2008, autocad2010

Bạn tạ0 1 Form bình thuờng bằng cách Add Form, sau đó gọi lệnh ShowModalDialog(Form) để hiển thị nó.

Tham khảo :

http://www.cadviet.com/forum/topic/31160-huong-dan-lap-trinh-voi-autocad-bang-vb-net/page-3?do=findComment&comment=134874

http://www.cadviet.com/forum/topic/73583-ai-giup-minh-lay-gia-tri-trong-form-voi/?do=findComment&comment=241404

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 1 ứng dụng AutoCAD .NET với MS Visual C#

 

Tạo Project :

1. Khởi động Microsoft Visual C#

2. Chọn menu : FILE → “New Project ...”. Hộp thoại New Project xuất hiện :

tut01.jpg

3. Chọn template “Class Lybrary” như trong hình và nhập tên vào mục Name, trong ví dụ này tôi để mặc định là : ClassLibrary1.

4. Một Project mới đuợc tạo và giao diện như sau : C# tạo 1 file có tên Class1.cs mà ta sẽ sử dụng ở buớc viết Code.

tut02.jpg

 

 

Khai báo thư viện (Reference):

1. Tại cửa sổ Solution Explorer bên phải phía trên, chọn tag Reference

tut03.jpg

2. Click chuột phải lên tag Reference rồi chọn “Add Reference” trên menu xổ xuống . Hộp thoại “Add Reference” xuất hiện :

tut04.jpg

3. Chọn tag “ Browse” như trong hình và chỉ đến thư mục cài đặt AutoCad, ví dụ : C:\Program Files\AutoCAD 2010.

 

4. chọn 2 file : "acmgd.dll" và "acdbmgd.dll" , click OK để kết thúc.

5. Đặt thuộc tính “Copy Local” : Tại cửa sổ Solution Explorer, tag Reference click chuột phải lên file "acmgd.dll" chọn Properties : trong cửa sổ Properties, đặt thuộc tính “Copy Local” là False.

Làm tuơng tự với file "acdbmgd.dll"

tut05.jpg

 

Tạo lệnh Cad (viết code):

Để tạo 1 lệnh mới, sử dụng thuộc tính CommandMethod

ví dụ : [CommandMethod("CadViet")]

 

Ví dụ 1 : khai báo lệnh CadViet1 thực hiện việc xuất ra màn hình thông báo

“Chào mừng bạn đến với ứng dụng AutoCAD .NET API”

 

Ví dụ 2 : khai báo lệnh CadViet2 thực hiện việc tạo 1 đối tuợng TEXT có nội dung :

“Chào mừng bạn đến với ứng dụng AutoCAD .NET API”, tại vị trí do nguời dùng chọn.

 

Bạn Copy và Paste toàn bộ đọan code sau vào file Class1.cs

 

using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace Gia_Bach
{
public class Cad_command
{
[CommandMethod("CadViet1")]
public static void ex1()
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Chào mừng bạn đến với ứng dụng AutoCAD .NET API!");
}

[CommandMethod("CadViet2")]
public static void ex2()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocume
nt;
Editor ed = doc.Editor;
Database db = doc.Database;

PromptPointResult Pr = ed.GetPoint("\nChọn điểm đặt Text : ");
Point3d pt = Pr.Value;

if (Pr.Status != PromptStatus.OK) return;

// Start a transaction
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec
= acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

// Create a single-line text object
DBText acText = new DBText();
acText.SetDatabaseDefaults();
acText.Position = pt;
acText.Height = 0.5;
acText.TextString = "Chào mừng bạn đến với ứng dụng AutoCAD .NET API!";

acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
// Save the changes and dispose of the transaction
acTrans.Commit();
}
}
}
}

Lưu Project :

Chọn menu : FILE → “Save All ...”. Hộp thoại Save Project xuất hiện, chọn đuờng dẫn thích hợp, click Save để kết thúc.

 

 

Build Project :

Chọn menu : BUILD → “Build Solution” : để tạo 1 ứng dụng.

Trong truờng hợp BUILD thành công, bạn sẽ có 1 file dll trùng tên với tên Project tại đuờng dẫn :

- “ProjectName\bin\Release\ projectName.dll “

trong đó ProjectName là tên Project bạn đã đặt ở buớc Lưu Project

 

 

Thực thi lệnh : (xem thành quả)

1. Khởi động AutoCAD

2. Tại dấu nhắc (Command ) của CAD gõ lệnh NETLOAD và chỉ đến vị trí file vừa BUILD ở trên, chọn OPEN để hoàn thành lệnh NETLOAD .

3. gõ lệnh CadViet1 hay CadViet2 để bắt đầu

 

Chúc bạn thành công.

Chao anh gia bach.

 

Em moi tim hieu .net co lam theo huong dan cua anh nhưng lúc add 2 .dll nó báo lỗi không thấy file nên không build được, anh và mọi người có thể chỉ em fix lỗi này?

 

Warning 1 The referenced component 'acmgd' could not be found. 

Warning 2 The referenced component 'acdbmgd' could not be found.
 

 

Em dùng ACad2015 và VS2008, Win 7 x64

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 9/14/2016 tại 22:13, Bee đã nói:

Chao anh gia bach.

 

Em moi tim hieu .net co lam theo huong dan cua anh nhưng lúc add 2 .dll nó báo lỗi không thấy file nên không build được, anh và mọi người có thể chỉ em fix lỗi này?

 

Warning 1 The referenced component 'acmgd' could not be found. 

Warning 2 The referenced component 'acdbmgd' could not be found.
 

 

Em dùng ACad2015 và VS2008, Win 7 x64

Với AutoCad 2015 bạn phải sử dụng Visual Studio 2012 (hoặc mới hơn VS 2013-2015) và Net Framework 4.5

 

Và từ AutoCAD 2013 chúng ta phải add thêm file AcCoreMgd.dll (dĩ nhiên cũng phải add 2 file AcMgd.dll và AcDbMgd.dll)

 

Tham khảo : 

AutoCAD_NET.png.cd105787272c6e4355eb4eed2012fe70.png

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ới AutoCad 2015 bạn phải sử dụng Visual Studio 2012 (hoặc mới hơn VS 2013-2015) và Net Framework 4.5

 

Và từ AutoCAD 2013 chúng ta phải add thêm file AcCoreMgd.dll (dĩ nhiên cũng phải add 2 file AcMgd.dll và AcDbMgd.dll)

 

Tham khảo : 

ACADvsVisualStudio.png

Oài sau khi cài VS 2012 là build ngon lành cành đào. 

 

Cám ơn anh gia bách nhiều.

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
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace Gia_Bach
{
public class Cad_command
{
[CommandMethod("CadViet1")]
public static void ex1()
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Chào mừng bạn đến với ứng dụng AutoCAD .NET API!");
}

[CommandMethod("CadViet2")]
public static void ex2()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocume
nt;
Editor ed = doc.Editor;
Database db = doc.Database;

PromptPointResult Pr = ed.GetPoint("\nChọn điểm đặt Text : ");
Point3d pt = Pr.Value;

if (Pr.Status != PromptStatus.OK) return;

// Start a transaction
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec
= acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

// Create a single-line text object
DBText acText = new DBText();
acText.SetDatabaseDefaults();
acText.Position = pt;
acText.Height = 0.5;
acText.TextString = "Chào mừng bạn đến với ứng dụng AutoCAD .NET API!";

acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
// Save the changes and dispose of the transaction
acTrans.Commit();
}
}
}
}

Chào anh gia bach và mọi người.

Anh (hoặc bạn nào)có thể chuyển ngữ đoạn lisp này sang C# để em hiểu cách thức làm việc từ lisp sang C#. Xin cám ơn.

(defun c:test (/ ss n pt str h att old_os num)
  (setq old_os (getvar 'osmode))
  ;ssget and repeat
  (if (setq ss (ssget '((0 . "*TEXT"))))
    (progn
      (setq n 0)
      (repeat (sslength ss)

	(setvar 'osmode 0)
	
	(setq num (getreal "\Chon so: "));number
	
	(setq pt (cdr (assoc 10 (entget (ssname ss n)))))
	(setq str (cdr (assoc 1 (entget (ssname ss n)))))
	(setq h (cdr (assoc 40 (entget (ssname ss n)))))
	(mapcar 'print (list pt str h)); print
	
	;change value text
	(entmod (subst (cons 1 (rtos num 2 2)) (assoc 1 (entget (ssname ss n))) (entget (ssname ss n))))
	
	(setq n (1+ n))
	);repeat
      )
    (princ "\nBan da khong chon doi tuong")
    );if
  
  ;;;While and nentsel
  (while (setq att (car (nentsel "\nChon att: ")))
    (progn
      (change_w att)
      )    
    )
  (setvar "osmode" old_os)
  (princ)
  )

(defun change_w (att / w_att w)
  (setq w_att (vla-get-ScaleFactor (vlax-ename->vla-object att)))
  (if (setq w (getreal (strcat "\nChon withfactor <" (rtos w_att 2 2) ">: ")))
    (vla-put-ScaleFactor (vlax-ename->vla-object att) w)
    )
  )  

(princ "\nCommand: test")
  

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 nên tách ra thành 2 lisp riêng biệt. Và ghi công dụng của từng Lisp.

Nếu có t/gian tôi sẽ convert sang C#.

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 nên tách ra thành 2 lisp riêng biệt. Và ghi công dụng của từng Lisp.

Nếu có t/gian tôi sẽ convert sang C#.

Chào anh.

 

Lisp này chỉ là ví dụ linh tinh. Em có đọc hết các hàm riêng lẻ của C# rồi, vấn đề là chưa biết kết hợp các hàm lẻ với nhau như trong lisp hay ActiveX.

VD:

1. Lisp dùng ssget tạo 1 selectionset của đối tượng TEXT or MTEXT sau đó chỉnh sửa value từng text bằng vòng lặp repeat với những số nhập vào từ dòng command.

2. Lisp chỉnh sửa độ rộng của attribute(tạo select bằng entsel hay là nentsel)  trong 1 block với vòng lặp while.

3. Tạo 1 function như (change_att) và call nó như 1 function của lisp.

4. Print dòng lệnh ở cuối lisp để nhớ được tên lệnh từng lisp riêng vì có quá nhiều lisp.

 

Mong được học hỏi thêm từ anh và mọi người.

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

Chào anh.

 

Lisp này chỉ là ví dụ linh tinh. Em có đọc hết các hàm riêng lẻ của C# rồi, vấn đề là chưa biết kết hợp các hàm lẻ với nhau như trong lisp hay ActiveX.

VD:

1. Lisp dùng ssget tạo 1 selectionset của đối tượng TEXT or MTEXT sau đó chỉnh sửa value từng text bằng vòng lặp repeat với những số nhập vào từ dòng command.

2. Lisp chỉnh sửa độ rộng của attribute(tạo select bằng entsel hay là nentsel)  trong 1 block với vòng lặp while.

3. Tạo 1 function như (change_att) và call nó như 1 function của lisp.

4. Print dòng lệnh ở cuối lisp để nhớ được tên lệnh từng lisp riêng vì có quá nhiều lisp.

 

Mong được học hỏi thêm từ anh và mọi người.

Convert lisp to C# theo yêu cầu.

Bee test thử nhé, (sorry anh chưa có th/gian test):

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace Lisp2Csharp
{
    public class CadCmds
    {
        [CommandMethod("ChangeTextContent")]
        public static void ChangeTextContent()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            TypedValue[] filter = new TypedValue[1] { new TypedValue(0, "*TEXT") };
            PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
            if (psr.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nBan da khong chon doi tuong.");
                return;
            }
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptDoubleOptions pdo = new PromptDoubleOptions("\nNhập số: ");
                PromptDoubleResult res = ed.GetDouble(pdo);
                if (res.Status != PromptStatus.OK) return;

                double num = res.Value;
                string numStr = num.ToString("F2");
                foreach (SelectedObject so in psr.Value)
                {
                    Entity ent = (Entity)tr.GetObject(so.ObjectId, OpenMode.ForRead);
                    if (ent is DBText)
                    {
                        DBText txt = ent as DBText;
                        txt.UpgradeOpen();
                        txt.TextString = numStr;
                    }
                    else if (ent is MText)
                    {
                        MText mtxt = ent as MText;
                        mtxt.UpgradeOpen();
                        mtxt.Contents = numStr;
                    }
                }
                tr.Commit();
            }
        }


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

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nChon att:");
                PromptNestedEntityResult res;
                do
                {
                    res = ed.GetNestedEntity(pno);
                    if (res.Status == PromptStatus.OK)
                    {
                        if (res.ObjectId.ObjectClass.DxfName.ToUpper() == "ATTRIB")
                        {
                            change_w(ed, res.ObjectId);
                        }
                    }

                    db.TransactionManager.QueueForGraphicsFlush();
                }
                while (res.Status == PromptStatus.OK);
                tr.Commit();
            }
        }

        static public void change_w(Editor ed, ObjectId attId)
        {
            using (Transaction tr = attId.Database.TransactionManager.StartTransaction())
            {
                PromptDoubleOptions pdo = new PromptDoubleOptions("\nNhập withfactor: ");
                PromptDoubleResult pdr = ed.GetDouble(pdo);
                if (pdr.Status != PromptStatus.OK) return;
                double withFactor = pdr.Value;

                AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
                att.WidthFactor = withFactor;
                tr.Commit();
            }
        }
    }

    public class ShowCommands : IExtensionApplication
    {
        void IExtensionApplication.Initialize()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nCommand : ChangeTextContent, ChangeAtt \n");
        }

        void IExtensionApplication.Terminate()
        {
        }
    }
}

Chỉnh sửa theo gia_bach

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

 

Convert lisp to C# theo yêu cầu.

Bee test thử nhé, (sorry anh chưa có th/gian test):

 

link download : https://dl.dropboxusercontent.com/u/101018313/Class1.cs

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace Lisp2Csharp
{
    public class CadCmds
    {
        [CommandMethod("ChangeTextContent")]
        public static void ChangeTextContent()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            TypedValue[] filter = new TypedValue[1] { new TypedValue(0, "*TEXT") };
            PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
            if (psr.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nBan da khong chon doi tuong.");
                return;
            }
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptDoubleOptions pdo = new PromptDoubleOptions("\nNhập số: ");
                PromptDoubleResult res = ed.GetDouble(pdo);
                if (res.Status != PromptStatus.OK) return;

                double num = res.Value;
                string numStr = num.ToString("F2");
                foreach (SelectedObject so in psr.Value)
                {
                    Entity ent = (Entity)tr.GetObject(so.ObjectId, OpenMode.ForRead);
                    if (ent is DBText)
                    {
                        DBText txt = ent as DBText;
                        txt.UpgradeOpen();
                        txt.TextString = numStr;
                    }
                    else if (ent is MText)
                    {
                        MText mtxt = ent as MText;
                        mtxt.UpgradeOpen();
                        mtxt.Contents = numStr;
                    }
                }
                tr.Commit();
            }
        }


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

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nChon att:");
                PromptNestedEntityResult res;
                do
                {
                    res = ed.GetNestedEntity(pno);
                    if (res.Status == PromptStatus.OK)
                    {
                        if (res.ObjectId.ObjectClass.DxfName.ToUpper() == "ATTRIB")
                        {
                            change_w(ed, res.ObjectId);
                        }
                    }

                    db.TransactionManager.QueueForGraphicsFlush();
                }
                while (res.Status == PromptStatus.OK);
                tr.Commit();
            }
        }

        static public void change_w(Editor ed, ObjectId attId)
        {
            using (Transaction tr = attId.Database.TransactionManager.StartTransaction())
            {
                PromptDoubleOptions pdo = new PromptDoubleOptions("\nNhập withfactor: ");
                PromptDoubleResult pdr = ed.GetDouble(pdo);
                if (pdr.Status != PromptStatus.OK) return;
                double withFactor = pdr.Value;

                AttributeDefinition att = (AttributeDefinition)tr.GetObject(attId, OpenMode.ForWrite);
                att.WidthFactor = withFactor;
                tr.Commit();
            }
        }
    }

    public class ShowCommands : IExtensionApplication
    {
        void IExtensionApplication.Initialize()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nCommand : ChangeTextContent, ChangeAtt \n");
        }

        void IExtensionApplication.Terminate()
        {
        }
    }
}

Chào anh,

 

Em vừa test thử thì lệnh changeatt lỗi, anh xem fix lại được lỗi này không a.

 

Anh có thể hướng dẫn thêm build 1 file exe không ạ ?

 

Cám ơn anh nhiều.

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 7/1/2019 tại 11:01, nguyenla đã nói:

Nhờ bạn hướng dẫn cách tạo ra file dxf từ C#. Ví dụ như tạo 1 đường thẳng hay tròn, 

Cảm ơn bạn

Sử dụng Database.DxfOut để xuất ra dxf file.

        [CommandMethod("DXFOUT")]
        public void DxfOut()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptSelectionOptions prOpts = new PromptSelectionOptions();
            prOpts.MessageForAdding = "Select entities for export to DXF: ";
            PromptSelectionResult res = ed.GetSelection(prOpts);
            if (res.Status != PromptStatus.OK || res.Value.Count == 0) return;

            string fileName = "";
            using (var dlg = new System.Windows.Forms.SaveFileDialog())
            {
                dlg.Title = "Select a dxf file";
                dlg.Filter = "Dxf file *.dxf|*.dxf";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    fileName = dlg.FileName;
                }
                else return;
            }

            // Create a new database and copy the selected entities to
            using (ObjectIdCollection objs = new ObjectIdCollection(res.Value.GetObjectIds()))
            {
                using (Database tempDb = db.Wblock(objs, Point3d.Origin))
                {
                    tempDb.DxfOut(fileName, 16, false);
                    // tempDb.DxfOut(dlg.Filename, 16, DwgVersion.AC1027);
                }
            }
        }

 

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

×