Author Topic: Finding the Right Condition  (Read 1146 times)

0 Members and 1 Guest are viewing this topic.

rgp

  • Guest
Finding the Right Condition
« on: March 20, 2018, 03:09:30 PM »
I have been trying to get this code to work for a while. The problem, as far as I can see, is that the program stops at the first "if" condition. In other words, if I pick from left to right and the variable "ang" is 0.0, the program works fine. If I pick in any other order, it doesn't. I tried using "cond" for each possibility but that didn't work either. I suspect my "if" structure is wrong, but I can't see where that happens. Could someone please point me in the right direction?

I appreciate any help.

Russ

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Finding the Right Condition
« Reply #1 on: March 20, 2018, 03:31:34 PM »
See if this works for you...
Actually the problem was in ang variable (angle) function returns always angle in radians and you used checking in degrees, so I replaced that line with (cvunit ang "radian" "degree")...

Also replaced (= ang 0.0) with (equal ang 0.0 1e-6) - you need to supply some tolerance 1e-6 while checking as angle defined by picking points are not always exact especially when using (getpoint)...

Code - Auto/Visual Lisp: [Select]
  1. (defun C:TrussText2 ( / blk blk2 cmdecho clayer ortho mlayerw ip bw ang mid1 )
  2.   (setq blk "C:/Lisp/T-ARROW.dwg"
  3.         blk2 "C:/Lisp/TrussTxt.dwg"
  4.   )
  5.   (setq cmdecho (getvar "cmdecho"))
  6.   (setq clayer (getvar "clayer"))
  7.   (setq ortho (getvar "orthomode"))
  8.   (defun mlayerw nil
  9.     (if (tblsearch "layer" "Symbols")
  10.       (command-s "-layer" "on" "Symbols" "s" "Symbols" "") ;then
  11.       (progn
  12.         (setq regen (getvar "regenmode"))
  13.         (setvar "regenmode" 0)
  14.         (command-s "-layer" "m" "Symbols" "c" "2" "" "")
  15.         (setvar "regenmode" regen)
  16.       );progn - else
  17.     )
  18.     (princ)
  19.   );defun
  20.   (mlayerw)
  21.   (while
  22.     (and
  23.       (setq ip (getpoint "\nSelect first truss: "))
  24.       (setq bw (getpoint ip "\nSelect last truss: "))
  25.     )
  26.     (setq ang (cvunit (angle ip bw) "radian" "degree"))
  27.     (if (and ip bw)
  28.       (setq mid1
  29.         (list (/ (+ (car  ip) (car  bw)) 2)
  30.               (/ (+ (cadr ip) (cadr bw)) 2)
  31.         );list
  32.       );setq
  33.     );if
  34.     (cond
  35.       ( (equal ang 0.0 1e-6)
  36.         (command-s ".line" ip bw "")
  37.         (command-s "-insert" blk ip "" "" 0.0 "")
  38.         (command-s "-insert" blk bw "" "" 180.0 "")
  39.         (command-s "-insert" blk2 mid1 "" "" 0.0 "")
  40.       );cond 1
  41.       ( (equal ang 90.0 1e-6)
  42.         (command-s ".line" ip bw "")
  43.         (command-s "-insert" blk ip "" "" 90.0 "")
  44.         (command-s "-insert" blk bw "" "" 270.0 "")
  45.         (command-s "-insert" blk2 mid1 "" "" 90.0 "")
  46.       );cond 2
  47.       ( (equal ang 180.0 1e-6)
  48.         (command-s ".line" ip bw "")
  49.         (command-s "-insert" blk ip "" "" 0.0 "")
  50.         (command-s "-insert" blk bw "" "" 180.0 "")
  51.         (command-s "-insert" blk2 mid1 "" "" 0.0 "")
  52.       );cond 3
  53.       ( (eqaul ang 270.0 1e-6)
  54.         (command-s ".line" ip bw "")
  55.         (command-s "-insert" blk ip "" "" 270.0 "")
  56.         (command-s "-insert" blk bw "" "" 90.0 "")
  57.         (command-s "-insert" blk2 mid1 "" "" 90.0 "")
  58.       );cond 4
  59.     );cond
  60.   );while
  61.   (setvar "cmdecho" cmdecho)
  62.   (setvar "clayer" clayer)
  63.   (setvar "orthomode" ortho)
  64.   (princ)
  65. );defun
  66.  
« Last Edit: March 20, 2018, 03:35:11 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

rgp

  • Guest
Re: Finding the Right Condition
« Reply #2 on: March 20, 2018, 05:22:36 PM »
Thank you for your help. The program works now as it should. I had thought it would need a conditional statement, I just haven't put one together before.