明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: hhh454

[基础] 自己学着写个代码,对象外围线,有些代码不会使用,请老师指正

  [复制链接]
发表于 2011-4-27 11:23:48 | 显示全部楼层
(defun getArea ( entName / )
   (command "_.AREA" "O" entName)
   (setq objArea (getvar "AREA"))
) ;defun

(defun c:wwq(/ oldEcho objsToWrap extMin extMax minX minY maxX maxY
                   diagDist boxOffset boxLL boxLR boxUR boxUL
                   boundPoint boxObj newObjs lastEntName entName
                   newObjsLen maxArea counter thisArea wrapOption oce vs1 ent_list id list_len ent msg od
               )
   (setq oldEcho (getvar "CMDECHO"))
   (setvar "CMDECHO" 0)
   (prompt "\nWrap Objects (w/Polyline(s)):")
   ;|
       Swap commenting with (setq) line below...
       If you want ELLIPSE and SPLINE objects to be selectable
   |;
   ;(setq objsToWrap (ssget))
   (setq objsToWrap
       (ssget '(
           (-4 . "<NOT")
               (-4 . "<OR")
                   (0 . "ELLIPSE")
                   (0 . "SPLINE")
               (-4 . "OR>")
           (-4 . "NOT>"))
       ) ;ssget
   ) ;setq
   
   (if (/= objsToWrap nil)
       (progn
           (command "_.UNDO" "BEGIN")
           (setq wrapOption "Single") ;default to single
           (initget "Single Multiple")
           (prompt "\nWrap Options:")
           (prompt "\n   Single: Only the largest outer profile will be created.")
           (prompt "\n   Multiple: Nested, or detatched profiles will also be created.")
           (setq wrapOption (getkword "\nWrap option [Single/Multiple] <Single>: "))
           (if (= wrapOption "Multiple")
               (setq wrapOption "Multiple")
               (setq wrapOption "Single")
           ) ;if
           
           ; Create bounding box, larger than existing drawing...
           (setq extMin (getvar "EXTMIN"))
           (setq extMax (getvar "EXTMAX"))
           (setq minX (car extMin))
           (setq minY (cadr extMin))
           (setq maxX (car extMax))
           (setq maxY (cadr extMax))
           
           (setq diagDist (distance extMin extMax))
           (setq boxOffset (* diagDist 0.1))
           
           (setq boxLL (list (- minX boxOffset) (- minY boxOffset) 0))
           (setq boxLR (list (+ maxX boxOffset) (- minY boxOffset) 0))
           (setq boxUR (list (+ maxX boxOffset) (+ maxY boxOffset) 0))
           (setq boxUL (list (- minX boxOffset) (+ maxY boxOffset) 0))

           (setq boundPoint (list (- minX (/ boxOffset 2)) (- minY (/ boxOffset 2)) 0))

           (command "_.PLINE" boxLL boxLR boxUR boxUL "C")
           (setq boxObj (entlast))

           (command "_.-BOUNDARY" "A" "O" "P" "I" "Y" "B" "N" boxObj objsToWrap "" "" boundPoint "")
           
           ; Get a list of the entities created by the boundary command...
           (setq lastEntName boxObj)
           (while (setq entName (entnext lastEntName))
               (setq newObjs (append newObjs (list entName)))
               (setq lastEntName entName)
           ) ;while

           ;Get the greatest object area, of the new objects (the duplicate polyline, of our temp box)
           (setq newObjsLen (length newObjs))
           (setq maxArea 0)
           
           (setq counter 0)
           (while (< counter newObjsLen)
               (setq thisArea (getArea (nth counter newObjs)))
               (if (>= thisArea maxArea)
                   (setq maxArea thisArea)
               ) ;if
               (setq counter (1+ counter))
           ) ;while
           
           ; Delete the Object, that matches the maxArea (the duplicate polyline)
           (setq counter 0)
           (while (< counter newObjsLen)
               (setq thisArea (getArea (nth counter newObjs)))
               (if (= thisArea maxArea)
                   (progn
                       (entdel (nth counter newObjs))
                       (setq counter newObjsLen) ;break loop
                   ) ;progn
               ) ;if
               (setq counter (1+ counter))
           ) ;while
           
           (if (= wrapOption "Single")
               (progn
                   ; Get the REMAINING new objects...
                   (setq newObjs nil)
                   (setq lastEntName boxObj)
                   (while (setq entName (entnext lastEntName))
                       (setq newObjs (append newObjs (list entName)))
                       (setq lastEntName entName)
                   ) ;while

                   ; Get the NEXT greatest object area, of the REMAINING new objects (this is the one we want to keep!)
                   (setq newObjsLen (length newObjs))
                   (setq maxArea 0)

                   (setq counter 0)
                   (while (< counter newObjsLen)
                       (setq thisArea (getArea (nth counter newObjs)))
                       (if (>= thisArea maxArea)
                           (setq maxArea thisArea)
                       ) ;if
                       (setq counter (1+ counter))
                   ) ;while

                   ; Delete the Object, UNLESS it matches the maxArea (the one we want to keep!)...
                   (setq counter 0)
                   (while (< counter newObjsLen)
                       (setq thisArea (getArea (nth counter newObjs)))
                       (if (/= thisArea maxArea)
                           (progn
                               (entdel (nth counter newObjs))
                           ) ;progn
                       ) ;if
                       (setq counter (1+ counter))
                   ) ;while
               ) ;progn
           ) ;if
           
           ;Erase the original box...
           (command "_.ERASE" boxObj "")
           (command "_.UNDO" "END")
   (princ)
  (setq vs1 (getvar "vsmax"))
  (setq msg (strcat "输入单边补偿量+/- <"  (rtos (getvar "offsetdist")) ">:" ))
  (setq od (getdist  msg ))
  (if (= nil od) 
   (setq od (getvar "offsetdist"))
   (setvar "offsetdist" od)
  )
  (setq ent_list (ssget))
  (setq list_len (sslength ent_list))
  (setq id 0)
  (ssname ent_list id)
  (repeat list_len 
    (setq ent (ssname ent_list id))
    (command ".offset"  "" ent vs1 "")
    (command ".change" (entlast) "" "p" "c" "red" "")
    (entdel ent)
    (setq id (1+ id))
  )
  (prinC (strcat (rtos list_len 2 0)  " 个对象,做单边补偿:" (rtos (getvar "offsetdist"))  "mm,已完成!"))
       ) ;progn
   ) ;if
   (setvar "CMDECHO" oldEcho)
) ;defun
(princ)


这个,可以改下,可以生成单个外形或者多个外形,偏移自己可以输入,但现只支持一个个的偏移
发表于 2011-4-29 23:09:14 | 显示全部楼层
挺好的,只是我用不上
发表于 2013-5-18 10:12:52 | 显示全部楼层
学习了,还是要找LSP
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-5-30 02:11 , Processed in 0.161766 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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