Jump to content

Điều khiển AutoCAD bằng VC++ (ObjectARX)

Hôm nay, tự nhiên thắc mắc vì sao mình sử dụng ARX làm gì cho phức tạp nhỉ? Do nghe người ta đồn nó chạy nhanh lắm??? :P
OK, vậy làm 1 cái test thử xem nó chạy nhanh đến mức nào và có đáng để "gặm" tiếp ko.
Do chỉ biết có vài thứ nên biết bao nhiêu xài bấy nhiêu.
yêu cầu test là tạo ra 10000 (mười ngàn) CIRCLE có bán kính R=5 tại điểm O(0,0,0)
Sau đây là kết quả test


Và đây là các "test thủ" lưu ý: trong code .NET có phần tính thời gian và tất cả các command trên đều dùng đoạn code đó để tính thời gian thực hiện.

code lisp:

Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.

Cách dùng

  1. Chép mã nguồn, lưu thành tệp 200622_cir_lisp.lsp.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh CIR LISP để chạy lisp.

Mã nguồn 200622_cir_lisp.lsp (trích từ bài viết gốc)

(defun C:Cir_LISP ()
 (setq i 0)
 (while (< i 10000)
   (progn
     (setq i (+ i 1))
     (entmake
'((0 . "CIRCLE")
  (10 0.0 0.0 0.0)
  (40 . 5.0)
 )
     )
   )
 )
)

Public Sub Cir_VBA()
Dim center(2) As Double
Dim r As Double
Dim i As Integer

r = 5
center(0) = 0: center(1) = 0: center(2) = 0
For i = 1 To 10000
   ThisDrawing.ModelSpace.AddCircle center, r
Next

End Sub

AcGeVector3d  vecNormal = AcGeVector3d::kZAxis;  
AcGePoint3d pt(0, 0, 0);
ads_real radius = 5;

AcDbDatabase *db = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTable *bt = NULL;
db->getSymbolTable(bt, AcDb::kForRead);

AcDbBlockTableRecord *btrModel = NULL;
bt->getAt(ACDB_MODEL_SPACE, btrModel,AcDb::kForWrite);
bt->close();
for (int i=0;i<10000;i++)
{
AcDbCircle *acCircle = new AcDbCircle(pt, vecNormal, radius);
btrModel->appendAcDbEntity(acCircle);
acCircle->close();
}
btrModel->close();

double r = 5d;
           Point3d pt = Point3d.Origin;
           Database db = HostApplicationServices.WorkingDatabase;
           using (Transaction tr = db.TransactionManager.StartTransaction())
           {
               BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
               BlockTableRecord btr = tr.GetObject(bt[blockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

               for (int i = 0; i < 10000; i++)
               {
                   using (Circle cir = new Circle())
                   {
                       cir.Center = pt;
                       cir.Radius = r;

                       btr.AppendEntity(cir);
                       tr.AddNewlyCreatedDBObject(cir, true);
                   }
               }

               tr.Commit();
           }

public void Initialize()
       {
           Document acDoc = Application.DocumentManager.MdiActiveDocument;
           acDoc.CommandWillStart += new CommandEventHandler(acDoc_CommandWillStart);
           acDoc.LispWillStart += new LispWillStartEventHandler(acDoc_LispWillStart);
           acDoc.CommandEnded += new CommandEventHandler(acDoc_CommandEnded);
           acDoc.LispEnded += new EventHandler(acDoc_LispEnded);
       }

       void acDoc_LispEnded(object sender, EventArgs e)
       {
           endStopwatch();
       }

       void acDoc_CommandEnded(object sender, CommandEventArgs e)
       {
           endStopwatch();
       }

       void acDoc_LispWillStart(object sender, LispWillStartEventArgs e)
       {
           startStopwatch();
       }

       void acDoc_CommandWillStart(object sender, CommandEventArgs e)
       {
           startStopwatch();
       }

       DateTime begin;
       private void startStopwatch()
       {
           Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
           begin = DateTime.Now;
           string mess = string.Format("\nstart at {0}", begin);
           ed.WriteMessage(mess);
       }

       private void endStopwatch()
       {
           Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;            
           TimeSpan duration = DateTime.Now.Subtract(begin);
           ed.WriteMessage("\nduration: {0}", duration);
       }

       public void Terminate()
       {
           Document acDoc = Application.DocumentManager.MdiActiveDocument;
           acDoc.CommandWillStart -= new CommandEventHandler(acDoc_CommandWillStart);
           acDoc.LispWillStart -= new LispWillStartEventHandler(acDoc_LispWillStart);
           acDoc.CommandEnded -= new CommandEventHandler(acDoc_CommandEnded);
           acDoc.LispEnded -= new EventHandler(acDoc_LispEnded);
       }

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Thảo luận] Điều khiển AutoCAD bằng VC++ (ObjectARX). Xem đúng bài viết #200622 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 142)