Utilities - Page II
Let's now have a look at getting some
keywords from the user.
The following example forces the user to
enter a keyword by setting the first parameter of InitializeUserInput to
1, which disallows NULL input (pressing ENTER).
_$ (vla-InitializeUserInput util 1
"Line Arc Circle")
nil
_$ (setq kword (vla-GetKeyword util "Enter an Option
(Line/Circle/Arc) : "))
"Line"
Let's have a closer look at "InitiliazeUserInput.
"InitiliazeUserInput" :
Syntax :
(vla-InitiliazeUserInput
Object Bits Keyword)
Object : The object or
objects this method applies to. In this case, the object is the Utility
Object.
Bits : Integer;
input-only
To set more than one condition at a time, add the values together in any
combination. If this value is not included or is set to 0, none of the
control conditions apply.
1
|
Disallows
NULL input. This prevents the user from responding to the request
by entering only [Return] or a space.
|
2
|
Disallows
input of zero (0). This prevents the user from responding to the
request by entering 0.
|
4
|
Disallows
negative values. This prevents the user from responding to the
request by entering a negative value.
|
8
|
Does not
check drawing limits, even if the LIMCHECK system variable is on.
This enables the user to enter a point outside the current drawing
limits. This condition applies to the next user-input function
even if the AutoCAD LIMCHECK system variable is currently set.
|
16
|
Not
currently used.
|
32
|
Uses
dashed lines when drawing rubber-band lines or boxes. This causes
the rubber-band line or box that AutoCAD displays to be dashed
instead of solid, for those methods that let the user specify a
point by selecting a location on the graphics screen. (Some
display drivers use a distinctive color instead of dashed lines.)
If the POPUPS system variable is 0, AutoCAD ignores this bit.
|
64
|
Ignores Z
coordinate of 3D points (GetDistance method only). This option
ignores the Z coordinate of 3D points returned by the GetDistance
method, so an application can ensure this function returns a 2D
distance.
|
128
|
Allows
arbitrary input—whatever the user types.
|
Keyword : Variant
(array of strings); input-only; optional
The keywords that the following user-input method will recognize.
Remarks : Keywords
must be defined with this method before the call to GetKeyword. Certain
user-input methods can accept keyword values in addition to the values
they normally return, provided that this method has been called to define
the keyword. The user-input methods that can accept keywords are:
GetKeyword, GetInteger, GetReal, GetDistance, GetAngle, GetOrientation,
GetPoint, and GetCorner.
Let's have a look at
another example :
A more user-friendly keyword prompt is
one that provides a default value if the user presses ENTER
(NULL input). Notice the minor modifications
to the following example:
(vla-InitializeUserInput util 0
"Line Arc Circle")
nil
_$ (setq kword (vla-GetKeyword util "Enter an Option
(Line/Circle/<Arc>) : "))
""
_$ (if (= kword "") (setq kword "Arc"))
"Arc"
Now we'll look at something very simple.
Try this :
(vla-prompt util "\nPress any
key to continue.....")
nil
And another interesting one :
_$ (setq pt1 (vla-getpoint util nil
"\nFirst Point :"))
#<variant 8197 ...>
_$ (setq pt2 (vla-getpoint util pt1 "\nSecond Point :"))
#<variant 8197 ...>
_$ (setq theangle (vla-AngleFromXAxis util pt1 pt2))
0.459321
Of course, the result is
returned in radians.
The Polar function, as
you are probably well aware, is one of the most powerful function in the
AutoLisp arsenal. Because of this, we are going to have a close look at
the PolarPoint method. Lets have a look at the syntax first :
"PolarPoint"
Syntax :
RetVal = (vla-PolarPoint
Object Point Angle Distance)
Example : (setq pt3 (vla-polarpoint
pt2 0.5 20.0))
-
Object : Utility
- The object or objects this method applies to.
-
Point : Variant
(three-element array of doubles); input-only
The 3D WCS coordinates specifying the start point.
-
Angle : Double;
input-only
The angle in radians.
-
Distance : Double;
input-only
The distance in current units.
-
RetVal : Variant
(three-element array of doubles)
The 3D WCS coordinates at the specified angle and distance from a
given point.
Here's a working example
:
The following coding will draw slotted
holes as per the above diagram.
It begins by asking the user for an insertion point, the slot length, and
then the slot diameter using Visual Lisp Get methods.
It then uses the PolarPoint method to calculate the other points required
to draw the slot. Then, using the AddLine and AddArc methods, it draws the
slotted hole.
;CODING STARTS HERE
(defun c:slot ( / acaddoc util mspace ip lg
dia P1 P2 P3 P4 P5 P6 P7)
;load VL functions
(vl-load-com)
;obtain reference to the Active Document
(setq acaddoc (vla-get-activeDocument (vlax-get-acad-object)))
;obtain reference to Utilities
(setq util (vla-get-utility acaddoc))
;obtain reference to Model Space
(setq mspace (vla-get-modelSpace acaddoc))
;get the insertion point
(setq ip (vla-getpoint util nil "\nInsertion Point: "))
;get the length
(setq lg (vla-getreal util "\nEnter Slot Length: "))
;get the diameter
(setq dia (vla-getreal util "\nEnter Slot Diameter: "))
;calculate all the points
(setq P1 (vla-polarpoint util IP (dtr 270.0) (/ dia 2)))
(setq P2 (vla-polarpoint util P1 (dtr 180.0) (/ lg 2)))
(setq P3 (vla-polarpoint util P2 (dtr 90.0) dia))
(setq P4 (vla-polarpoint util P3 0 lg))
(setq P5 (vla-polarpoint util P4 (dtr 270.0) dia))
(setq P6 (vla-polarpoint util P2 (dtr 90.0) (/ dia 2)))
(setq P7 (vla-polarpoint util P5 (dtr 90.0) (/ dia 2)))
;draw the lines
(vla-AddLine mspace p2 p5)
(vla-AddLine mspace p3 p4)
;add the arcs
(vla-AddArc mspace p6 (/ dia 2) (dtr 90.0) (dtr 270.0))
(vla-AddArc mspace p7 (/ dia 2) (dtr 270.0) (dtr 90.0))
(princ)
);defun
----------
;convert degrees to radians
(defun DTR (a)
(* pi (/ a 180))
);defun
----------
(princ)
;CODING ENDS HERE
|
|