AutoLisp Chapter - 3

AutoLisp Chapter - 3
Site menu


Login form


Search


Site friends
  • Create your own site


  • Statistics

    Total online: 1
    Guests: 1
    Users: 0


    Welcome, Guest ยท RSS 03 December 2024
    AutoLisp Tutorial for Intermediate
     

    Chapter - 3
     
     
    Topics Coverd In this Chapter
    •  Using Functions
    • Getdist- Pauses for user input of a distance
    • Getangle or Getorient - Pauses for user input of an angle
     


    Using Functions

    We have broken the box program down conceptually into three processes. We can also break it down physically by turning each process into a function as in the following:
      
    (defun getinfo ()  
    (setq pt1 (getpoint "Pick first corner: "))  
    (setq pt3 (getcorner pt1 "Pick opposite corner: "))  
    )
      
    (defun procinfo ()  
    (setq pt2 (list (car pt3) (cadr pt1)))  
    (setq pt4 (list (car pt1) (cadr pt3)))  
    )
      
    (defun output ()  
    (command "line'' pt1 pt2 pt3 pt4 "c'' )  
    )
     
    You now have three functions that can be called independently. We now need a way to tie these functions together. We can write a main program that does just that:
      
    (defun C:BOX1 (/ pt1 pt2 pt3 pt4)  
    (getinfo)  
    (procinfo) 
    (output)  
    )

    Let's see how this new version of the Box drawing program works.

    1. Open a new AutoLISP file called box1.lsp.

    2. Enter the above three functions, Getinfo, Procinfo, and output, along with the new box program.

    3. Open a new AutoCAD file and load the Box1.lsp file.

    4. Run the BOX1 program. You will see that it acts no differently from the first box1 program.

     

    Reusing Functions

    You have seen that by breaking the box program into independent functions, you can add other functions to your program to alter the way the program works. In the following section, you will create two more programs, one to draw a 3D rectangle, and one to draw a wedge, using those same functions from the box program.

     

    Creating an 3D Box program

    To create a 3D box from a rectangle, all you really need to do is extrude your rectangle in the Z axis then add a 3dface to the top and bottom of the box. Extruding the box can be accomplished using the Change command.
      
    (defun C:3DBOX (/ pt1 pt2 pt3 pt4 h)
    (getinfo)
    (setq h (getreal "Enter height of box: "))
    (procinfo)
    (output)
    (command "change" "L" "" "P" "th" h "" 
    "3dface" pt1 pt2 pt3 pt4 "" "3dface" 
    ".xy" pt1 h ".xy" pt2 h ".xy" pt3 h ".xy" pt4 h "")
    ) 

    In the following exercise, you will add the 3D box program to the Box1.lsp file then run 3DBOX.

    • Exit AutoCAD temporarily either by using the AutoCAD Shell command or by using the End command to exit AutoCAD entirely.
    • Open the box1.lsp file again and add the program listed above to the file. download code.
    • Load Box1.lsp again. If you had to Exit AutoCAD to edit Box1.lsp.
    • Start the C:3DBOX program by entering 3dbox at the command prompt.
    • At the Pick first corner prompt, pick a point near the coordinate 2,3.
    • At the Pick opposite corner prompt, move the cursor until the coordinate readout lists 7.0000,5.0000 then pick that point.
    • At the Enter height prompt, enter 6. A box appears.
    • Issue the Vpoint command and at the prompt:
            Rotate/<View point> <0.0000,0.0000,1.0000>:
      
    • Enter -1,-1,1.Now you can see that the box is actually a 3d box.
    • Issue the Hide command. The box appears as a solid object.

    Getdist - Pauses for user input of a distance
     
    The user can specify the distance by selecting two points, or by specifying just the second point, if a base point is provided.
    In your program where you need to ask user for Distance you can use Getdist Funtion.
     
    The syntax for Getdist is
     
    (getdist [pt] [msg])
     

    pt (Optional)

    A 2D or 3D point to be used as the base point in the current UCS. If pt is provided, the user is prompted for the second point. you can also provide value by keyboard too.

    msg (Optional)

    A string to be displayed to prompt the user. If no string is supplied, AutoCAD does not display a message.

    Return Values

    A real number. If a 3D point is provided, the returned value is a 3D distance.
     
    you can use getdist by three ways.
     
    -->>  1.   you can use getdist by entering value from keyboard. type this at command line.
     
    (setq dist1 (getdist "Pick First point or enter Distance :"))
     
    The below prompt will appear
     
    Pick First point or enter Distance :
     
    Now type 150 from keyboard. once you entered, 150 is assigned to variable dist1. check value of variable dist1 by entering following
     
    !dist1
     
    the distance value 150 displyed.
     
    -->> 2. you can use getdist by pick point from screen. type this at command line.
     
    (setq dist1 (getdist "Pick First point or enter Distance :"))
     
    The below prompt will appear
     
    Pick First point or enter Distance :
     
    now pick a point on graphic screen. you will get following prompt immediatly
     
    Specify second point:
     
    And a rubber band line from the first point to the current crosshairs position will appear. it helps the user visualize the distance.
     
    now pick second point on graphic screen. the distance between first and second point is assigned to variable dist1. to check the value of variable dist1 enter following at command line
     
    !dist1
     
    And you get the distance between two point we picked earlier.
     
    -->> 3. you can use getpoint for first point or use your previous point variable. to do this enter following
     
    (setq pt1 (getpoint "Pick a Point :"))
     
    when pick a Point prompt appear pick point anywhere on the screen. the coordinate is assigned to variable pt1. now we use this pt1 variable as our primary argument in getdist function. type this at command line
     
    (setq dist1 (getpoint pt1 "Specify second point :"))
     
    Specify second point:
     
    above prompt will appear And a rubber band line from the first point to the current crosshairs position will appear. now pick a point on graphic screen. the distance between first and second point is assigned to variable dist1.

    Getangle or Getorient - Pauses for user input of an angle
     
    The difference between Getangle and Getorient is that Getangle will return an angle value relative to the current Unit setting for the 0 angle while Getorient will return an angle with default zero-radian direction to the right (east). For example, the default zero-radian direction is set to east side but you can use the Units command or the Angbase system variables to make 0 degrees a vertical direction. If a drawing is set up with 0 degrees being a direction from bottom to top, Getangle will return a value relative to this orientation while Getorient will return a value based on the "Defalt" orientation regardless of the Units, Angbase settings. below Figure showing the default zero-radian direction of base angle (units command).
      
      
    Autocad's Unit command Dialogue Box
     
     
     
    Comparision of Getangle & Getorient
     
    Now if we set base angle to north by Units command or angbase system variable as shown in image above. the getangle is return angle 5.757 and for the same getorient will return 1.0446. both function uses anticlock wise direction. now we look about this two functions syntax and its use.
     
    The syntax for getangle is
    (getangle [pt] [msg]) 
     
     and the syntax for getorient is
     
    (getorient [pt] [msg] ) 
     

    pt (Optional)

    A 2D base point in the current UCS. If pt is provided, the user is prompted for the second point. you can also provide value by keyboard too.

    msg (Optional)

    A string to be displayed to prompt the user. If no string is supplied, AutoCAD does not display a message.

    Return Values

    The angle specified by the user, in radians. 
     
    you can use getangle or getorient by both keyboard enty and by pick a point on screen. first type below on command line
     
    (setq ang1 (getangle "Pick First point or enter Distance :"))
     
    you will see the prompt Pick First point or enter Distance : at command line now type 60 by keyboard and check value of variable ang1.
    you get 1.0472 as answer. why?  you type 60 degree on screen. because getangle returns the output value in radians.
     
    as we do with getdist previously, you can supply a point variable as an argument to getangle
     
    enter this
     
    (setq pt1 (list 2 5))
     
    now enter this expression on command line
     
    (setq ang1 (getangle pt1 "Pick Second point :"))
     

    A rubber-banding line appears from the coordinate 3,3. now Pick a point other than coordinate 2,5. The angle from pt1 to second point is assigned to ang1.

    To get the value of ang1, enter:

     !ang1
    the value of ang1 is returned.