Small Fish Posted May 14, 2009 Posted May 14, 2009 Is it possible to make variables if it is not known how many there are to create? If I have say for example 4 rows of circles and I select the all of them can I assign variables for circle row total areas to "Row1Area" "Row2Area" "Row3Area" "Row4Area". The below code will give total areas of circle rows selected and the result is shown on a text screen - There is no problem as to how many rows there are. - but can it be done so that these values are set to variables? Thanks a bunch if you can help out To use code draw rows of circles and then a vertical line through the middle ;Original Author - Wizman(defun c:Rowtest (/ lEnt lDat ss tCirc subSs total test subSs_lst cLst Rad flag) (vl-load-com) (if (and (setq lEnt (car (entsel "\nSelect Vertical Line: "))) (eq "LINE" (cdadr (entget lEnt)))) (progn (setq lDat (list (cdr (assoc 10 (entget lEnt))) (cdr (assoc 11 (entget lEnt))))) (princ "\n\nLEFT:\n") (repeat 2 (setq test 1) (if (setq ss (ssget "_X" (list '(0 . "CIRCLE")(cons -4 (if flag ">=,*,*" " (cons 10 (append (list (apply (if flag 'max 'min) (mapcar 'car lDat))) '(0 0)))))) (while (not (zerop (sslength ss))) (setq tCirc (ssname ss 0)) (if (setq subSs (ssget "_X" (list (cons 0 "CIRCLE") (cons -4 (if flag ">=,=,*" " (cons 10 (append (list (apply (if flag 'max 'min) (mapcar 'car lDat))) (cddr (assoc 10 (entget tCirc)))))))) (progn (setq total 0.0) (foreach ent (setq subSs_lst (mapcar 'cadr (ssnamex subSs))) (setq total (+ total (vla-get-Area (vlax-ename->vla-object ent))))) (mapcar '(lambda (x) (ssdel x ss)) subSs_lst) (princ (strcat "\nRow " (itoa test) " => " " Total Area = "(rtos total) "\n-----------------------------------------")))) (ssdel tCirc ss) (setq test (1+ test)))) (and (not flag) (princ "\n\nRIGHT:\n")) (setq flag T)) (textscr)) (princ "\n No Line Selected ")) (princ )) Quote
Freerefill Posted May 14, 2009 Posted May 14, 2009 I'm not 100% positive what you're getting at, but I think what you're asking is, is it possible to assign some amount of data to some number of variables if you don't know how many there are going to be? First thing that pops into my mind would be using a list.. not exactly a variable, but if you type: (cons "flag" "value") at the command line, you'll get something like this: ("flag" . "value") which I believe is called a "dotted pair". Using this method and a loop, you can construct a list full of dotted pairs, and then, at any time, retrieve the one you want using ASSOC. I believe many programming languages tackle the problem of "I don't know how many variables I'll need!" with methods like this. I hope this helps.. ^^' Quote
wizman Posted May 14, 2009 Posted May 14, 2009 also another way: you use setq to assign any value to one variable (setq Row1Area "myvalue1"); ; => Row1Area = "myvalue1" but if you use set within a loop you can assign any value to varying/dynamic variable (setq counter 1) (while ( (set (read (strcat "Row" (itoa counter) "Area")) "myvalue2") (setq counter (1+ counter)) ) ; ; => Row1Area = "myvalue2" ; => Row2Area = "myvalue2" ; => Row3Area = "myvalue2" ; => Row4Area = "myvalue2" ; => Row5Area = "myvalue2" ; => Row6Area = "myvalue2" ; => Row7Area = "myvalue2" ; => Row8Area = "myvalue2" ; => Row9Area = "myvalue2" ; => Row10Area = "myvalue2" Quote
Small Fish Posted May 14, 2009 Author Posted May 14, 2009 Thanks Wizman it works but thats what I had previously- it prints to the text screen - unless I misunderstand. How can I get those values assigned to variables? Quote
CAB Posted May 14, 2009 Posted May 14, 2009 There are 10 new variables create! The var name is Row1Area throught Row10Area Quote
Lee Mac Posted May 14, 2009 Posted May 14, 2009 Thanks Wizmanit works but thats what I had previously- it prints to the text screen - unless I misunderstand. How can I get those values assigned to variables? Set evaluates both the variable name and the value it is being set to. - hence you can pass it a concatenated string and use that as a variable as Wizman has shown. Quote
wizman Posted May 14, 2009 Posted May 14, 2009 set can be confusing at first. it does not only print to screen, as cab said it creates the 10 variables for you. in order to avoid using multiple setqs like this: (setq row1area 1)(setq row2area 2) (setq row3area 3) (setq row4area 4) (setq row5area 5) (setq row6area 6) set can be used to make any number of variables on the fly depending on the how many variables are needed. its principle was explained by lee. this creates any number of variables based on length of selection set "mysset" (setq counter 1)(while ( (set (read (strcat "Row" (itoa counter) "Area")) counter); sets current variable name to the value of counter (setq counter (1+ counter)) ) ; ; !row1area = 1 ; !row2area = 2 ; !row3area = 3 ; !row4area = 4 ; !row5area = 5 ; !row6area = 6 ; . ; . ; . ; . ; . ; . Quote
Lee Mac Posted May 14, 2009 Posted May 14, 2009 Another example of it in use: http://www.cadtutor.net/forum/showpost.php?p=227224&postcount=5 Quote
wizman Posted May 14, 2009 Posted May 14, 2009 thanks for another sample of set lee though particularly not on variable on the fly, you used set in that case because you used it with mapcar and you cant do mapcar 'setq. Quote
Lee Mac Posted May 14, 2009 Posted May 14, 2009 thanks for another sample of set lee though particularly not on variable on the fly, you used set in that case because you used it with mapcar and you cant do mapcar 'setq. Yes, which illustrates why we are using set Quote
Small Fish Posted May 14, 2009 Author Posted May 14, 2009 hey thanks guys - now understand - I have never used 'set' before....interesting idea. Perhaps though it only partially solves my problem. In the while loop I have two values that are spat out : 'Row1' and the corresponding 'Area' 'Row2' and corresponding Area and so on.... At the moment with the code I posted unfortunately its in a while loop so I can't capture the values for each row. (princ (strcat "\nRow " (itoa test);row number " => " " Total Area = "(rtos total);total area "\n-----------------------------------------")) As Freerefill suggested perhaps I need to make a dotted pair list: (cons test Total) Maybe then I use 'set' as you have suggested. Though I would not know how to use 'set' with the dotted pair list, and its where I sink. Would this be the correct approach or am I making it complicated? thanks Quote
Small Fish Posted May 15, 2009 Author Posted May 15, 2009 Okay I have done some more digging. Now I can create my dotted pair list using : (setq AListR (append (list (cons test total)) AListR)) where test=row no. and total = area for row For example: mylist = ((2 . 524 )(3 . 74)(1. 352)) I can retrieve row 1 value using: (setq Area1 (cdr (assoc 1 AListL))) But how can I assign variables using 'set' or otherwise ? Perhaps I am asking the impossible. But thanks for a reply Quote
wizman Posted May 15, 2009 Posted May 15, 2009 now that youve got a list of dotted pairs, variables can be created by this: (foreach x mylist (set (read (strcat "Row" (itoa (car x)) "Area")) (cdr x))) ; ; !row2area = 524 ; !row3area = 74 ; !row1area = 1.352 but also since youve already got a list, you may not need to assign a variable for each and need only one variable which is mylist. its easier to work with one variable than numerous variables. similar thread: http://www.cadtutor.net/forum/showthread.php?t=18504 Quote
Small Fish Posted May 16, 2009 Author Posted May 16, 2009 Thanks - useful stuff - interesting to learn about how set works Small fish 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.