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

Excel Length x Width to AutoCAD Rectangles

29 REPLIES 29
SOLVED
Reply
Message 1 of 30
bedros.j
8861 Views, 29 Replies

Excel Length x Width to AutoCAD Rectangles

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

29 REPLIES 29
Message 2 of 30
ВeekeeCZ
in reply to: bedros.j

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... 

Message 3 of 30
Kent1Cooper
in reply to: bedros.j

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]?

Kent Cooper, AIA
Message 4 of 30
ВeekeeCZ
in reply to: Kent1Cooper

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).

 

Message 5 of 30
bedros.j
in reply to: Kent1Cooper

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

 

 

#LengthWidthQty
1771901
2801901
3901901
41001901
51461901
61901901
72001901
82151901
92301901
102501901
112601901
122601901
Message 6 of 30
Kent1Cooper
in reply to: bedros.j


@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:

 

Rectangles.png

 

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.]

Kent Cooper, AIA
Message 7 of 30
ВeekeeCZ
in reply to: bedros.j

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.

Message 8 of 30
Ranjit_Singh1
in reply to: bedros.j

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)))

 

Message 9 of 30
bedros.j
in reply to: Kent1Cooper

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

123.jpg

 

 

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

 

 

Message 10 of 30
Kent1Cooper
in reply to: bedros.j


@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_Singh1'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.

Kent Cooper, AIA
Message 11 of 30
bedros.j
in reply to: Kent1Cooper

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?

 

 

#LengthWidthQtyLABEL
1771901A
2801901B
3901905C
41001901D
51461901E
61901901F
72001901G
82151901H
92301901I
102501901J
112601901K
122601901L

 

 

Message 12 of 30
Kent1Cooper
in reply to: bedros.j


@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
Kent Cooper, AIA
Message 13 of 30
bedros.j
in reply to: bedros.j

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

Message 14 of 30
Kent1Cooper
in reply to: bedros.j

warning.PNG

Kent Cooper, AIA
Message 15 of 30
bedros.j
in reply to: Kent1Cooper

  • i have saved as CSV (Comma Delimited) - it can be opened normally by excel

can you please save as the required Type and attach back?

 

was that the reason for it not working? 

Message 16 of 30
Kent1Cooper
in reply to: bedros.j


@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.

Kent Cooper, AIA
Message 17 of 30
kim.dedeyne
in reply to: ВeekeeCZ

hallo,

 

zou het dan ook mogelijk zijn om de rectangles een nr te geven in excel die hij mee overneemt in cad?

 

kim

Message 18 of 30
Anonymous
in reply to: Kent1Cooper

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. 

Message 19 of 30
Anonymous
in reply to: Anonymous

; error: bad argument type: stringp nil HELP

Message 20 of 30
Anonymous
in reply to: Anonymous

; error: bad argument type: stringp nil

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report