TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: masao on July 03, 2022, 10:45:35 AM

Title: How to draw?
Post by: masao on July 03, 2022, 10:45:35 AM
How to draw center line?
Title: Re: How to draw?
Post by: didier on July 03, 2022, 12:04:42 PM
Bonjour

I would say that the centerline command is perfect, but you should know more about the qualities of your entities if this native command is not the solution.

Amicalement
Title: Re: How to draw?
Post by: Tharwat on July 03, 2022, 12:44:42 PM
Use getpoint & getcorn functions then create the center lines based on width & length of the imaginary rectangle.
Title: Re: How to draw?
Post by: Lee Mac on July 03, 2022, 06:53:06 PM
I would suggest something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:cl ( / i l s x )
  2.     (if (setq s (ssget '((0 . "LINE"))))
  3.         (progn
  4.             (repeat (setq i (sslength s))
  5.                 (setq i (1- i)
  6.                       x (entget (ssname s i))
  7.                       l (vl-list* (cdr (assoc 10 x)) (cdr (assoc 11 x)) l)
  8.                 )
  9.             )
  10.             (setq l (mapcar '(lambda ( x ) (apply 'mapcar (cons x l))) '(min max)))
  11.             (entmake
  12.                 (list
  13.                    '(0 . "LINE")
  14.                     (list 10 (caar  l) (/ (+ (cadar l) (cadadr l)) 2.0))
  15.                     (list 11 (caadr l) (/ (+ (cadar l) (cadadr l)) 2.0))
  16.                 )
  17.             )
  18.             (entmake
  19.                 (list
  20.                    '(0 . "LINE")
  21.                     (list 10 (/ (+ (caar l) (caadr l)) 2.0) (cadar  l))
  22.                     (list 11 (/ (+ (caar l) (caadr l)) 2.0) (cadadr l))
  23.                 )
  24.             )
  25.         )
  26.     )
  27.     (princ)
  28. )
Title: Re: How to draw?
Post by: masao on July 04, 2022, 08:31:17 AM
I would suggest something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:cl ( / i l s x )
  2.     (if (setq s (ssget '((0 . "LINE"))))
  3.         (progn
  4.             (repeat (setq i (sslength s))
  5.                 (setq i (1- i)
  6.                       x (entget (ssname s i))
  7.                       l (vl-list* (cdr (assoc 10 x)) (cdr (assoc 11 x)) l)
  8.                 )
  9.             )
  10.             (setq l (mapcar '(lambda ( x ) (apply 'mapcar (cons x l))) '(min max)))
  11.             (entmake
  12.                 (list
  13.                    '(0 . "LINE")
  14.                     (list 10 (caar  l) (/ (+ (cadar l) (cadadr l)) 2.0))
  15.                     (list 11 (caadr l) (/ (+ (cadar l) (cadadr l)) 2.0))
  16.                 )
  17.             )
  18.             (entmake
  19.                 (list
  20.                    '(0 . "LINE")
  21.                     (list 10 (/ (+ (caar l) (caadr l)) 2.0) (cadar  l))
  22.                     (list 11 (/ (+ (caar l) (caadr l)) 2.0) (cadadr l))
  23.                 )
  24.             )
  25.         )
  26.     )
  27.     (princ)
  28. )

Lee Mac thank you so much! :-D

but i can't understand this LISP now. :cry:

this LISP can add else? for example LWpolyline or circle.
Title: Re: How to draw?
Post by: dexus on July 04, 2022, 10:07:24 AM
[...]
Lee Mac thank you so much! :-D

but i can't understand this LISP now. :cry:

this LISP can add else? for example LWpolyline or circle.
Maybe these comments will help understanding the code better and you can try to change it to your needs:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:cl ( / i l s x )
  2.     (if (setq s (ssget '((0 . "LINE")))) ; Create selectionset (ssget), only valid entrys are lines
  3.         (progn
  4.             (repeat (setq i (sslength s)) ; Repeat over the amount of items in the selectionset
  5.                 (setq i (1- i) ; Use a counter to iterate over the selectionset
  6.                       x (entget (ssname s i)) ; Get the dxf data of object i
  7.                       l (vl-list* (cdr (assoc 10 x)) (cdr (assoc 11 x)) l) ; Add the start point and end point to the start of a list (l)
  8.                 )
  9.             )
  10.             ; This one is complicated, but it compares all the coordinates and gets the lowest and highest x and y values in the saved list (l)
  11.             ; It creates an array which goes like this:
  12.             ; '(
  13.             ;   (lowest-x lowest-y)
  14.             ;   (highest-x highest-y)
  15.             ; )
  16.             (setq l (mapcar '(lambda ( x ) (apply 'mapcar (cons x l))) '(min max)))
  17.             (entmake ; Create a new object
  18.                 (list
  19.                    '(0 . "LINE") ; type line
  20.                     (list 10 ; start point of line
  21.                       (caar  l) ; lowest x value
  22.                       (/ (+ (cadar l) (cadadr l)) 2.0) ; lowest + highest y value and divide by 2. So this is the average height
  23.                     )
  24.                     (list 11 ; end point of line
  25.                       (caadr l) ; highest x value
  26.                       (/ (+ (cadar l) (cadadr l)) 2.0) ; same formula as above
  27.                     )
  28.                 )
  29.             )
  30.             (entmake ; Create the other line, but flip x and y
  31.                 (list
  32.                    '(0 . "LINE")
  33.                     (list 10 (/ (+ (caar l) (caadr l)) 2.0) (cadar  l))
  34.                     (list 11 (/ (+ (caar l) (caadr l)) 2.0) (cadadr l))
  35.                 )
  36.             )
  37.         )
  38.     )
  39.     (princ)
  40. )
[/quote]
Title: Re: How to draw?
Post by: Lee Mac on July 04, 2022, 05:12:59 PM
Great explanation dexus  :-)