woolenthreads 10 Posted August 7 Hi I've been working on this Lisp for a couple of days. Originally one of my colleagues asked for a grade lisp and I found one by Gordon Stephens. Then it turned out that they wanted a %grade with arrow lisp and/or a batter slope with triangle lisp. I managed to make the Batter Slope Lisp work but the grade one has been giving me trouble. I currently have it able to give me text with the right orientation as long as the user chooses the first point on the left and second on the right. With the other direction either the text disappears or it's upside down and I haven't been able to work out why. Could I be given some guidance on where my logic or coding is going wrong? (defun c:grade (/ pnt1 pnt2 ang1 ang2 tanofang grade1 midpt osmd txt1 hp1 vp1 hp2 vp2 midapt lpt1 lpt2) ; Grade Lisp by Gordon Stephens. Heavily edited to add a dim arrow by Mark Clews with snippets of code from experts like Lee Mac ;; clear most command text from commandline (setvar "cmdecho" 0) ;; Error coding begins (defun *error* ( msg ) (if osm (setvar 'osmode osm)) (cond ((not msg)) ((member msg '("Function cancelled" "quit / exit abort"))) ((princ (strcat "\n*** Error: " msg " ** "))) ) (princ) ) ;; End of error Coding ;; Set basic variables (prompt "\npick points for the grade") (setq pnt1 (getpoint) pnt2 (getpoint pnt1)) ;;If statements to determine angle standard between -90° and +90° (setq osmd (getvar "osmode")) (setvar "osmode" 0) (setq ang1 (angle pnt1 pnt2)) (setq tanofang (/ (sin ang1) (cos ang1))) (if (= tanofang 0) (setq grade1 0.0) (setq grade1 (/ 1 tanofang)) ) ; endif ;;Create the Text (setq txt1 (strcat (rtos (abs (/ 100 grade1)) 2 1) "%" )) (setq midpt (list (/ (+ (car pnt1) (car pnt2)) 2) (+ (/ (+ (cadr pnt1) (cadr pnt2)) 2) 5))) ;;Orientation of text If the value of the number from multiplying x and y is positive (If (> 0 (- (car pnt1) (car pnt2))) ;this is the one going wrong (setq ang2 (rtd ang1)) ;; Add Text (command "text" "mc" midpt 2.5 ang2 txt1) ; middle centre justified ) ;;End If ;;Orientation of text If the value of the number from multiplying x and y is negative (If (< 0 (- (car pnt1) (car pnt2))) (setq ang2 (rtd (- ang1 180))) ;; Add Text (command "text" "mc" midpt 2.5 ang2 txt1) ; middle centre justified ) ;;End If ;;Create the arrows ;;Set independant origin point (setq midapt (list (/ (+ (car pnt1) (car pnt2)) 2) (+ (/ (+ (cadr pnt1) (cadr pnt2)) 2) 2.5))) ;;Determine the value of Sin * Cos (negative will be between 90° to 180° or above 270° and down will ;;be to the right. For all other angles, down will be to the left) (setq sinvscos (* (- (car pnt2) (car pnt1)) (- (cadr pnt2) (cadr pnt1)))) ;;If the value of the sin and cos lengths is negative create leader arrow pointing left (if (< 0 sinvscos) (progn ;; First set XY values for Points (setq hp1 (- (car midapt) (abs (* (cos ang1) 5)))) (setq vp1 (- (cadr midapt) (abs (* (sin ang1) 5)))) (setq lpt1 (strcat (rtos hp1) "," (rtos vp1))) (setq hp2 (+ (car midapt) (abs (* (cos ang1) 5)))) (setq vp2 (+ (cadr midapt) (abs (* (sin ang1) 5)))) (setq lpt2 (strcat (rtos hp2) "," (rtos vp2))) ;;Draw Arrow (command "leader" lpt1 lpt2 "a" "" "n") ) ;end progn ) ;end if ;;If the value of the sin and cos lengths is positive create leader arrow pointing right (if (> 0 sinvscos) (progn ;; First set XY values for Points (setq hp1 (+ (car midapt) (abs (* (cos ang1) 5)))) (setq vp1 (- (cadr midapt) (abs (* (sin ang1) 5)))) (setq lpt1 (strcat (rtos hp1) "," (rtos vp1))) (setq hp2 (- (car midapt) (abs (* (cos ang1) 5)))) (setq vp2 (+ (cadr midapt) (abs (* (sin ang1) 5)))) (setq lpt2 (strcat (rtos hp2) "," (rtos vp2))) ;;Draw Arrow (command "leader" lpt1 lpt2 "a" "" "n") ) ;end progn ) ;end if (setvar "osmode" osmd) (princ) ) ; end defun (defun rtd (a) (/ (* a 180.0) pi) ) ; end defun Share this post Link to post Share on other sites

BIGAL 0 Posted August 7 You can check the text angle and if greater than a value add Pi (+ pi ang) this will flip the angle for you.Angles are done in radians so 360 deg's = 2*pi Share this post Link to post Share on other sites

woolenthreads 10 Posted August 7 Thanks Bigal I'll have a closer look into the math function using radians at work tomorrow. That being said it doesn't help me understand why the If logic is failing me. When I changed the first If statement to use a radian base the text was still missing and in addition because I wanted to retain the x+ logical text that worked I switched the two if statement sets, whereupon the text disappeared in both arguments sets. So clearly my If statements are not actually correct since if the second was correctly logical then switching the positions shouldn't affect the test. I'll also take the time to check out the "similar" threads tomorrow because they might give me another clue as to what I'm doing wrong. Share this post Link to post Share on other sites

woolenthreads 10 Posted August 7 You can check the text angle and if greater than a value add Pi (+ pi ang) this will flip the angle for you.Angles are done in radians so 360 deg's = 2*pi Thank you Bigal, I looked carefully and decided to replace the entire Text Orientation "if" set with a "cond" set which works perfectly with the radians (cond ;;if the angle is less than 90° ((<= ang1 (* PI 0.5)) (setq ang2 ang1) ;; Add Text (command "text" "mc" midpt 2.5 (rtd ang2) txt1) ; middle centre justified ) ;;if the angle is more than 90° but less than 270° ((<= ang1 (* PI 1.5)) (setq ang2 (- ang1 PI)) ;; Add Text (command "text" "mc" midpt 2.5 (rtd ang2) txt1) ; middle centre justified ) ;;if the angle is more than 270° ((> ang1 (* PI 1.5)) (setq ang2 (- ang1 (* PI 2))) ;; Add Text (command "text" "mc" midpt 2.5 (rtd ang2) txt1) ; middle centre justified ) ) ;;End cond I probably would have kept banging my head against a brick wall without some clue. Technically the radians weren't the clue but they pushed me into revising how I looked at it and I changed my thinking . Share this post Link to post Share on other sites