How to add parallel dimension lines in AutoCAD using EXCEL VBA

EXCEL×AutoCAD

Introduction.

Hello. In this article, I will explain how to use Excel to fill in parallel dimension lines in AutoCAD. I have also provided a Function procedure for easy use in the second half of the article. We hope you will find it useful.

Sample code to add parallel dimension lines

Let me show you a sample code to draw parallel dimension lines using the values entered in Excel.
First, prepare an Excel sheet like the one below.

The sequence of cells A2 to I2 are the input cells.
J2 is a cell to get the handle value of the drawn parallel dimension line. No input is required.

The VBA code is as follows

VB
Sub DrawAlDimInAutoCAD()
  Dim acadApp As Object 'Declare AutoCAD application object
  Dim acadDoc As Object 'Declare AutoCAD document object
  Dim dimLine As Object 'Declare aligned dimension line object
  Dim startPoint(2) As Double 'Array to store start point coordinates
  Dim endPoint(2) As Double 'Array to store end point coordinates
  Dim dimPoint(2) As Double 'Array to store dimension text position coordinates
  
  'Start AutoCAD if not running, or get the existing instance
  On Error Resume Next
  Set acadApp = GetObject(,"AutoCAD.Application")
  If acadApp Is Nothing Then
    Set acadApp = CreateObject("AutoCAD.Application")
  End If
  On Error GoTo 0
  
  'Show AutoCAD
  acadApp.Visible = True
  
  'Create a new document if no document is open, otherwise get the active document
  On Error Resume Next
  Set acadDoc = acadApp.ActiveDocument
  If acadDoc Is Nothing Then
    Set acadDoc = acadApp.Documents.Add
  End If
  On Error GoTo 0 
  
  'Set start point coordinates
  startPoint(0) = Cells(2,1) 'X coordinate
  startPoint(1) = Cells(2,2) 'Y coordinate
  startPoint(2) = Cells(2,3) 'Z coordinate
  
  'Set end point coordinates
  endPoint(0) = Cells(2,4) 'X coordinate
  endPoint(1) = Cells(2,5) 'Y coordinate
  endPoint(2) = Cells(2,6) 'Z coordinate 
  
  'Set dimension text position coordinates
  dimPoint(0) = Cells(2,7) 'X coordinate
  dimPoint(1) = Cells(2,8) 'Y coordinate
  dimPoint(2) = Cells(2,9) 'Z coordinate   
  
  'Add aligned dimension line
  Set dimLine = acadDoc.ModelSpace.AddDimAligned(startPoint, endPoint, dimPoint)
  
  ' Get and store the handle of the added dimension line
  Cells(2,10) = dimLine.Handle
  
  'Display message
  MsgBox ”Aligned dimension line has been added!”
End Sub
  

Preparation of VBA in EXCEL

Next, we will show you a Function procedure that allows you to easily add parallel dimension lines, but before we can do that, we need to do some preparation: we need to set up a reference to the AutoCAD object library. Please perform the following operation.

  • Select “Tools” -> “Reference Settings” from the VBA Editor menu.
  • Select “AutoCAD Type Library” or “AutoCAD Object Libraly” from the list, check the box and click “OK”.(*The name may change slightly depending on the version. AutoCAD 2021 Type Library” etc.)

VBA Function procedure to add parallel dimension lines

If you create your code as a Function procedure, you can more easily draw a large number of parallel dimension lines. Here we describe how to do this.

First, let’s write the following code in the standard module of EXCEL VBA as it is.

VB
Public acadApp As AcadApplication       'Variable to hold the AutoCAD application document
Public acadDoc As AutoCAD.AcadDocument  'Variable to hold the AutoCAD document object

