Bhanson Posted December 30, 2010 Posted December 30, 2010 I have a set of code that I need to alternate between plus (+) and minus (-) depending on a condition in a while loop. It would make things much easier than re-typing all of the (setq sel_n code) (see example code) (while (< count 4) (setq count (+ count 1)) (setq sel_3 (list (+ ptBx 1.4) (- ptBy 1.2)) ) (setq sel_4 (list (+ ptBx 1.41) (- ptBy 1.55)) ) (setq sel_7 (list (+ ptBx 1.7) (- ptBy 1.0)) ) (setq sel_8 (list (+ ptBx 2.0) (- ptBy 1.2)) ) (setq sel_9 (list (- ptBx 0.2) (- ptBy 2.0)) ) (setq sel_10 (list (+ ptBx 0.75) (- ptBy 2.25)) ) (setq sel_11 (list (+ ptBx 1.0) (- ptBy 2.5)) ) (setq sel_12 (list (+ ptBx 1.25) (- ptBy 3.0)) ) (setq sel_14 (list (- ptBx 0.6) (- ptBy 0.) ) (setq sel_15 (list (- ptBx 0.3) (- ptBy 0.9)) ) (THEN DO SOME PROCESS USING THE sel_N POINTS FROM ABOVE) ); END WHILE LOOP Thanks! Quote
Lee Mac Posted December 30, 2010 Posted December 30, 2010 Not sure I understand quite what you are trying to achieve with your code, but in answer to your thread title: (setq plus +) (plus 2 3) => 7 Quote
Bhanson Posted December 30, 2010 Author Posted December 30, 2010 Thanks Lee Mac I didn't think that would work but I guess sometimes the answer is right in front of your face. By the way how did you come to know so much about LISP? Would you recommend any reading materials to learn from? Thanks Again!! Quote
Lee Mac Posted December 30, 2010 Posted December 30, 2010 You're very welcome BHanson By the way how did you come to know so much about LISP? Would you recommend any reading materials to learn from? I haven't read any books on LISP, only references really (the help documentation in the VLIDE mostly). The majority of my knowledge is just from forums Quote
Bhanson Posted December 30, 2010 Author Posted December 30, 2010 Ok well so far that has helped so I'll just keep doing that. Thanks Quote
irneb Posted January 3, 2011 Posted January 3, 2011 This is one of the "nice" things about lisp. Because of its "so-called-functional-approach" and non-typed-variables a variable can actually also contain a function ... thus it's possible to use this re-assignment directly. Other languages have similar concepts, but rarely as easy to use (e.g. C++'s macros). BTW, this particular problem doesn't truly need this method - though it's the "simplest" (and probably most efficient) way. If you were using VB you'd have followed a more mathematical approach: ;; To subtract (setq Sign -1) (setq Answer (+ Variable (* Sign Value))) ;; To add (setq Sign 1) (setq Answer (+ Variable (* Sign Value))) But clearly this would be limited to just +/-. Doing the same thing between * and / would necessitate something like the function assignment as Lee's shown (or maybe some strange use of the expt function) - otherwise some wangling using conditionals (which is going to make your code a lot larger). Quote
Bhanson Posted January 4, 2011 Author Posted January 4, 2011 Lee Mac's code worked just fine but thanks for clarifying that little part about variables storing functions. Here is the finished part of the code. (these variables ptAx, ptAy etc. are broken down points (xyz) from an entity selection part of the code. The variables using the + and - functions are alternated based on the count shown below in order to avoid repeatability typing the alternating + and - functions for the "selection points". (setq count 1) (while (<= count 4.0) (print)(princ "Count is: ")(princ count)(print) (cond ((= count 1); point A (setq calca -)(setq calcb +)(setq calcc -) (setq ptNx ptAx)(setq ptNy ptAy) (setq scale_ammt 0.97) ); end of first condition ((= count 2); point B (setq calca +)(setq calcb -)(setq calcc -) (setq ptNx ptBx)(setq ptNy ptBy) (setq scale_ammt 0.935) ); end of second condition ((= count 3); point C (setq calca -)(setq calcb +)(setq calcc +) (setq ptNx ptCx)(setq ptNy ptCy) (setq scale_ammt 0.97) ); end of third condition ((= count 4); point D (setq calca +)(setq calcb -)(setq calcc +) (setq ptNx ptDx)(setq ptNy ptDy) (setq scale_ammt 0.935) ); end of fourth condition ) ; Set Selection points for Top Right (setq sel_1 (list (calca ptNx 1.2) (calcc ptNy 0.9)) ) (setq sel_2 (list (calca ptNx 1.4) (calcc ptNy 1.25)) ) (setq sel_3 (list (calca ptNx 1.4) (calcc ptNy 1.3)) ) (setq sel_4 (list (calca ptNx 1.41) (calcc ptNy 1.65)) ) (setq sel_9 (list (calcb ptNx 0.2) (calcc ptNy 2.0)) ) (setq sel_10 (list (calca ptNx 0.75) (calcc ptNy 2.25)) ) (setq sel_11 (list (calca ptNx 0.5) (calcc ptNy 2.5)) ) (setq sel_12 (list (calca ptNx 1.25) (calcc ptNy 3.0)) ) (setq sel_14 (list (calcb ptNx 0.6) (calcc ptNy 0.) ) (setq sel_15 (list (calcb ptNx 0.3) (calcc ptNy 0.9)) ) ; Edit Drawing lines on cover to New Style ;***************************************** ; Edit Drawing lines (command "zoom" "e") (command ".explode" (ssget "_C" sel_1 sel_2)); Explode Dart in corner (command ".offset" 0.375 (ssget "_C" sel_1 sel_2) sel_12 ""); Offset Dart (command "zoom" "e") (setq selection (ssget "_C" sel_3 sel_4)); Modify Newline Entities (setq ent_name (ssname selection 0)) (setq ent_temp (entget ent_name)) (setq ptM (cdr (assoc 10 ent_temp))) (setq ptN (cdr (assoc 11 ent_temp))) ; Find Center of Newline (setq center (list (+ (/(-(car ptM) (car ptN))2.0) (car ptN)); Find Center X Value (+(/(-(cadr ptM) (cadr ptN))2.0)(cadr ptN)) ) ); Find Center Y Value (command "scale" (ssget "_C" sel_3 sel_4) "" center scale_ammt ""); Scale Down Newline Entities (command "explode" (ssget "_C" sel_11 sel_12) ); Explode L shape pline close to center (command "zoom" "e") (command ".extend" (ssget "_C" sel_9 sel_10) "" (ssget "_C" sel_3 sel_4) ""); EXtend Newline to Left side of L (command ".trim" (ssget "_C" sel_3 sel_4) "" (ssget "_C" sel_9 sel_10) "") (Command ".explode" (ssget "_C" sel_14 sel_15)) (command "erase" (ssget "_C" sel_11 sel_12) ""); Erase Bottom of L (this one ^) (setq selection (ssget "_C" sel_14 sel_15)) (setq ent_name (ssname selection 0)) (setq ent_temp (entget ent_name)) (setq ptM (cdr (assoc 10 ent_temp))) (setq ptN (cdr (assoc 11 ent_temp))) (setq **** (list (car ptN)(calcc (cadr ptN) 0.7)(caddr ptN) ) ) (setq ent_temp (subst (cons 11 ****)(assoc 11 ent_temp) ent_temp)) (entmod ent_temp) (setq count (+ count 1)) ); END WHILE LOOP Thanks for everyone's help. Quote
irneb Posted January 4, 2011 Posted January 4, 2011 Glad you got it working. I hope you don't mind, but here's a few suggestions: With setq you can assign multiple variables at once. E.g. instead of having (setq calca -)(setq calcb +)(setq calcc -) you could have combined it into:(setq calca - calcb + calcc -) You can combine the count increment into the while header thus: (setq count 0) (while (<= (setq count (1+ count)) 4) ... ) If your count could go higher than 4, you could still split it into 4 conditions using the rem function. E.g. ((= (rem count 5) 1) ... Probably not necessary here ... but good to know. When issuing commands from lisp it's good practise to use the underscore prefix to the command names - this ensures your lisp would work even in a different language version of ACad. As you've done the dot-prefix to the trim & explode commands, it's probably also a good idea to use it on the rest of the standard ACad commands. This ensures that the normal command is used even if something else has re- / undefined it. Be careful about using ssget inside a loop. ACad has a limitation on how many selection sets can be used at once. And even though you only use it once lisp doesn't immediately clear it from RAM. If you have to stick with the ssget's inside the command calls I'd suggest doing a garbage collection (gc) in between ... just to be on the safe side. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.