Hello!
I would like to set a new Z-value on coulple of hundred .dwg.
Based on a list of building name they will be assigned a specific Z-value.
I have the list in excel and it continous like this:
The building name is the same as the dwgfile.
How do I search for the building name in colum A and assign colum B to a variable?
My code:
(setq BUILDING (getvar "dwgname"))
(setq BUILDING (substr BUILDING 1 (- (strlen BUILDING) 4))) ;Remove .dwg
(setq ZVALUE (***SEARCH LIST OF BUIDLING NAME AND ASSIGN COLUM B VALUE***)) ;this is the tricky part
(setq XYZ (strcat "0,0" ZVALUE))
(command "MOVE" "ALL" "" "0,0,0" XYZ)
I guess you must save the excel as a .csv and create a list within Autolisp and search through it. But Im not sure how.
Thanks!
/Johannes
Solved! Go to Solution.
Solved by pbejse. Go to Solution.
Hello Johannes and welcome to the Autodesk Community!
It's easier to access a .csv file.
As a demo
(defun c:demo (/ BUILDING FN I LINE LST OF POS ZVALUE) (vl-load-com) (setq BUILDING (vl-filename-base (getvar "dwgname"))) (if (setq fn (getfiled "" (getvar 'DWGPREFIX) "csv" 0)) (progn (setq of (open fn "r")) (while (setq line (read-line of)) (if (setq i (vl-string-search ";" line)) (setq lst (cons (list (substr line (+ i 2)) (substr line 1 i)) lst)) ) ) (close of) ) ) (if lst (progn (reverse lst) (foreach x lst (if (setq pos (vl-position BUILDING (mapcar 'car lst))) (setq ZVALUE (nth pos (mapcar 'cadr lst))) ) ) ) ) (if ZVALUE (print ZVALUE) (prompt "\nBUILDING not found in the list!") ) (princ) )
And you'll nead to add a comma at
(setq XYZ (strcat "0,0," ZVALUE))
When using a command, we should safeguard the osnap settings, so it's safer to use
(command "MOVE" "ALL" "" "_NONE" "0,0,0" "_NONE" XYZ)
HTH
Henrique
This is awesome Henrique! Thanks!
Unfortunately it did not work properly.
I get the "BUILDING not found in the list!"-message. And yeah, the building name is in the .csv list...
I tried to go through the code but I am clueless.
Thanks again for your help!
@Anonymous wrote:
This is awesome Henrique! Thanks!
Unfortunately it did not work properly.
I get the "BUILDING not found in the list!"-message. And yeah, the building name is in the .csv list...
I tried to go through the code but I am clueless.
Thanks again for your help!
Couple of things, is your csv comma delimited
Then on the hmsilva's code
change ";" ","
change (reverse lst) to (setq lst (mapcar 'reverse lst))
then you would see the corresponding Z-value at the command prompt
EDIT: i did not see your post there Henrique
FWIW, i would approach it with the use of assoc function instead of using foreach
(if (setq f (assoc BUILDING lst)) (command "_move" "ALL" "" (strcat "0,0," (cadr f)) "") )
HTH
Hi again!
Thank you.
My .csv-list is seperated by ";".
Anyway:
I changed (reverse lst) to (setq lst (mapcar 'reverse lst)) and that did the trick! Now it works.
Fantastic!
Thank you Henrique and Pbejse!
/Johannes
The "demo", revised...
(defun c:demo (/ BUILDING FN I LINE LST OF POS ZVALUE) (vl-load-com) (setq BUILDING (vl-filename-base (getvar "dwgname"))) (if (setq fn (getfiled "" (getvar 'DWGPREFIX) "csv" 0)) (progn (setq of (open fn "r")) (while (setq line (read-line of)) (if (setq i (vl-string-search ";" line)) (setq lst (cons (reverse (list (substr line (+ i 2)) (substr line 1 i))) lst)) ) ) (close of) ) ) (if lst (progn (setq lst (reverse lst)) (foreach x lst (if (setq pos (vl-position BUILDING (mapcar 'car lst))) (setq ZVALUE (nth pos (mapcar 'cadr lst))) ) ) ) ) (if ZVALUE (print ZVALUE) (prompt "\nBUILDING not found in the list!") ) (princ) )
HTH
Henrique
Can't find what you're looking for? Ask the community or share your knowledge.