One of my readers has requested some help regarding the drawing of windows. I’m not 100% sure of his exact requirements, but it’s a good opportunity for me to build on what I’ve already shown you. This will be a brief post – the main content of which is some annotated VB code (below). This simply takes the previous Excel and AutoCAD® post a little further by creating a practical implementation.
What this does is allows the user to enter an X,Y,Width and Height coordinate in Excel, in columns A,B,C and D respectively. Running the code draws them as rectangles in modelspace.
I’ve decided to compile this as an Excel file, which is downloadable below. It is worth mentioning that I have referenced a specific version of AutoCAD® in this file (Under Tools>References from the VBA editor accessible from Excel using Alt+F11). The code should work if you reference your own version of AutoCAD.
The source code is here also for you to look at, or paste into your own project. Again, you’ll need to create a reference to AutoCAD® as explained in the previous article.
Sub Main() Dim ACAD As AcadApplication 'Create ACAD variable of type AcadApplication On Error Resume Next 'This tells VBA to ignore errors Set ACAD = GetObject(, "AutoCAD.Application") 'Get a running instance of the class AutoCAD.Application On Error GoTo 0 'This tells VBA to go back to NOT ignoring errors If ACAD Is Nothing Then 'Check to see if the above worked Set ACAD = New AcadApplication 'Set the ACAD variable to equal a new instance of AutoCAD ACAD.Visible = True 'Once loaded, set AutoCAD® to be visible End If ACAD.ActiveDocument.Utility.Prompt "Hello from Excel!" 'Print a message to the AutoCAD® command line Dim Coords(7) As Double 'This is an array of double precision floating point numbers Dim n As Integer 'Create the variable n as the type Integer For n = 1 To 10 'Loop this code, incrementing the value of n from 1 to 10 'Variables for X,Y,Width and Height Dim X As Double Dim Y As Double Dim Width As Double Dim Height As Double 'Store the values from Excel in memory - We could just use Sheet1.Cells(), but 'this makes it much more readable for us as programmers, and it is faster to retrieve 'data from variables than from the worksheet. X = Sheet1.Cells(n, 1) Y = Sheet1.Cells(n, 2) Width = Sheet1.Cells(n, 3) Height = Sheet1.Cells(n, 4) 'Lightweight polylines are defined by a series of 2D coords in an array 'Bottom Left Corner Coords(0) = X Coords(1) = Y 'Bottom right Corner Coords(2) = X + Width Coords(3) = Y 'Top Right Corner Coords(4) = X + Width Coords(5) = Y + Height 'Top Left Corner Coords(6) = X Coords(7) = Y + Height 'Create a polyline-type variable - its initial value will be empty (non-existent) Dim PL As AcadLWPolyline 'Create a polyline based on these coordinates 'The Set statement is used when creating a reference to objects (complex data types) 'The value of PL will be a reference to our new polyline Set PL = ACAD.ActiveDocument.ModelSpace.AddLightWeightPolyline(Coords) 'Add a point in AutoCAD® at this location 'Make PL a closed polyline PL.Closed = True Next End Sub
Hope this helps! And if you want to reap the benefit of spectacular hints and tips like this regularly, subscribe below! Not to mention my outstandingly witty writing style…errmm…so yes please do subscribe!