Vba with autocad tutorial


















A subroutine is a container for our code. So in this example, we would pick the option that refers to the ConvertText2MText subroutine. We can also run the subroutine from within VBA — perhaps from another subroutine.

The way to do that, is to write the subroutine name in our code. So to run the subroutine in the example above, we would simply write:. Then, it will run the content, and when it reaches the End Sub statement, VB will return to where it was originally, and continue to process code as normal.

This can be a useful way of splitting up our VBA applications into logical and understandable chunks that can be called upon whenever necessary. So lets expand on our example. Lets say that we have 4 subroutines. But if we actually want to run all of them however, it might be useful to set up another subroutine to call them all:. When you execute the ReformatDrawing subroutine, it will run the other 3 subroutines in the order that they appear. Well yes you could, but when you start making complicated code its not advisable because it makes it hard to follow.

Also, programming in this way makes your code nicely reusable because it easy to transfer whole subroutines from one project to another.

Now I will show you what else subroutines can be useful for. It is sometimes useful for the subroutine to do certain things based on given input. Say for example we wanted a subroutine that inserts some text into the drawing at a changable position.

We can make it so that we can give the subroutine some parameters, and make it do things based on the information we provide. In this example, we could make it so that the subroutine will put text into our drawing based on coordinates that we supply. So I will explain how to pass parameters to a subroutine. However, if we alter the code slightly, we can make it a bit more intuative. We can alter the subroutine so that it is anticipating additional information.

When editing code, different colours of text represent different things. Green text represents a comment or note — this text can be anything and is for the purpose of you making your own notes. Dark blue text represents key words that VB understands.

You probably guessed that to execute the code, you click the run button in the toolbar at the top. The shortcut for this button is the F5 key. As an additional point, it is sometimes useful to know how to execute your code without opening the IDE. This command can conveniently be stored in a toolbar or pallette using the normal process for editing the user interface — CUI. If so, I assure you this is the tip of a very large iceberg. Code is executed from the top down in the same manner you would read a book, unless you tell VB to do something else.

This is the first line of code. This line creates what is known as a variable. A variable is basically a memory bank with a name.

We specified the name MyString, but it could have been called something else if we liked. In this case, it is what is known as a String variable, or a Text-String. This basically means that anything that is attempted to be stored into the variable MyString should be interepretted as Text. If it is not text, VB will try to convert it to text.

This line of code shows you how to set the contents of a variable. The text after the equals sign must be enclosed inside quotation marks. If it was not enclosed in quotation marks, VB would try to execute the text as code, and an error would occur the text would also be red. So enclosing it in quotation marks tells VB not to interpret this as code, but use it as a text value. This line of code causes a messagebox to be displayed. The command for doing so is MsgBox.

We could have explicitly put in any text we like enclosed in quotation marks, and it would be executed in the same way, displaying the inside of the quotation marks instead of the contents of the variable.

In this instance it is a number. You might also have noticed the brackets containing the number 2. The name of the variable is still just Point but this time it is an array. Think of an array as a list within that variable. This particular array has 3 elements to that list — 0, 1, and 2. Similarly to before, this is assigning values to the variable. Realise that all of the values are actually stored in the same variable, but just organised into a list.

The final line of code in this subroutine. The first part is quite self explanatory — Thisdrawing. This adds some text into the modelspace of the active drawing.



0コメント

  • 1000 / 1000