AutoLISP lesson #6 - NUMBERS & LETTERS Mathematical expression format 1 + 2 = X In AutoLISP it would be written as: (+ 1 2) ;Returns 3 As you should know by now, all lists in AutoLISP start with a function and the remainder of the list is the arguments to the function. Math in AutoLISP is no different. If you want to add two numbers you first give the function name which is "+" then you give the numbers you want the addition performed on. The rest of the math function works the same way. (/ 4 2) ;Returns 2 If you get confused at first just try to visualize the operator placed between the two numbers that follow the operator. Predefined mathematic functions + The addition operator. - The subtraction operator. * The multiplication operator. / The division operator. 1+ Adds one to a number. 1- Subtracts one from a number. min Gets the minimum number from a list of numbers. max Gets the maximum number from a list of numbers. abs Returns the absolute value of a number. sqrt Returns the square root of a number. exp Returns e raised to the power of a number. e is the constant 2.71828. expt Returns a number raised to the power of another number. log Returns the natural log of a number. gcd Returns the greatest common denominator of two integer numbers. rem Returns the remainder of the division of two numbers. sin Returns the sine of a number in radians. cos Returns the cosine of a number in radians. atan Returns the arctangent of a number in radians.
Numbers Magic numbers Type conversions An integer can be any number between -2,147,483,648 and +2,147,483,647. The + sign is optional as any number without a - sign is taken to be positive. Real numbers, or floating point, are double precision floating point values with at least 14 significant digits of precision. A floating-point number; must be written with leading 0's if the number is less than 1. The AutoLISP interpreter would, not accept the number .4. The number 0.4 would be recognized as a floating-point value. AutoLISP will automatically convert types for you when you mix them in your equations. The predefined function FIX converts a floating-point number to an integer number. Its complementary function is FLOAT, which converts integer numbers to floating-point numbers. Most of the time you will be dealing with floating point numbers. Point data is returned as floating-point numbers, and that is usually what we are dealing with. The real problem with using numbers when you’re not sure of their type is when you end up with two integer numbers. Integer division is not like floating point division. If you divide two integers you will end up with an integer. (/ 1 2) ;Returns 0 As you can see from the examples, an integer returns the result of the division as the integer portion of the result and leaves off the decimal portion. This is handy at times but can be a real "gotcha" if that's not what you had in mind. The best way to make sure this doesn't happen to you is to do the type conversions yourself, that way you know what you are dealing with. Strings Predefined String Functions Note: Arguments enclosed in brackets [] are optional. angtof (angtof string [mode]) Converts a string, which represents an angle in the display format, specified by mode, into a floating-point value. The return value will be expressed in radians if ANGTOF succeeds, else nil will be returned. The mode argument specifies the units in which the string is formatted. The value should match the values used by the AUNITS system variable. If mode is left out, ANGTOF uses the current setting of the AUNITS system variable. The possible values allowed for use with ANGTOF are shown below. Mode value String format Input to ANGTOF must be in a format acceptable as input to an AutoCAD command. angtos (angtos angle [mode [precision]]) Converts an angle in radians (a real number) to a string according to the values of "mode" and "precision". Mode and precision follow the same rules as applied to ANGTOF. The mode table shown above may be used. read (read "string") ;Returns the string string Returns the first list or atom obtained from the string. The string cannot contain blanks except within a list or string. Read attempts to convert the string into the closest data type. If it cannot figure out what data type to convert to it will return nil.
strcase (strcase "string" [which]) ;If [which] is nil Returns Returns a copy of a string with all the characters converted to either upper or lower case. If the "which" argument is omitted all characters are converted to uppercase. If "which" is supplied and non-nil STRCASE converts the characters to lowercase. strcat (strcase "string1" "string2");Returns "string1string2" Returns the concatenation of one or more strings. For those of you who don't know, "concatenation" simply means it combines the two strings into one string. strlen (strlen "string") ;Returns 6 Returns the length, in characters, of a string. If more than one string is given it returns the sum of all the string lengths. substr (substr "string" start [length]) Returns a sub-string of a string. It starts at "start" and goes on for [length]. If length is unspecified it returns the remainder of the string. ascii (ascii "abc") ;Returns 97 Returns the conversion of the first character of a string into its ASCII character code. atof (atof "25.4") ;Returns the real number 25.4 Returns the conversion of a string into a real value. atoi (atoi "25.4") ;Returns the integer 25 Returns the conversion of a string into an integer. chr (chr 66) ;Returns "B" Returns the conversion of an integer representing an ASCII character code into a single-character string. itoa (itoa 65) ;Returns "65" Returns the conversion of an integer into a string. rtos (rtos number [mode [precision]]) Returns the conversion of a real number into a string. The mode argument corresponds to the LUNITS system variable in AutoCAD. The precision corresponds to the LUPREC system variable. The unit mode values are set as follows. Mode Value String format If you omit the units RTOS uses the current settings of LUNITS and LUPREC to format the string. String considerations \\ Backslash character As you notice all the escape code use a backslash as the first character. This means that in AutoLISP you cannot use a backslash by its self. If you use a backslash such as in a path string the backslash will be ignored and all the characters will be run together. The following example illustrates this point. (setq path "c:\acad\lisp") ;Returns "c:acadlisp" Be careful not to do this in your lisp programs. You must use the forward slash when entering paths. Prompts If you were to give a person the choice between two different courses of action in one prompt and output them on two different command lines it would look something like the following. (prompt "\nPlease pick insertion point...\nor press enter to exit.") This would be perfectly acceptable to the AutoLISP interpreter and it would put the correct prompts to the screen. They would look like the following. command:Please pick insertion point... The better way to write the line would be as follows. Either: (prompt "\nPlease pick insertion point...") (prompt "\nor press enter to exit.") Or: (prompt (strcat "\nPlease pick insertion point ..." You would get the same output to the screen, but the program code is much more readable. When writing informative prompts to the user of your program, many times you will need to use STRCAT to get string and information all tied together for output to the screen. This can also get messy if you don't pay attention to how you code it. (setq Val1 "Dave") The example above shows a way to code the prompt, but once again I think can do better. (prompt (strcat "\nI think " The second way takes a little more typing but if you do many of these type prompts, you will find they are much easier to see what strings are being brought together. It becomes increasingly important as you bring more variables into the strings. |
|