From the course: Learning AutoLISP & Visual LISP
Function: getstring - AutoCAD Tutorial
From the course: Learning AutoLISP & Visual LISP
Function: getstring
- [Instructor] Some of the most challenging work of writing automation is interacting with the user. So we're going to learn about the LISP functions that allow us to get information from the user. And since we're gathering data and cannot anticipate every entry a user would make, we're also going to get introduced to how we can trap some of the errors or the entries that are entered by the user that we may not be expecting. Most of the functions we're going to look at start with the get, so they're often referred to as the get functions. The first one we're going to look at is the get string. So I'm going to create a variable and assign to that variable the value that's returned from the get string function. Now the only required argument is simply what is going to be presented to the user, the prompt at the command line. Enter the name of your company. And so if I inspect this, it'll take me back to the AutoCAD graphics screen and ask me to enter the name of my company. Well, if I enter LinkedIn works great, but how about LinkedIn Corporation? I would have to put a space. Now a space in AutoCAD also means the same as return. Notice that the value is correct, but it stops at LinkedIn. What if I want a space or a tab or something else like that? I can add an optional argument to the get string function that says true. The true stands for we can allow spaces and tabs. So let's run this again. Enter the name of your company. We're going to be LinkedIn Corporation. So now we have the ability to add spaces into that value, because of the T. What if the user simply pressed enter at this prompt? Enter the name of your company. The result will simply be an empty string. So let's create a function here and see how we can use the values from the user. Create a function using the defunct function. We're going to name our function get data, and we see we already have the variable here that we want to store locally. Closing portion here, and we're just going to copy this up into our function, and then return the value to the user. Now, what are we returning? A combined string of your company name is and then the value of the variable, name string. So we're combining the strings. We looked at stringCAD earlier. We're using the print to command line, we're using the variable name string, that stores the results that the user has typed. So let's go ahead and load our get data LISP function to the drawing and run it, get data. Now enter the name of your company LinkedIn, and notice what's written to the command line, your company name is LinkedIn.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.