Drawing Polylines from data in Excel file

Drawing Polylines from data in Excel file

Anonymous
Not applicable
5,579 Views
4 Replies
Message 1 of 5

Drawing Polylines from data in Excel file

Anonymous
Not applicable

I would like an VB application that from within autocad can open an excel file,
from there select 2 columns containing x,y co-ordinates. Which when selected will draw a polyline in the acad drawing.

I usually do this the long way, but it seems I'm doing it more and more so would like to have it fairly automated.

New to VBA and stuck at even opening the excel file....nevermind the actual selecting columns and drawing of the polyline!!!

Thanks for your help

0 Likes
Accepted solutions (1)
5,580 Views
4 Replies
Replies (4)
Message 2 of 5

Hallex
Advisor
Advisor
Accepted solution

Add this code to .xlsm file,

see my poor explanation within the code

Option Explicit

' require references to:

'Windows Script Host Object model

'AutoCAD 20XX Type Library

'AutoCAD Focus Control for VBA Type Library (omit this if you do not show the result on the screen)

'in VBA editor -> Tools -> Options -> General -> Error Trapping box -> check "Break on Unhandled Errors"

Function acadVerNum() As String
Dim verNum As String

verNum = "HKEY_CLASSES_ROOT\AutoCAD.Drawing\CurVer\"
Dim wsh As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set wsh = CreateObject("WScript.Shell")
  'read key from registry
  Dim resKey As String
  resKey = wsh.RegRead(verNum)
  
 acadVerNum = Right(resKey, 2)
  Exit Function
  
ErrorHandler:
  'key was not found
 acadVerNum = ""
    
End Function

Public Sub testOpenAutoCAD()
Dim xlsheet As Worksheet
Dim xlRange As Range
Set xlsheet = ThisWorkbook.Worksheets.Item(2)
' define Excel range by address
Set xlRange = xlsheet.Range("A1:B10")
' you can use instead the previous selected Excel range this way:
'Set xlRange = Selection
Dim pts As Variant
pts = xlRange.Value
' you can use instead this expression too:
' pts = xlRange.Value2
Dim n As Integer, i As Integer, j As Integer
n = UBound(pts) * 2 - 1

ReDim coords(0 To n) As Double
For i = 1 To UBound(pts)
coords(j) = CDbl(pts(i, 1))
j = j + 1
coords(j) = CDbl(pts(i, 2))
j = j + 1
Next i
Dim acad As AutoCAD.AcadApplication
Dim appNum As String
On Error GoTo ErrorHandler
appNum = acadVerNum
If appNum = "" Then
MsgBox "Could not read registry."
Exit Sub
End If
On Error Resume Next
Set acad = GetObject(, "Autocad.Application." & appNum)
If Err.Number = 429 Then
Err.Clear

On Error GoTo 0
Set acad = CreateObject("Autocad.Application." & appNum)
If Err Then
Exit Sub
End If
End If

acad.WindowState = acMax

Dim adoc As AutoCAD.AcadDocument
Set adoc = acad.ActiveDocument

Dim aspace As AutoCAD.AcadBlock

Set aspace = adoc.ModelSpace

Dim lw As Double

lw = acLnWt030

Dim oPline As AcadLWPolyline
' add pline to Modelspace
Set oPline = aspace.AddLightWeightPolyline(coords)
' do some changes here, e.g.:
oPline.Lineweight = lw
oPline.Color = acGreen

adoc.SetVariable "lwdisplay", 1

ZoomExtents

Set aspace = Nothing
Set adoc = Nothing
Set acad = Nothing

ErrorHandler:

  If Err.Number <> 0 Then
  MsgBox Err.Description
  End If
  
End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 5

Anonymous
Not applicable

Thanks very much. Just tried it and works perfectly. 

Lewis

Message 4 of 5

Hallex
Advisor
Advisor
You're welcome
Glad to help
Cheers 🙂
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 5 of 5

Statschippac
Observer
Observer

Hi, I want to learn from your idea of importing Excel into Autocad. As from your example give before, how should execute it into AutoCad and how I link the Excel file?

0 Likes