明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1799|回复: 22

[源码] 修剪水平线和垂直线

[复制链接]
发表于 2023-6-25 12:02 | 显示全部楼层 |阅读模式
1明经币

附件: 您需要 登录 才可以下载或查看,没有账号?注册
发表于 2023-6-25 19:39 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复

使用道具 举报

 楼主| 发表于 2023-6-25 21:53 | 显示全部楼层

大佬,这个哪儿有?能分享吗?
回复

使用道具 举报

发表于 2023-6-25 22:52 | 显示全部楼层
(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" borderHorizLine1  borderHorizLine2 "" "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" borderVertLine1  borderVertLine2 "" "c" tempPt1 tempPt2  "")
                )
            )
        )
    )
    (setvar "cmdecho" #Vcomecho)
    (princ)
)
回复

使用道具 举报

发表于 2023-6-25 22:56 | 显示全部楼层
jiman 发表于 2023-6-25 22:52
(defun c:TestTrim( / #Vcomecho startPt endPt ss horizEntList vertEntList startTempValue endTempValue ...

中望CAD2020与浩辰CAD2020测试没问题;autoCAD2010会出问题。问题在使用“trim”命令时,窗交的两个点(tempPt1、tempPt2)有可能会出现在一个图元上导致窗交失败。
回复

使用道具 举报

 楼主| 发表于 2023-6-25 23:10 | 显示全部楼层
jiman 发表于 2023-6-25 22:56
中望CAD2020与浩辰CAD2020测试没问题;autoCAD2010会出问题。问题在使用“trim”命令时,窗交的两个点(t ...

谢谢你无私的回复,待会测试看看。
回复

使用道具 举报

 楼主| 发表于 2023-6-26 17:13 | 显示全部楼层
jiman 发表于 2023-6-25 22:56
中望CAD2020与浩辰CAD2020测试没问题;autoCAD2010会出问题。问题在使用“trim”命令时,窗交的两个点(t ...

大佬,auto2022测试了一下,只能框选两根线,多了就没用,不过还是真心感谢你的回复。
回复

使用道具 举报

发表于 2023-6-26 17:19 | 显示全部楼层
kzd2004 发表于 2023-6-26 17:13
大佬,auto2022测试了一下,只能框选两根线,多了就没用,不过还是真心感谢你的回复。

晚上下班回家了帮你改改
回复

使用道具 举报

 楼主| 发表于 2023-6-26 18:53 | 显示全部楼层
本帖最后由 kzd2004 于 2023-6-26 21:14 编辑
jiman 发表于 2023-6-26 17:19
晚上下班回家了帮你改改

万分感谢!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复

使用道具 举报

发表于 2023-6-26 21:00 | 显示全部楼层

目前使用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)
                )
                )
            )
            ;水平直线按从上到下排序
            (setq  i 0)
            (setq  j 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))
            )
            ;竖直直线按从左到右排序
            (setq  i 0)
            (setq  j 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" borderHorizLine1  borderHorizLine2 "" "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" borderVertLine1  borderVertLine2 "" "c" tempPt1 tempPt2  "")
                )
            )
        )
    )
    (setvar "cmdecho" #Vcomecho)
    (setvar "osmode" #Vosmode)
    (princ)
)
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-9 18:10 , Processed in 0.161290 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表