乐筑天下

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

[编程交流] 从dr创建块的Lsp

[复制链接]

1

主题

9

帖子

8

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 10:03:26 | 显示全部楼层 |阅读模式
再一次,我在lsp上寻求帮助,这可以帮助我减少CAD处理时间和错误。
 
情况:
我必须放大或缩小客户提供的图纸中的“多段线”尺寸。这些多段线大多是“矩形”或“正方形”。
由于它们不是块形式,我必须手动将它们编辑到我想要的维度。这花了我很多时间,以前我甚至把它们放大到了错误的尺寸。
 
问题:
是否可以使用lsp脚本将相同尺寸的“矩形”或“正方形”转换为块形式?
如果不同的块可以分为不同的层就好了。(层的名称并不重要)
这个脚本是否太难构建?
 
我附上了这幅画的样本。(仅需转换3种类型)
 
如果有专家能就此问题向我提供建议,我将不胜感激。
谢谢您!:眨眼:
 
卡尔
实例拉链
回复

使用道具 举报

VVA

1

主题

308

帖子

308

银币

初来乍到

Rank: 1

铜币
8
发表于 2022-7-6 10:17:12 | 显示全部楼层
试试看
SetNB-将选定实体转换为块
SetNB1-在单独的命名块中转换每个选定的基元
  1. (defun c:setnb (/ ss adoc pt_lst center blk *error* bi bname bpat)
  2. ;;;Selected Entities To Named Block
  3. (setq bpat "BLOCK-") ;_ <- Edit block name pattern here
  4. (defun *error* (msg)
  5.    (vla-endundomark adoc)
  6.    (princ msg)
  7.    (princ)
  8.    ) ;_ end of defun
  9. (vl-load-com)
  10. (vla-startundomark
  11.    (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  12.    ) ;_ end of vla-StartUndoMark
  13. (if (not (vl-catch-all-error-p
  14.             (vl-catch-all-apply '(lambda () (setq ss (ssget "_:L"))))
  15.             ) ;_ end of vl-catch-all-error-p
  16.           ) ;_ end of not
  17.    (progn
  18.      (setq
  19.        ss     (mapcar 'vlax-ename->vla-object
  20.                       (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
  21.                       ) ;_ end of mapcar
  22.        pt_lst (apply 'append
  23.                      (mapcar
  24.                        '(lambda (x / minp maxp)
  25.                           (vla-getboundingbox x 'minp 'maxp)
  26.                           (list (vlax-safearray->list minp)
  27.                                 (vlax-safearray->list maxp)
  28.                                 ) ;_ end of append
  29.                           ) ;_ end of lambda
  30.                        ss
  31.                        ) ;_ end of mapcar
  32.                      ) ;_ end of append
  33.        center (mapcar '(lambda (a b) (/ (+ a b) 2.))
  34.                       (list (apply 'min (mapcar 'car pt_lst))
  35.                             (apply 'min (mapcar 'cadr pt_lst))
  36.                             (apply 'min (mapcar 'caddr pt_lst))
  37.                             ) ;_ end of list
  38.                       (list (apply 'max (mapcar 'car pt_lst))
  39.                             (apply 'max (mapcar 'cadr pt_lst))
  40.                             (apply 'max (mapcar 'caddr pt_lst))
  41.                             ) ;_ end of list
  42.                       ) ;_ end of mapcar
  43.        bname
  44.        (progn
  45.          (setq bi 0)
  46.          (while (tblsearch "BLOCK" (setq bname (strcat bpat (itoa(setq bi(1+ bi)))))))
  47.             bname)
  48.        blk    (vla-add (vla-get-blocks adoc)
  49.                        (vlax-3d-point center)
  50.                        bname
  51.                        ) ;_ end of vla-add
  52.        ) ;_ end of setq
  53.      (vla-copyobjects
  54.        adoc
  55.        (vlax-make-variant
  56.          (vlax-safearray-fill
  57.            (vlax-make-safearray vlax-vbobject (cons 0 (1- (length ss))))
  58.            ss
  59.            ) ;_ end of vlax-safearray-fill
  60.          ) ;_ end of vlax-make-variant
  61.        blk
  62.        ) ;_ end of vla-copyobjects
  63.      (vla-insertblock
  64.        (vla-objectidtoobject adoc (vla-get-ownerid (car ss)))
  65.        (vlax-3d-point center)
  66.        (vla-get-name blk)
  67.        1.0
  68.        1.0
  69.        1.0
  70.        0.0
  71.        ) ;_ end of vla-insertblock
  72.      (mapcar 'vla-erase ss)
  73.      ) ;_ end of and
  74.    ) ;_ end of if
  75. (vla-endundomark adoc)
  76. (princ)
  77. ) ;_ end of defun
  78. (defun c:SETNB1 (/ ss adoc pt_lst center blk *error* lst bpat bname bi)
  79. ;;;Each primitive in a separate named block
  80. ;;;Каждый примитив в отдельный Имсенованный блок
  81. (defun *error* (msg)
  82.    (vla-endundomark adoc)
  83.    (princ msg)
  84.    (princ)
  85.    ) ;_ end of defun
  86. (setq bpat "BLOCK-") ;_ <- Edit block name pattern here
  87. (vl-load-com)
  88. (vla-startundomark
  89.    (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  90.    ) ;_ end of vla-StartUndoMark
  91. (if (not (vl-catch-all-error-p
  92.             (vl-catch-all-apply '(lambda () (setq ss (ssget "_:L"))))
  93.             ) ;_ end of vl-catch-all-error-p
  94.           ) ;_ end of not
  95.    (progn
  96.      (mapcar '(lambda(item)
  97.      (setq
  98. ss (list item)
  99.        pt_lst (apply 'append
  100.                      (mapcar
  101.                        '(lambda (x / minp maxp)
  102.                           (vla-getboundingbox x 'minp 'maxp)
  103.                           (list (vlax-safearray->list minp)
  104.                                 (vlax-safearray->list maxp)
  105.                                 ) ;_ end of append
  106.                           ) ;_ end of lambda
  107.                        ss
  108.                        ) ;_ end of mapcar
  109.                      ) ;_ end of append
  110.        center (mapcar '(lambda (a b) (/ (+ a b) 2.))
  111.                       (list (apply 'min (mapcar 'car pt_lst))
  112.                             (apply 'min (mapcar 'cadr pt_lst))
  113.                             (apply 'min (mapcar 'caddr pt_lst))
  114.                             ) ;_ end of list
  115.                       (list (apply 'max (mapcar 'car pt_lst))
  116.                             (apply 'max (mapcar 'cadr pt_lst))
  117.                             (apply 'max (mapcar 'caddr pt_lst))
  118.                             ) ;_ end of list
  119.                       ) ;_ end of mapcar
  120.        bname
  121.        (progn
  122.          (setq bi 0)
  123.          (while (tblsearch "BLOCK" (setq bname (strcat bpat (itoa(setq bi(1+ bi)))))))
  124.             bname)
  125.        blk    (vla-add (vla-get-blocks adoc)
  126.                        (vlax-3d-point center)
  127.                        bname
  128.                        ) ;_ end of vla-add
  129.        ) ;_ end of setq
  130.      (vla-copyobjects
  131.        adoc
  132.        (vlax-make-variant
  133.          (vlax-safearray-fill
  134.            (vlax-make-safearray vlax-vbobject (cons 0 (1- (length ss))))
  135.            ss
  136.            ) ;_ end of vlax-safearray-fill
  137.          ) ;_ end of vlax-make-variant
  138.        blk
  139.        ) ;_ end of vla-copyobjects
  140.      (vla-insertblock
  141.        (vla-objectidtoobject adoc (vla-get-ownerid (car ss)))
  142.        (vlax-3d-point center)
  143.        (vla-get-name blk)
  144.        1.0
  145.        1.0
  146.        1.0
  147.        0.0
  148.        ) ;_ end of vla-insertblock
  149.          )
  150.   (setq
  151.        lst     (mapcar 'vlax-ename->vla-object
  152.                       (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
  153.                       ) ;_ end of mapcar
  154. )
  155.       )
  156.      
  157.      (mapcar 'vla-erase lst)
  158.      ) ;_ end of and
  159.    ) ;_ end of if
  160. (vla-endundomark adoc)
  161. (princ)
  162. )
