AutoLisp Chapter - 1

AutoLisp Chapter - 1
Site menu


Login form


Search


Site friends
  • Create your own site


  • Statistics

    Total online: 1
    Guests: 1
    Users: 0


    Welcome, Guest ยท RSS 19 September 2024
     
     

    AutoLisp Tutorial for Beginner
    Chapter -1
     
    Topic Covered In this chepter
     
    • AutoLisp Basics
    • Variables 

    Tip
     
    For Better Comunication Where I wrote "type this at the command line" I will put the actual key strokes in red except for the enter key.  This will mean that you should type the red letters in at the Command Prompt inside AutoCAD as if you were typing the command "Line".  To clarify, If I say "type this at the command line: Line and press the enter key, you would type the word "Line" at the command prompt inside of AutoCAD and press the enter key afterwards.
    Is It O.k. ?

     
    AutoLisp Basics
     
    AutoLisp always store in a closed Paranthesis "(      )".
     
    Every AutoLisp program should look similar to this:

    (defun c:test ()
          (princ "Hello AutoLisp")
          (princ)
    )

    You can execute AutoLisp at the Command Prompt inside AutoCAD.
     

    Understanding the AutoLISP Interpreter and Evaluation

     
    Now we look about what AutoLisp Do after we enter some on Command prompt. open AutoCad and write this at the command line.
     
    (+ 2 5)
     
     
     
     
    Do you see the next line of you typed at command prompt ? It give the answer 7. Look above image. AutoLisp always return a value after you execute somthing on it.
     
    This structure + 2 5 enclosed by parentheses, is called an expression and it is the basic structure for all AutoLISP programs. Everything intended for the AutoLISP interpreter, from the simplest expression to the most complex program, must be written with this structure. The result returned from evaluating an expression is called the value of the expression.
     

    Variable
     
    In your program you need to remember value given by the expression. you can do this by define a Variable. A Variable is like a container that holds a value. That value can change any time of a program's operation. You can create a Variable by Setq Function.
     
     

    Understanding Data Types

    Variables can take on several types of values or data types. Here is what some of these data types look like in AutoLiSP.

     

    DATA TYPE
    EXAMPLE
    Integer
    24
    Real Number
    0.618
    String
    "India"
    List
    (4.5021 6.3011 0.0)
    File Descriptor
    <File: a620>
    Object Name
    <Object name: 60000014c>
    Selection Set
    <Selection set: 1>
    Symbols
    Point1
    Subrs
    Setq

     
    Difference between Integer & Real No.
     
    Integers are whole numbers from -32768 to + 32767. The value of an expression containing only integers is always give result an integer. on other hand Real numbers are numbers that include a decimal value. now try the following expression at command prompt.
     
    (+ 75 12)
    87
     
    (+ 75.0 12.0)
    87.0
     
    do you notice the difference ? when you give only integer in AutoLisp expression it give result in Integer, but when you use real value it give result in Real Value. now try this expression
     
    (+ .215 15)
     
    the above expression looks perfectly normal, but the following error message appears:
     
    error: invalid dotted pair
     
    the real number 0.215 is preceded by a zero and not written as .215. In AutoLISP, real numbers with values between 1.0 and 0.0 must begin with zero. If you do not follow this rule, you will get an error message. remember this.
     
     
    Strings
     
    The term string refers to text. Strings are often used as prompts in AutoLISP expressions but they can also be manipulated using AutoLISP. For example, using the Strcat AutoLISP function, you could combine two strings, "My Name is" and "Dhimant Panchal", into one string "My Name is Dhimant Panchal". Try entering this:
     
    (strcat "My Name is " "Dhimant Panchal")
     
    The following is returned:
     
    "My Name is Dhimant Panchal"
     
    Lists
     
     Lists are data elements enclosed in parentheses. They are the basic data structure in AutoLISP. A list can be made up of any number of integers, real numbers, strings, and even other lists.
    There are two types of lists. An example of a list as a  coordinate location. For example, the list
     
    (5.0 10.0 20.0)
     
    contains three elements, an X, Y, and Z coordinate. The first element, 5.0, is the x coordinate, the second element, 10.0 is the y coordinate, and the third element, 20.0, is the z coordinate.
     
    We will learn about other type of data later.
     
     
    Use Setq to assigne a Value to Variable
     
    the setq function tells AutoLisp to assigne a value to variable. You can use alfabates, numbers or combination of alphabate & number as  variable. there are no problem with upper case or lower case. for example VAR1 is equal to var1. try the following expression.
     
    (setq Var1 1.500)
     
    You can now obtain the value of a variable by preceding the variable name by an exclamation point. Now check the value of Var1
     
    !Var1
    the value 1.500 is returned.
     
    Setq will assigne a value to the variable if the variable already has a value. try following.
     
    (setq var1 1.500)
     
    (setq var1 (+ var1 2.500))
     
    the value 4.00 is returned.