Jump to content

Lisp lệnh T1 trong chủ đề “Bạn nào có ý tưởng nào hay về thuật toán để giải quyết vấn đề này không ?”

Một bài toán hay, lâu rùi mới có hứng thú lại với Lisp ^^. Đây là giải pháp của mình dựa trên thuật toán của các bác đưa ra, nếu viết có luộm thuộm các bác thông cảm :D . Chưa có thời gian test kỹ, các bác dùng thử nhé ^_^ .

* Lưu ý kích thước MN quá nhỏ so với PQ có thể gây ra lỗi xác định diện tích, nếu PQ có nhiều điểm cực tiểu, chương trình chỉ tính toán cho điểm điểm cực tiểu thấp nhất.

;; Skywings - Revised 240412
(vl-load-com)
(defun c:t1 (/           AREALST     ENDPNT       ENDPNTMN  FROMPNT
         FUZZ      I0     I1       I2         I3
         INTPNTS   IX     IY       LWP0         LWP1
         LWPENT    LWPOBJ     MN       MNDIST    MSPACE
         PNT0      PNT1     PQ       S0         S1
         S2           S3     STARTPNT  STARTPNTMN
         STARTPNTPQ         SUB01       SUB01NEW  SUB32
         SUB32NEW  THISDRAWING       TMPLWP    TOPNTX
         TOPNTY    MAXPOINT     MINPOINT  ISFLIPED  TMPLWPOBJ
         BOTPNT    MINMAXPNTS       TOPPNT    N
        )
  ;; Thiet lap
  (setq thisdrawing (vla-get-activedocument (vlax-get-acad-object)))
  (setq mspace (vla-get-modelspace thisdrawing))
  (vla-startundomark thisdrawing)
  (setvar "CMDECHO" 0)
  (if (null MNdistSv)
    (setq MNdistSv 5)
  )
  (if (null fuzzSv)
    (setq fuzzSv 0.1)
  )
  ;; Du lieu dau vao
  (initget (+ 2 4))
  (setq    MNdist (getreal    (strcat    "\nXac dinh chieu dai MN: <"
                (rtos MNdistSv 2 2)
                ">"
            )
           )
  )
  (if (null MNdist)
    (setq MNdist MNdistSv)
    (setq MNdistSv MNdist)
  )
  (initget (+ 2 4))
  (setq    fuzz (getreal (strcat "\nXac dinh do chinh xac dien tich: <"
                  (rtos fuzzSv 2 2)
                  ">"
              )
         )
  )
  (if (null fuzz)
    (setq fuzz fuzzSv)
    (setq fuzzSv fuzz)
  )
  (while (null
       (setq lwpEnt (car (entsel "\nChon duong cong tich luy: ")))
     )
    (setq lwpEnt (car (entsel "\nChon duong cong tich luy: ")))
  )
  (setq lwpObj (vlax-ename->vla-object lwpEnt))
  (setq    startPnt (vlax-curve-getStartPoint lwpObj)
    endPnt     (vlax-curve-getEndPoint lwpObj)
  )
  ;; Kiem tra diem cuc tieu
  (setq MinMaxPnts (LM:CurveMinMax lwpObj 1e-8))
  (setq    TopPnt (cadr MinMaxPnts)
    BotPnt (car MinMaxPnts)
  )
  (if (or (equal (distance BotPnt startPnt) 0.0 0.01)
      (equal (distance BotPnt endPnt) 0.0 0.01)
      )
    (progn
      (setq tmpLwpObj (vla-mirror
            lwpObj
            (vlax-3d-point startPnt)
            (vlax-3d-point (polar startPnt 0 0.1))
              )
              ;; lat bung
      )
      (vla-delete lwpObj)
      (setq lwpObj   tmpLwpObj
        isFliped 1
      )
      (setq startPnt (vlax-curve-getStartPoint lwpObj)
        endPnt   (vlax-curve-getEndPoint lwpObj)
      )
    )
  )
  (vla-GetBoundingBox lwpObj 'minpoint 'maxpoint)
  (vla-ZoomWindow (vlax-get-acad-object) minpoint maxpoint)
  ;; Xac dinh duong PQ
  (if (< (cadr startPnt) (cadr endPnt))
    (setq startPntPQ startPnt)
    (setq startPntPQ endPnt)
  )
  (setq    tmpLwp (AddLwpolyline
         (list startPntPQ (polar startPntPQ 0 1))
         0
         mspace
           )
  )
  (setq intPnts (LM:Intersections tmpLwp lwpObj acExtendThisEntity))
  (if (> (length intPnts) 1)
    (setq PQ (AddLwpolyline intPnts 0 mspace))
    (progn
      (vla-delete tmpLwp)
      (reset)
      (exit)
    )
  )
  (vla-delete tmpLwp)
  ;; Chieu dai doan MN
  (if (= isFliped 1)
    (setq BotPnt (car (LM:CurveMinMax lwpObj 1e-8)))
  )
  (setq    startPntMN (polar BotPnt pi (/ MNdist 2))
    endPntMN   (polar BotPnt 0 (/ MNdist 2))
  )
  (setq MN (AddLwpolyline (list startPntMN endPntMN) 0 mspace))
  ;; Xac dinh 2 duong giong tu MN den PQ
  (setq    pnt0 (vlax-curve-getClosestPointTo PQ startPntMN)
    pnt1 (vlax-curve-getClosestPointTo PQ endPntMN)
  )
  (setq    lwp0 (AddLwpolyline (list startPntMN pnt0) 0 mspace)
    lwp1 (AddLwpolyline (list endPntMN pnt1) 0 mspace)
  )
  ;; Kiem tra dien tich
  (setq AreaLst (CalArea (list pnt0 startPntMN endPntMN pnt1) lwpObj))
  (setq    s0 (nth 0 AreaLst)
    s1 (nth 1 AreaLst)
    s2 (nth 2 AreaLst)
    s3 (nth 3 AreaLst)
  )
  (if (and (< s0 s1)
       (< s3 s2)
      )
    (progn
      (alert "Khong the dieu chinh!")
      (reset)
      (exit)
    )
  )
  ;; Can bang so bo 1 cap dien tich
  (textscr)
  (setq iY (- 0 (/ (distance startPntMN pnt0) 20)))
                    ; buoc nhay phuong doc
  (setq    FromPnt    (vlax-3d-point pnt0)
    ToPntY    (vlax-3d-point (polar pnt0 (/ pi 2) iY))
  )
  (while (and (>= (nth 0 AreaLst) (nth 1 AreaLst))
          (>= (nth 3 AreaLst) (nth 2 AreaLst))
     )
    (vla-move PQ FromPnt ToPntY)
    (setq pnt0 (polar pnt0 (/ pi 2) iY)
      pnt1 (polar pnt1 (/ pi 2) iY)
    )
    (setq AreaLst (CalArea (list pnt0 startPntMN endPntMN pnt1) lwpObj))
  )
  (if (> (- (nth 0 AreaLst) (nth 1 AreaLst))
     (- (nth 3 AreaLst) (nth 2 AreaLst))
      )
    (setq i0 3
      i1 2
      i2 1
      i3 0
    )
    (setq i0 0
      i1 1
      i2 2
      i3 3
    )
  )
  (setq n 0)
  (setq sub32 (- (nth i3 AreaLst) (nth i2 AreaLst)))
  ;; Can bang dien tich dua tren dich chuyen PQ
  (while (not (equal (nth i3 AreaLst) (nth i2 AreaLst) fuzz))
    (if    (> n 100) ;; Gioi han so vong lap
      (progn
    (alert "Vuot qua gioi han 100 lan thu!")
    (reset)
    (exit)
      )
    )
    (vla-move PQ
          (vlax-3d-point pnt0)
          (vlax-3d-point (polar pnt0 (/ pi 2) iY))
    )
    (setq pnt0 (polar pnt0 (/ pi 2) iY)
      pnt1 (polar pnt1 (/ pi 2) iY)
    )
    (setq iX (/ MNdist 20.00))            ; buoc nhay phuong ngang
    (setq AreaLst (reverse (CalArea (list pnt0 startPntMN endPntMN pnt1) lwpObj)))
    (setq sub01    (- (nth i0 AreaLst) (nth i1 AreaLst)))
    (setq FromPnt (vlax-3d-point pnt0)
      ToPntX  (vlax-3d-point (polar pnt0 0 iX))
    )
    ;; Can bang dien tich dua tren dich chuyen MN
    (while (> (abs sub01) (abs sub32))
      (vla-move MN FromPnt ToPntX)
      (vla-move lwp0 FromPnt ToPntX)
      (vla-move lwp1 FromPnt ToPntX)
      (setq pnt0       (polar pnt0 0 iX)
        pnt1       (polar pnt1 0 iX)
        startPntMN (polar startPntMN 0 iX)
        endPntMN   (polar endPntMN 0 iX)
      )
      (setq AreaLst (reverse (CalArea (list pnt0 startPntMN endPntMN pnt1) lwpObj)))
      (setq sub01new (- (nth i0 AreaLst) (nth i1 AreaLst)))
      (if (> (abs sub01new) (abs sub01))
    (setq iX (/ (- 0 iX) 2))
      )
      (setq sub01 sub01new)
      (setq FromPnt (vlax-3d-point pnt0)
        ToPntX  (vlax-3d-point (polar pnt0 0 iX))
      )
    )
    (setq sub32new (- (nth i3 AreaLst) (nth i2 AreaLst)))
    (if    (> (abs sub32new) (abs sub32))
      (setq iY (/ (- 0 iY) 2))
    )
    (setq sub32 sub32new)
    (setq n (1+ n))
    (print AreaLst)
  )
  (vla-zoomprevious (vlax-get-acad-object))
  (reset)
  (princ)
)
(defun reset ()
  (if (= isFliped 1)
    (progn
      (foreach obj (list lwpObj MN PQ lwp0 lwp1)
    (vla-mirror
      obj
      (vlax-3d-point startPnt)
      (vlax-3d-point (polar startPnt 0 0.1))
    )
    (vla-delete obj)
      )
    )
  )
  (vla-endundomark thisdrawing)
  (graphscr)
)