回复

使用道具 举报

1

主题

9

帖子

8

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 10:25:18 | 显示全部楼层
你好,VVA,
 
非常感谢您的LSP!
 
我可以再要一个功能吗?
“SetNB1”正是我想要的。
但是,是否可以将选定的基本体转换为相同的块名?
当然,我会确保选择的形状是相同的。
对不起,如果我解释得不好。
希望你能理解我所说的。
 
我很高兴收到你的回复。
谢谢
 
卡尔
回复

使用道具 举报

VVA

1

主题

308

帖子

308

银币

初来乍到

Rank: 1

铜币
8
发表于 2022-7-6 10:32:38 | 显示全部楼层
另一种方法:在选定对象的中心(边界框)缩放选定对象
  1. (defun c:sm (/ ERRCOUNT MAXPT MINPT MIPT MNPT MXPT
  2.          OBJSET PTLST XLST YLST old Flg)
  3. (vl-load-com)
  4. (if(not sm:scale)(setq sm:scale 1))
  5.    (initget 6)   
  6.    (setq old sm:scale
  7.          sm:scale(getdist
  8.         (strcat"\nSpecify the scale factor <"(rtos sm:scale 2 2)">: "))
  9.   ); end setq
  10. (if(null sm:scale)(setq sm:scale old))
  11. (setq errCount 0 ptLst 'nil Flg t); en setq
  12. (while Flg
  13.    (princ "\n§§§ Select objects and press Enter or Esc to exit. §§§")
  14. (if
  15.   (not(setq objSet(ssget "_I")))
  16.   (setq objSet(ssget))
  17.   ); end if
  18. (if objSet
  19.    (progn
  20.      (setq objSet
  21.       (mapcar 'vlax-ename->vla-object
  22.                    (vl-remove-if 'listp
  23.                     (mapcar 'cadr(ssnamex objSet)))))
  24.      (foreach obj objSet
  25.   (vla-GetBoundingBox obj 'MinPt 'MaxPt)
  26.    (setq mnPt(vlax-safearray->list MinPt)
  27.     mxPt(vlax-safearray->list MaxPt)
  28.     miPt (polar mnPt (angle mnPt mxPt)(* 0.5 (distance mnPt mxPt)))      
  29.     )
  30.     (if (vlax-method-applicable-p obj 'ScaleEntity)
  31.     (if
  32.      (vl-catch-all-error-p
  33.   (vl-catch-all-apply 'vla-ScaleEntity
  34.     (list obj(vlax-3D-Point miPt) sm:scale)))
  35.      (setq errCount(1+ errCount))
  36.      )
  37.       )
  38.   ); end foreach
  39.   (princ(strcat "\n" (itoa errCount) " objects blocked layer! "))
  40.   )
  41.      ); if objset
  42.    (setq Flg nil)
  43. ); end while
  44. )
回复

使用道具 举报

VVA

1

主题

308

帖子

308

银币

初来乍到

Rank: 1

铜币
8
发表于 2022-7-6 10:45:43 | 显示全部楼层
您必须确保所选形状相同
  1. (defun c:SETNB2 (/ ss adoc pt_lst center blk *error* lst bpat bname bi first)
  2. ;;;Each primitive in a separate named block
  3. ;;;http://www.cadtutor.net/forum/showthread.php?p=287449&posted=1#post287449
  4. (defun *error* (msg)
  5.    (vla-endundomark adoc)
  6.    (princ msg)
  7.    (princ)
  8.    ) ;_ end of defun
  9. (setq bpat "BLOCK-") ;_ <- Edit block name pattern here
  10. (vl-load-com)
  11. (vla-startundomark
  12.    (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  13.    ) ;_ end of vla-StartUndoMark
  14. (if (not (vl-catch-all-error-p
  15.             (vl-catch-all-apply '(lambda () (setq ss (ssget "_:L"))))
  16.             ) ;_ end of vl-catch-all-error-p
  17.           ) ;_ end of not
  18.    (progn
  19.      (mapcar '(lambda(item)
  20.      (setq
  21. ss (list item)
  22.        pt_lst (apply 'append
  23.                      (mapcar
  24.                        '(lambda (x / minp maxp)
  25.                           (vla-getboundingbox x 'minp 'maxp)
  26.                           (list (vlax-safearray->list minp)
  27.                                 (vlax-safearray->list maxp)
  28.                                 ) ;_ end of append
  29.                           ) ;_ end of lambda
  30.                        ss
  31.                        ) ;_ end of mapcar
  32.                      ) ;_ end of append
  33.        center (mapcar '(lambda (a b) (/ (+ a b) 2.))
  34.                       (list (apply 'min (mapcar 'car pt_lst))
  35.                             (apply 'min (mapcar 'cadr pt_lst))
  36.                             (apply 'min (mapcar 'caddr pt_lst))
  37.                             ) ;_ end of list
  38.                       (list (apply 'max (mapcar 'car pt_lst))
  39.                             (apply 'max (mapcar 'cadr pt_lst))
  40.                             (apply 'max (mapcar 'caddr pt_lst))
  41.                             ) ;_ end of list
  42.                       ) ;_ end of mapcar
  43. )
  44. (if (null first)
  45.   (progn
  46.     (setq
  47.       bname  
  48.      (progn
  49.        (setq bi 0)
  50.        (while (tblsearch "BLOCK" (setq bname (strcat bpat (itoa(setq bi(1+ bi)))))))
  51.        bname)
  52.       blk
  53.      (vla-add (vla-get-blocks adoc)
  54.                        (vlax-3d-point center)
  55.                        bname
  56.               )
  57.       ) ;_ end of setq
  58.    (vla-copyobjects
  59.        adoc
  60.        (vlax-make-variant
  61.          (vlax-safearray-fill
  62.            (vlax-make-safearray vlax-vbobject (cons 0 (1- (length ss))))
  63.            ss
  64.            ) ;_ end of vlax-safearray-fill
  65.          ) ;_ end of vlax-make-variant
  66.        blk
  67.        );_ end of vla-copyobjects
  68.     (setq first t)
  69.   )
  70. )
  71.    
  72.      (vla-insertblock
  73.        (vla-objectidtoobject adoc (vla-get-ownerid (car ss)))
  74.        (vlax-3d-point center)
  75.        (vla-get-name blk)
  76.        1.0
  77.        1.0
  78.        1.0
  79.        0.0
  80.        ) ;_ end of vla-insertblock
  81.          )
  82.   (setq
  83.        lst     (mapcar 'vlax-ename->vla-object
  84.                       (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
  85.                       ) ;_ end of mapcar
  86. )
  87.       )
  88.      
  89.      (mapcar 'vla-erase lst)
  90.      ) ;_ end of and
  91.    ) ;_ end of if
  92. (vla-endundomark adoc)
  93. (princ)
  94. )
回复

使用道具 举报

1

主题

9

帖子

8

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 10:52:25 | 显示全部楼层
VVA,
 
我衷心感谢你。
你的好意深深打动了我的感激之情。
 
卡尔
回复

使用道具 举报

1

主题

316

帖子

311

银币

初来乍到

Rank: 1

铜币
29
发表于 2022-7-6 10:56:05 | 显示全部楼层
只需添加,我将通过将块对齐为直线和多段线来实现,以下是我之前所做的:
 
[code];|******************************************************************创建用于将块恢复/对齐/替换为具有不同旋转的类似直线/多段线的程序日期:2008年11月20日创建人:RONALD MANEJA(WIZMAN)。。。email\u add:[email=“ron”_09812001@yahoo.com“]罗恩_09812001@yahoo.com[/email]----也可用于修改块的插入点***********************;(defun c:blocker(/blkname blocker\u kword bloker\u newset blok\u rot\u strng counter fpoint ronblock ronblockdata seta spoint*error*);;;;------------------------------------------------------------------------------------------;;;初始化setvar、rtd、dtr和*error*函数(setvar的cmdecho 0)(vl load com)(defun rtd(a)(/(*a 180)pi));_end_defun(defun DTR(a)(*PI(/a 180.0));_end_defun(defun*error*(msg)(vla EndUndoMark(vla Get ActiveDocument(vlax Get Acad Object));_end_vla-Get-ActiveDocument);_end_vla-EndUndoMark(setvar'cmdecho 1));_end_defun;;;--------------------------------------------------------------------------------;;;blok\u rot函数-相对旋转选择中的对象(defun blok\u rot(blok\u set/blok\u rot\u allset blok\u rot\u计数器)(setq blok\u rot\u allset blok\u set)(while(not)(成员(strcase)(setq blok\u rot\u strng(getstring“\n>>…按空格键/回车键继续旋转,[M]移动或[E]退出…>>:”);\uend_getint);_end_setq);_end_strcase’(“M”“E”);_end_成员);_end\u not(setq blok\u rot\u counter 0)(while(<blok\u rot\u counter(sslength blok\u rot\u allset))(命令“rotate”(ssname blok\u rot\u allset blok\u rot\u counter)”(cdr(assoc 10(entget(ssname blok\u rot\u allset blok\u rot\u counter));\uend_assoc);_end_cdr 90);_end_命令(setq blok_rot_计数器(1+blok_rot_计数器));_end_while);_end_while(if(=(strcase blok\u rot\u strng)“M”)(bmov bloker\u newset));_end\u if(if(=(strcase blok\u rot\u strng)“E”)(progn(initget 1“Yes No”)(setq blok\u del\u kword(getkword“\n>>>…是否要删除参考线[是/否]?…>>:”);\uend_getkword);_end\u setq(if(=blok\u del\u kword“Yes”)(progn(vl cmdf).\u erase“seta”)(*error*nil)(exit));\uend_progn(progn(*error*nil)(exit));_end_progn);_end_if);_end_progn);_end_if);_end_defun;;;--------------------------------------------------------------------------------;;;bmov函数-相对移动选择中的对象(defun bmov(bmov\u集/2nd\u pt\u bmov allset\u bmov ang\u bmov counter\u bmov dist\u bmov ent\u rot\u bmov)(setq allset\u bmov bmov\u集)(while(and(not(initget 129))(setq dist\u bmov(getdist“\n>>…输入移动距离,[R]旋转或[E]退出…>>:”;\uend_getdist);_end_setq;(成员(strcase dist\u bmov)“(R”“E”);_end\u和(cond((=(numberp dist\u bmov)T)(progn(而(progn(setq ang\u bmov(cond)((getint“\n输入相对方向[0/90/180/270]));\uend_getint)(0));_end_cond);_end_setq(if(not(成员ang_bmov’(0 90 180 270-90));_end\u not(not(提示“\n错误角度必须为0 90 180 270,请重新输入”);\uend_提示符);_end_not);_end_if);_end_progn);_end\u while(setq counter\u bmov 0)(while(<counter\u bmov(sslength allset\u bmov))(setq ent\u rot\u bmov(cdr(assoc 50(entget(ssname allset\u bmov counter\u bmov)));\uend_cdr);_end\u setq(setq 2nd\u pt\u bmov(polar(cdr(assoc 10(entget(ssname allset\u bmov counter\u bmov))))(+ent\u rot\u bmov(dtr ang\u bmov))dist\u bmov);\uend_polar);_end_setq(命令“move”(ssname allset\u bmov counter\u bmov)“”“_non”(cdr(assoc 10(entget(ssname allset\u bmov counter\u bmov)))“_non”2nd\u pt\u bmov);_end_命令(setq counter_bmov(1+counter_bmov))end_while);_end\u progn)((=(strcase dist\u bmov)“R”)(blok\u rot bloker\u newset))((=(strcase dist\u bmov)“E”)(progn(initget 1“Yes No”)(setq blok\u del\u kword(getkword“\n>>…是否要删除参考线[是/否]?…>>:”;\uend_getkword);_end\u setq(if(=blok\u del\u kword“Yes”)(progn(vl cmdf).\u erase“seta”)(*error*nil)(exit));\uend_progn(progn(*error*nil)(exit));_end_progn);_end_if);_end_progn));_end_cond);_end_while);_end_defun;;;--------------------------------------------------------------------------------;;;结束撤消,开始撤消(vla EndUndoMark(vla Get ActiveDocument(vlax Get Acad Object));_end_vla-Get-ActiveDocument);_end_vla-EndUndoMark(vla StartUndoMark(vla Get ActiveDocument(vlax Get Acad Object));_end_vla-Get-ActiveDocument);_end_vla-StartUndoMark;;;-----------------------------------------------------------------;;;主例程(while(not(and)(或(setq ronblock(car(entsel“\n>>>…选择一个块…>>”))))(提示“\n missed,重试。”);\uend\u或(或(=(cdr(assoc 0(entget ronblock)))“INSERT”)(提示“\n>>…这不是块,这次选择块……选择要对齐的线或多段线…>>>”;\uend\u提示符(setq seta(ssget’((0。“LWpolyline,line”)))(setq计数器0)(setq bloker\u newset(ssadd))(mapcar’(lambda(x)(setq fpoint(vlax curve getPointAtParam x 0))(setq spoint(vlax curve getPointAtParam x 1))(entmake(list(cons 0“INSERT”)(cons 2 blkname)(10 fpoint)(cons 41 1)(cons 42 1)(cons 43 1)(cons 50(角度fpoint spoint));_end_列表);_end\u entmake(ssadd(entlast)bloker\u newset);(vla删除x));_end_lambda(mapcar“vlax ename->vla object(vl remove if”listp(mapcar”cadr(ssnamex seta));_vl结束删除if);_end_mapcar);_end\u mapcar(princ(strcat“\n添加到图形的块的数量=(vl princ to string(sslength bloker\u newset));\uend_strcat);_end\u princ(initget 7“Move Rotate Exit”)(if(setq blocker\u kword(getkword“\n是否要[旋转/移动/退出]?:”))(progn(if(null blocker\u kword)(setq blocker\u kword“退出”);\uend\u if(cond((=blocker\u kword“Move”)((bmov bloker\u newset))(=blocker\u kword“Rotate”)((blok\u rot bloker\u newset))((=blocker\u kword“Exit”)(progn(initget 1“Yes No”)(setq blok\u del\u kword(getkword“\n>>>…是否要删除参考线[是/否]?…>>:”;\uend\u ge公司
回复

使用道具 举报

13

主题

46

帖子

33

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-6 11:04:20 | 显示全部楼层
Is it possible with SETNB2.lsp to keep the hyperlink attached.
 
 
Jaap
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-7 00:12 , Processed in 0.580121 second(s), 68 queries .

© 2020-2025 乐筑天下

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