Kreaten_vhar Posted April 15, 2022 Posted April 15, 2022 (edited) Good morning/afternoon/evening everyone. I've been digging around the forum to see if I can find a solution to what I need and I've gotten rather close, I'm just not sure how to bring everything together to actually do what I want. Essentially, I want to go into a .csv, rip the first 2 columns from each row (1st column is a quantity, 2nd column is a filename that corresponds to a .dwg file) and then take the two column values and insert blocks that correspond to the filename in the quantity listed. Using this code (courtesy of an old thread Tharwat helped out in) I can break the csv down into a list where each member is a string that contains the relevant data from the first two columns separated by a comma. So like ("5,block1" "2,block2" "15,block3"...) (defun c:ColumnRipTest () ;;Function courtesy of forum post by Tharwat @ Cadtutor.net (if (and (setq o (getfiled "" (getvar 'dwgprefix) "csv" 16)) (setq f (open o "r")) ) (progn (while (setq str (read-line f)) (if (setq p (vl-string-search "," str 2)) (setq lst (cons (substr str 1 p) lst)) ) ) (close f) ) ) (if lst (setq lst (reverse lst)) ) ) Where I'm stuck is taking those individual strings and breaking them apart so I can use the two column values. I expect that once I have a variable holding the count and a variable holding the string that corresponds to the filename I can utilize Lee Mac's Find File script with an insert command thrown in to add what I need. I've attached a little test csv that, when running the above code, outputs ("a,b" "h,i" "o,p" "v,w") so I'd like to break this further down to act on the comma separated values. Thanks for any help, and apologies if I'm not too clear, just starting learning LISP earlier this week lol randomcsvtest.csv Edited April 15, 2022 by Kreaten_vhar Typo Quote
Kreaten_vhar Posted April 18, 2022 Author Posted April 18, 2022 (edited) Huzzah I figured it out myself, posting the "final" (still gotta test and probably debug some more really) script as an attachment for reference if anyone takes a gander at this thread. Also I'm like 99% sure this could be improved, but I got a literal week of LISP programming under my belt right now so I'm happy it just works! AllinOne.lsp Edited April 18, 2022 by Kreaten_vhar added words for clarity Quote
Steven P Posted April 19, 2022 Posted April 19, 2022 Since you are liking Lee Mac and his work, try this: http://lee-mac.com/stringtolist.html 1 Quote
Tharwat Posted April 19, 2022 Posted April 19, 2022 Hi, You described your csv file into your first post here differently than the attached csv file ! Anyway, here is my go with it which is untested so please test it and let me know. I am sure that you can learn from the following codes since you were able to come up with working method. (defun c:Test ( / *error* csv opn ins str pos qty bkn ) ;;------------------------------------------------------------;; ;; Author: Tharwat Al Choufi - Date: 19.Apr.2022 ;; ;; website: https://autolispprograms.wordpress.com ;; ;;------------------------------------------------------------;; (defun *error* (msg) (and opn (= (type opn) 'FILE) (close opn)) (princ "\n*Cancelled*") ) (and (setq csv (getfiled "" (getvar 'dwgprefix) "csv" 16)) (setq opn (open csv "r")) (setq ins '(5.0 0.0 0.0)) (or (while (setq str (read-line opn)) (and (setq pos (vl-string-search "," str 9)) (setq qty (atoi (substr str 1 (vl-string-search "," str)))) (tblsearch "BLOCK" (setq bkn (substr str 1 pos))) (repeat qty (command "_.-INSERT" bkn "_non" ins "1" "1" "0") (setq ins (polar ins 0.0 5.0)) ) ) ) t ) (setq opn (close opn)) ) (princ) ) (vl-load-com) 1 Quote
Kreaten_vhar Posted April 19, 2022 Author Posted April 19, 2022 Hey Tharwat, thank you so much for providing a function I can look over and learn from. And yeah, what I described and the CSV I posted where indeed different, the one I was posted was arbitrary for as long as the first two columns are acted upon in the way described I'm happy Thanks again everyone who replied, LISP is a surprisingly rewarding language to learn and I'm looking forward to getting better! Quote
Tharwat Posted April 19, 2022 Posted April 19, 2022 You're welcome @Kreaten_vhar and happy coding. 1 Quote
BIGAL Posted April 20, 2022 Posted April 20, 2022 Just another suggestion say you have csv "1,2,3,4,5,6,7" convert to a list as suggested (1 2 3 4 5 6 7) so I don't want 1st 2 items I use the nth function on the list the only thing to remember is that the 1st item is zero not 1. (nth 2 lst) = 3 (nth 4 lst)= 5 and so on. You can also look at car functions (car lst) = 1 (cadr lst) = 2 (caddr lst) = 3 and so on. 1 Quote
myloveflyer Posted April 22, 2022 Posted April 22, 2022 On 4/20/2022 at 12:14 PM, BIGAL said: Just another suggestion say you have csv "1,2,3,4,5,6,7" convert to a list as suggested (1 2 3 4 5 6 7) so I don't want 1st 2 items I use the nth function on the list the only thing to remember is that the 1st item is zero not 1. (nth 2 lst) = 3 (nth 4 lst)= 5 and so on. You can also look at car functions (car lst) = 1 (cadr lst) = 2 (caddr lst) = 3 and so on. (nth 0 lst) = 1 (nth 1 lst )= 2 (nth 2 lst )= 3 How about this? 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.