原贴地址:消除合并重复直线、圆弧或圆

    1. ;;修正了一个错误。
    2. ;;这个更快,可以连接线条。
    3. ;;消除合并重复线条
    4. (defun c:yad_undup(/ dxf tang chg_ent on_ent undup os 2pi sline sarc scircle n ss ent pmt)
    5. (defun dxf(ent i)
    6. (cdr (assoc i (entget ent)))
    7. )
    8. (defun tang(ang sty)
    9. (rem (+ 2pi ang) sty)
    10. )
    11. (defun chg_ent(ent i pt / en)
    12. (setq en (entget ent) en (subst (cons i pt) (assoc i en) en))
    13. (entmod en)
    14. )
    15. (defun on_ent(a a1 a2 sty)
    16. (if (= sty "直线")
    17. (equal (+ (distance a1 a) (distance a a2)) (distance a1 a2) 0.01)
    18. (if (> a2 a1) (>= a2 a a1) (or (<= a a2) (>= a a1)))
    19. )
    20. )
    21. (defun undup(s sty / nm m ss ent1 spt1 ept1 cpt1 r1 ent2 spt2 ept2 cpt2 r2)
    22. (setq n -1 nm 0)
    23. (if (= sty "圆")
    24. (repeat (sslength s)
    25. (setq ent1 (ssname s (setq n (1+ n))))
    26. (if (entget ent1)
    27. (progn
    28. (setq cpt1 (dxf ent1 10) r1 (dxf ent1 40))
    29. (if (setq ss (ssget "x" (list (cons 0 "circle") (cons 10 cpt1) (cons 40 r1))))
    30. (progn
    31. (ssdel ent1 ss)
    32. (setq m -1)
    33. (repeat (sslength ss)
    34. (setq ent (ssname ss (setq m (1+ m))))
    35. (if (not (ssmemb ent s))
    36. (progn
    37. (ssdel ent ss)
    38. (setq m (1- m))
    39. )
    40. )
    41. )
    42. (command "_.erase" ss "")
    43. (setq nm (+ nm (sslength ss)))
    44. )
    45. )
    46. )
    47. )
    48. )
    49. (repeat (sslength s)
    50. (setq ent1 (ssname s (setq n (1+ n))))
    51. (if (entget ent1)
    52. (progn
    53. (if (= sty "直线")
    54. (setq spt1 (dxf ent1 10) ept1 (dxf ent1 11))
    55. (setq spt1 (tang (dxf ent1 50) 2pi)
    56. ept1 (tang (dxf ent1 51) 2pi)
    57. cpt1 (dxf ent1 10)
    58. r1 (dxf ent1 40)
    59. )
    60. )
    61. (if (setq ss (if (= sty "直线")
    62. (ssget "cp" (list (polar spt1 (angle ept1 spt1) 0.1)
    63. (polar ept1 (- (angle spt1 ept1) (/ pi 4)) 0.15)
    64. (polar ept1 (+ (angle spt1 ept1) (/ pi 4)) 0.15)
    65. ) '((0 . "line"))
    66. )
    67. (ssget "x" (list (cons 0 "arc") (cons 10 cpt1) (cons 40 r1)))
    68. )
    69. )
    70. (progn
    71. (ssdel ent1 ss)
    72. (setq m -1)
    73. (repeat (sslength ss)
    74. (setq ent (ssname ss (setq m (1+ m))))
    75. (if (not (ssmemb ent s))
    76. (progn
    77. (ssdel ent ss)
    78. (setq m (1- m))
    79. )
    80. )
    81. )
    82. (setq m -1)
    83. (repeat (sslength ss)
    84. (setq ent2 (ssname ss (setq m (1+ m))))
    85. (if (= sty "直线")
    86. (setq spt2 (dxf ent2 10)
    87. ept2 (dxf ent2 11)
    88. )
    89. (setq spt2 (tang (dxf ent2 50) 2pi)
    90. ept2 (tang (dxf ent2 51) 2pi)
    91. cpt2 (dxf ent2 10)
    92. r2 (dxf ent2 40)
    93. )
    94. )
    95. (cond
    96. ((and (on_ent spt2 spt1 ept1 sty) (on_ent ept2 spt1 ept1 sty))
    97. (entdel ent2)
    98. )
    99. ((and (on_ent spt1 spt2 ept2 sty) (on_ent ept1 spt2 ept2 sty))
    100. (entdel ent1)
    101. (setq ent1 ent2 spt1 spt2 ept1 ept2)
    102. (if (= sty "圆弧")
    103. (setq cpt1 cpt2 r1 r2)
    104. )
    105. )
    106. ((and (if (= sty "直线")
    107. (equal (tang (angle spt1 ept1) pi) (tang (angle spt2 ept2) pi) 0.001)
    108. T
    109. )
    110. (or (on_ent spt2 spt1 ept1 sty) (on_ent ept2 spt1 ept1 sty))
    111. )
    112. (entdel ent2)
    113. (if (= sty "直线")
    114. (progn
    115. (if (on_ent spt2 spt1 ept1 sty)
    116. (setq spt2 ept2)
    117. )
    118. (if (> (distance spt1 spt2) (distance ept1 spt2))
    119. (progn (chg_ent ent1 11 spt2) (setq ept1 spt2))
    120. (progn (chg_ent ent1 10 spt2) (setq spt1 spt2))
    121. )
    122. )
    123. (if (on_ent spt2 spt1 ept1 sty)
    124. (progn (chg_ent ent1 51 ept2) (setq ept1 ept2))
    125. (progn (chg_ent ent1 50 spt2) (setq spt1 spt2))
    126. )
    127. )
    128. )
    129. (T (setq nm (1- nm)))
    130. )
    131. (setq nm (1+ nm))
    132. )
    133. )
    134. )
    135. )
    136. )
    137. )
    138. )
    139. (prompt (strcat "选到" (itoa (sslength s)) "个" sty "消去" (itoa nm) "个!"))
    140. )
    141. (command "_.undo" "_be")
    142. (command "_.ucs" "")
    143. (setq os (getvar "osmode")
    144. 2pi (* 2 pi)
    145. sline (ssadd)
    146. sarc (ssadd)
    147. scircle (ssadd)
    148. n -1
    149. )
    150. (setvar "cmdecho" 0)
    151. (setvar "osmode" 0)
    152. (prompt "\n请选择直线、圆弧或圆:")
    153. (if (setq ss (ssget '((0 . "line,arc,circle"))))
    154. (progn
    155. (prompt "\n正在消除合并重复线条,请稍候...")
    156. (prompt "\n")
    157. (repeat (sslength ss)
    158. (setq ent (ssname ss (setq n (1+ n))))
    159. (cond
    160. ((= (dxf ent 0) "LINE") (ssadd ent sline))
    161. ((= (dxf ent 0) "ARC") (ssadd ent sarc))
    162. ((= (dxf ent 0) "CIRCLE") (ssadd ent scircle))
    163. )
    164. )
    165. )
    166. )
    167. (if (> (sslength sline) 1) (progn (undup sline "直线") (setq pmt T)))
    168. (if (> (sslength sarc) 1) (progn (undup sarc "圆弧") (setq pmt T)))
    169. (if (> (sslength scircle) 1) (progn (undup scircle "圆") (setq pmt T)))
    170. (if (and ss (not pmt)) (prompt "\n没有重复实体!"))
    171. (setvar "osmode" os)
    172. (command "_.undo" "_e")
    173. (princ)
    174. )
    175. (prompt "\n***消除合并重复线条yad_undup*** YAD建筑")
    176. (princ)