本帖最后由 kucha007 于 2023-10-29 13:00 编辑
在明经看到有人发帖问如何快速建块,手痒也折腾了一下,加了while可以循环选对象建块:http://bbs.mjtd.com/thread-186618-1-1.html
之后为了更快就想到也许可以求选择集中心点作为基点,就在论坛上翻了一下,原来已经有人这样做过了:http://bbs.mjtd.com/forum.php?mod=viewthread&tid=178210&highlight=%BF%EC%CB%D9%2B%BF%E9
但这个插件不能在一秒内连续建块,而且最大边界框的算法在选择集较大的时候可能会出问题。选对象的时候也没有加while,批量建块速度不算快,于是我查了一下,找到了另外几个最大边界框的算法,但最后还是用lee-mac的
https://www.cadforum.cz/en/get-center-point-of-a-group-of-selected-objects-tip12403
http://www.lee-mac.com/ssboundingbox.html
;获取实体最小外接矩形的WCS坐标,来自G版:
- (defun K:GetMinBox (en / p1 p2 p3 p4)
- (if
- (not
- (vl-catch-all-error-p
- (vl-catch-all-apply 'vla-GetBoundingBox
- (list (vlax-ename->vla-object en) 'p1 'p3)
- )
- )
- );没有捕获错误
- (progn
- (setq p1 (vlax-safearray->list p1)
- p3 (vlax-safearray->list p3)
- p2 (list (car p1) (cadr p3) (caddr p1))
- p4 (list (car p3) (cadr p1) (caddr p1))
- )
- (if (= "SPLINE" (cdr (assoc 0 (entget en))))
- (progn
- (setq lst (mapcar
- '(lambda (a b)
- (vlax-curve-getClosestPointToProjection en a b t)
- )
- (list p1 p2 p3 p4)
- '((1.0 0 0) (0 -1.0 0) (-1.0 0 0) (0 1.0 0))
- )
- )
- (list
- (apply 'mapcar (cons 'min lst))
- (apply 'mapcar (cons 'max lst))
- )
- )
- (list p1 p3)
- )
- )
- )
- )
;选择集最小外接矩形中心点:
- (defun K:GetCenter (ss / i Lst MinPt MaxPt)
- (repeat (setq i (sslength ss))
- (setq Lst (K:GetMinBox (ssname ss (setq i (1- i)))))
- (setq
- MinPt (mapcar 'min (car Lst) (cond (MinPt)((car Lst))))
- MaxPt (mapcar 'max (cadr Lst) (cond (MaxPt)((cadr Lst))))
- )
- );获取选择集最小外接矩形的WCS坐标
- (if (and MinPt MaxPt)
- (mapcar
- '(lambda (a b) (/ (+ a b) 2.0))
- MinPt
- MaxPt
- )
- )
- )
;根据当前时间制作块名,避免跨文件块名重复:月$日时年-分秒:大概这种感觉:L$18Q22-4421
(1秒内建多个块导致块名重复就直接加数字后缀,完整逻辑见源码)
- (defun K:GetBlkNam (/ K:GetTime i pre Bnam)
- (defun K:GetTime (/ Old_Dim time yr mn dy hr mt sc)
- (setq Old_Dim (getvar "dimzin"))
- (setvar "dimzin" 0)
- (setq time (rtos (getvar "cdate") 2 20))
- (setq yr (substr time 3 2))
- (setq mn (chr (+ 65 (read (substr time 5 2)))));转大写字母
- (setq dy (substr time 7 2))
- (setq hr (chr (+ 65 (read (substr time 10 2)))));转大写字母
- (setq mt (substr time 12 2))
- (setq sc (substr time 14 2))
- (setvar "dimzin" Old_Dim)
- (strcat mn "$" dy hr yr "-" mt sc )
-
- )
- (setq i 0)
- (setq pre (K:GetTime));前缀
- (setq Bnam Pre)
- (while (tblsearch "BLOCK" Bnam)
- (setq Bnam (strcat pre "-" (itoa (setq i (1+ i)))))
- );查找块名避免重复
- Bnam
- )
;选择对象后可以通过关键词S,决定建好的块要不要放零图层(默认放当前图层)。主要代码是这两句:
- (setq *ent* (entget (entlast)))
- (entmod (subst (cons 8 "0") (assoc 8 *ent*) *ent*))
|