乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 66|回复: 1

[编程交流] 批量块插入。

[复制链接]

13

主题

126

帖子

114

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-6 12:44:43 | 显示全部楼层 |阅读模式
我终于有了一个LISP例程,我觉得我可以发布它了。我不知道这是否对其他人有帮助,但如果没有CADTutor的帮助,我不可能做到这一点,我想我应该把它贴出来。我仍然需要添加错误处理,但现在它似乎运行正常。
  1. ;;;******************************************************************************
  2. ;;;The hard parts of this code written by Lee Mac and posted on CADTutor.        
  3. ;;;Duct tape, baling twine and nails sticking out at odd angles                  
  4. ;;; holding that code together added by Glen Smith.                              
  5. ;;;                                                                              
  6. ;;;  [url]http://www.cadtutor.net/forum/showthread.php?t=38230[/url]                        
  7. ;;;                                                                              
  8. ;;;Copyright August - September 2009                                             
  9. ;;;                                                                              
  10. ;;;  The LISP takes an input file, searches by block handle for the block in     
  11. ;;;   the drawing, zooms in on the block and inserts three blocks at the same   
  12. ;;;   insertion point. The searched block list is then brought to the front of   
  13. ;;;   the drawing.                                                               
  14. ;;;                                                                              
  15. ;;;                                                                              
  16. ;;;******************************************************************************
  17. ;;;                                                                              
  18. ;;;          Additional files required in the working directory:                 
  19. ;;;                                                                              
  20. ;;; KEY_SCHED.dwg - The key schedule block.                                      
  21. ;;; KEY_SCHED_WIPEOUT.dwg - A wipeout block so the KEY_SCHED attributes can be read.
  22. ;;; KEY_CHG_*.DWG - Multiple different colored blocks to visually distinguish   
  23. ;;;                  between the key groups that have been assigned.            
  24. ;;; KEY_MG_*.DWG - Multiple different colored blocks to visually distinguish     
  25. ;;;                  between the different master groups that have been assigned.
  26. ;;;                                                                              
  27. ;;; NOTE: The insertion point/orgin for all of these blocks is assumed to be in  
  28. ;;;       the lower left corner such that they 'stack' when inserted at the same
  29. ;;;       point.                                                                 
  30. ;;;                                                                              
  31. ;;;******************************************************************************
  32. ;;;                                                                              
  33. ;;; USEAGE:                                                                     
  34. ;;; Insert KEY_SCHED.dwg block at all door locations to be color coded for keys.
  35. ;;; Assign values to the 5 attributes in the KEY_SCHED block.                    
  36. ;;; Export the attributes of the KEY_SCHED block, and open in a spreadsheeet.   
  37. ;;; In the first column the block handle must remain, put the filename for the   
  38. ;;; key change code in the second column, the filename for the master group color
  39. ;;; code in the third column. The filename for the wipeout should be in the fourth
  40. ;;; column. The remaining columns and the header line should be deleted.         
  41. ;;; Save the file.                                                               
  42. ;;;                                                                              
  43. ;;; Save the drawing!                                                            
  44. ;;;                                                                              
  45. ;;; Load keysched.lsp by typing appload at the command line and selecting it.   
  46. ;;; Type keysched at the command line, select the input file which was previously
  47. ;;; created. The LSIP will run and there will be a lag time after it appears to  
  48. ;;; complete and the time that control is returned to you.  Color coding will be
  49. ;;; placed on the KEY_COLORS layer, and the wipeout will be placed on the wipeout
  50. ;;; layer. These layers will be created if they do not exist.                    
  51. ;;;                                                                              
  52. ;;; It is important to remember that this routine will not delete previuos color
  53. ;;; coding. Either manually change the coding or delete them all and recode the  
  54. ;;; entire drawing.                                                              
  55. ;;;                                                                              
  56. ;;;                                                                              
  57. ;;;******************************************************************************
  58. ;;;                                                                              
  59. (defun c:KEYSCHED
  60.       (/ file nl inslst Minp Maxp pts elst ipt xScale yScale rot entity kclst mglst wipelst count oldlayer )
  61.   (vl-load-com)
  62.   (defun StrBrk (str delim / pos lst)
  63.      (while (setq pos (vl-string-position delim str))
  64.         (setq lst (cons (substr str 1 pos) lst)
  65.               str (substr str (+ pos 2))
  66.         )
  67.      )
  68.      (reverse (cons str lst))
  69.   )
  70.   (defun RTD (a)                       ;radians to degrees function
  71.      (/ (* a 180.0) PI)                ;takes angle in radians, returns angle in degrees
  72.   )                                    ;end function RTD
  73.   (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  74.   (vla-startUndoMark doc)
  75.   (command "Layer" "M" "KEY_COLORS" "")
  76.   (command "Layer" "M" "WIPEOUT" "")
  77.   (setq oldlayer (getvar "CLAYER"))
  78.   (if (setq file (getfiled "Select Text File" (if *load *load "") "txt" 0))
  79.         ;the file selected is stored in the GLOBAL variable *load and so defaults to the same filename in subsequent runs.
  80.      (progn
  81.         (setq *load file file  (open file "r"))
  82.         (while (setq nl (read-line file))
  83.            (setq entity (StrBrk nl 9))             ;entity should be a list of the entries on the nl
  84.     (setq inslst (cons (nth 0 entity) inslst))      ;inslst gets the first entry  from the entity list
  85.            (setq kclst (cons (nth 1 entity) kclst))      ;kclst gets the second entry from the entity list
  86.            (setq mglst (cons (nth 2 entity) mglst))      ;mglst gets the third entry from the entity list
  87.            (setq wipelst (cons (nth 3 entity) wipelst))  ;wipelst gets the fourth entry from the entity list
  88.         )
  89.         (close file)
  90.         (if (setq elst
  91.                     (vl-remove-if
  92.                        'null
  93.                        (mapcar 'handent
  94.                                (mapcar
  95.                                   (function (lambda (x) (substr x 2)))
  96.                                   (reverse inslst)
  97.                                )
  98.                        )
  99.                     )
  100.             )
  101.             (progn
  102.                ;put the lists back into the right order so all four lists match.
  103.                (setq kclst (reverse kclst))
  104.                (setq mglst (reverse mglst))
  105.                (setq wipelst (reverse wipelst))
  106.                (setq count 0)
  107.         (foreach Obj (mapcar 'vlax-ename->vla-object elst)
  108.                   (progn
  109.                      (vla-getBoundingBox Obj 'Minp 'Maxp)
  110.        (setq pts (mapcar 'vlax-safearray->list (list Minp Maxp)))
  111.        (vla-ZoomCenter
  112.           (vlax-get-acad-object)
  113.           (vlax-3D-point
  114.              (polar (car pts)
  115.                     (apply 'angle pts)
  116.                     (/ (apply 'distance pts) 2.)
  117.              )
  118.           )
  119.           400.
  120.                      )
  121.                   )
  122.     (setq ipt
  123.             (vlax-safearray->list
  124.                (vlax-variant-value
  125.                   (vla-get-InsertionPoint obj)
  126.                )
  127.             )
  128.     )
  129.     (setq rot (RTD (vla-get-rotation obj)))
  130.     (setq xScale (vla-get-xscalefactor obj))
  131.     (setq yScale (vla-get-yscalefactor obj))
  132.     (setvar "CLAYER" "WIPEOUT")
  133.                   (if (nth count wipelst)
  134.        (command "-insert" (nth count wipelst) ipt xScale yScale rot)
  135.                   )
  136.                   (setvar "CLAYER" "KEY_COLORS")
  137.                   (if (nth count kclst)
  138.        (command "-insert" (nth count kclst) ipt xScale yScale rot)
  139.                   )
  140.                   (if (nth count mglst)
  141.                      (command "-insert" (nth count mglst) ipt xScale yScale rot)
  142.                   )
  143.     (setq count(1+ count))
  144.                )
  145.             )
  146.          )
  147.          ;bring the key block and attributes to the front so they can be read.
  148.    (setq ent (car elst))
  149.   (progn
  150.      (setq ss (ssget "X"
  151.                      (list (cons 8 (cdr (assoc 8 (entget ent)))))
  152.               )
  153.      )
  154.   (command "_draworder" ss "" "_Front")
  155.   )
  156.       )
  157.       (princ "\n<< No File Selected >>")
  158.   )
  159.   (setvar "CLAYER" oldlayer)
  160.   (vla-EndUndoMark doc)
  161.   (princ)
  162. )

 
感谢Lee Mac为我提供了一个构建框架:http://www.cadtutor.net/forum/showthread.php?t=38230
 
同时也要感谢大量的帖子,这些帖子提供了数不胜数的代码片段。
回复

使用道具 举报

13

主题

126

帖子

114

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-6 14:38:04 | 显示全部楼层
忘记附加块。
密钥调度。图纸
KEY_CHG_蓝色。图纸
KEY_MG_红色。图纸
KEY\u SCHED\u擦除。图纸
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-5 04:39 , Processed in 0.700447 second(s), 56 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表