Jump to content

Lisp lệnh TEST trong chủ đề “Tạo 1 ứng dụng AutoCAD .NET với MS Visual 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")
  

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 407730_test.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 TEST để chạy lisp.

Mã nguồn 407730_test.lsp (trích từ bài viết gố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();
}
}
}
}

(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")

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Tạo 1 ứng dụng AutoCAD .NET với MS Visual C#. Xem đúng bài viết #407730 để đọc hướng dẫn và trao đổi thêm.

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