Author Topic: Reactor that responds to overlength of a vertex of a polyline  (Read 1408 times)

0 Members and 1 Guest are viewing this topic.

martinle

  • Newt
  • Posts: 22
Hello gods of the Lispuniverse,
I always forget that my rectangles can't be longer than 2780 mm.
The objects are not always created with the Rectangle command. I also use lisp which draws a double line and after that
is converted into a rectangle.
The result is always a polyline with 4 sides.
Now I would have thought that I could use a support so that I no longer draw objects where a leg length
is longer than 2780 mm long. However, if I draw an object that is longer than 2780 mm, a warning message should appear on the screen
which I then appear with o.k. must confirm. However, the object should remain as drawn.
Would something like this be possible in a reactor?
I ask the kind gods to help me!
Thanks!

Crank

  • Water Moccasin
  • Posts: 1503
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #1 on: April 29, 2023, 05:47:58 PM »
I think this is very hard to do in code. More practical would be to use a parametric block,
Vault Professional 2023     +     AEC Collection

BIGAL

  • Swamp Rat
  • Posts: 1424
  • 40 + years of using Autocad
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #2 on: April 29, 2023, 09:00:48 PM »
Google Lee-mac i remember he had something about setting maximum length in a program.

If your using  a lisp why not have a check built in ? Can redefine the "Rectang" command so runs your lisp instead.
A man who never made a mistake never made anything

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #3 on: April 30, 2023, 04:27:13 AM »
Hi, martinle
Here is my take on this:
Code - Auto/Visual Lisp: [Select]
  1. ; Alert when a rectangle is drawn over the specified overall-limited length
  2. ; Author: Grrr / Grrr1337
  3. ; https://www.theswamp.org/index.php?topic=58227.0
  4.  
  5. ; ----------------------
  6. ; ---- Main section ----
  7.  
  8. (defun C:StartReactor nil (_ExIncludeRtr t) (princ) )
  9. (defun C:StopReactor nil (_ExIncludeRtr nil) (princ) )
  10.  
  11.  
  12. (defun _ExIncludeRtr ( b / rtrName limitLength d )
  13.   (setq rtrName "RectangleLimitLength")
  14.   (setq limitLength 2780)
  15.   (foreach rtr (cdar (vlr-reactors :vlr-DocManager-Reactor)) (if (and (listp (setq d (vlr-data rtr))) (= rtrName (car d))) (vlr-remove rtr)) )
  16.   (initget (+ 0 2 4))
  17.   (if (and b (setq limitLength (cond ((getint (strcat "\nInput desired rectangle `limitLength` <" (itoa limitLength) ">: "))) (limitLength))))
  18.     (progn
  19.       (vlr-DocManager-Reactor (list rtrName limitLength) '((:VLR-documentLockModeChanged . test:CB)))
  20.       (alert (strcat "`Rectangle Limit Length` Reactor is active\nLimit is: " (itoa limitLength) " units."))
  21.     )
  22.   )
  23. ); defun
  24.  
  25.  
  26. ; ---------------------------
  27. ; ---- Callbacks section ----
  28.  
  29. (defun test:CB ( rtr arg / lockModes limitLength e o h len )
  30.  
  31.   (and
  32.     (or
  33.       (wcmatch (last arg) "`#(C:*)")
  34.       (vl-some (function (lambda (x) (eq (last arg) (strcat "#" x)))) '("PLINE" "RECTANG"))
  35.     )
  36.     (setq lockModes (cdr (reverse (cddr arg))))
  37.     (or
  38.       (apply '= (cons 2 lockModes))
  39.       (vl-every (function (lambda (x) (<= 1 x 2))) lockModes)
  40.     ); or
  41.     (setq e (entlast))
  42.     (not (vlax-erased-p e))
  43.     (setq o (vlax-ename->vla-object e))
  44.     (setq h (vlax-get o 'Handle))
  45.     (Rectangle-p e)
  46.     (setq limitLength (cadr (vlr-data rtr)))
  47.     (>= len limitLength)
  48.     (not (member h (cddr (vlr-data rtr))))
  49.     (progn
  50.       (vlr-data-set rtr (append (vlr-data rtr) (list h)))
  51.       (LM:popup "Warning"
  52.         (strcat
  53.           "• Warning •"
  54.           "\nYou have drawn a rectangle"
  55.           "\nWith approx length of " (rtos len 2 2) " units"
  56.           (cond
  57.             ( (cddr (vlr-data rtr))
  58.               (vl-string-right-trim ", " (strcat "\n\nCached handles:\n" (apply 'strcat (mapcar '(lambda (x) (strcat x ", ")) (cddr (vlr-data rtr))))))
  59.             )
  60.             ("")
  61.           ); cond
  62.         ); strcat
  63.         (+ 0 48 4096)
  64.       ); LM:popup
  65.     ); progn
  66.   ); and
  67. ); defun test:CB
  68.  
  69. ; ------------------------------
  70. ; ---- Subfunctions section ----
  71.  
  72.  
  73. ;; Popup  -  Lee Mac
  74. ;; A wrapper for the WSH popup method to display a message box prompting the user.
  75. ;; ttl - [str] Text to be displayed in the pop-up title bar
  76. ;; msg - [str] Text content of the message box
  77. ;; bit - [int] Bit-coded integer indicating icon & button appearance
  78. ;; Returns: [int] Integer indicating the button pressed to exit
  79.  
  80. (defun LM:popup ( ttl msg bit / wsh rtn )
  81.   (if (setq wsh (vlax-create-object "wscript.shell"))
  82.     (progn
  83.       (setq rtn (vl-catch-all-apply 'vlax-invoke-method (list wsh 'popup msg 0 ttl bit)))
  84.       (vlax-release-object wsh)
  85.       (if (not (vl-catch-all-error-p rtn)) rtn)
  86.     )
  87.   )
  88. )
  89.  
  90.  
  91. ; Rectangle-p - Lee Mac
  92. ; http://www.theswamp.org/index.php?topic=40249.msg455137#msg455137
  93. (defun Rectangle-p ( ent / elist p1 p2 p3 p4 )
  94.   (and
  95.     (eq "LWPOLYLINE" (cdr (assoc 0 (setq elist (entget ent))))) ;; Is it an LWPolyline?
  96.     (= 4 (cdr (assoc 90 elist))) ;; Does it have 4 vertices?
  97.     (= 1 (logand 1 (cdr (assoc 70 elist)))) ;; Is it Closed?
  98.     (not (HasBulge elist)) ;; Arc segments?
  99.     (mapcar 'set '(p1 p2 p3 p4) ;; Collect the Vertices for further investigation
  100.       (apply 'append
  101.         (mapcar '(lambda ( pair ) (if (= 10 (car pair)) (list (cdr pair)))) elist)
  102.       )
  103.     )
  104.     (equal (distance p1 p2) (distance p3 p4) 1e-8) ;; Two pairs of
  105.     (equal (distance p1 p4) (distance p2 p3) 1e-8) ;; equal sides?
  106.     (equal (distance p1 p3) (distance p2 p4) 1e-8) ;; Is it a parallelogram?
  107.   )
  108. )
  109.  
  110. (defun HasBulge ( elist / pair )
  111.   (and (setq pair (assoc 42 elist))
  112.     (or (not (equal 0.0 (cdr pair) 1e-10))
  113.       (HasBulge (cdr (member pair elist)))
  114.     )
  115.   )
  116. )

Should work upon invoking commands like "RECTANGLE" or "POLYLINE" (only when a rectangular-p PLINE is drawn) and for test LISP functions like this one:
Code - Auto/Visual Lisp: [Select]
  1. (defun C:testDraw ( / size )
  2.   (initget (+ 2 4))
  3.   (if (setq size (getint "\nInput rectangle size: "))
  4.     (
  5.       (lambda (L)
  6.         (entmakex (append (list (cons 0 "LWPOLYLINE")
  7.           (cons 100 "AcDbEntity")
  8.           (cons 100 "AcDbPolyline")
  9.           (cons 90 (length L))
  10.           (cons 70 1)
  11.         )
  12.         (mapcar (function (lambda (p) (cons 10 p))) L)))
  13.       )
  14.       (list (list 0 0) (list size 0) (list size size) (list 0 size))
  15.     )
  16.   )
  17. )

Unfortunately it won't store all the rectangles drawn via LISP functions, where the rectangles are drawn within a loop. Like this one -
Code - Auto/Visual Lisp: [Select]
  1. (defun C:testDraw2 ( / size )
  2.  
  3.   (foreach size '(500 600 900 1000 1050)
  4.     (
  5.       (lambda (L)
  6.         (entmakex (append (list (cons 0 "LWPOLYLINE")
  7.           (cons 100 "AcDbEntity")
  8.           (cons 100 "AcDbPolyline")
  9.           (cons 90 (length L))
  10.           (cons 70 1)
  11.         )
  12.         (mapcar (function (lambda (p) (cons 10 p))) L)))
  13.       )
  14.       (list (list 0 0) (list size 0) (list size size) (list 0 size))
  15.     )
  16.   )
  17. )

Lastly, its not possible to modify the object within the callback (of my code) - like changing its color or erasing it (only read methods are available).
« Last Edit: April 30, 2023, 05:05:12 AM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #4 on: April 30, 2023, 08:24:27 AM »
I created a Limited Length Polyline program some time ago, which may be of use.

martinle

  • Newt
  • Posts: 22
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #5 on: May 01, 2023, 11:46:25 AM »
Hello Grrr1337,
Yes, it works very well for the total length of the rectangle. However, the reactor has to check the individual leg lengths of the polyline and if a length is longer than 2780mm, the alarm should appear.
Thanks for your help!
Martin

dexus

  • Bull Frog
  • Posts: 210
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #6 on: May 02, 2023, 02:49:19 AM »
Hello Grrr1337,
Yes, it works very well for the total length of the rectangle. However, the reactor has to check the individual leg lengths of the polyline and if a length is longer than 2780mm, the alarm should appear.
Thanks for your help!
Martin
You can replace the total length line:
Code - Auto/Visual Lisp: [Select]
With something that gets the longest segment length:
Code - Auto/Visual Lisp: [Select]
  1.       par (vlax-curve-getStartParam o)
  2.       len 0.0)
  3. (while (< par end)
  4.   (setq len
  5.     (max
  6.       len
  7.       (-
  8.         (vlax-curve-getDistAtParam o (setq par (1+ par)))
  9.         (vlax-curve-getDistAtParam o (1- par))
  10.       )
  11.     )
  12.   )
  13. )

martinle

  • Newt
  • Posts: 22
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #7 on: May 02, 2023, 03:35:22 AM »
hello dexus,
great this works very well for me!
Thanks for your help.
Now I make fewer mistakes!
Thanks!
Martin

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #8 on: May 02, 2023, 10:47:21 AM »
Thank you for the assistance, dexus!
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

martinle

  • Newt
  • Posts: 22
Re: Reactor that responds to overlength of a vertex of a polyline
« Reply #9 on: May 02, 2023, 11:43:51 PM »
Hello Grrr1337
Can you explain to me what this code does?
Thanks!