Jump to content

Recommended Posts

Posted

I need a lisp file where I can get corner points of a polygon instead of whole vertices. 

Posted

Could you explain what you mean for corner points, is ti like bounding box?, Maybe a graphic illustration would help. 

  • Like 1
Posted

Assuming rectangular bounding box for LWPolyline -

(defun polybb ( ent )
    (   (lambda ( lst ) (mapcar '(lambda ( x ) (apply 'mapcar (cons x lst))) '(min max)))
        (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget ent)))
    )
)

 

Usage:

(defun c:test ( / sel )
    (cond
        (   (not (setq sel (car (entsel)))))
        (   (/= "LWPOLYLINE" (cdr (assoc 0 (entget sel))))
            (princ "\nObject is not an LWPolyline.")
        )
        (   (foreach x (polybb sel)
                (entmake (list '(0 . "POINT") (cons 10 (trans x sel 0))))
            )
        )
    )
    (princ)
)

 

  • Like 1
Posted
7 hours ago, Lee Mac said:

Assuming rectangular bounding box for LWPolyline -

(defun polybb ( ent )
    (   (lambda ( lst ) (mapcar '(lambda ( x ) (apply 'mapcar (cons x lst))) '(min max)))
        (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget ent)))
    )
)

 

Usage:

(defun c:test ( / sel )
    (cond
        (   (not (setq sel (car (entsel)))))
        (   (/= "LWPOLYLINE" (cdr (assoc 0 (entget sel))))
            (princ "\nObject is not an LWPolyline.")
        )
        (   (foreach x (polybb sel)
                (entmake (list '(0 . "POINT") (cons 10 (trans x sel 0))))
            )
        )
    )
    (princ)
)

 

Thank you @Lee Mac for the reply. The code you sent work like a bounding box but my requirement is to get four corners of a polygon.

17267234917505214837049036455750.jpg

Posted

Maybe something like this, I am at home, but works on 2000i.  I think I started this back when I first started some tutorials, probably AfraLISP or something.

 

I just have this going to the commandline, you have provided limited information to the use of the points.

 

;;; Get the four corners of a rectangle.
;;;
;;; https://www.cadtutor.net/forum/topic/91208-corner-points-of-a-polygon/?do=findComment&comment=651476
;;;
;;; By SLW210 (Steve Wilson)
;;;
;;;

(defun c:GetCorners ()
  (setq pt1 (getpoint "\nSelect first corner of rectangle: "))
  (setq pt2 (getpoint pt1 "\nSelect opposite corner: "))
  
  ;; Calculate the corners based on the input points
  (setq x1 (car pt1)
        y1 (cadr pt1)
        x2 (car pt2)
        y2 (cadr pt2))
  
  (setq corner1 (list x1 y1) ; Bottom-left
        corner2 (list x2 y1) ; Bottom-right
        corner3 (list x2 y2) ; Top-right
        corner4 (list x1 y2)) ; Top-left
  
  ;; Output the corners
  (princ (strcat "\nCorners of the rectangle are: "
                 (rtos (car corner1) 2 2) "," (rtos (cadr corner1) 2 2) "; "
                 (rtos (car corner2) 2 2) "," (rtos (cadr corner2) 2 2) "; "
                 (rtos (car corner3) 2 2) "," (rtos (cadr corner3) 2 2) "; "
                 (rtos (car corner4) 2 2) "," (rtos (cadr corner4) 2 2)))
  
  (princ)
)

 

Similar posts...

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/rectangle-corner-points/td-p/9501488

 

 

Posted

Here's another approach -

(defun c:test ( / elv ent enx )
    (cond
        (   (not (setq ent (car (entsel)))))
        (   (/= "LWPOLYLINE" (cdr (assoc 0 (setq enx (entget ent)))))
            (princ "\nObject is not a polyline.")
        )
        (   t
            (setq elv (list (cdr (assoc 38 enx))))
            (foreach x (getcorners ent)
                (entmake (list '(0 . "POINT") (cons 10 (trans (append x elv) ent 0))))
            )
        )
    )
    (princ)
)

(defun getcorners ( ent / enx lst pnt rtn )
    (setq enx (entget ent)
          lst (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) enx))
    )
    (if (< (length lst) 3)
        lst
        (progn
            (setq rtn (list (cadr lst) (car lst))
                  lst (cddr lst)
            )
            (while (setq pnt (car lst))
                (while (and (cadr rtn) (LM:collinear-p pnt (car rtn) (cadr rtn)))
                    (setq rtn (cdr rtn))
                )
                (setq rtn (cons pnt rtn)
                      lst (cdr lst)
                )
            )
            (setq rtn (reverse rtn))
            (if (and (= 1 (logand 1 (cdr (assoc 70 enx))))
                     (LM:collinear-p (last rtn) (car rtn) (cadr rtn))
                )
                (setq rtn (cdr rtn))
            )
            rtn
        )
    )
)

;; Collinear-p  -  Lee Mac
;; Returns T if p1,p2,p3 are collinear

(defun LM:Collinear-p ( p1 p2 p3 )
    (
        (lambda ( a b c )
            (or
                (equal (+ a b) c 1e-8)
                (equal (+ b c) a 1e-8)
                (equal (+ c a) b 1e-8)
            )
        )
        (distance p1 p2) (distance p2 p3) (distance p1 p3)
    )
)

(princ)

 

  • Like 1
Posted (edited)
15 hours ago, Lee Mac said:
(defun c: test ( / elv ent enx )

You can add a point type to the code, because if PDMODE=0, then the reaction of the command is not visible...
For example:
(setvar 'PDMODE 35)  
(setvar 'PDSIZE 20)

Edited by Nikon

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...