Using Local Variables in the Program

AutoCAD Visual LISP

 
Using Local Variables in the Program
 
 
 

Refer to the gp:getPointInput function you created in Lesson 1:

(defun gp:getPointInput	()
  (alert
    "Function gp:getPointInput will get user drawing input"
  )
  ;; For now, return T, as if the function worked correctly.
  T
)

So far, the function does not do much work. You will now begin to build on it by adding functions to get input from the user, which will define the start point, endpoint, and width of the path.

It is a good practice when creating AutoLISP programs to emulate the behavior of AutoCAD. For this reason, instead of asking the user to indicate the width by selecting a point in the drawing in respect to the centerline of a linear shape, your program should ask for a selection of the half-width.

Once the gp:getPointInput function is complete, the variables, as well as the values assigned to them, will no longer exist. Therefore, you will store user-supplied values in local variables. Here's what the function might look like:

(defun gp:getPointInput	(/ StartPt EndPt HalfWidth)
  (if (setq StartPt (getpoint "\nStart point of path: "))
    (if (setq EndPt (getpoint StartPt "\nEndpoint of path: "))
      (if (setq HalfWidth (getdist EndPt "\nhalf-width of path: "))
          T
      )
    )
  )
)

The local variables are declared following the slash character, in the defun statement that begins the function. The first call to getpoint prompts the user to indicate a start point. The endpoint is then acquired in relation to the chosen start point. While selecting the endpoint, the user will observe a rubber-band line extending from the start point. Similarly, while setting the half-width value, the user will view another rubber-band line, this time representing distance, emanating from the endpoint.

To see how gp:getPointInput works

  1. Type the gp:getPointInput code into the VLISP Console window.
  2. With the Console window cursor following the last parenthesis of the block of code (or on the next line below it), press ENTER and you will replace any previously loaded version of the gp:getPointInput function.
  3. Execute the function from the Console window by entering (gp:getPointInput) at the Console prompt.
  4. Pick points when prompted, and enter a half-width value.