Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Bad function error on custom LISP.

BugFinder
Advocate

Bad function error on custom LISP.

BugFinder
Advocate
Advocate

I've made an attempt at writing a LISP function which will display initials on a drawing.

 

So far I have this which when I run, gives me an error:

(defun C:initials ( / init initial)

  (setq init (Sparser (strcase(getvar "loginname")) "."))
  (if (init)
    (setq initial (strcat (substr (nth 0 init) 1 1) (substr (nth 1 init) 1 1)))
    (setq initial "UNKNOWN")
  )
  initial
)

;run code
(C:initials)

; error: bad function: ("Bob" "Deblaise")
_$ 

The idea is that the login name is put in to a variable and tested to ensure it is not blank.  If there is a login name, then it the name is split and the first letters of the first and second elements are concatenated and returned from the function.

 

Does anyone have any ideas as to what is causing the bad function error?

 

Thanks

0 Likes
Reply
Accepted solutions (1)
434 Views
5 Replies
Replies (5)

Travis.Biddle
Advocate
Advocate

Here is what I do to set initials, but I do not have many users (5-6) so this works.  Long term with users changing this may not work

 

(setq login (getvar "loginname"))
(if
(= login "travbidd01")
(setq login "TJB")
(if (= login "samatayl01")
(setq login "SJT")
(if (= login "isissard01")
(setq login "IS")
(if (= login "angela.ospina")
(setq login "AO")
(set login " ")
)
)
)
)

0 Likes

paullimapa
Mentor
Mentor

Question:

What is Sparser?

Since init is a symbol, don't surround it with parenthesis: (init)

Instead do this:  

(if init

or:

(if (setq init (Sparser (strcase(getvar "loginname")) "."))

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

ВeekeeCZ
Consultant
Consultant
Accepted solution

This should be enough.

 

 

(defun c:Initials ( / n)
  (if (and (setq n (getvar 'loginname))
	   (setq n (sparser (strcase n) ".")))
    (strcat (substr (car n) 1 1) (substr (cadr n) 1 1))
    "UNKNOWN"))


;run code
(C:initials)
0 Likes

BugFinder
Advocate
Advocate

@Travis.BiddleThis is what I had originally.  When it came to rolling out across other sites and the maintenance of new users, this all became a pain.  I'm not as fluent in LISP as I am on other languages hence the 'hash' at what I did manage to achieve 😁

 

@paullimapaSorry, just realised I hadn't included the sparser code.  It's a function which was posted here (cannot find the link at the moment) and is used to split a string in to a list with a delimiter.

 

@ВeekeeCZ  Many thanks 😁.  Looks like a much simpler version of what I cobbled together.  I'll try Monday when I get back to the office.

 

0 Likes

paullimapa
Mentor
Mentor

the following revised code includes the string to list using delimitator function that I use:

; initials function gets loginname, if it has a period separates into 2 list items and returns the first letters of the first and second elements 
(defun C:initials ( / aec_str2lst init initial)
;;;--- aec_str2lst function
; splits given string with separator 
; returns list with each item
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-do-i-split-a-file-path-into-bits-based-on-back-slash/td-p/2396237
; Usage:
; (aec_str2lst (findfile "acad.exe") "\\")
; Returns:
; ("C:" "Program Files" "Autodesk" "AutoCAD 2020" "acad.exe")
; Usage:
; (aec_str2lst "0 1 2 3" " ")
; Returns:
; ("0" "1" "2" "3")
(defun aec_str2lst (str sep / pos)
  (if (setq pos (vl-string-search sep str))
   (cons (substr str 1 pos)
    (aec_str2lst (substr str (+ (strlen sep) pos 1)) sep)
   )
   (if(not(zerop(strlen str)))(list str))
  )
) ; defun
  (setq init (aec_str2lst (strcase(getvar "loginname")) ".")) ; get loginname string and return as list with period as deliminator
  (if init
    (if(> (length init) 1) ; chk if list has more than 1 item
      (setq initial (strcat (substr (nth 0 init) 1 1) (substr (nth 1 init) 1 1))) ; if more than 1
      (setq initial (strcat (substr (nth 0 init) 1 1) (substr (nth 0 init) 2 1))) ; if only one
	)
    (setq initial "UNKNOWN") ; if no loginname 
  )
  initial
) ; defun initials

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes