Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Search through list and assign a number to a variable

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Anonymous
528 Views, 8 Replies

Search through list and assign a number to a variable

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: 

Screenshot - 2014-05-07 , 19_18_40.png

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

8 REPLIES 8
Message 2 of 9
hmsilva
in reply to: Anonymous

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

EESignature

Message 3 of 9
Anonymous
in reply to: hmsilva

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!

Message 4 of 9
hmsilva
in reply to: Anonymous

You're welcome, Johannes.

 

Attache a sample .csv, (rename the .csv to .txt before post).

 

Henrique

EESignature

Message 5 of 9
pbejse
in reply to: Anonymous


@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

 

Message 6 of 9
Anonymous
in reply to: pbejse

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

Message 7 of 9
hmsilva
in reply to: Anonymous

You're welcome, Johannes.

 

@pbejse 

Nice catch, pbejse. Smiley Embarassed

 

Henrique

EESignature

Message 8 of 9
hmsilva
in reply to: Anonymous

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

EESignature

Message 9 of 9
pbejse
in reply to: hmsilva


@hmsilva wrote:

You're welcome, Johannes.

 

@pbejse 

Nice catch, pbejse. Smiley Embarassed

 

Henrique


Glad i could help out HenriqueSmiley Happy

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report