修剪水平线和垂直线
xyp1964 发表于 2023-6-25 19:39
大佬,这个哪儿有?能分享吗? (defun c:TestTrim( / #Vcomecho startPt endPt ss horizEntList vertEntList startTempValue endTempValue ssLen borderHorizLine1 borderHorizLine2 tempEnt borderVertLine1 borderVertLine2 tempPt1 tempPt2)
(setq #Vcomecho (getvar "cmdecho"))
(setvar "cmdecho" 0)
(setq startPt(getpoint "框选起点\n"))
(setq endPt(getcorner startPt "框选终点\n"))
(setq ss(ssget "_C" startPt endPt(list(cons 0 "LINE"))))
(setq horizEntList '())
(setq vertEntList '())
(setq startTempValue 0)
(setq endTempValue 0)
(setq i 0)
(if (/= ss nil)
(progn
(setq ssLen(sslength ss))
(while (< i ssLen)
(setq startTempValue (cadr(assoc 10(entget (ssname ss i)))))
(setq endTempValue (cadr(assoc 11(entget (ssname ss i)))))
(if (= startTempValue endTempValue) (setq vertEntList (cons (ssname ss i) vertEntList)) (setq horizEntList (cons (ssname ss i) horizEntList)))
(setq i (+ i 1))
)
;获取水平边界线
(setq borderHorizLine1 (nth 0 horizEntList))
(setq borderHorizLine2 (nth 0 horizEntList))
(foreach tempEnt horizEntList
(if (< (caddr(assoc 10 (entget borderHorizLine1)) ) (caddr(assoc 10 (entget tempEnt))))
(setq borderHorizLine1 tempEnt)
)
(if (> (caddr(assoc 10 (entget borderHorizLine2)) ) (caddr(assoc 10 (entget tempEnt))))
(setq borderHorizLine2 tempEnt)
)
)
;获取竖直边界线
(setq borderVertLine1 (nth 0 vertEntList))
(setq borderVertLine2 (nth 0 vertEntList))
(foreach tempEnt vertEntList
(if (< (cadr(assoc 10 (entget borderVertLine1)) ) (cadr(assoc 10 (entget tempEnt))))
(setq borderVertLine1 tempEnt)
)
(if (> (cadr(assoc 10 (entget borderVertLine2)) ) (cadr(assoc 10 (entget tempEnt))))
(setq borderVertLine2 tempEnt)
)
)
;剪裁
(if (> (cadr endPt) (cadr startPt) )
(progn
(setq tempPt1 (list (car startPt) (/ (+ (cadr(cdr(assoc 11 (entget borderHorizLine1))))(cadr(cdr(assoc 11 (entget borderHorizLine2))))) 2) 0))
(setq tempPt2 (list (car endPt) (/ (+ (cadr tempPt1)(cadr(cdr(assoc 11 (entget borderHorizLine2))))) 2) 0))
(command "trim" borderHorizLine1borderHorizLine2 "" "c" tempPt1 tempPt2"")
)
(progn
(setq tempPt1 (list (/ (+ (car(cdr(assoc 11 (entget borderVertLine1))))(car(cdr(assoc 11 (entget borderVertLine2))))) 2)(cadr startPt)0))
(setq tempPt2 (list (/ (+ (car tempPt1)(car(cdr(assoc 10 (entget borderVertLine2))))) 2) (cadr endPt) 0))
(command "trim" borderVertLine1borderVertLine2 "" "c" tempPt1 tempPt2"")
)
)
)
)
(setvar "cmdecho" #Vcomecho)
(princ)
) jiman 发表于 2023-6-25 22:52
(defun c:TestTrim( / #Vcomecho startPt endPt ss horizEntList vertEntList startTempValue endTempValue ...
中望CAD2020与浩辰CAD2020测试没问题;autoCAD2010会出问题。问题在使用“trim”命令时,窗交的两个点(tempPt1、tempPt2)有可能会出现在一个图元上导致窗交失败。 jiman 发表于 2023-6-25 22:56
中望CAD2020与浩辰CAD2020测试没问题;autoCAD2010会出问题。问题在使用“trim”命令时,窗交的两个点(t ...
谢谢你无私的回复,待会测试看看。 jiman 发表于 2023-6-25 22:56
中望CAD2020与浩辰CAD2020测试没问题;autoCAD2010会出问题。问题在使用“trim”命令时,窗交的两个点(t ...
大佬,auto2022测试了一下,只能框选两根线,多了就没用,:handshake不过还是真心感谢你的回复。 kzd2004 发表于 2023-6-26 17:13
大佬,auto2022测试了一下,只能框选两根线,多了就没用,不过还是真心感谢你的回复。
晚上下班回家了帮你改改 本帖最后由 kzd2004 于 2023-6-26 21:14 编辑
jiman 发表于 2023-6-26 17:19
晚上下班回家了帮你改改
万分感谢! kzd2004 发表于 2023-6-26 18:53
成分感谢!
目前使用AutoCAD2010测试OK。晚点看看有时间的话,我再改一版不使用剪裁命令的发给你测试测试。
我也是初学者,权当练手。
(defun c:TTrim( / #Vcomecho startPt endPt ss horizEntList vertEntList startTempValue endTempValue ssLen borderHorizLine1 borderHorizLine2 tempEnt borderVertLine1 borderVertLine2 tempPt1 tempPt2 i j TempValue1 TempValue2)
(setq #Vcomecho (getvar "cmdecho"))
(setq #Vosmode (getvar "osmode")) ;获取捕捉对象的系统变量
(setvar "cmdecho" 0)
(setvar "osmode" 0) ;设置不捕捉
(setq startPt(getpoint "框选起点\n"))
(setq endPt(getcorner startPt "框选终点\n"))
(setq ss(ssget "_C" startPt endPt(list(cons 0 "LINE"))))
(setq horizEntList '()) ;水平直线的表集合
(setq vertEntList '()) ;竖直直线的表集合
(setq startTempValue 0)
(setq endTempValue 0)
(setq i 0)
(if (/= ss nil)
(progn
(setq ssLen(sslength ss))
;将所选直线分类为水平和竖直,放置两个表中
(while (< i ssLen)
(setq startTempValue (cadr(assoc 10(entget (ssname ss i)))))
(setq endTempValue (cadr(assoc 11(entget (ssname ss i)))))
(if (= startTempValue endTempValue) (setq vertEntList (cons (ssname ss i) vertEntList)) (setq horizEntList (cons (ssname ss i) horizEntList)))
(setq i (+ i 1))
)
;判断直线数量,少于2条则退出程序
(cond
( (< (length horizEntList) 2)
(progn
(alert "框选的水平和竖直线都不能少于2条!")
(setvar "cmdecho" #Vcomecho)
(setvar "osmode" #Vosmode)
(quit)
)
)
( (< (length vertEntList) 2)
(progn
(alert "框选的水平和竖直线都不能少于2条!")
(setvar "cmdecho" #Vcomecho)
(setvar "osmode" #Vosmode)
(quit)
)
)
)
;水平直线按从上到下排序
(setqi 0)
(setqj 0)
(while (< i (length horizEntList))
(setq j (+ i 1))
(while (< j (length horizEntList))
(if (> (cadr (cdr (assoc 10 (entget (nth j horizEntList))))) (cadr (cdr (assoc 10 (entget (nth i horizEntList) ) ) )))
(progn
(setq TempValue1 (nth i horizEntList))
(setq TempValue2 (nth j horizEntList))
(setq horizEntList( subst "fst" (nth i horizEntList) horizEntList))
(setq horizEntList( subst "scd" (nth j horizEntList) horizEntList))
(setq horizEntList( subst TempValue2 (nth i horizEntList) horizEntList))
(setq horizEntList( subst TempValue1 (nth j horizEntList) horizEntList))
)
)
(setq j (+ j 1))
)
(setq i (+ i 1))
)
;竖直直线按从左到右排序
(setqi 0)
(setqj 0)
(while (< i (length vertEntList))
(setq j (+ i 1))
(while (< j (length vertEntList))
(if (< (car (cdr (assoc 10 (entget (nth j vertEntList))))) (car (cdr (assoc 10 (entget (nth i vertEntList) ) ) )))
(progn
(setq TempValue1 (nth i vertEntList))
(setq TempValue2 (nth j vertEntList))
(setq vertEntList( subst "fst" (nth i vertEntList) vertEntList))
(setq vertEntList( subst "scd" (nth j vertEntList) vertEntList))
(setq vertEntList( subst TempValue2 (nth i vertEntList) vertEntList))
(setq vertEntList( subst TempValue1 (nth j vertEntList) vertEntList))
)
)
(setq j (+ j 1))
)
(setq i (+ i 1))
)
;获取水平边界线
(setq borderHorizLine1 (nth 0 horizEntList))
(setq borderHorizLine2 (nth (- (length horizEntList) 1) horizEntList))
;获取竖直边界线
(setq borderVertLine1 (nth 0 vertEntList))
(setq borderVertLine2 (nth (- (length vertEntList) 1) vertEntList))
;剪裁;通过计算赋值,排除窗交的点处于框选的直线上从而导致剪裁失效
(if (> (cadr endPt) (cadr startPt) )
(progn
(setq tempPt1 (list (car startPt) (/ (+ (cadr(cdr(assoc 11 (entget borderHorizLine1))))(cadr(cdr(assoc 11 (entget (nth 1 horizEntList) ))))) 2) 0))
(setq tempPt2 (list (car endPt) (/ (+ (cadr tempPt1) (cadr(cdr(assoc 11 (entget (nth 1 horizEntList))))) )2) 0))
(command "trim" borderHorizLine1borderHorizLine2 "" "c" tempPt1 tempPt2"")
)
(progn
(setq tempPt1 (list (/ (+ (car(cdr(assoc 11 (entget (nth 1 vertEntList)))))(car(cdr(assoc 11 (entget borderVertLine1))))) 2)(cadr startPt)0))
(setq tempPt2 (list (/ (+ (car tempPt1)(car(cdr(assoc 11 (entget (nth 1 vertEntList)))))) 2) (cadr endPt) 0))
(command "trim" borderVertLine1borderVertLine2 "" "c" tempPt1 tempPt2"")
)
)
)
)
(setvar "cmdecho" #Vcomecho)
(setvar "osmode" #Vosmode)
(princ)
)