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

Bee

Thành viên
  • Số lượng nội dung

    553
  • Đã tham gia

  • Lần ghé thăm cuối

  • Ngày trúng

    37

Bài đăng được đăng bởi Bee


  1. Tong một bản vẽ có nhiều trục (nhiếu att block) theo mình nghĩ lsp nên chọn những att block nào cần thay góc quay thôi 

    (có những att block ở những vị trí khác nhưng trong cùng một bản vẽ có khi không cần thay góc quay)

    Mình đã gửi file cho bạn rồi đó. 

     

    "Vay co cach nao lam cho no trung voi tam duong tron khong vay ban? Boi vi att block nay do nguoi khac tao truoc do roi. "

    Uhm trong block đấy là ellip mà. không phải đường tròn. Mình có sửa code lại đây. Có bản vẽ khám bênh mới chính xác được. Lisp này là move att vào tâm thôi không xoay att. Muốn xoay thì dùng lisp XO hoặc torrient tùy bạn. Dùng kết hợp 2 lisp. Còn muốn add thêm xoay att vào lisp của mình thì để sau thứ 3 mình semina xong sẽ sửa lại lisp.

    Thử lisp mới và ngồi xem nhé. ^_^

    (defun c:MAC (/ acdoc mspace blk center temp)
      (setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
      (setq mspace (vla-get-modelspace acdoc))
      (if (setq blk (car (entsel "\nChon block: ")))
        (progn
          (vlax-for	blks (vla-get-blocks acdoc)
    	(if (wcmatch (vla-get-Name blks) (cdr (assoc 2 (entget blk))))
    	  (progn
    	    (vlax-for obj blks
    	      (if (or (= (vla-get-ObjectName obj) "AcDbCircle")
    		      (= (vla-get-ObjectName obj) "AcDbEllipse")
                          (= (vla-get-ObjectName obj) "AcDbArc")
    		      )
    		(setq center (vlax-get obj 'Center))
    	      )
    	    )
    	   
    	    (vlax-for obj blks
    	      (if
    		(= (vla-get-ObjectName obj) "AcDbAttributeDefinition")
    		 (progn
    		   ;(vla-put-Rotation obj 0.0)
    		   (vla-put-Alignment obj acAlignmentMiddleCenter)
    		   (vla-put-TextAlignmentPoint
    		     obj
    		     (vlax-3d-point center)
    		   )
    		 )			;progn
    	      )				;if
    	    )				;vlax-for obj
    	  )				;progn then
    	)				;if
          )					;vlax-for blks
          (setq temp (vla-insertblock
    		   mspace
    		   (vlax-3d-point '(0. 0. 0.))
    		   (cdr (assoc 2 (entget blk)))
    		   1
    		   1
    		   1
    		   0
    		 )
          )
          (vla-sendcommand
    	acdoc
    	(strcat	"ATTSYNC\n"
    		"Name\n"
    		""
    		(cdr (assoc 2 (entget blk)))
    		"\n"
    		""
    	)
          )
          (vla-delete temp)
        )					;progn
        (princ "\nBan da khong chon block.")
      )					;if
      (princ)
    )					;defun
    
    ;;;END CODE VISUAL LISP HERE
    
    • Vote tăng 1

  2. Bạn test thử chưa ? Do lsp bi lỗi mà

    Do điễn đàn bị lỗi nên mình up bv lên không được nữa

     

    Vấn đề là điểm chèn của nó không trùng với tâm đường tròn đó bạn .

    Mình test lại rồi mà, không thấy lỗi. Up lên mediafire hoặc fshare.... mới được. Diễn đàn còn ko up đc ảnh xem lỗi gì. :(


  3. Nếu điểm insert của block trùng với tâm đường tròn thì dùng lsp sửa lại này cho nhanh (Block gốc kệ nó - chỉ sửa các block được chọn)

    (defun c:XO  (/ ent i lse lst poi ss)

     (if (setq ss (ssget '((0 . "INSERT") (66 . 1))))

      (repeat (setq i (sslength ss))

       (setq ent (ssname ss (setq i (1- i)))

             poi (cdr (assoc 10 (entget ent)))

             ent (entnext ent)

             lst (list (cons 50 0.0) (cons 10 poi) (cons 71 0) (cons 72 1) (cons 11 poi))

             lse (entget ent))

       (mapcar '(lambda (x) (entmod (subst x (assoc (car x) lse) lse))) lst)

       (entupd ent)))

     (princ))

    Chuẩn, nếu trùng tâm và điểm chèn thì nhàn hơi nhiều :D


  4. - Trong 4 ý của bạn có ý 4 là ok nhưng lệnh này có khi cũng không dùng được. (có những bv vẽ ta không dùng được lệnh này)

    - Ý 1 thì chỉ sử dụng được 1 att block được chọn thôi. 

    ---> Nói chung cũng không ổn lắm ! 

    Thử cả VL xem nào ^_^ Còn cái này thì chơi tất. :D

    (defun c:MAC (/ acdoc mspace blk center temp)
      (setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
      (setq mspace (vla-get-modelspace acdoc))
      (if (setq blk (car (entsel "\nChon block: ")))
        (progn
          (vlax-for	blks (vla-get-blocks acdoc)
    	(if (wcmatch (vla-get-Name blks) (cdr (assoc 2 (entget blk))))
    	  (progn
    	    (vlax-for obj blks
    	      (if (= (vla-get-ObjectName obj) "AcDbCircle")
    		(setq center (vlax-get obj 'Center))
    	      )
    	    )
    	    (vlax-for obj blks
    	      (if
    		(= (vla-get-ObjectName obj) "AcDbAttributeDefinition")
    		 (progn
    		   (vla-put-Rotation obj 0.0)
    		   (vla-put-Alignment obj acAlignmentMiddleCenter)
    		   (vla-put-TextAlignmentPoint
    		     obj
    		     (vlax-3d-point center)
    		   )
    		 )			;progn
    	      )				;if
    	    )				;vlax-for obj
    	  )				;progn then
    	)				;if
          )					;vlax-for blks
          (setq temp (vla-insertblock
    		   mspace
    		   (vlax-3d-point '(0. 0. 0.))
    		   (cdr (assoc 2 (entget blk)))
    		   1
    		   1
    		   1
    		   0
    		 )
          )
          (vla-sendcommand
    	acdoc
    	(strcat	"ATTSYNC\n"
    		"Name\n"
    		""
    		(cdr (assoc 2 (entget blk)))
    		"\n"
    		""
    	)
          )
          (vla-delete temp)
        )					;progn
      )					;if
    (command "regen")
      (princ)
    )					;defun
    
    ;;;END CODE VISUAL LISP HERE
    

  5. - Trong 4 ý của bạn có ý 4 là ok nhưng lệnh này có khi cũng không dùng được. (có những bv vẽ ta không dùng được lệnh này)

    - Ý 1 thì chỉ sử dụng được 1 att block được chọn thôi. 

    ---> Nói chung cũng không ổn lắm ! 

    Ok, nếu thử lisp thì nghịch tí nào. Thêm cả 2 cách Autolisp và Visual lisp.

     

    Autolisp này: ^_^ Ah tất nhiên dùng lisp trong trường hợp này là các block trục không nằm trong block tổng nữa nhé.  :D

    (defun c:test (/ blk ss n name ins lst value)
      (if (setq blk (car (entsel "\nChon block: ")))
        (progn
          (setq
    	ss (ssget "_X" (list '(0 . "INSERT") (assoc 2 (entget blk))))
          )
          (command "_justifytext" ss "" "MC")
          (setq name (cdr (assoc 2 (entget (ssname ss 0)))))
          (setq lst (get-block-entities name))
          (foreach ob lst
    	(if (eq (cdr (assoc 0 (entget ob))) "CIRCLE")
    	  (setq center (trans (cdr (assoc 10 (entget ob))) 1 0))
    	  )
    	);foreach
          (foreach ob lst
    	(if (eq (cdr (assoc 0 (entget ob))) "ATTDEF")
    	  (progn
    	    (setq value (cdr (assoc 1 (entget ob))))
    	    (entmod (subst (cons 11 center) (assoc 11 (entget ob)) (entget ob)))
    	    )
    	  )
    	);foreach
          (entupd (ssname ss 0))
          (setq n 0)
          (repeat (sslength ss)
    	(setq ins (cdr (assoc 10 (entget (ssname ss n)))))
    	(setq value (cdr (assoc 1 (entget (entnext (ssname ss n))))))
    	(command "_-insert" name "_none" ins 1 1 0 value
    		 "_erase" (ssname ss n) "")
    	(setq n (1+ n))
    	)
          )
        )
      )
    (defun get-block-entities ( blk / ent lst )
        (if (setq ent (tblobjname "block" blk))
          (while (setq ent (entnext ent))
    	(setq lst (cons ent lst))
            ) ;; end WHILE
        ) ;; end IF
        (reverse lst) ;; Return the list
    ) ;; end DEFUN
    ;;;END CODE AUTOLISP HERE
    
    
    

  6. Vay co cach nao lam cho no trung voi tam duong tron khong vay ban? Boi vi att block nay do nguoi khac tao truoc do roi.

    1.Blockeditor 1 block bất kỳ. Thay justifi của att thành MC, Move vào tâm đường tròn. Save and close.

    2.Insert block đó tại vị trí bất kỳ (xong sẽ xóa) với value attt bất kỳ.

    3.Command: ATTSYNC -> select block vừa insert và Yes. Xong xóa block vừa insert đó. Done.

    4.Dùng torient trong express để xoay att tùy ý.

     

    ^_^


  7. chào mọi người, nhờ các bác giúp mình lisp khi chọn text với đường tròn cho ra kết quả bán kính của đường tròn bằng chiều cao của text , tâm của đườn tròn trùng tâm của  text (dạng middle center ).thank

    Thử cái này xem: ^_^ viết nhanh chỉ dùng cho 1 text với 1 đường tròn . Chọn đối tượng lần đầu có thể chọn hết các đường tròn. 

    Chúc ngon miệng. ^_^

    (defun c:test (/ ss n ename center radius pt1 pt2 ss1)
      (setvar "CMDECHO" 0)
      (princ "\nChon duong tron: ")
      (if (setq ss (ssget '((0 . "CIRCLE"))))
        (progn
          (command "zoom" "ob" ss "")
          (setq n 0)
          (repeat (sslength ss)
    	(setq ename (ssname ss n))
    	(setq center (cdr (assoc 10 (entget ename))))
    	(setq radius (cdr (assoc 40 (entget ename))))
    	(setq pt1 (polar center (/ (* pi 3) 4) (/ (* 3 radius) 4)))
    	(setq pt2 (polar pt1 (angle pt1 center) (/ (* 3 radius) 2)))
    	(setq ss1 (ssget "C" pt1 pt2 '((0 . "TEXT"))))
    	(if ss1
    	  (progn
    	    (command "_justifytext" ss1 "" "MC"
    		     "move" (ssname ss1 0) "" "_none" (cdr (assoc 11 (entget (ssname ss1 0)))) "_none" center)
    	    (entmod (subst (assoc 40 (entget (ssname ss1 0)))
    			   (assoc 40 (entget ename))
    			   (entget ename))
    		    )
    	    )
    	  )
    	
    	(setq n (1+ n))
    	);repeat
          );progn
        );if
      (command "zoom" "P")
      (princ)
      )
    
    • Vote tăng 1

  8. Hi,

    Mình nghĩ chèn text vào mỗi đỉnh nhanh hơn và đơn giản hơn. Bạn thử chạy lisp này. Thay tên lệnh Test nhé.

     

    1. (defun c:test (/ pl vt_lst point z)
        (if (setq pl (car (entsel "\nChon 3Dpline: ")))
          (progn
            (if (eq (vla-get-ObjectName (vlax-ename->vla-object pl)) "AcDb3dPolyline")
      	(progn
      	  (setq vt_lst (vlax-get (vlax-ename->vla-object pl) 'Coordinates))
      	  (setq n 0)
      	  (repeat (/ (length vt_lst) 3)
      	    (setq point (list (nth n vt_lst) (nth (1+ n) vt_lst) (nth (+ n 2) vt_lst)))
      	    (setq z (nth (+ n 2) vt_lst))
      	    (command "text"
      		     point
      		     20 ;<---thay doi chieu cao text
      		     0
      		     (rtos z 2 2)
      		     )
      	    (setq n (+ n 3))
      	    );repeat
      	  )
      	(princ "\nDoi tuong ban chon khong phai 3Dpolyline")
      	)
            );progn
          (princ "\nBan da khong chon doi tuong.")
          );if
        (princ)
        );defun
      

  9.  

    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.


  10. 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.


  11. 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")
      
    

  12. 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.

×