Jump to content

Read CSV - String Values returning as symbols


Recommended Posts

Posted

Hi All - Long time viewer, first time poster. Excuse my inexperience with LISP - I'm currently trying to teach myself, but I'm hitting a stumbling block with the below example.  

I have written a function to get a single cell value, based on the Column & row position of the value. This uses Lee Mac's ReadCSV program - I'm bringing the List of lists in, and locating the nth, nth value . this is all working fine, and I am able to use this functionality to write values to an Autocad table and some attributes/ text. the CSV file in question is a bill of materials export from Autodesk inventor, which contains a mixture of numbers & text.  

(defun JS:CELLVALGET ( xval yval / data file cellval )
	(princ cell Value Ver 0.26")(princ)
	;FILE PATH OF CSV FILE EXPORT
	(setq file "XXXXX.csv")
	(setq data (LM:readcsv file))
	(setq cellval (nth (- xval 1) (nth (- yval 1) data)))
	(princ cellval)(princ)

 

The problem I am having is that if I want to do further manipulation, such as the simplified example below, I am running into an error.

 

(setq test (JS:CELLVALGET (2 1)))

(cond 
((> (vl-string-search "APPLE" test 0) 1)
;DO STUFF
)

((> (vl-string-search "PLUM" test 0) 1)
;DO DIFFERENT STUFF
)

)

 

the data type for the variable "test" is a symbol and therefore vl-string-search fails with the error message:

Quote

error: bad argument type: symbolp  "APPLE"

 

I have tried the following functions with as part of the cellvalget subfunction with no joy: vl-symbol-value, vl-symbol-name, vl-princ-to-string but all return an empty value/error. 

 

Any ideas? I suspect I am missing something obvious! 

 

 

Posted

Why would it not be ((> (vl-string-search "APPLE" cellval 0) 1) ? 

Another way

(setq cellval "APPLE")
"APPLE"

(wcmatch cellval "*APPLE*")
T

 

Posted

I do not know the function you use to read the CSV. But I suppose it expects string values to be formatted as such.

So a line in the CSV should look like this:

"abc",1,2.3,"def"

And not like this:

abc,1,2.3,def

 

Posted

No Roy  the csv ->lst accepts that they are strings. Any way this is what is required csv to table. It is hard coded for file needs to be changed, (setq fo (open "d:\\Acadtemp\\setout2.csv"  "R")) or change to getfiled.

 

; csv to list by lee-mac
(defun _csv->lst ( str / pos )
	(if (setq pos (vl-string-position 44 str))
		(cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
		(list str)
    )
)


(defun AH:addlinet ( / rowcnt rowhgt)
(setq rowcnt (vla-get-rows objtable))
(setq rowhgt (vla-getRowHeight objtable 2))
(vla-InsertRows objTable rowcnt rowhgt 1)
(vla-settext objtable (- rowcnt 1) 0 (nth 0 lst))
(vla-settext objtable (- rowcnt 1) 1 (nth 1 lst))
(vla-settext objtable (- rowcnt 1) 2 (nth 2 lst))
(vla-settext objtable (- rowcnt 1) 3 (nth 3 lst))
(vla-settext objtable (- rowcnt 1) 4 (nth 4 lst))
(princ)
)

(defun c:x2tab ( / )
(setq sp (vlax-3d-point (getpoint "pick a point for table")))
(Setq vgms (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))

(setq numrows 3)
(setq numcolumns 5)
(setq rowheight 3.04)
(setq colwidth 60)
(setq objtable (vla-addtable vgms sp numrows numcolumns rowheight colwidth))
(vla-settext objtable 0 0 "SETOUT")
(vla-settext objtable 1 0 "NUMBER")
(vla-settext objtable 1 1 "EASTING")
(vla-settext objtable 1 2 "NORTHING")
(vla-settext objtable 1 3 "RL")
(vla-settext objtable 1 4 "DESCRIPTION")

(command "_zoom" "e")

(princ)

(setq objtable (vlax-ename->vla-object (entlast)))
(setq fo (open "d:\\Acadtemp\\setout2.csv"  "R"))
(while (setq nline (read-line fo))
(setq lst (_csv->lst nline))
(AH:addlinet)
)
(close fo)

;  (setq objtable (vlax-ename->vla-object (entlast)))
(SETQ Y 0 )
(repeat 5
(setq x 0)
(repeat (- (vla-get-rows objtable) 2)
(vla-setcelltextheight objtable (setq x  (+ x 1)) y 1.25)
)
(setq y (+ y 1))
)
(SETQ X 0)
(repeat (- (vla-get-rows objtable) 2)
(VLA-SETROWHEIGHT OBJTABLE (setq x  (+ x 1)) 3.04)
)

(setq x 0)
(repeat 5
(VLA-SetColumnWidth OBJTABLE x 25)
(setq x (+ x 1))
)

(princ)
)

 

  • 3 weeks later...
Posted

All, 

Cheers for the help - it turns out the princ statement in the subroutine was the culprit. After removing this, it ran nicely, and returned a value as a real number (as expected originally).

 

This could then be handled by the cond statements, triggering further subroutines. 

I've now managed to complete the program - a task that previously took 3-4 hours, is now taking 10 minutes. 

 

I love programming sometimes! 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...