Drawing and Moving a Box with VBA



Author Özgür Özkurt
Profession Mechanical Engineer
  Software Programmer
Location Istanbul, TURKEY
e-mail  

Created in AutoCAD 2008

Hi all,

I am going to explain a new example of VBA use in Autocad. By the help of this example, a box can be drawn and inserted to a point given according to the input parameters.
I have explained the starting technique of a connection between VBA and Autocad by means of OLE in my earlier articles.

Let us load the macro into Autocad. Firstly, download the macro from this link. Select the Macro Load Project from the Tools menu. Run the Visual Basic Editor by typing

Command: _vbaide

Select code form the View menu or simply press F7.

The next thing is examining the codes. Run macro first if you want (F5). Input the values and the observe the results. You can observe the codes directly if you want. AddBox is run by obeying the hiyerarchy of the Object-Model tree. The parameters of Addbox function is taken from the control of the TextBox in the user form shown below. Later, the parameters that will be used by the Move method will be taken by the same method.

Fig.1
Figure.1

Dim ins(0 To 2) As Double
ins(0) = TextBox4.Text
ins(1) = TextBox5.Text
ins(2) = TextBox6.Text

One of the parameters that will be used for Move method is the starting point and the other one is the destination point. The first parameter is the center of the box. And the second one is the tas variable that we defined and assigned a value (The contents of the TextBox control) in the former paragraph. The move process between these two defined points is going to be done soon.

wdth= TextBox1.Text
hgth= TextBox2.Text
lgth= TextBox3.Text

The parameters that are going to be used for the drawing of the box are taken as an input by the user by means of appropriate controls.

Set box = autocad_session1.ActiveDocument.ModelSpace.AddBox_
(center, lgth, wdth, hgth)

By placing the user defined values into the appropriate space in the Addbox function, the box is drawn.

Fig.2
Figure.2

This code is same with “View >Zoom >All”.

ThisDrawing.Regen acAllViewports
The drawing can be regenerated by this useful code.
autocad_session1.ZoomAll

This code hides main dialog box

Me.hide

This code pauses for user

MsgBox "Box is created and ready to insert."

Fig.3
Figure.3

User is informed with the text box.

Box.Move center, ins

Inserting process is carried out by running the Move method of the Text Box we have just defined. The box is moved to point (200,210,300).

What I explained is not the whole thing that you can do with VBA in Autocad. You can improve more useful macros with the basic idea I mentioned and reading help files.
That’s all for now. But I am going to prepare a new article based on the e-mails from you. And it will be more guideful.

Hope to see you soon.

Related topics:

Technorati Tags: , ,

Similar Posts

Rate this article:

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply