Author Topic: Nil function definition when key is pressed  (Read 2776 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Nil function definition when key is pressed
« on: July 23, 2018, 06:21:06 PM »
I am working on rewriting a routine and whenever I press a key, I keep getting the following error: ** Error: no function definition: nil **

Here is the code I have so far, I am not sure what is causing this, any ideas?

Edit: I figured that out, but for some reason F10 causes an error, it doesn't toggle polar mode.

Code: [Select]
(defun c:uleader (/ *ThisDrawing* *Space* *Scale* *IsCivil* *Path* *Prompt* *Align* *Match* *Types* Pt1 Pt2 Input Data Code Ent1 POS)
    (vl-cmdf "._-insert" "Uleader.dwg")(command); Insert in leader styles
    (setq *Types* (LIST "MTEXT" "1M08" "PL08" "AH08" "CFM" "FXU" "GPM" "PANEL" "FEEDER"))
    (vl-load-com)
    ;Supporting Functions
    (defun eraseold (obj /) ;Erases the previous version of a VLA object - Code modifcation provided by ronjonp at http://www.theswamp.org/index.php?topic=54340.msg589085#msg589085
        (and (= 'vla-object (type obj))
            (not (vlax-erased-p obj))
            (vlax-write-enabled-p obj)
            (vla-delete obj)
        )
    )
    (defun GetVportScale ()
        (/ 1 (vla-get-CustomScale (vla-get-ActivePViewport (vla-get-activedocument (vlax-get-acad-object)))))
    )
    (defun PolarRound (ang deg)
        (* (/ pi (/ 180 (r2d deg))) (fix (/ (+ (/ pi (/ 360 (r2d deg))) ang) (/ pi (/ 180 (r2d deg))))))
    )
    (defun bitcodef (value bit)
        ;; Originally Written by Lee Ambrosius on: 6/6/04
        ;; Modified by Chris Wade 1/13/2010
        (if (zerop(logand bit value))
            nil
            T
        );;
    ) ;defun bitcode
    (defun D2R (numberOfDegrees)
        (* pi (/ numberOfDegrees 180.0))
    ) ;_ end of defun
    (defun R2D (nbrOfRadians)
        (* 180.0 (/ nbrOfRadians pi))
    )
    ;End of Supporting Functions
    (setq *Path* (getvar "dwgprefix")
          *ThisDrawing* (vla-get-activedocument (vlax-get-acad-object)) ;_ Set Drawing and Space
      *Space*
            (if (zerop (vla-get-activespace *ThisDrawing*))
                (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
                    (vla-get-modelspace *ThisDrawing*) ; active VP
                    (vla-get-paperspace *ThisDrawing*)
                )
                (vla-get-modelspace *ThisDrawing*)
            )
)
(If (> (strlen *path*) 5);Determine if this is a civil drawing
(progn
(setq pos (vl-string-position (ascii "\\") *Path* (+ (vl-string-position (ascii "\\") *path* 5) 1)))
(If (or (= (strcase (substr *path* (+ pos 8) 1)) "C") (= (getvar "USERS5") "CIVIL"))
(setq *IsCivil* T)
)
)
)
    (if (zerop (vla-get-activespace *ThisDrawing*)); Set scale
        (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
            (setq *Scale* (GetVportScale))
            (setq *Scale* 1.0)
        )
        (setq *Scale* (getvar "DimScale"))
    )
    (vla-StartUndoMark *ThisDrawing*)
    ;ERROR HANDLER
    (defun *error* (msg)
        (if mlObj
            (progn
                (vla-highlight mlObj :vlax-false)
                (EraseOld mlObj)   
            )
        )
        ;; Added by Pkenewell on 3/28/2014
        (vla-EndUndoMark *ThisDrawing*)
        (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
            (princ (strcat "\n** Error: " msg " **"))
        )
        (princ)
    )
    (if (not (tblsearch "LAYER" "LEADER")) ; Check to see if layer exists
(if (= 1 (getvar "pstylemode"))
(command "._-layer" "n" "LEADER" "color" "WHITE" "" "lweight" "DEFAULT" "" "ltype" "CONTINUOUS" "" "")
(command "._-layer" "n" "LEADER" "color" "WHITE" "" "lweight" "DEFAULT" "" "ltype" "CONTINUOUS" "" "pstyle" "BLACK" "" "")
)
)
(setvar "annoautoscale" 4)
(while (not Pt1)
    (initget "Select _Select")
    (setq Pt1 (getpoint "\nPick starting point of leader [Select leader]:"))
    (cond
        ((= Pt1 "Select")
            (while (not Ent1)
                (setq Ent1 "\nSelect leader to modify or align to:")
                (if (not Ent1)
                    (princ "\r*** You must select a leader ***")
                )
                (setq Pt1 (cadr Ent1))
            )
        )
    )
)
(if (not Ent1)
    (setq Ent1 (nentselp "" Pt1))
)
(if Ent1
    (progn ;If existing leader is selected allow for modifying or aligning to the leader
        (princ "\nAdd code for modifying/aligning leader here")
    )
    (progn ;If existing leader is not selected, draw a new leader
        (while (not Pt2)
            (setq *Prompt* "\rSpeciy second point [Content ")
            (if (or (not *Content*) (= *Content* ""))
                (setq *Content* "MTEXT")
            )
            (setq *Prompt* (strcat *Prompt* "(" *Content* ")"))
            (if *Note*
                (setq *Prompt* (strcat *Prompt* "/** Note **"))
                (setq *Prompt* (strcat *Prompt* "/Note"))
            )
            (if *Typ*
                (setq *Prompt* (strcat *Prompt* "/** Typical **"))
                (setq *Prompt* (strcat *Prompt* "/Typical"))
            )
            (if (not *POC*)
                (setq *Prompt* (strcat *Prompt* "/Poc(d)"))
                (setq *Prompt* (strcat *Prompt* *POC*))
            )
            (setq *prompt* (strcat *Prompt* "/Arrowheads/Exit]:"))
            (if *Align*
                (setq *Prompt* (strcat *Prompt* " " *Align*))
            )
            (if *Match*
                (setq *Prompt* (strcat *Prompt* " " *Match*))
            )
            (princ *Prompt*)
                (setq Input (grread t 4 4)
                      Data (cadr Input)
                      Code (car Input)
                )
                (cond
                    ((= Code 2);Handle all keypresses
                        (cond
                            ((= data 15); Change Orthomode when F8 is pressed
                                (if (= (getvar "orthomode") 1)
        (setvar "orthomode" 0)
        (setvar "orthomode" 1)
        )
                            )
                            ((= data 21); Toggle Polarmode when F10 is pressed
        (setvar "autosnap" (bitcodef (getvar "autosnap") 8))
        )
        ((or (= data 67) (= data 99))
            (setq POS (vl-position *Content* *Types*))
            (if (< POS (length *Types*))
                (setq *Content* (nth (+ POS 1) *Types*))
                (setq *Content* (nth 0 *Types*))
            )
        )
        )
                    )
                )
        )
    )
)
    (vla-EndUndoMark *ThisDrawing*)
)
« Last Edit: July 23, 2018, 06:27:53 PM by cmwade77 »

kpblc

  • Bull Frog
  • Posts: 396
Re: Nil function definition when key is pressed
« Reply #1 on: July 24, 2018, 01:05:54 AM »
I think error is in string
Code - Auto/Visual Lisp: [Select]
  1. (command "._-layer" "n" "LEADER" "color" "WHITE" "" "lweight" "DEFAULT" "" "ltype" "CONTINUOUS" "" "pstyle" "BLACK" "" "")
There is no option "pstyle" (ACAD2018):
Code: [Select]
Command: ._-layer
Current layer:  "0"
Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile/Xref]:
Sorry for my English.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Nil function definition when key is pressed
« Reply #2 on: July 24, 2018, 11:41:25 AM »
No, that one is quite correct when using style based plotting and not color based plotting and the if statement checks to make sure if style based or color based plotting is used.

No, the only issue with the posted code is pressing F10 causes an error at present. It should toggle polar snapping on and off, not cause an error.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Nil function definition when key is pressed
« Reply #3 on: July 24, 2018, 01:24:38 PM »
A few things jump out:

Code - Auto/Visual Lisp: [Select]
  1.     (defun bitcodef (value bit)
  2.         ;; Originally Written by Lee Ambrosius on: 6/6/04
  3.         ;; Modified by Chris Wade 1/13/2010
  4.         (if (zerop(logand bit value))
  5.             nil
  6.             T
  7.         );;
  8.     ) ;defun bitcode

Could be written more succinctly as:
Code - Auto/Visual Lisp: [Select]
  1. (defun bitcodef ( v b ) (< 0 (logand b v)))



Code - Auto/Visual Lisp: [Select]
  1.     (defun PolarRound (ang deg)
  2.         (* (/ pi (/ 180 (r2d deg))) (fix (/ (+ (/ pi (/ 360 (r2d deg))) ang) (/ pi (/ 180 (r2d deg))))))
  3.     )
  4.     (defun D2R (numberOfDegrees)
  5.         (* pi (/ numberOfDegrees 180.0))
  6.     ) ;_ end of defun
  7.     (defun R2D (nbrOfRadians)
  8.         (* 180.0 (/ nbrOfRadians pi))
  9.     )

These functions are not evaluated anywhere in the code and could therefore be removed.



Code - Auto/Visual Lisp: [Select]
  1.         *Space*
  2.             (if (zerop (vla-get-activespace *ThisDrawing*))
  3.                 (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
  4.                     (vla-get-modelspace *ThisDrawing*) ; active VP
  5.                     (vla-get-paperspace *ThisDrawing*)
  6.                 )
  7.                 (vla-get-modelspace *ThisDrawing*)
  8.             )

This could be written more succinctly as:

Code - Auto/Visual Lisp: [Select]
  1.         *Space* (vlax-get-property *ThisDrawing* (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))



This:

Code - Auto/Visual Lisp: [Select]
  1. (if (= (getvar "orthomode") 1)
  2.     (setvar "orthomode" 0)
  3.     (setvar "orthomode" 1)
  4. )

Could become:

Code - Auto/Visual Lisp: [Select]
  1. (setvar 'orthomode (- 1 (getvar 'orthomode)))



This:

Code - Auto/Visual Lisp: [Select]
  1. (setvar "autosnap" (bitcodef (getvar "autosnap") 8))

Should be changed to the following to XOR bit 8:

Code - Auto/Visual Lisp: [Select]
  1. (setvar 'autosnap (boole 6 8 (getvar 'autosnap)))

Since your bitcodef function will return a boolean value, not an integer.



An error of 'no function definition: nil' would indicate that you are attempting to evaluate a symbol with no value as a function, however, I can't see anywhere in your code where this is occurring. As such, I would suggest following this process to determine the line on which the code fails.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Nil function definition when key is pressed
« Reply #4 on: July 24, 2018, 01:30:35 PM »
Sorry, I should have mentioned the code is a work in progress, the functions that aren't currently being called will be in the end code. And as always, thank you Lee, lots of great info. And I as I mentioned about, I got rid of the nil error, it was stupidity on my part not adding a (cond) that should have been there.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Nil function definition when key is pressed
« Reply #5 on: July 24, 2018, 01:42:00 PM »
Hi,

Not so often... ;)

Code - Auto/Visual Lisp: [Select]
  1. *Space* (vlax-get-property *ThisDrawing* (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))

This could be written more succinctly as:

Code - Auto/Visual Lisp: [Select]
  1. *Space* (vla-get-Block (vla-get-ActiveLayout *ThisDrawing*))
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Nil function definition when key is pressed
« Reply #6 on: July 24, 2018, 01:48:11 PM »
This could be written more succinctly as:

Code - Auto/Visual Lisp: [Select]
  1. *Space* (vla-get-Block (vla-get-ActiveLayout *ThisDrawing*))

This won't return Modelspace for an active Paperspace viewport...  :wink:


cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Nil function definition when key is pressed
« Reply #7 on: July 24, 2018, 01:49:08 PM »
Gile, your code is a very close second to Lee's as to how often it appears in our routines. Honestly, I think you have had some solutions that no one else could have solved. In the end this will be a complex piece of code, but I hope to get it working more reliably than my old uleader routine, so starting off with these recommendations is very helpful.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Nil function definition when key is pressed
« Reply #8 on: July 24, 2018, 01:49:47 PM »
Sorry, I should have mentioned the code is a work in progress, the functions that aren't currently being called will be in the end code. And as always, thank you Lee, lots of great info. And I as I mentioned about, I got rid of the nil error, it was stupidity on my part not adding a (cond) that should have been there.

You're welcome Chris, the F10 issue should be solved when you implement my suggestion:
Code - Auto/Visual Lisp: [Select]
  1. (setvar 'autosnap (boole 6 8 (getvar 'autosnap)))

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Nil function definition when key is pressed
« Reply #9 on: July 24, 2018, 02:05:53 PM »
Ok, I have implemented several of the recommendations so far and made some tweaks, as well as added some additional code, so here is where things stand now:

Code: [Select]
(defun c:uleader (/ *ThisDrawing* *Space* *Scale* *IsCivil* *Path* *Prompt* *Align* *Match* *Types* *Style* *POC* *POCS* *Typ* *Note* Pt1 Pt2 Input Data Code Ent1 POS Pt1Tst)
    (vl-cmdf "._-insert" "Uleader.dwg")(command); Insert in leader styles
    (setq *Types* (LIST "MTEXT" "1M08" "PL08" "AH08" "CFM" "FXU" "GPM" "PANEL" "FEEDER")
          *POCS* (list nil "-Poc" "-Pod")
    )
    (vl-load-com)
    ;Supporting Functions
    (defun eraseold (obj /) ;Erases the previous version of a VLA object - Code modifcation provided by ronjonp at http://www.theswamp.org/index.php?topic=54340.msg589085#msg589085
        (and (= 'vla-object (type obj))
            (not (vlax-erased-p obj))
            (vlax-write-enabled-p obj)
            (vla-delete obj)
        )
    )
    (defun GetVportScale ()
        (/ 1 (vla-get-CustomScale (vla-get-ActivePViewport (vla-get-activedocument (vlax-get-acad-object)))))
    )
    (defun PolarRound (ang deg)
        (* (/ pi (/ 180 (r2d deg))) (fix (/ (+ (/ pi (/ 360 (r2d deg))) ang) (/ pi (/ 180 (r2d deg))))))
    )
    (defun D2R (numberOfDegrees)
        (* pi (/ numberOfDegrees 180.0))
    ) ;_ end of defun
    (defun R2D (nbrOfRadians)
        (* 180.0 (/ nbrOfRadians pi))
    )
    ;End of Supporting Functions
    (setq *Path* (getvar "dwgprefix")
          *ThisDrawing* (vla-get-activedocument (vlax-get-acad-object)) ;_ Set Drawing and Space
      *Space* (vlax-get-property *ThisDrawing* (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)); Code adjustment from Lee Mac at: http://www.theswamp.org/index.php?topic=54349.msg589159#msg589159
      *Scale* (if (zerop (vla-get-activespace *ThisDrawing*))
                        (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
                            (GetVportScale)
                            1.0
                        )
                        (getvar "DimScale")
                  )
)
(If (> (strlen *path*) 5);Determine if this is a civil drawing
(progn
(setq pos (vl-string-position (ascii "\\") *Path* (+ (vl-string-position (ascii "\\") *path* 5) 1)))
(If (or (= (strcase (substr *path* (+ pos 8) 1)) "C") (= (getvar "USERS5") "CIVIL"))
    (setq *IsCivil* T)
)
)
)
    (if (zerop (vla-get-activespace *ThisDrawing*)); Set scale
        (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
            (setq *Scale* (GetVportScale))
            (setq *Scale* 1.0)
        )
        (setq *Scale* (getvar "DimScale"))
    )
    (vla-StartUndoMark *ThisDrawing*)
    ;ERROR HANDLER
    (defun *error* (msg)
        (if mlObj
            (progn
                (vla-highlight mlObj :vlax-false)
                (EraseOld mlObj)   
            )
        )
        ;; Added by Pkenewell on 3/28/2014
        (vla-EndUndoMark *ThisDrawing*)
        (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
            (princ (strcat "\n** Error: " msg " **"))
        )
        (princ)
    )
    (if (not (tblsearch "LAYER" "LEADER")) ; Check to see if layer exists
(if (= 1 (getvar "pstylemode"))
(command "._-layer" "n" "LEADER" "color" "WHITE" "" "lweight" "DEFAULT" "" "ltype" "CONTINUOUS" "" "")
(command "._-layer" "n" "LEADER" "color" "WHITE" "" "lweight" "DEFAULT" "" "ltype" "CONTINUOUS" "" "pstyle" "BLACK" "" "")
)
)
(setvar "annoautoscale" 4)
(while (not Pt1)
    (initget "Select _Select")
    (setq Pt1 (getpoint "\nPick starting point of leader [Select leader]:"))
    (cond
        ((= Pt1 "Select")
            (while (not Ent1)
                (setq Ent1 "\nSelect leader to modify or align to:")
                (if (not Ent1)
                    (princ "\r*** You must select a leader ***")
                )
                (setq Pt1 (cadr Ent1))
            )
        )
    )
)
(if (not Ent1)
    (setq Ent1 (nentselp "" Pt1))
)
(if Ent1
    (progn ;If existing leader is selected allow for modifying or aligning to the leader
        (princ "\nAdd code for modifying/aligning leader here")
    )
    (progn ;If existing leader is not selected, draw a new leader
        (while (not Pt2)
        (if (or (not *Content*) (= *Content* ""))
                (setq *Content* (nth 0 *Types*))
            )
            (If (= *Content* "MTEXT")
                (progn
                    (if *IsCivil*
                        (setq *Arrowheads* (list "CIVIL" "BEIOB-R1" "BEINOB-R1" "BOX-R1" "LOOP-R1" "BEISPL"))
                        (setq *Arrowheads* (list "BEIOB-R1" "BEINOB-R1" "BOX-R1" "LOOP-R1" "BEISPL"))
                    )
                )
                (progn
                    (if *IsCivil*
                        (setq *Arrowheads* (list "CIVIL" "BEIOB-R1" "BEINOB-R1" "BOX-R1" "LOOP-R1"))
                        (setq *Arrowheads* (list "BEIOB-R1" "BEINOB-R1" "BOX-R1" "LOOP-R1"))
                    )
                )
            )
            (setq *Prompt* (strcat "\rSpecify second point [Content (" *Content* ")"))
            (if (or (not *Style*) (= *Style* ""))
                (setq *Style* (nth 0 *Arrowheads*))
            )
            (if (and (/= *Content* "1M08") (/= *Content* "MTEXT"))
                (progn
                    (if *Note*
                        (setq *Prompt* (strcat *Prompt* "/** Note **"))
                        (setq *Prompt* (strcat *Prompt* "/Note"))
                    )
                    )
                )
                (if (/= *Content* "MTEXT")
                (progn
                    (if *Typ*
                        (setq *Prompt* (strcat *Prompt* "/** Typical **"))
                        (setq *Prompt* (strcat *Prompt* "/Typical"))
                    )
                    )
                )
            (if (not *POC*)
                (setq *Prompt* (strcat *Prompt* "/Poc(d)"))
                (setq *Prompt* (strcat *Prompt* "/" (substr *POC* 2)))
            )
            (setq *prompt* (strcat *Prompt* "/Arrowheads (" *Style* ")/Exit]:"))
           
            (if *Align*
                (setq *Prompt* (strcat *Prompt* " " *Align*))
            )
            (if *Match*
                (setq *Prompt* (strcat *Prompt* " " *Match*))
            )
            (princ *Prompt*)
                (setq Input (grread t 4 4)
                      Data (cadr Input)
                      Code (car Input)
                )
                (cond
                    ((= Code 2);Handle all keypresses
                        (cond
                            ((= data 15); Change Orthomode when F8 is pressed
                                ;Code adjustment from Lee at http://www.theswamp.org/index.php?topic=54349.msg589159#msg589159
                                (setvar 'orthomode (- 1 (getvar 'orthomode)))
                            )
                            ((= data 21); Toggle Polarmode when F10 is pressed
                                ;Code adjustment from Lee at http://www.theswamp.org/index.php?topic=54349.msg589159#msg589159
        (setvar 'autosnap (boole 6 8 (getvar 'autosnap)))
        )
        ((or (= data 65) (= data 97)); Change arrowheads when A is pressed
            (setq POS (vl-position *Style* *Arrowheads*))
            (if (< POS (length *Arrowheads*))
                (setq *Style* (nth (+ POS 1) *Arrowheads*))
                (setq *Style* (nth 0 *Arrowheads*))
            )
        )
        ((or (= data 67) (= data 99)); Change content when C is pressed
            (setq POS (vl-position *Content* *Types*))
            (if (< POS (length *Types*))
                (setq *Content* (nth (+ POS 1) *Types*))
                (setq *Content* (nth 0 *Types*))
            )
        )
        ((or (= data 80) (= data 112)); Cycle through Point of connection/disconnection when P is pressed
            (setq POS (vl-position *POC* *POCS*))
            (if (< POS (length *POCS*))
                (setq *POC* (nth (+ POS 1) *POCS*))
                (setq *POC* (nth 0 *POCS*))
            )
        )
        ((and (or (= data 78) (= data 110)) (/= *Content* "1M08") (/= *Content* "MTEXT")); Change Note when N is pessed
            (if *Note*
                (setq *Note* nil)
                (setq *Note* T)
            )
        )
        ((and (or (= data 84) (= data 116)) (/= *Content* "MTEXT")); Change Typical when T is pessed
            (if *Typ*
                (setq *Typ* nil)
                (setq *Typ* T)
            )
        )
        )
                    )
                )
        )
    )
)
    (vla-EndUndoMark *ThisDrawing*)
)


I will have more changes coming, but I am wondering if there are things I can do to simplify this code further.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Nil function definition when key is pressed
« Reply #10 on: July 24, 2018, 03:04:17 PM »
As promised, here is more, for some reason when I put the style to the leader, the leader always ends at 0,0, does anyone have an idea as to why?

Code: [Select]
(defun c:uleader (/ *ThisDrawing* *Space* *Scale* *IsCivil* *Path* *Prompt* *Align* *Match* *Types* *Style* *POC* *POCS* *Typ* *Note* *NewPoints* *MlObj* Pt1 Pt2 FxPt Input Data Code Ent1 POS Pt1Tst)
    (vl-cmdf "._-insert" "Uleader.dwg")(command); Insert in leader styles
    (setq *Types* (LIST "MTEXT" "1M08" "PL08" "AH08" "CFM" "FXU" "GPM" "PANEL" "FEEDER")
          *POCS* (list nil "-Poc" "-Pod")
    )
    (vl-load-com)
    ;Supporting Functions
    (defun eraseold (obj /) ;Erases the previous version of a VLA object - Code modifcation provided by ronjonp at http://www.theswamp.org/index.php?topic=54340.msg589085#msg589085
        (and (= 'vla-object (type obj))
            (not (vlax-erased-p obj))
            (vlax-write-enabled-p obj)
            (vla-delete obj)
        )
    )
    (defun GetVportScale ()
        (/ 1 (vla-get-CustomScale (vla-get-ActivePViewport (vla-get-activedocument (vlax-get-acad-object)))))
    )
    (defun PolarRound (ang deg)
        (* (/ pi (/ 180 (r2d deg))) (fix (/ (+ (/ pi (/ 360 (r2d deg))) ang) (/ pi (/ 180 (r2d deg))))))
    )
    (defun D2R (numberOfDegrees)
        (* pi (/ numberOfDegrees 180.0))
    ) ;_ end of defun
    (defun R2D (nbrOfRadians)
        (* 180.0 (/ nbrOfRadians pi))
    )
    ;End of Supporting Functions
    (setq *Path* (getvar "dwgprefix")
          *ThisDrawing* (vla-get-activedocument (vlax-get-acad-object)) ;_ Set Drawing and Space
      *Space* (vlax-get-property *ThisDrawing* (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)); Code adjustment from Lee Mac at: http://www.theswamp.org/index.php?topic=54349.msg589159#msg589159
      *Scale* (if (zerop (vla-get-activespace *ThisDrawing*))
                        (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
                            (GetVportScale)
                            1.0
                        )
                        (getvar "DimScale")
                  )
)
(setq newpoints (vlax-make-safearray vlax-vbDouble '(1 . 6)))
(If (> (strlen *path*) 5);Determine if this is a civil drawing
(progn
(setq pos (vl-string-position (ascii "\\") *Path* (+ (vl-string-position (ascii "\\") *path* 5) 1)))
(If (or (= (strcase (substr *path* (+ pos 8) 1)) "C") (= (getvar "USERS5") "CIVIL"))
    (setq *IsCivil* T)
)
)
)
    (if (zerop (vla-get-activespace *ThisDrawing*)); Set scale
        (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
            (setq *Scale* (GetVportScale))
            (setq *Scale* 1.0)
        )
        (setq *Scale* (getvar "DimScale"))
    )
    (vla-StartUndoMark *ThisDrawing*)
    ;ERROR HANDLER
    (defun *error* (msg)
        (if mlObj
            (progn
                (vla-highlight mlObj :vlax-false)
                (EraseOld mlObj)   
            )
        )
        ;; Added by Pkenewell on 3/28/2014
        (vla-EndUndoMark *ThisDrawing*)
        (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
            (princ (strcat "\n** Error: " msg " **"))
        )
        (princ)
    )
    (if (not (tblsearch "LAYER" "LEADER")) ; Check to see if layer exists
(if (= 1 (getvar "pstylemode"))
(command "._-layer" "n" "LEADER" "color" "WHITE" "" "lweight" "DEFAULT" "" "ltype" "CONTINUOUS" "" "")
(command "._-layer" "n" "LEADER" "color" "WHITE" "" "lweight" "DEFAULT" "" "ltype" "CONTINUOUS" "" "pstyle" "BLACK" "" "")
)
)
(setvar "annoautoscale" 4)
(while (not Pt1)
    (initget "Select _Select")
    (setq Pt1 (getpoint "\nPick starting point of leader [Select leader]:"))
    (cond
        ((= Pt1 "Select")
            (while (not Ent1)
                (setq Ent1 "\nSelect leader to modify or align to:")
                (if (not Ent1)
                    (princ "\r*** You must select a leader ***")
                )
                (setq Pt1 (cadr Ent1))
            )
        )
    )
)
(vlax-safearray-put-element newpoints 1 (car pt1))
(vlax-safearray-put-element newpoints 2 (cadr pt1))
(vlax-safearray-put-element newpoints 3 (caddr pt1))
(if (not Ent1)
    (setq Ent1 (nentselp "" Pt1))
)
(if Ent1
    (if (/= (cdr (assoc 0 (entget (car Ent1)))) "MULTILEADER")
        (progn
            (setq *STYLE* "BEIOB-R1"
                  Ent1 nil
            )
        )
        (setq *STYLE* "BEINOB-R1")
    )
    (setq *STYLE* "BEINOB-R1")
)
(if Ent1
    (progn ;If existing leader is selected allow for modifying or aligning to the leader
        (princ "\nAdd code for modifying/aligning leader here")
    )
    (progn ;If existing leader is not selected, draw a new leader
        (while (not Pt2)
        (if (or (not *Content*) (= *Content* ""))
                (setq *Content* (nth 0 *Types*))
            )
            (If (= *Content* "MTEXT")
                (progn
                    (if *IsCivil*
                        (setq *Arrowheads* (list "CIVIL" "BEIOB-R1" "BEINOB-R1" "BOX-R1" "LOOP-R1" "BEISPL"))
                        (setq *Arrowheads* (list "BEIOB-R1" "BEINOB-R1" "BOX-R1" "LOOP-R1" "BEISPL"))
                    )
                )
                (progn
                    (if *IsCivil*
                        (setq *Arrowheads* (list "CIVIL" "BEIOB-R1" "BEINOB-R1" "BOX-R1" "LOOP-R1"))
                        (setq *Arrowheads* (list "BEIOB-R1" "BEINOB-R1" "BOX-R1" "LOOP-R1"))
                    )
                )
            )
            (setq *Prompt* (strcat "\rSpecify second point [Content (" *Content* ")"))
            (if (or (not *Style*) (= *Style* ""))
                (setq *Style* (nth 0 *Arrowheads*))
            )
            (if (and (/= *Content* "1M08") (/= *Content* "MTEXT"))
                (progn
                    (if *Note*
                        (setq *Prompt* (strcat *Prompt* "/** Note **"))
                        (setq *Prompt* (strcat *Prompt* "/Note"))
                    )
                    )
                )
                (if (/= *Content* "MTEXT")
                (progn
                    (if *Typ*
                        (setq *Prompt* (strcat *Prompt* "/** Typical **"))
                        (setq *Prompt* (strcat *Prompt* "/Typical"))
                    )
                    )
                )
            (if (not *POC*)
                (setq *Prompt* (strcat *Prompt* "/Poc(d)"))
                (setq *Prompt* (strcat *Prompt* "/" (substr *POC* 2)))
            )
            (setq *prompt* (strcat *Prompt* "/Arrowheads (" *Style* ")]:"))
           
            (if *Align*
                (setq *Prompt* (strcat *Prompt* " " *Align*))
            )
            (if *Match*
                (setq *Prompt* (strcat *Prompt* " " *Match*))
            )
            (princ *Prompt*)
                (setq Input (grread t 4 4)
                      Data (cadr Input)
                      Code (car Input)
                )
                (cond
                    ((= Code 2);Handle all keypresses
                        (cond
                            ((= data 15); Change Orthomode when F8 is pressed
                                ;Code adjustment from Lee at http://www.theswamp.org/index.php?topic=54349.msg589159#msg589159
                                (setvar 'orthomode (- 1 (getvar 'orthomode)))
                            )
                            ((= data 21); Toggle Polarmode when F10 is pressed
                                ;Code adjustment from Lee at http://www.theswamp.org/index.php?topic=54349.msg589159#msg589159
        (setvar 'autosnap (boole 6 8 (getvar 'autosnap)))
        )
        ((or (= data 65) (= data 97)); Change arrowheads when A is pressed
            (setq POS (vl-position *Style* *Arrowheads*))
            (if (< POS (length *Arrowheads*))
                (setq *Style* (nth (+ POS 1) *Arrowheads*))
                (setq *Style* (nth 0 *Arrowheads*))
            )
        )
        ((or (= data 67) (= data 99)); Change content when C is pressed
            (setq POS (vl-position *Content* *Types*))
            (if (< POS (length *Types*))
                (setq *Content* (nth (+ POS 1) *Types*))
                (setq *Content* (nth 0 *Types*))
            )
        )
        ((or (= data 80) (= data 112)); Cycle through Point of connection/disconnection when P is pressed
            (setq POS (vl-position *POC* *POCS*))
            (if (< POS (length *POCS*))
                (setq *POC* (nth (+ POS 1) *POCS*))
                (setq *POC* (nth 0 *POCS*))
            )
        )
        ((and (or (= data 78) (= data 110)) (/= *Content* "1M08") (/= *Content* "MTEXT")); Change Note when N is pessed
            (if *Note*
                (setq *Note* nil)
                (setq *Note* T)
            )
        )
        ((and (or (= data 84) (= data 116)) (/= *Content* "MTEXT")); Change Typical when T is pessed
            (if *Typ*
                (setq *Typ* nil)
                (setq *Typ* T)
            )
        )
        )
                    )
                    ((= Code 5)
                        (setq FxPt (trans data 1 0))
                        (EraseOld *MlObj*)
                        (vlax-safearray-put-element newpoints 4 (car FxPt))
(vlax-safearray-put-element newpoints 5 (cadr FxPt))
(vlax-safearray-put-element newpoints 6 (caddr FxPt))
(setq *MlObj* (vla-AddMleader *Space* newpoints 0))
(vla-put-stylename *MlObj* *Style*);Something appears to be wrong with this line, it causes the leader to always end at 0,0
(vla-put-layer *MlObj* "LEADER")
                    )
                )
        )
    )
)
    (vla-EndUndoMark *ThisDrawing*)
)