Lisp lệnh XMINSERT trong chủ đề “Đố vui”
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
- Chép mã nguồn, lưu thành tệp 4482_xminsert.lsp.
- 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ẽ.
- Gõ lệnh XMINSERT để chạy lisp.
Mã nguồn 4482_xminsert.lsp (trích từ bài viết gốc)
;;;CADALYST 01/07 Tip 2169: XMINSERT.lsp Explode Minsert © 2007 Jeffery P. Sanders
;;;--- XMINSERT - Replace a minsert entity with individual blocks arrayed.
;;;
;;;
;;;
;;;--- This program will delete the XMINSERT entity after an array is created.
;;; It will restore the attribute values contained inside the minsert.
(defun C:XMINSERT()
;;;--- Function to change an attributes value
(defun repAttVal(en tagName newVal)
;;;--- Get the DXF group codes of the entity
(setq enlist(entget en))
;;;--- Get the name of the block
(setq blkName(cdr(assoc 2 enlist)))
;;;--- Check to see if the block's attribute flag is there
(if(cdr(assoc 66 enlist))
(progn
;;;--- Get the entity name
(setq en(entnext en))
;;;--- Get the entity dxf group codes
(setq enlist(entget en))
;;;--- Get the type of block
(setq blkType (cdr(assoc 0 enlist)))
;;;--- If group 66 then there are attributes nested inside this block
(setq group66(cdr(assoc 66 enlist)))
;;;--- Loop while the type is an attribute or a nested attribute exist
(while(or (= blkType "ATTRIB")(= group66 1))
;;;--- Get the block type
(setq blkType (cdr(assoc 0 enlist)))
;;;--- Get the block name
(setq entName (cdr(assoc 2 enlist)))
;;;--- Check to see if this is the first attribute
(if(= blkType "ATTRIB")
(progn
;;;--- Get the attribute tag
(setq attTag(cdr(assoc 2 enlist)))
;;;--- Get the value of the attribute
(setq attVal(cdr(assoc 1 enlist)))
;;;--- If this tag matches our search tag name
(if(= (strcase tagName)(strcase attTag))
(progn
;;;--- Replace the attribute's value
(setq enlist(subst (cons 1 newVal)(assoc 1 enlist)enlist))
(entmod enlist)
(entupd en)
)
)
)
)
;;;--- Get the next sub-entity or nested entity as you will
(setq en(entnext en))
;;;--- Get the dxf group codes of the next sub-entity
(setq enlist(entget en))
;;;--- Get the block type of the next sub-entity
(setq blkType (cdr(assoc 0 enlist)))
;;;--- See if the dxf group code 66 exist. if so, there are more nested attributes
(setq group66(cdr(assoc 66 enlist)))
)
)
)
)
;;;--- Function to return a list of tags and values from attributes in a block
(defun getAttData(en / attList attVal enlist blkType group66)
(setq attList(list))
;;;--- Get the DXF group codes of the entity
(setq enlist(entget en))
;;;--- Check to see if the block's attribute flag is there
(if(cdr(assoc 66 enlist))
(progn
;;;--- Get the entity name
(setq en(entnext en))
;;;--- Get the entity dxf group codes
(setq enlist(entget en))
;;;--- Get the type of block
(setq blkType (cdr(assoc 0 enlist)))
;;;--- If group 66 then there are attributes nested inside this block
(setq group66(cdr(assoc 66 enlist)))
;;;--- Loop while the type is an attribute or a nested attribute exist
(while(or (= blkType "ATTRIB")(= group66 1))
;;;--- Get the block type
(setq blkType (cdr(assoc 0 enlist)))
;;;--- Check to see if this is an attribute
(if(= blkType "ATTRIB")
(progn
;;;--- If this matches your tag name
(setq attTag(cdr(assoc 2 enlist)))
;;;--- Get the value of the attribute
(setq attVal(cdr(assoc 1 enlist)))
(setq attList(append attList (list (list attTag attVal))))
)
)
;;;--- Get the next sub-entity or nested entity as you will
(setq en(entnext en))
;;;--- Get the dxf group codes of the next sub-entity
(setq enlist(entget en))
;;;--- Get the block type of the next sub-entity
(setq blkType (cdr(assoc 0 enlist)))
;;;--- See if the dxf group code 66 exist. if so, there are more nested attributes
(setq group66(cdr(assoc 66 enlist)))
)
)
)
attList
)
;;;--- Main application
(setvar "cmdecho" 0)
;;;--- If the user selects an object
(if(setq ent(entsel "\nSelect MINSERT: "))
(progn
;;;--- If it is an object selected and not an point
(if(setq en(car ent))
(progn
;;;--- Make a copy of the entity name
(setq origEn en)
;;;--- Get the entities dxf group codes
(setq enlist(entget en))
;;;--- Get the data from the minsert
(setq blkName(cdr(assoc 2 enlist)))
(setq layName(cdr(assoc 8 enlist)))
(setq insPt(cdr(assoc 10 enlist)))
(setq cols(cdr(assoc 70 enlist)))
(setq rows(cdr(assoc 71 enlist)))
(setq colWidth(cdr(assoc 44 enlist)))
(setq rowWidth(cdr(assoc 45 enlist)))
(setq xScale(cdr(assoc 41 enlist)))
(setq yScale(cdr(assoc 42 enlist)))
(setq rAngle(cdr(assoc 50 enlist)))
;;;--- If group code 66 exist [ attributes follow flag ]
(if(assoc 66 enlist)
;;;--- Get the group code 66
(setq group66(cdr(assoc 66 enlist)))
)
;;;--- If it equals 1 then attributes follow..so
(if(= group66 1)
(progn
;;;--- Turn off attribute request during insertion
(setq oldAttReq(getvar "attreq"))
(setvar "attreq" 0)
;;;--- Get the attribute tags and values
(setq attList(getAttData en))
)
)
;;;--- Insert the new block
(command "-insert" blkName insPt xScale yScale (angtos rAngle))
;;;--- If it had attributes ...
(if(= group66 1)
;;;--- Reset the attribute request
(setvar "attreq" oldAttReq)
)
;;;--- Get the last entity created...
(if(setq en(entlast))
(progn
;;;--- Get the group codes
(setq enlist(entget en))
;;;--- Get the name of the block
(setq blkName2(cdr(assoc 2 enlist)))
;;;--- If this is the block I just created...
(if(= blkName blkName2)
(progn
;;;--- If it has an attributes flag code...
(if(assoc 66 enlist)
(progn
;;;--- And attributes follow [code 66 = 1]
(if(= (cdr(assoc 66 enlist)) 1)
(progn
;;;--- Replace the attribute values
(foreach a attList
(repAttVal en (car a) (cadr a))
)
)
)
)
)
;;;--- Delete the minsert entity
(entdel origEn)
;;;--- Array the new block to represent the minsert entity
(command "-array" en "" "R" rows cols rowWidth colWidth)
;;;--- Inform the user
(setq str(itoa (* rows cols)))
(alert (strcat "Deleted MINSERT and added (" str ") blocks."))
)
)
)
)
)
)
)
)
(princ)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Đố vui. Xem đúng bài viết #4482 để đọc hướng dẫn và trao đổi thêm.