Function DrawAlDimInAutoCAD(x1 As Double, y1 As Double, z1 As Double, x2 As Double, y2 As Double, z2 As Double, x3 As Double, y3 As Double, z3 As Double) As String
  'Function to draw an aligned dimension line in AutoCAD
  'Parameters in order:
  'Start point X, Y, Z coordinates, End point X, Y, Z coordinates, Dimension text position X, Y, Z coordinates
  'Returns the handle of the drawn aligned dimension line
  
  Dim dimLine As Object 'Variable to hold the dimension line object
  
  Dim startPoint(2) As Double 'Array to hold the start point coordinates
  Dim endPoint(2) As Double 'Array to hold the end point coordinates
  Dim dimPoint(2) As Double 'Array to hold the dimension text coordinates
  
  'Set start point coordinates
  startPoint(0) = x1
  startPoint(1) = y1
  startPoint(2) = z1
  
  'Set end point coordinates
  endPoint(0) = x2
  endPoint(1) = y2
  endPoint(2) = z2
  
  'Set dimension text coordinates
  dimPoint(0) = x3
  dimPoint(1) = y3
  dimPoint(2) = z3  
  
  'Add an aligned dimension line to AutoCAD model space
  Set dimLine = acadDoc.ModelSpace.AddDimAligned(startPoint, endPoint, dimPoint)
  
  'Return the handle of the added dimension line
  DrawAlDimInAutoCAD = dimLine.Handle
  
End Function

Now drawing parallel dimension lines is as easy as calling DrawAlDimInAutoCAD(x1,y1,z1,x2,y2,z2,x3,y3,z3).

For example, prepare the following sheet

Then write a sample of the main procedure. This sample should be executed with AutoCAD open. It draws a line segment in the active drawing.

VB
public Sub main()
  On Error GoTo OUT1 ' Error handling: AutoCAD is not running
  Set acadApp =GetObject(,"AutoCAD.Application") ' Get the existing AutoCAD application
  
  On Error GoTo OUT2 ' Error handling: No AutoCAD file is open
  Set acadDoc = acadApp.ActiveDocument ' Get the active AutoCAD document
  
  On Error GoTo 0 ' Reset error handling
  
   ' Draw parallel dimension lines in AutoCAD and write their handles to Excel
  Cells(2,10) = DrawAlDimInAutoCAD(Cells(2,1), Cells(2,2), Cells(2,3), Cells(2,4), Cells(2,5), Cells(2,6), Cells(2,7), Cells(2,8), Cells(2,9))
  Cells(3,10) = DrawAlDimInAutoCAD(Cells(3,1), Cells(3,2), Cells(3,3), Cells(3,4), Cells(3,5), Cells(3,6), Cells(3,7), Cells(3,8), Cells(3,9))
  Cells(4,10) = DrawAlDimInAutoCAD(Cells(4,1), Cells(4,2), Cells(4,3), Cells(4,4), Cells(4,5), Cells(4,6), Cells(4,7), Cells(4,8), Cells(4,9))  
  Exit Sub
OUT1:
  MsgBox "Please start AutoCAD"
  Exit Sub
OUT2:
 MsgBox "Please open an AutoCAD file"
End Sub

Lines 11 to 13 are calling the Function procedure. For the sake of the sample, I am describing each line one by one, but if you use a For statement, you can draw a large number of line segments.

Of course, you can use the following without reading values from cells.

VB
  Cells(2,10) = DrawAlDimInAutoCAD(0, 0, 0, 1000, 1000, 0, 1000, 0, 0) ' Draw a dimension line from (0,0,0) to (1000,1000,0)

If you want to draw a line segment but do not need a handle, you can prepare a variable such as dummy as shown below.

VB
 dummy = DrawAlDimInAutoCAD(0, 0, 0, 1000, 1000, 0, 1000, 0, 0) ' Draw a dimension line from (0,0,0) to (1000,1000,0)

Conclusion

In this article, we showed how to draw parallel dimension lines in AutoCAD using Excel VBA.
By utilizing VBA, you can automate your work in AutoCAD and further improve your design efficiency.

A relationship lasts longer when you keep the right distance—precisely measured!

コメント

Copied title and URL