PDA

View Full Version : Solved: Placing a circle in Autocad at a coordinate from an Excel file



KennyJ
07-27-2010, 01:11 PM
Hello Everyone,
I am about to start my first attempt at coding this but a little push in the right direction would help to get me started. My goal is to create a macro that will layout holes on a solid for me. Example: I create a solid lets say a 10" x 10" x 1/2" and on that solid I need circles or holes at points given in an excel spreadsheet.
1. I want to create the solid myself manually.
2. Then click on a corner of it to tell the macro that that's 0,0. 3. Then the macro will read the excel sheet Xcord in column A Ycord in column B and place the circle which diameter is given in column C on that point.

Thanks for any help in advance.
I am very new to using VBA in AutoCAD.

Here is a snippet of my spreadsheet.
XLOC YLOC Hole Size
0.575 2.77 0.067
0.455 2.77 0.067
0.575 2.89 0.067
0.455 2.89 0.067
6.735 3.405 0.067

fixo
07-28-2010, 02:04 AM
Hello Everyone,
I am about to start my first attempt at coding this but a little push in the right direction would help to get me started. My goal is to create a macro that will layout holes on a solid for me. Example: I create a solid lets say a 10" x 10" x 1/2" and on that solid I need circles or holes at points given in an excel spreadsheet.
1. I want to create the solid myself manually.
2. Then click on a corner of it to tell the macro that that's 0,0. 3. Then the macro will read the excel sheet Xcord in column A Ycord in column B and place the circle which diameter is given in column C on that point.

Thanks for any help in advance.
I am very new to using VBA in AutoCAD.

Here is a snippet of my spreadsheet.
XLOC YLOC Hole Size
0.575 2.77 0.067
0.455 2.77 0.067
0.575 2.89 0.067
0.455 2.89 0.067
6.735 3.405 0.067



Drop the button on desired sheet then
add this code to button module:


Option Explicit
Sub cmdCircle_Click()

Dim selRng As Range
Set selRng = Selection
Dim circleData As Variant
circleData = selRng.Value2
Dim acad As AcadApplication
Set acad = GetObject(, "Autocad.Application")
Dim adoc As AcadDocument
Set adoc = acad.ActiveDocument
Dim aspace As AcadBlock
Dim oCircle As AcadCircle
Set aspace = adoc.ActiveLayout.Block
Dim i
For i = 1 To UBound(circleData, 1)
Dim insertPt(0 To 2) As Double
insertPt(0) = CDbl(circleData(i, 1))
insertPt(1) = CDbl(circleData(i, 2))
insertPt(2) = 0#
Dim Radii As Double
Radii = CDbl(circleData(i, 3))
Set oCircle = aspace.AddCircle(insertPt, Radii)
Next i
Set aspace = Nothing
Set adoc = Nothing
Set acad = Nothing
MsgBox "Done"
End Sub


Change code to your suit
Select Excel range you need without headers
Push a button
(AutoCAD drawing must be opened before)
Hth

~'J'~

KennyJ
07-28-2010, 07:19 AM
Thank you! This does exactly what I needed it to do. Now I just need to do a few tuts so I can write these myself. Two things that I would like to understand here though.

1. circleData = selRng.Value2
What is the Value2? Where does it come from?
2. I'm trying to understand whats going on with:
Dim insertPt (0 To 2) and the 3 lines below it containing insertPt.
If anyone is up for explaining or commenting the code so I can fully understand whats going on that would be very helpful too.