Hi,
i have an excel list of Length x Width (2 columns)
2400 50
2340 45
etc
etc
is there a way to DRAW Rectangles from the excel List in AutoCAD? End result i want Rectangles drawn near each other from the Excel List
SInce there are many Rectangles in AutoCAD, it should be done in a way to give a space between each item (i,e first one is drawn in 0,0 coordinate the other one can be drawn, 0,4000) 4 meters away
thanks,
Bedros
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
See the example xls file - you've said you want to use 0,4 as a distance in between - I've used 1 for example to avoid following issue: Whatever is used in your country in Excel as comma delimiter, you need to somehow change that to autocads syntax. You can use the excel text functions or use Find and Replace... or you can temporary change the Excel setting...
I don't think it should be necessary to change how Excel delimits entries -- if you post a sample file, we can see how it does, and presumably read lines from it and process them according to whatever the delimiter is.
Which direction is "length" and which is "width" [horizontal vs. vertical, or X-axis direction vs. Y-axis direction]?
I should have use a "decimal delimiter" term. That's what matters.
Excel follows the country rules, AutoCAD does not (as far as I know).
Let's say i have an excel list like this and i need AutoCAD to read this list in a way and generate Rectangles (First Size 77x190 units drawn somewhere in coordinate) second size 80x190 away from the first coordinate, 3rd size etc...
So i will get 1 AutoCAD File with rectangles drawn away from each other
# | Length | Width | Qty |
1 | 77 | 190 | 1 |
2 | 80 | 190 | 1 |
3 | 90 | 190 | 1 |
4 | 100 | 190 | 1 |
5 | 146 | 190 | 1 |
6 | 190 | 190 | 1 |
7 | 200 | 190 | 1 |
8 | 215 | 190 | 1 |
9 | 230 | 190 | 1 |
10 | 250 | 190 | 1 |
11 | 260 | 190 | 1 |
12 | 260 | 190 | 1 |
@bedros.j wrote:
Let's say i have an excel list like this and i need AutoCAD to read this list in a way and generate Rectangles (First Size 77x190 units drawn somewhere in coordinate) second size 80x190 away from the first coordinate, 3rd size etc...
So i will get 1 AutoCAD File with rectangles drawn away from each other
Would I be correct in the following assumptions?
A) It is a .csv filetype, not .xls or .xlsx or something.
B) It contains a line with those headers at the top, so that if I do this:
Command: (setq test (open "X:\\file\\path\\TheFileName.csv" "r"))
#<file "X:\\file\\path\\TheFileName.csv">
and then this:
Command: (read-line test)
first I will get:
"#,Length,Width,Qty"
C) Then subsequently I will get returns like this:
Command: (read-line test)
"1,77,190,1"
Given all those assumptions, that could be worked out, but you haven't answered the question in my previous Reply. Given your first three rectangles there are at least four possible results:
ALSO:
What if the Quantity column says more than 1? Should all of them be drawn separately, or just one to represent that size?
What kind of drawing units are you talking about? From Post 1:
"...the other one can be drawn, 0,4000) 4 meters away"
suggests millimeters, but surely if the rectangles are in the range of 80ish x 190 millimeters, you don't want them 4 meters apart. Or do you? In my image they're 190 drawing units in the greater dimension, and 4 drawing units apart, whatever the drawing unit may be.
[EDIT: Come to think of it, the "0,4000" suggests only two possible results, the bottom-edge one and the middle-most one in my image, but you still need to say which.]
If that's the one time thing, then I suggest to use this simple copy - paste approach, no routine needed.
If you need to do that more often, Kent's suggestion using a CSV file might be better - but it requires some of programming.
See the SCREENCAST how it works.
Good luck.
Something like below should allow you to add those rectangles. I have used the format you provided for excel. Excel file attached. It should work fine but I would advise to add error trap If you end up using it on a regular basis. Search these forums for standard error trap.
;;;Insert rectangles from excel file (file format as shown at ;;;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/excel-length-x-width-to-autocad-rectangles/m-p/6918598#M350605 ;;;Created by Ranjit Singh 03/03/2017 (defun c:somefunc (/ filep i lst pt1 shft wkbks xclobj) (setq filep (getfiled "Select File" "" "xlsx" 0) lst (reverse (vlax-for i (vlax-get (vlax-get (vlax-invoke (setq wkbks (vlax-get (setq xclobj (vlax-get-or-create-object "excel.application")) 'workbooks)) 'add filep) 'activesheet) 'usedrange) (setq lst (cons (vlax-get i 'value2) lst)))) pt1 '(0 0 0) shft 400) (mapcar '(lambda (x y z) (setq i 0) (while (< i z) (command-s "._rectangle" pt1 "_d" x y (mapcar '+ '(0.1 0 0) pt1)) (command-s "._move" (entlast) "" '(0 0 0) (list 0 (- (* i (+ y shft))) 0)) (setq i (1+ i))) (setq pt1 (mapcar '+ pt1 (list (+ shft x) 0 0)))) (somefunc2 lst 2) (somefunc2 lst 3) (somefunc2 lst 4)) (vlax-invoke wkbks 'close) (vlax-release-object xclobj)) (defun somefunc2 (x y / i a tmplst) (setq i 0) (while (setq a (nth (- (+ y (* i 4)) 1) x)) (setq tmplst (cons a tmplst) i (1+ i))) (cdr (reverse tmplst)))
Hi Kent
thanks for your input, will look further on what you are trying to achieve but sorry for not being so much clear,
the idea was to draw rectangles not touching each other, so I assumed 4000mm away from each other although the first pieces were going to create a lot of Gap, but that was okay since I was going to use the cad File to NEST them inside sheets with another software. (4000mm can be X or variable that user can input if we are talking programming) see snapshot below but here X is not 4000mm for sure
A) not aware of .csv will check further
B) will check this is new for me
C) any of the snapshot solutions is acceptable as long as rectangles are not touching each other
D) what is Qty is 2 or 55 well this is tricky not thought of it: either I would copy the same thing 55 times and consider qty always 1, or you can propose a better solution (adding annotation inside that box which is let's say more than 1?? - not sure)
hope I addressed all the points you mentioned,
again thanks for your input,
Bedros
@bedros.j wrote:
...B) will check this is new for me
...
You can take a .XLSX file and SaveAs it into a .CSV file [the file-type pull-down under the file name slot in the Excel SaveAs dialog box]. I did that with @Ranjit_Singh2's Book1.xlsx file and turned it into a Book1.csv file, which is so much easier to deal with in AutoLisp. In simplest terms, and minimally tested, and without all the usual bells and whistles, this seems to do it, with [arbitrarily -- make it what you want] 5 drawing units' space between each rectangle:
(defun C:DOIT (/ source base str X n Y) (setq source (open (findfile "Book1.csv") "r") base '(0 0 0) ); setq (read-line source); header line [do nothing with it] (while (setq str (read-line source)) (setq str (substr str (+ (vl-string-search "," str) 2)); remove line number X (atof (substr str 1 (setq n (vl-string-search "," str)))) Y (atof (substr str (+ n 3) (1+ (vl-string-search "," str n)))) ); setq (command "_.rectang" "_none" base "_none" (mapcar '+ base (list X Y))) (setq base (polar base 0 (+ X 5))) ); while (close source) (princ) ); defun
The result is not quite like your image, where the spacing between left edges is apparently intended to be a regular multiple, but in the case of the sizes in that file, they'd overlap a lot within a few of them from the start. It could be made to get all the X and Y dimensions first, calculate a spacing from the largest X dimension, and then draw them, if that's preferred.
Many Thanks, Kent and Ranjit,
Kent the Lisp worked fine with the CSV. off course the quantity column will be only 1, I am satisfied with the result anyways. thanks
Another Query or Add to the Lisp... maybe i might need this in the future IF it works,
I might Need to add Label Column which is a Text to be displayed inside the drawn rectangle (Anyplace but not touching the lines) and the height of the text can be the Width minus 50 or X units. can this be done?
# | Length | Width | Qty | LABEL |
1 | 77 | 190 | 1 | A |
2 | 80 | 190 | 1 | B |
3 | 90 | 190 | 5 | C |
4 | 100 | 190 | 1 | D |
5 | 146 | 190 | 1 | E |
6 | 190 | 190 | 1 | F |
7 | 200 | 190 | 1 | G |
8 | 215 | 190 | 1 | H |
9 | 230 | 190 | 1 | I |
10 | 250 | 190 | 1 | J |
11 | 260 | 190 | 1 | K |
12 | 260 | 190 | 1 | L |
@bedros.j wrote:
....
I might Need to add Label Column which is a Text to be displayed inside the drawn rectangle (Anyplace but not touching the lines) and the height of the text can be the Width minus 50 or X units. can this be done?
....
One nice little feature of the (vl-string-position) function is that it can find a character's position from the end rather than from the beginning of the string. The ASCII character code for a comma is 44. So as long as the label part is always following the last comma in the .csv line, try this [the grey stuff is the same as before]:
(defun C:DOIT (/ source base str X n Y) (setq source (open (findfile "Book1.csv") "r") base '(0 0 0) ); setq (read-line source); header line [do nothing with it] (while (setq str (read-line source)) (setq str (substr str (+ (vl-string-search "," str) 2)); remove line number X (atof (substr str 1 (setq n (vl-string-search "," str)))) Y (atof (substr str (+ n 3) (1+ (vl-string-search "," str n)))) ); setq (command "_.rectang" "_none" base "_none" (mapcar '+ base (list X Y)) "_.text" "_style" "YourNonFixedHeightStyle" "_mc" "_none" (mapcar '+ base (list (/ X 2) (/ Y 2))); midpoint of rectangle (- Y 50) 0 ; height & rotation (substr str (+ (vl-string-position 44 str nil T) 2)); content ); command (setq base (polar base 0 (+ X 5))) ); while (close source) (princ) ); defun
Hi Guys
i have prepared the CSV file to generate the rectangles,
also try the text as Labels inside Them
the Rectangles were not drawn fully
and text was not getting it,
can you please try the attached Book1.csv if it is working well with you?
you can keep the text style as standard inside the script
thanks,
Bedros
can you please save as the required Type and attach back?
was that the reason for it not working?
@bedros.j wrote:
- i have saved as CSV (Comma Delimited) - it can be opened normally by excel
....
was that the reason for it not working?
I don't know the reason, but when I SaveAs choosing the .csv format, I don't get that warning when I go to open the result. But in any case, that file is internally in .csv format [the lines read in the proper way], so even though when I downloaded it, the result had the .xls filetype ending, with a renaming, it worked for me, with this modified code:
(defun C:DOIT (/ source base str X n Y) (setq source (open (findfile "Book1.csv") "r") base '(0 0 0) ); setq (read-line source); header line [do nothing with it] (while (setq str (read-line source)) (setq str (substr str (+ (vl-string-search "," str) 2)); remove line number X (atof (substr str 1 (setq n (vl-string-search "," str)))) Y (atof (substr str (+ n 2) (1+ (vl-string-search "," str n)))) ); setq (command "_.rectang" "_none" base "_none" (mapcar '+ base (list X Y)) "_.text" "_style" "Standard" "_mc" "_none" (mapcar '+ base (list (/ X 2) (/ Y 2))); midpoint of rectangle (/ Y 2.0) 0 ; height & rotation (substr str (+ (vl-string-position 44 str nil T) 2)); content ); command (setq base (polar base 0 (+ X 5))) ); while (close source) (princ) ); defun
That 2 was 3 before -- I haven't dug back to figure out what may have been different if 3 worked for me in the simpler file before, but this seems to work with the current file.
I filled in "Standard" as the Text Style. Make sure that has a defined height of 0.
I changed the text height part, because it was originally the width minus 50 as you requested in Post 1, but in the current file that results in a negative value for many of them, which is rejected.
hallo,
zou het dan ook mogelijk zijn om de rectangles een nr te geven in excel die hij mee overneemt in cad?
kim
Hi,
Thank you for the code. I need exactly what are you discussing here.
How can I run this code in AutoCAD. I've tried, to paste this code in a text file and save as DOIT.lsp.
Then appload and type command DOIT. Didn't work.
Please help.
Can't find what you're looking for? Ask the community or share your knowledge.