AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Utilities

Whilst looking around the web at other peoples coding, I noticed that nearly everyone still uses the traditional AutoCAD "get" functions to retrieve entities and values.
In this tutorial, we're going to have a look at the Visual Lisp methods that are available to us to achieve the same results. We find these methods in the "Utility" object.

Note!  Most of the Visual Lisp "get" methods do not flip to the AutoCAD screen from the Visual Lisp Editor. You need to activate AutoCAD manually and then return to the Visual Lisp Editor.

Let's first have a look at the "standard" way of retrieving points and the drawing a line. Type the following in the console :

(vl-load-com)

_$ (setq acaddoc (vla-get-activedocument (vlax-get-acad-object)))
#<VLA-OBJECT IAcadDocument 00b94e14>

_$ (setq mspace (vla-get-modelspace acaddoc))
#<VLA-OBJECT IAcadModelSpace 01e42494>

_$ (setq PT1 (getpoint "\nSpecify First Point: "))
(228.279 430.843 0.0)

_$ (setq PT2 (getpoint "\nSpecify next point: " apt))
(503.866 538.358 0.0)

_$ (setq myline (vla-addline mspace (vlax-3d-point PT1)(vlax-3d-point PT2)))
#<VLA-OBJECT IAcadLine 00f9da94>

First we referenced Model Space, then we retrieved two points using the (getpoint) function.
We then used the (addline) method to draw our line, firstly converting the two point lists into variant arrays.

We can though, use Visual Lisp to obtain our two points. Try this :

_$ (setq util (vla-get-utility acaddoc))
#<VLA-OBJECT IAcadUtility 00f9e014>

_$ (setq PT1 (vla-getpoint util nil "\nSpecify First Point : "))
#<variant 8197 ...>

_$ (setq PT2 (vla-getpoint util PT1 "\nSpecify Second Point : "))
#<variant 8197 ...>

$ (setq myline (vla-addline mspace PT1 PT2))
#<VLA-OBJECT IAcadLine 00f9da94>

Because we used the Visual Lisp method to retrieve our points, the values were not returned as list, but as variant arrays. This means that we don't have to use (vlax-3d-point) to convert our points into variable arrays. 
As you probably noticed from the line
               (setq util (vla-get-utility acaddoc))
the (vla-get) method, is a method of the "Utilitity" object.
Let's have a closer look.

Application
  (Object)
        |
        |
        |------------- Active Document
        |                    (Object)
                                   |
                                   |
                                   |
                                   |------------- Utility
                                   |            (Object)

_$ (vl-load-com)

_$ (setq acaddoc (vla-get-activeDocument (vlax-get-acad-object)))
#<VLA-OBJECT IAcadDocument 00f5db9c>

_$ (setq util (vla-get-utility acaddoc))
#<VLA-OBJECT IAcadUtility 00f9e014>

_$ (vlax-dump-object util T)
; IAcadUtility: A series of methods provided for utility purposes

; No properties

; Methods supported:
; AngleFromXAxis (2)
; AngleToReal (2)
; AngleToString (3)
; CreateTypedArray (3)
; DistanceToReal (2)
; GetAngle (2)
; GetCorner (2)
; GetDistance (2)
; GetEntity (3)
; GetInput ()
; GetInteger (1)
; GetKeyword (1)
; GetOrientation (2)
; GetPoint (2)
; GetReal (1)
; GetRemoteFile (3)
; GetString (2)
; GetSubEntity (5)
; InitializeUserInput (2)
; IsRemoteFile (2)
; IsURL (1)
; LaunchBrowserDialog (6)
; PolarPoint (3)
; Prompt (1)
; PutRemoteFile (2)
; RealToString (3)
; TranslateCoordinates (5)
T

Well, the Utility object doesn't seem to have any Properties, but it's got a treasure trove of Methods. We'll now have a wee look at a few of them.


First though, let's recap on the way we convert VBA methods to Visual Lisp methods.
Let's look at the VBA "GetDistance" method :

GetDistance Method

Gets the distance from the prompt line or a selected set of points on the screen.

Syntax

RetVal = Object.GetDistance([Point][, Prompt])

  • Object -Utility - The object or objects this method applies to.

  • Point - Variant (three-element array of doubles); input-only; optional
    The 3D WCS coordinates specifying the base point. If this point is not provided, the user must input two points.

  • Prompt - Variant (string); input-only; optional
    The text to display to prompt the user for input.

  • RetVal - Variant (double or array of doubles)
    The distance from the prompt line or a selected set of points on the screen.

Remarks

AutoCAD pauses for user input of a linear distance and sets the return value to the value of the selected distance. The Point parameter specifies a base point in WCS coordinates. The Prompt parameter specifies a string that AutoCAD displays before it pauses. Both Point and Prompt are optional.

The AutoCAD user can specify the distance by entering a number in the current units format. The user can also set the distance by specifying two locations on the graphics screen. AutoCAD draws a rubber-band line from the first point to the current crosshair position to help the user visualize the distance. If the Point parameter is provided, AutoCAD uses this value as the first of the two points.

By default, GetDistance treats Point and the return value as three-dimensional points. A prior call to the InitializeUserInput method can force Point to be two-dimensional, ensuring that this method returns a planar distance.

Regardless of the method used to specify the distance or the current linear units (for example, feet and inches), this method always sets the return value to a double-precision floating-point value.

In Visual Lisp, the syntax would be :

RetVal = (vla-getdistance object [point] [prompt])

Example :

_$ (setq dist (vla-getdistance util nil "\nFirst Point : \n"))
535.428

Sometimes you retrieve a distance value as a string. Here's how you could convert it :

_$ (setq dist "123.45")
"123.45"

_$ (setq dist (vla-DistancetoReal util dist acDecimal))
123.45

Let's have a look at a few more "get" examples :

_$ (setq s (vla-getstring util 0 "Enter String: "))
"Kenny"

Note the "0" argument that disallows spaces.
Let's allow spaces this time :

_$ (setq s (vla-getstring util 1 "Enter String: "))
"Kenny Ramage is brilliant"

Let's get some Integers :

_$ (setq i (vla-getinteger util "\nEnter an Integer: "))
2

Now enter a real number for example 6.3

"Requires An Integer Value"
"Enter an Integer: "

Try it with a letter such as "A"
Same message.

Now we'll get some real's :

_$ (setq i (vla-getreal util "\nEnter and Integer: "))
5.7

Again, try it with a letter such as "C"

"Requires Numeric Value"
"Enter a Real: "

Now for an angle :

_$ (setq a (vla-getangle util nil "\nSelect Angle: "))
0.473349

Let 's give it a base point :

_$ (setq bpt (vla-getpoint util nil "/nSelect Base Point: "))
#<variant 8197 ...>

And use it to "rubber band" :

_$ (setq a (vla-getangle util bpt "\nSelect Angle: "))
0.707583


Now here is a couple of VERY interesting methods.

Want to select just one entity on the screen?

_$ (vla-getentity util 'obj 'ip "\nSelect Object: ")
nil
_$ obj
#<VLA-OBJECT IAcadLine 01ea57c4>
_$ ip
#<safearray...>

The reference to the Object is stored in the variable "obj" and the pickpoint is stored in variable "ip" in the form of a safearray.

Would you like to create a safearray very easily and quickly :

_$ (vla-createtypedarray util 'thearray vlax-vbDouble 1.2 2.3 0.0)
nil
_$ thearray
#<safearray...>

Your safearray is stored in the variable "thearray"


On the next page we'll continue our discussion on Visual Lisp Utilities.

 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website