strebrad danyque saffire

0 views
Skip to first unread message

Priamo Gregory

unread,
Aug 2, 2024, 9:21:24 PM8/2/24
to solewato

Knowing a little bit of programming can be extremely useful in the AutoCAD environment. Before you cringe with dread at the prospect of having to learn a programming language, it sounds far more difficult than it really is. Especially since VB stands for "Visual Basic" - there's a clue in the name... Writing simple applications for automating tasks isn't hard - really.

This tutorial assumes no previous knowledge of VBA in AutoCAD or otherwise. I will explain the very basics of using VBA in AutoCAD. I will give a brief overview of the VBA IDE (Integrated Development Environment) and I will explain a small snippet of code and how it works.

If I assume you know nothing about VBA, it follows that you probably don't know what VBA stands for. VBA stands for Visual Basic for Applications. Most people reading this will probably have heard of Visual Basic, and are aware that it is a Microsoft Windows based programming language. The "for Applications" bit means that it is integrated behind the scenes in the software application you're using, be it Word, Excel, or in our case AutoCAD.

In order to begin, we need to access the VBA IDE. This is where we will be working. Input VBAIDE into the command line. This opens up a new window which is the VBA IDE. You should have something that looks like the below:

The section of the screen with the big red "1" is the project window. This shows you all the sections of your VBA project. Right click in this area and select insert/module. This will (surprisingly!) insert a module. A module is basically a container for your code. If you like, you can rename the module from "Module1" to something more descriptive of what the module does or is for.

The section of the screen represented by the number 3 is the editing window. If it's grey at the moment (like in the example), you're not editing anything. If you've added a module, chances are you're looking at a white screen, and you can type stuff. When you add a module, it automatically opens it up in the editing portion of the screen.

The section of the screen represented by the number 4 is a toolbar - in particular the buttons that handle the execution of code. The run button causes VBA to start executing code from the current subroutine (i.e. the subroutine in the module you're editing that contains the cursor). The pause button will pause execution, and take you to the debugging screen. The current position that VBA has got to will be highlighted with yellow. The stop button halts execution.

Make sure you're at a state where you are editing a module. You might have at the top of the screen the words "Option Explicit". This is fine, but also don't worry if it's not there. You don't really need to know about this right now. Paste the following code into the module:

Everything should have pasted in nicely. The text should automatically change into a lovely assortment of black, dark blue, and green colours. If you get any red, then something went wrong when you copy and pasted. 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. Red text indicates anything that contains errors, or the structure of what you've written is wrong. However, just because it's not red, doesn't necessarily mean that it contains no errors. Black is anything that doesn't fall under any of the above.

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. To do this, you would type in -VBARUN into the command line, followed by you're subroutine name. This command can conveniently be stored in a toolbar or pallette using the normal process for editing the user interface - CUI. Notice the preceeding "-" in front of the VBARUN command. You probably already know this is an AutoCAD command modifier. The dash explicitly tells AutoCAD to expect command line entry. If the dash is omitted, VBARUN opens a little dialog box, and you can pick your subroutine from there.

Maybe you executed the code and thought "that's not very interesting..". If so, I assure you this is the tip of a very large iceberg. In the code above, I've added comments in such a way that it explains it a little, but I'll give a little more detail now.

This is the first line of code. This line creates what is known as a variable. The Keyword "Dim" is the part of the code that tells VB to create a variable. A variable is basically a memory bank with a name. The name of the "memory bank" or variable is MyString. We specified the name MyString, but it could have been called something else if we liked. The line Dim This_is_a_variable as string would be equally valid, but it's a bit of a mouthful. Also, any reference to MyString in the code would need to be changed to This_is_a_variable in order for the code to work in the same way. The part of the code that says "As String" tells VB what type of variable it is. 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. If it cannot, an error will occur with the message "Type Mismatch"

This line of code shows you how to set the contents of a variable. It is simply a case of stating that Variable = SOMETHING. 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. The part following MsgBox in this instance is our variable called MyString, however we didn't have to use a variable here. 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.

This should look quite familiar - again we're declaring a variable. You'll notice that it is a different type. In this instance it is a number. Specifically, a double precision floating point number, but we don't need to know the details yet. 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. We'll probably talk more about arrays at another time.

Similarly to before, this is assigning values to the variable. However this time, as we're dealing with an array, we need to specify which part of the list we want to assign our value to. 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.Modelspace.Addtext. This adds some text into the modelspace of the active drawing. If there is no active drawing (i.e, they're all closed) an error will occur. In order for AutoCAD to be able to add text, it needs to know certain things: What text do you want to display? Where do you want to display it? What size do you want it to be? At this stage I will point out that the text we are adding is NOT MText. It is Text - what you would get by using the Text command in AutoCAD. Conveniently, we've already set up some variables that contain the values we need. Pop them after Thisdrawing.Modelspace.Addtext in the right order, delimited with a comma, and you've now got some text in the modelspace of the current drawing.

I hope you found this a useful introduction - we haven't don't anything particularly useful yet, but there's much much more great info where this came from. If you liked what you read I would encourage a subscription - you won't regret it!

Have you ever seen a text file in a notepad or a source code file in visual studio code? These all are just basic files where you can write some code or information. Similarly, we have modules in excel, each module has its code window, where you can write some code and allow it to automate your repetitive tasks. There can be multiple modules. We can create, rename or delete a module in just a few mouse clicks. In this article, we will learn how to create and delete a module in excel VBA.

In data science, coding languages are used across all job roles. They enable Data scientists to pull data from multiple datasets, clean and analyze that data, visually convey the importance of the data, and design databases and machine-learning algorithms. The best programming language for you will depend on your role as a Data scientist, specific project goals, and your level of experience.

Some disadvantages include slower speed due to its dynamic, line-by-line execution of code, and high memory usage. Python has some shortfalls in client-side or mobile applications because of its memory inefficiency and slower processing. Python also has less-developed database access than other languages and more frequent runtime errors.

Structured Query Language, known as SQL, is an essential language for Data scientists managing relational databases and working with large amounts of structured data. SQL allows a Data scientist to query, perform analysis of and manipulate data within relational databases and create test environments for data.

While it is the most used programming language for statistical functions, R has advantages and disadvantages. It is open-source, platform-independent, and has a large collection of libraries. It supports various data types, and it is useful for Data scientists responsible for data cleansing, data wrangling, and web scraping. Its libraries offer access to high-quality graphs, visualizations, and resources to help process large data sets using parallel or distributed computing. R does not require a compiler to turn code into an executable program, is compatible with other programming languages, is used in machine-learning applications, and has a comprehensive development environment.

c01484d022
Reply all
Reply to author
Forward
0 new messages