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
8859 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 21 of 30
Anonymous
in reply to: ВeekeeCZ

 

Message 22 of 30
tmwatte
in reply to: Kent1Cooper

This is exactly what I was looking for!

I have a huge amount of rectangle dimensions in Excel that I would like created in autocad. I created a .scr file from the code that was written, and I downloaded the book1 excel file and changed it it a .csv.

 

However, whenever I run the .scr, I get... well you can see my screenshot.

 

I've attached a screenshot of the code in my notedpad, as well as the autocad screen when I try to run it. I am a bit new to automation in autocad, so please excuse my ignorance.

Message 23 of 30
cadffm
in reply to: tmwatte

This is a LISP, not a Script.

Normal way:

Rename file extension from scr to lsp

load the Lisp by drag&drop or command APPLOAD,

 

Also prossible but not usual:

Open your SCR file, go to the last char, then press ENTER for a new line,

in the new line write DOIT and press ENTER again.

Save the file.

 

LSP after loading

Start the new command: DOIT

 

SCR after loading 

Start the new Command: DOIT

(Or if you used my "Option" the Command started after loading automatically)

Sebastian

EESignature

Message 24 of 30
Anonymous
in reply to: cadffm

Hi

 

I tried this exact lsp which is loaded by autocad but i get stringp nil after I type DOIT.

 

The DOIT.lsp is saved into D:\DOIT.lsp

 

I used your last script modified upon my needs:

Quote

 

(defun C:DOIT (/ source base str X n Y)
(setq
source (open (findfile "D:\CARGO PLANS\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

 

Unquote

 

Thanks for any help.

Message 25 of 30
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

....

I tried this exact lsp which is loaded by autocad but i get stringp nil after I type DOIT.

....

source (open (findfile "D:\CARGO PLANS\Book1.csv") "r")

.....


Filepath subdividers in AutoLisp functions need to be either forward  slashes or double  backslashes:

source (open (findfile "D:/CARGO PLANS/Book1.csv") "r")

or

source (open (findfile "D:\\CARGO PLANS\\Book1.csv") "r")

 

Fix that and see whether it works.

 

@Anonymous , if you're still watching [I didn't catch your message at the time], the same may be the answer to your problem.  And @tmwatte , you would need to make the same correction in addition to treating it as an AutoLisp routine rather than a Script.

Kent Cooper, AIA
Message 26 of 30
Anonymous
in reply to: Kent1Cooper

Hi, 

Many thanks. It worked. The only problem with the lisp is that it does not take care of the quantity. If for example you have 14 pcs it draws only one.

Message 27 of 30
bedros.j
in reply to: Anonymous

at First, after rectangles were drawn, I wanted the QTY and LABEL to appear as TEXT inside the Rectangle - but then it was forgotten, am sure there is a way to do that

 

 

 

Message 28 of 30
Anonymous
in reply to: Anonymous

 From what I observed if I have a lot of rectangulars the text inserted in the rectangular remain quite small. But if I try to insert only a few of them the text gets enlarged up to 205. Standard when opening the autocad is 0.200. 

Or the best solution to make it become on multi line text and stick inside the rectangular area (not to get outside the rectangular)

 

Any way to make it a fix value everytime I execute DOIT function.

 

Anticipated thanks!

Message 29 of 30
bedros.j
in reply to: Anonymous

Guys,

can someone paste back the Working Final/Updated Version of the Lisp and the Excel, please?

thanks,

Bedros

Message 30 of 30
Sea-Haven
in reply to: bedros.j

Just as a matter of interest Excel can control Autocad so you would draw the rectangs from excel not Acad. 

 

Here is a link to a good tutorial and some sample code that demonstrates drawing objects from excel, its macro code ie VBA.  https://www.youtube.com/watch?v=754g0gcGsjY&feature=youtu.be

  
Public Sub Opendwg()
 
    Dim acadApp As Object
    Dim acadDoc As Object

 'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
 
    'Check (again) if there is an AutoCAD object.
    If acadApp Is Nothing Then
        MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
    On Error GoTo 0
 
    'If there is no active drawing create a new one.
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add
    End If
    On Error GoTo 0
  
    'Check if the active space is paper space and change it to model space.
    If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
        acadDoc.ActiveSpace = 1     '1 = acModelSpace in early binding
    End If

 End Sub
 
Public Sub addline(x1, y1, z1, x2, y2, z2)
  
 ' Create the line in model space
    'Dim acadApp As Object
    'Dim acadDoc As Object
    Set acadApp = GetObject(, "AutoCAD.Application")
    Set acadDoc = acadApp.ActiveDocument

    Dim startpoint(0 To 2) As Double
    Dim endpoint(0 To 2) As Double
    Dim lineobj As Object

    startpoint(0) = x1: startpoint(1) = y1: startpoint(2) = z1
    endpoint(0) = x2: endpoint(1) = y2: endpoint(2) = z2

    Set lineobj = acadDoc.ModelSpace.addline(startpoint, endpoint)
    acadApp.ZoomExtents
    
    End Sub
    Public Sub addcirc(x1, y1, z1, rad)
  
 ' Create the circle in model space
   ' Dim acadApp As Object
   ' Dim acadDoc As Object
    Set acadApp = GetObject(, "AutoCAD.Application")
    Set acadDoc = acadApp.ActiveDocument

    Dim cenpoint(0 To 2) As Double
   
    Dim circobj As Object

   cenpoint(0) = x1: cenpoint(1) = y1: cenpoint(2) = z1
    Set circobj = acadDoc.ModelSpace.addcircle(cenpoint, rad)
    acadApp.ZoomExtents
    
    End Sub
    
    
    Sub addpoly(cords, col)
    
    ' Dim acadApp As Object
    ' Dim acadDoc As Object
    Set acadApp = GetObject(, "AutoCAD.Application")
    Set acadDoc = acadApp.ActiveDocument

    Dim oPline As Object
    
' add pline to Modelspace
Set oPline = acadDoc.ModelSpace.AddLightWeightPolyline(cords)
oPline.Color = col

End Sub
   
    Sub alan1()
    
   
' This example adds a line in model space
' Define the start and end points for the line
   
    px1 = 1
    px2 = 5
    py1 = 1
    py2 = 5
    pz1 = 0
    pz2 = 0
    

Call addline(px1, py1, pz1, px2, py2, pz2)

End Sub

 Sub alan2()
 
    px1 = 1
    py1 = 1
    pz1 = 0
    Radius = 8.5
 
 Call addcirc(px1, py1, pz1, Radius)

 End Sub
 
 Sub alan3()
 'Dim coords(0 To n) As Double
 Dim coords(0 To 5) As Double
 coords(0) = -6: coords(1) = 1:
 coords(2) = 3: coords(3) = 5:
 coords(4) = 7.55: coords(5) = 6.25:
 
 col = 1
    
 Call addpoly(coords, col)

 End Sub

 

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report