top of page
Search
  • Writer's pictureRosangela Rodriguez

AE2. INSERTING AUTOCAD OBJECTS FROM VBA

Hi All


It has been a while since the last time I posted... but it feels good to be back.

In this post, I will show you how to insert some objects into the CAD model space.



HOW TO INSERT A CIRCLE

In order to insert an object you will need to declare the object and its parameters, in the case of a circle you will need a radius and a center point.


STEP 1. declaring the object and parameters

Dim circleObj As AcadCircle

Dim radius As Double

Dim centerpoint(0 To 2) As Double


STEP 2. Setting the values for the radios and the center point

radius = 500#

centerpoint(0) = 0#: centerpoint(1) = 0#: centerpoint(2) = 0#


STEP 3. Finally sending the command


Set circleObj = AcadFile.ModelSpace.AddCircle(centerpoint, radius)


*AcadFile must have been previously declared and assigned, I show how to do it on my post AE1. HOW TO LAUNCH AUTOCAD FROM EXCEL. https://discoverthevalueof.wixsite.com/blog/post/ae1-how-to-launch-autocad-from-excel



HOW TO INSERT A LINE

To insert a line you will only need to define 2 points.


Dim LineObj As AcadLine


Point1(0) = 1000#: Point1(1) = -500#: Point1(2) = 0#

Point2(0) = 2000#: Point2(1) = 500#: Point2(2) = 0#

Set LineObj = AcadFile.ModelSpace.AddLine(Point1, Point2)



HOW TO INSERT A TEXT

the command to insert a text will ask you for the CAPTION to display, the point where you want to place the text and the size of the text.


Dim textObj As AcadText


Point1(0) = 2500#: Point1(1) = 0#: Point1(2) = 0#


Set textObj = AcadFile.ModelSpace.AddText("SAMPLE", Point1, 150)


This is a very simplified version of how to do it, there are ways to select the text style, and also you could refer the caption to a variable.



HOW TO INSERT A DIMENSION

As in any CAD software, there are 2 different types of dimension, we have rotated dimensions. and aligned dimensions.


For both objects, you will need to assign 3 points and for the case of the rotated dimension, you will also need the angle to rotate the object. You must be aware VBA reads angles as radians' value.


So here we go...


DimRotated

Dim Dimension(0 To 0) As Object


Point1(0) = 4000#: Point1(1) = -500#: Point1(2) = 0#

Point2(0) = 5000#: Point2(1) = -500#: Point1(2) = 0#

Point3(0) = 4000#: Point3(1) = -1500#: Point3(2) = 0#


Set Dimension(0) = AcadFile.ModelSpace.AddDimRotated(Point1, Point2, Point3, 0)


DimAligned

Point1(0) = 4000#: Point1(1) = 500#: Point1(2) = 0#

Point2(0) = 5000#: Point2(1) = -500#: Point1(2) = 0#

Point3(0) = 5000#: Point3(1) = 1500#: Point3(2) = 0#


Application.Wait (Now + TimeValue("0:00:01"))

Set Dimension(0) = AcadFile.ModelSpace.AddDimAligned(Point1, Point2, Point3)




SAMPLE CODE:

The following is a copy of the code that i used to make the video for this post.



Sub AUTOCAD_COMMANDS_FROM_VBA()

Dim ACAD As AcadApplication

Dim Point1(0 To 2) As Double

Dim Point2(0 To 2) As Double

Dim Point3(0 To 2) As Double


'***** OPENING AUTOCAD EXISTING DRAWING ****

On Error Resume Next

Set ACAD = GetObject(, "ACAD.Application")

If (Err <> 0) Then

Err.Clear

Set ACAD = CreateObject("autocad.Application")

If (Err <> 0) Then

MsgBox "Could Not Load AutoCAD!", vbExclamation

End

End If

End If

ACAD.Visible = True 'If you want to see AutoCAD on screen

dwgName = "C:\Users\User\Desktop\SAMPLEAE2"

bReadOnly = True

Set AcadFile = ACAD.Documents.Open(dwgName, bReadOnly)

If AcadFile.ActiveSpace = acModelSpace Then

End If


'***** INSERTING A CIRCLE *****

Dim circleObj As AcadCircle

Dim radius As Double

radius = 500#

Point1(0) = 0#: Point1(1) = 0#: Point1(2) = 0#

Set circleObj = AcadFile.ModelSpace.AddCircle(Point1, radius)

'***** INSERTING A LINE *****

Dim LineObj As AcadLine


Point1(0) = 1000#: Point1(1) = -500#: Point1(2) = 0#

Point2(0) = 2000#: Point2(1) = 500#: Point2(2) = 0#

Set LineObj = AcadFile.ModelSpace.AddLine(Point1, Point2)



'***** INSERTING A TEXT *****

Dim textObj As AcadText


Point1(0) = 2500#: Point1(1) = 0#: Point1(2) = 0#


Set textObj = AcadFile.ModelSpace.AddText("SAMPLE", Point1, 150)



'***** INSERTING A DIMENSION *****

Dim Dimension(0 To 0) As Object


Point1(0) = 4000#: Point1(1) = -500#: Point1(2) = 0#

Point2(0) = 5000#: Point2(1) = -500#: Point1(2) = 0#

Point3(0) = 4000#: Point3(1) = -1500#: Point3(2) = 0#


Set Dimension(0) = AcadFile.ModelSpace.AddDimRotated(Point1, Point2, Point3, 0)


Point1(0) = 4000#: Point1(1) = 500#: Point1(2) = 0#

Point2(0) = 5000#: Point2(1) = -500#: Point1(2) = 0#

Point3(0) = 5000#: Point3(1) = 1500#: Point3(2) = 0#


Set Dimension(0) = AcadFile.ModelSpace.AddDimAligned(Point1, Point2, Point3)


End Sub


As always, I hope that this post is helpful and that you enjoyed it.


I will read you on the comments.


Until next time!

136 views0 comments

Recent Posts

See All
Post: Blog2_Post
bottom of page