TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Fabricio28 on August 03, 2017, 10:51:20 AM

Title: Arc Radius
Post by: Fabricio28 on August 03, 2017, 10:51:20 AM
Hi guys!

How are you friends?

I have a very hard task to insert points start and end of the arcs and radius value.
I'm doing that manually. 

I was wondering if anyone could help me, please.


Thanks in advance
Fabricio
Title: Re: Arc Radius
Post by: ChrisCarlson on August 03, 2017, 11:02:06 AM
That's readily do-able. Let's break it down into sub-functions and build the routine.

First, you'll need to select an entity which contains a radius, in this case an arc.

My suggestion is Lee-Mac's select if routine

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  2. ;;---------------------=={ Select if }==----------------------;;
  3. ;;                                                            ;;
  4. ;;  Provides continuous selection prompts until either a      ;;
  5. ;;  predicate function is validated or a keyword is supplied. ;;
  6. ;;------------------------------------------------------------;;
  7. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  8. ;;------------------------------------------------------------;;
  9. ;;  Arguments:                                                ;;
  10. ;;  msg  - prompt string                                      ;;
  11. ;;  pred - optional predicate function [selection list arg]   ;;
  12. ;;  func - selection function to invoke                       ;;
  13. ;;  keyw - optional initget argument list                     ;;
  14. ;;------------------------------------------------------------;;
  15. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  16. ;;------------------------------------------------------------;;
  17.  
  18.   (while
  19.     (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  20.       (cond
  21.         ( (= 7 (getvar 'ERRNO))
  22.           (princ "\nMissed, Try again.")
  23.         )
  24.         ( (eq 'STR (type sel))
  25.           nil
  26.         )
  27.         ( (vl-consp sel)
  28.           (if (and pred (not (pred sel)))
  29.             (princ "\nInvalid Object Selected.")
  30.           )
  31.         )
  32.       )
  33.     )
  34.   )
  35.   sel
  36. )

In order to call the sub-function, additional parameters must be passed. Lee's website includes the full command call.

Code - Auto/Visual Lisp: [Select]
  1.        
  2. (LM:SelectIf "\nSelect an Arc: "
  3.         (lambda ( x ) (eq "ARC" (cdr (assoc 0 (entget (car x)))))) entsel nil
  4. )

Next you need to obtain the radius, if you have access to VLISP it's a rather simple operation

Code - Auto/Visual Lisp: [Select]
  1. (setq rad (vla-get-radius obj)) ;obj being the entity returned by selectif

To test that everything up until now works, create an alert window to display the radius of obj.

Code - Auto/Visual Lisp: [Select]
  1. (alert (strcat "Arc has a radius of " (rtos rad 2 4))
Title: Re: Arc Radius
Post by: ronjonp on August 03, 2017, 12:06:32 PM
Here's a quickie  8)

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ _angle a ep h mp ss)
  2.   (defun _angle (ename pt / clpt e param)
  3.     (if (and (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-curve-getendparam (list ename))))
  4.              (setq clpt (vlax-curve-getclosestpointto ename pt))
  5.              (setq param (vlax-curve-getparamatpoint ename clpt))
  6.         )
  7.       (angle '(0 0) (vlax-curve-getfirstderiv ename param))
  8.       0.0
  9.     )
  10.   )
  11.   ;; Text height
  12.   (setq h 1)
  13.   (if (setq ss (ssget '((0 . "arc"))))
  14.     (foreach x (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
  15.       (entmakex (list '(0 . "point") '(8 . "Point") (cons 10 (vlax-curve-getstartpoint x))))
  16.       (entmakex (list '(0 . "point") '(8 . "Point") (cons 10 (setq ep (vlax-curve-getendpoint x)))))
  17.       (setq mp (polar mp (+ (/ pi 2) (setq a (_angle x mp))) (* 0.75 h)))
  18.       (entmake (list '(0 . "TEXT")
  19.                      '(8 . "Text")
  20.                      ;; Insertionpoint
  21.                      (cons 10 mp)
  22.                      ;; Text height
  23.                      (cons 40 h)
  24.                      ;; Radius
  25.                      (cons 1 (strcat "R=" (vl-princ-to-string (cdr (assoc 40 (entget x)))) "m"))
  26.                      ;; Rotation
  27.                      (cons 50 a)
  28.                      '(72 . 1)
  29.                      ;; Alignment point
  30.                      (cons 11 mp)
  31.                      '(73 . 2)
  32.                )
  33.       )
  34.     )
  35.   )
  36.   (princ)
  37. )
Title: Re: Arc Radius
Post by: Fabricio28 on August 03, 2017, 01:32:02 PM
Here's a quickie  8)

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ _angle a ep h mp ss)
  2.   (defun _angle (ename pt / clpt e param)
  3.     (if (and (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-curve-getendparam (list ename))))
  4.              (setq clpt (vlax-curve-getclosestpointto ename pt))
  5.              (setq param (vlax-curve-getparamatpoint ename clpt))
  6.         )
  7.       (angle '(0 0) (vlax-curve-getfirstderiv ename param))
  8.       0.0
  9.     )
  10.   )
  11.   ;; Text height
  12.   (setq h 1)
  13.   (if (setq ss (ssget '((0 . "arc"))))
  14.     (foreach x (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
  15.       (entmakex (list '(0 . "point") '(8 . "Point") (cons 10 (vlax-curve-getstartpoint x))))
  16.       (entmakex (list '(0 . "point") '(8 . "Point") (cons 10 (setq ep (vlax-curve-getendpoint x)))))
  17.       (setq mp (polar mp (+ (/ pi 2) (setq a (_angle x mp))) (* 0.75 h)))
  18.       (entmake (list '(0 . "TEXT")
  19.                      '(8 . "Text")
  20.                      ;; Insertionpoint
  21.                      (cons 10 mp)
  22.                      ;; Text height
  23.                      (cons 40 h)
  24.                      ;; Radius
  25.                      (cons 1 (strcat "R=" (vl-princ-to-string (cdr (assoc 40 (entget x)))) "m"))
  26.                      ;; Rotation
  27.                      (cons 50 a)
  28.                      '(72 . 1)
  29.                      ;; Alignment point
  30.                      (cons 11 mp)
  31.                      '(73 . 2)
  32.                )
  33.       )
  34.     )
  35.   )
  36.   (princ)
  37. )

Thank you very much Ron.
Worked perfect your code.

Thanks
Fabricio
Title: Re: Arc Radius
Post by: Fabricio28 on August 03, 2017, 01:35:10 PM
That's readily do-able. Let's break it down into sub-functions and build the routine.

First, you'll need to select an entity which contains a radius, in this case an arc.

My suggestion is Lee-Mac's select if routine

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  2. ;;---------------------=={ Select if }==----------------------;;
  3. ;;                                                            ;;
  4. ;;  Provides continuous selection prompts until either a      ;;
  5. ;;  predicate function is validated or a keyword is supplied. ;;
  6. ;;------------------------------------------------------------;;
  7. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  8. ;;------------------------------------------------------------;;
  9. ;;  Arguments:                                                ;;
  10. ;;  msg  - prompt string                                      ;;
  11. ;;  pred - optional predicate function [selection list arg]   ;;
  12. ;;  func - selection function to invoke                       ;;
  13. ;;  keyw - optional initget argument list                     ;;
  14. ;;------------------------------------------------------------;;
  15. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  16. ;;------------------------------------------------------------;;
  17.  
  18.   (while
  19.     (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  20.       (cond
  21.         ( (= 7 (getvar 'ERRNO))
  22.           (princ "\nMissed, Try again.")
  23.         )
  24.         ( (eq 'STR (type sel))
  25.           nil
  26.         )
  27.         ( (vl-consp sel)
  28.           (if (and pred (not (pred sel)))
  29.             (princ "\nInvalid Object Selected.")
  30.           )
  31.         )
  32.       )
  33.     )
  34.   )
  35.   sel
  36. )

In order to call the sub-function, additional parameters must be passed. Lee's website includes the full command call.

Code - Auto/Visual Lisp: [Select]
  1.        
  2. (LM:SelectIf "\nSelect an Arc: "
  3.         (lambda ( x ) (eq "ARC" (cdr (assoc 0 (entget (car x)))))) entsel nil
  4. )

Next you need to obtain the radius, if you have access to VLISP it's a rather simple operation

Code - Auto/Visual Lisp: [Select]
  1. (setq rad (vla-get-radius obj)) ;obj being the entity returned by selectif

To test that everything up until now works, create an alert window to display the radius of obj.

Code - Auto/Visual Lisp: [Select]
  1. (alert (strcat "Arc has a radius of " (rtos rad 2 4))

I really appreciated your help my friend.
I'm just beginner with lisp. I'll study the code.

Fabricio
Title: Re: Arc Radius
Post by: ChrisCarlson on August 03, 2017, 01:37:19 PM
Ron's is great, the way it's written you should be able to break down line by line on what's happening.
Title: Re: Arc Radius
Post by: ronjonp on August 03, 2017, 02:32:20 PM
Glad to help out :)