;; Sub function
(defun GetMidPnt (pt0 pt1 / midPnt)
  (setq    midPnt (list (/ (+ (car pt0) (car pt1)) 2)
             (/ (+ (cadr pt0) (cadr pt1)) 2)
             0
           )
  )
  midPnt
)
(defun CalArea (pntLst obj / AreaLst tmpLwp)
  (setq
    midPntLst (mapcar
        '(lambda (x)
           (GetMidPnt x (vlax-curve-getClosestPointTo obj x))
         )
        pntLst
          )
  )
  (foreach pt midPntLst
    (setq HdlEnt (cdr (assoc 5 (entget (entlast)))))
    (command ".boundary" pt "")
    (if    (/= HdlEnt (cdr (assoc 5 (entget (entlast)))))
      (progn
    (setq tmpLwp (vlax-ename->vla-object (entlast)))
    (setq AreaLst (cons (vlax-get-property tmpLwp 'AREA) AreaLst))
    (vla-delete tmpLwp)
      )
      (progn
    (alert "Khong the tao Boundary!")
    (reset)
    (exit)
      )
    )
  )
  AreaLst
)
(defun LM:Intersections    (obj1 obj2 mode / l r)
  (setq l (vlax-invoke obj1 'intersectwith obj2 mode))
  (repeat (/ (length l) 3)
    (setq r (cons (list (car l) (cadr l) (caddr l)) r)
      l (cdddr l)
    )
  )
  (reverse r)
)

(defun AddLwpolyline (lst-pnt layer *model-space* / array-pt myPline)
  (setq    array-pt (list->variantArray
           (apply 'append (mapcar '3dPnt->2dPnt lst-pnt))
         )
    myPline     (vla-AddLightWeightPolyline *model-space* array-pt)
  )
  (vla-put-layer myPline layer)
  myPline
)

(defun list->variantArray (ptsList / arraySpace sArray)
  (setq    arraySpace
     (vlax-make-safearray
       vlax-vbdouble
       (cons 0
         (- (length ptsList) 1)
       )
     )
  )
  (setq sArray (vlax-safearray-fill arraySpace ptsList))
  (vlax-make-variant sArray)
)

(defun 3dPnt->2dPnt (3dpt)
  (list (float (car 3dpt)) (float (cadr 3dpt)))
)

(defun LM:CurveMinMax
              (obj         fuzz       /
               _GetBoundingBoxWithOffset   _GroupByNum
               _FlattenPoint a           acdoc
               acspc         lst       obj
               tmp
              )

  (defun _GetBoundingBoxWithOffset (obj o / ll ur)
    (
     (lambda (a)
       (mapcar
     (function
       (lambda (b)
         (mapcar
           (function
         (lambda (c) ((eval c) a))
           )
           b
         )
       )
     )
     '(
       (
        (lambda (x) (- (caar x) o))
        (lambda (x) (- (cadar x) o))
       )
       (
        (lambda (x) (+ (caadr x) o))
        (lambda (x) (- (cadar x) o))
       )
       (
        (lambda (x) (+ (caadr x) o))
        (lambda (x) (+ (cadadr x) o))
       )
       (
        (lambda (x) (- (caar x) o))
        (lambda (x) (+ (cadadr x) o))
       )
      )
       )
     )
      (mapcar 'vlax-safearray->list
          (progn (vla-getboundingbox obj 'll 'ur) (list ll ur))
      )
    )
  )

  (defun _GroupByNum (l n / r)
    (if    l
      (cons
    (reverse (repeat n
           (setq r (cons (car l) r)
             l (cdr l)
           )
           r
         )
    )
    (_GroupByNum l n)
      )
    )
  )

  (defun _FlattenPoint (p)
    (list (car p) (cadr p) 0.0)
  )

  (setq    acdoc (vla-get-activedocument (vlax-get-acad-object))
    acspc (vlax-get-property
        acdoc
        (if (= 1 (getvar 'CVPORT))
          'Paperspace
          'Modelspace
        )
          )
  )
  (cond
    ((not (vlax-method-applicable-p obj 'GetBoundingBox))
    )
    (t
     (setq tmp
        (mapcar
          (function
        (lambda    (x)
          (apply 'vla-addline (cons acspc (mapcar 'vlax-3D-point x)))
        )
          )
          (_GroupByNum
        (mapcar    '_FlattenPoint
            (_GetBoundingBoxWithOffset obj (- fuzz))
        )
        2
          )
        )
     )
     (setq lst
        (mapcar
          (function
        (lambda    (x)
          (car
            (_GroupByNum
              (vlax-invoke obj 'Intersectwith x acExtendOtherEntity)
              3
            )
          )
        )
          )
          tmp
        )
     )
     (mapcar 'vla-delete tmp)
     lst
    )
  )
)

File bản vẽ test

https://www.cadviet.com/upfiles/3/86115_test.dwg

Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.

Cách dùng

  1. Chép mã nguồn, lưu thành tệp 232523_t1.lsp.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh T1 để chạy lisp.

Mã nguồn 232523_t1.lsp (trích từ bài viết gốc)

;; Skywings - Revised 240412
(vl-load-com)
(defun c:t1 (/           AREALST     ENDPNT       ENDPNTMN  FROMPNT
         FUZZ      I0     I1       I2         I3
         INTPNTS   IX     IY       LWP0         LWP1
         LWPENT    LWPOBJ     MN       MNDIST    MSPACE
         PNT0      PNT1     PQ       S0         S1
         S2           S3     STARTPNT  STARTPNTMN
         STARTPNTPQ         SUB01       SUB01NEW  SUB32
         SUB32NEW  THISDRAWING       TMPLWP    TOPNTX
         TOPNTY    MAXPOINT     MINPOINT  ISFLIPED  TMPLWPOBJ
         BOTPNT    MINMAXPNTS       TOPPNT    N
        )
  ;; Thiet lap
  (setq thisdrawing (vla-get-activedocument (vlax-get-acad-object)))
  (setq mspace (vla-get-modelspace thisdrawing))
  (vla-startundomark thisdrawing)
  (setvar "CMDECHO" 0)
  (if (null MNdistSv)
    (setq MNdistSv 5)
  )
  (if (null fuzzSv)
    (setq fuzzSv 0.1)
  )
  ;; Du lieu dau vao
  (initget (+ 2 4))
  (setq    MNdist (getreal    (strcat    "\nXac dinh chieu dai MN: <"
                (rtos MNdistSv 2 2)
                ">"
            )
           )
  )
  (if (null MNdist)
    (setq MNdist MNdistSv)
    (setq MNdistSv MNdist)
  )
  (initget (+ 2 4))
  (setq    fuzz (getreal (strcat "\nXac dinh do chinh xac dien tich: <"
                  (rtos fuzzSv 2 2)
                  ">"
              )
         )
  )
  (if (null fuzz)
    (setq fuzz fuzzSv)
    (setq fuzzSv fuzz)
  )
  (while (null
       (setq lwpEnt (car (entsel "\nChon duong cong tich luy: ")))
     )
    (setq lwpEnt (car (entsel "\nChon duong cong tich luy: ")))
  )
  (setq lwpObj (vlax-ename->vla-object lwpEnt))
  (setq    startPnt (vlax-curve-getStartPoint lwpObj)
    endPnt     (vlax-curve-getEndPoint lwpObj)
  )
  ;; Kiem tra diem cuc tieu
  (setq MinMaxPnts (LM:CurveMinMax lwpObj 1e-8))
  (setq    TopPnt (cadr MinMaxPnts)
    BotPnt (car MinMaxPnts)
  )
  (if (or (equal (distance BotPnt startPnt) 0.0 0.01)
      (equal (distance BotPnt endPnt) 0.0 0.01)
      )
    (progn
      (setq tmpLwpObj (vla-mirror
            lwpObj
            (vlax-3d-point startPnt)
            (vlax-3d-point (polar startPnt 0 0.1))
              )
              ;; lat bung
      )
      (vla-delete lwpObj)
      (setq lwpObj   tmpLwpObj
        isFliped 1
      )
      (setq startPnt (vlax-curve-getStartPoint lwpObj)
        endPnt   (vlax-curve-getEndPoint lwpObj)
      )
    )
  )
  (vla-GetBoundingBox lwpObj 'minpoint 'maxpoint)
  (vla-ZoomWindow (vlax-get-acad-object) minpoint maxpoint)
  ;; Xac dinh duong PQ
  (if (< (cadr startPnt) (cadr endPnt))
    (setq startPntPQ startPnt)
    (setq startPntPQ endPnt)
  )
  (setq    tmpLwp (AddLwpolyline
         (list startPntPQ (polar startPntPQ 0 1))
         0
         mspace
           )
  )
  (setq intPnts (LM:Intersections tmpLwp lwpObj acExtendThisEntity))
  (if (> (length intPnts) 1)
    (setq PQ (AddLwpolyline intPnts 0 mspace))
    (progn
      (vla-delete tmpLwp)
      (reset)
      (exit)
    )
  )
  (vla-delete tmpLwp)
  ;; Chieu dai doan MN
  (if (= isFliped 1)
    (setq BotPnt (car (LM:CurveMinMax lwpObj 1e-8)))
  )
  (setq    startPntMN (polar BotPnt pi (/ MNdist 2))
    endPntMN   (polar BotPnt 0 (/ MNdist 2))
  )
  (setq MN (AddLwpolyline (list startPntMN endPntMN) 0 mspace))
  ;; Xac dinh 2 duong giong tu MN den PQ
  (setq    pnt0 (vlax-curve-getClosestPointTo PQ startPntMN)
    pnt1 (vlax-curve-getClosestPointTo PQ endPntMN)
  )
  (setq    lwp0 (AddLwpolyline (list startPntMN pnt0) 0 mspace)
    lwp1 (AddLwpolyline (list endPntMN pnt1) 0 mspace)
  )
  ;; Kiem tra dien tich
  (setq AreaLst (CalArea (list pnt0 startPntMN endPntMN pnt1) lwpObj))
  (setq    s0 (nth 0 AreaLst)
    s1 (nth 1 AreaLst)
    s2 (nth 2 AreaLst)
    s3 (nth 3 AreaLst)
  )
  (if (and (< s0 s1)
       (< s3 s2)
      )
    (progn
      (alert "Khong the dieu chinh!")
      (reset)
      (exit)
    )
  )
  ;; Can bang so bo 1 cap dien tich
  (textscr)
  (setq iY (- 0 (/ (distance startPntMN pnt0) 20)))
                    ; buoc nhay phuong doc
  (setq    FromPnt    (vlax-3d-point pnt0)
    ToPntY    (vlax-3d-point (polar pnt0 (/ pi 2) iY))
  )
  (while (and (>= (nth 0 AreaLst) (nth 1 AreaLst))
          (>= (nth 3 AreaLst) (nth 2 AreaLst))
     )
    (vla-move PQ FromPnt ToPntY)
    (setq pnt0 (polar pnt0 (/ pi 2) iY)
      pnt1 (polar pnt1 (/ pi 2) iY)
    )
    (setq AreaLst (CalArea (list pnt0 startPntMN endPntMN pnt1) lwpObj))
  )
  (if (> (- (nth 0 AreaLst) (nth 1 AreaLst))
     (- (nth 3 AreaLst) (nth 2 AreaLst))
      )
    (setq i0 3
      i1 2
      i2 1
      i3 0
    )
    (setq i0 0
      i1 1
      i2 2
      i3 3
    )
  )
  (setq n 0)
  (setq sub32 (- (nth i3 AreaLst) (nth i2 AreaLst)))
  ;; Can bang dien tich dua tren dich chuyen PQ
  (while (not (equal (nth i3 AreaLst) (nth i2 AreaLst) fuzz))
    (if    (> n 100) ;; Gioi han so vong lap
      (progn
    (alert "Vuot qua gioi han 100 lan thu!")
    (reset)
    (exit)
      )
    )
    (vla-move PQ
          (vlax-3d-point pnt0)
          (vlax-3d-point (polar pnt0 (/ pi 2) iY))
    )
    (setq pnt0 (polar pnt0 (/ pi 2) iY)
      pnt1 (polar pnt1 (/ pi 2) iY)
    )
    (setq iX (/ MNdist 20.00))            ; buoc nhay phuong ngang
    (setq AreaLst (reverse (CalArea (list pnt0 startPntMN endPntMN pnt1) lwpObj)))
    (setq sub01    (- (nth i0 AreaLst) (nth i1 AreaLst)))
    (setq FromPnt (vlax-3d-point pnt0)
      ToPntX  (vlax-3d-point (polar pnt0 0 iX))
    )
    ;; Can bang dien tich dua tren dich chuyen MN
    (while (> (abs sub01) (abs sub32))
      (vla-move MN FromPnt ToPntX)
      (vla-move lwp0 FromPnt ToPntX)
      (vla-move lwp1 FromPnt ToPntX)
      (setq pnt0       (polar pnt0 0 iX)
        pnt1       (polar pnt1 0 iX)
        startPntMN (polar startPntMN 0 iX)
        endPntMN   (polar endPntMN 0 iX)
      )
      (setq AreaLst (reverse (CalArea (list pnt0 startPntMN endPntMN pnt1) lwpObj)))
      (setq sub01new (- (nth i0 AreaLst) (nth i1 AreaLst)))
      (if (> (abs sub01new) (abs sub01))
    (setq iX (/ (- 0 iX) 2))
      )
      (setq sub01 sub01new)
      (setq FromPnt (vlax-3d-point pnt0)
        ToPntX  (vlax-3d-point (polar pnt0 0 iX))
      )
    )
    (setq sub32new (- (nth i3 AreaLst) (nth i2 AreaLst)))
    (if    (> (abs sub32new) (abs sub32))
      (setq iY (/ (- 0 iY) 2))
    )
    (setq sub32 sub32new)
    (setq n (1+ n))
    (print AreaLst)
  )
  (vla-zoomprevious (vlax-get-acad-object))
  (reset)
  (princ)
)
(defun reset ()
  (if (= isFliped 1)
    (progn
      (foreach obj (list lwpObj MN PQ lwp0 lwp1)
    (vla-mirror
      obj
      (vlax-3d-point startPnt)
      (vlax-3d-point (polar startPnt 0 0.1))
    )
    (vla-delete obj)
      )
    )
  )
  (vla-endundomark thisdrawing)
  (graphscr)
)

;; Sub function
(defun GetMidPnt (pt0 pt1 / midPnt)
  (setq    midPnt (list (/ (+ (car pt0) (car pt1)) 2)
             (/ (+ (cadr pt0) (cadr pt1)) 2)
             0
           )
  )
  midPnt
)
(defun CalArea (pntLst obj / AreaLst tmpLwp)
  (setq
    midPntLst (mapcar
        '(lambda (x)
           (GetMidPnt x (vlax-curve-getClosestPointTo obj x))
         )
        pntLst
          )
  )
  (foreach pt midPntLst
    (setq HdlEnt (cdr (assoc 5 (entget (entlast)))))
    (command ".boundary" pt "")
    (if    (/= HdlEnt (cdr (assoc 5 (entget (entlast)))))
      (progn
    (setq tmpLwp (vlax-ename->vla-object (entlast)))
    (setq AreaLst (cons (vlax-get-property tmpLwp 'AREA) AreaLst))
    (vla-delete tmpLwp)
      )
      (progn
    (alert "Khong the tao Boundary!")
    (reset)
    (exit)
      )
    )
  )
  AreaLst
)
(defun LM:Intersections    (obj1 obj2 mode / l r)
  (setq l (vlax-invoke obj1 'intersectwith obj2 mode))
  (repeat (/ (length l) 3)
    (setq r (cons (list (car l) (cadr l) (caddr l)) r)
      l (cdddr l)
    )
  )
  (reverse r)
)

(defun AddLwpolyline (lst-pnt layer *model-space* / array-pt myPline)
  (setq    array-pt (list->variantArray
           (apply 'append (mapcar '3dPnt->2dPnt lst-pnt))
         )
    myPline     (vla-AddLightWeightPolyline *model-space* array-pt)
  )
  (vla-put-layer myPline layer)
  myPline
)

(defun list->variantArray (ptsList / arraySpace sArray)
  (setq    arraySpace
     (vlax-make-safearray
       vlax-vbdouble
       (cons 0
         (- (length ptsList) 1)
       )
     )
  )
  (setq sArray (vlax-safearray-fill arraySpace ptsList))
  (vlax-make-variant sArray)
)

(defun 3dPnt->2dPnt (3dpt)
  (list (float (car 3dpt)) (float (cadr 3dpt)))
)

(defun LM:CurveMinMax
              (obj         fuzz       /
               _GetBoundingBoxWithOffset   _GroupByNum
               _FlattenPoint a           acdoc
               acspc         lst       obj
               tmp
              )

  (defun _GetBoundingBoxWithOffset (obj o / ll ur)
    (
     (lambda (a)
       (mapcar
     (function
       (lambda (b)
         (mapcar
           (function
         (lambda (c) ((eval c) a))
           )
           b
         )
       )
     )
     '(
       (
        (lambda (x) (- (caar x) o))
        (lambda (x) (- (cadar x) o))
       )
       (
        (lambda (x) (+ (caadr x) o))
        (lambda (x) (- (cadar x) o))
       )
       (
        (lambda (x) (+ (caadr x) o))
        (lambda (x) (+ (cadadr x) o))
       )
       (
        (lambda (x) (- (caar x) o))
        (lambda (x) (+ (cadadr x) o))
       )
      )
       )
     )
      (mapcar 'vlax-safearray->list
          (progn (vla-getboundingbox obj 'll 'ur) (list ll ur))
      )
    )
  )

  (defun _GroupByNum (l n / r)
    (if    l
      (cons
    (reverse (repeat n
           (setq r (cons (car l) r)
             l (cdr l)
           )
           r
         )
    )
    (_GroupByNum l n)
      )
    )
  )

  (defun _FlattenPoint (p)
    (list (car p) (cadr p) 0.0)
  )

  (setq    acdoc (vla-get-activedocument (vlax-get-acad-object))
    acspc (vlax-get-property
        acdoc
        (if (= 1 (getvar 'CVPORT))
          'Paperspace
          'Modelspace
        )
          )
  )
  (cond
    ((not (vlax-method-applicable-p obj 'GetBoundingBox))
    )
    (t
     (setq tmp
        (mapcar
          (function
        (lambda    (x)
          (apply 'vla-addline (cons acspc (mapcar 'vlax-3D-point x)))
        )
          )
          (_GroupByNum
        (mapcar    '_FlattenPoint
            (_GetBoundingBoxWithOffset obj (- fuzz))
        )
        2
          )
        )
     )
     (setq lst
        (mapcar
          (function
        (lambda    (x)
          (car
            (_GroupByNum
              (vlax-invoke obj 'Intersectwith x acExtendOtherEntity)
              3
            )
          )
        )
          )
          tmp
        )
     )
     (mapcar 'vla-delete tmp)
     lst
    )
  )
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Bạn nào có ý tưởng nào hay về thuật toán để giải quyết vấn đề này không ?. Xem đúng bài viết #232523 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 126)