dannywi77iams Posted February 10, 2012 Posted February 10, 2012 Hi All I'm new to CADTutor, I've been using AutoCAD now getting on for 4 year but have found out about LISP over the past year and have a really small knowlegde of it. I'm trying to write a routine on finding out who is logon to the system (here at my place of work when using "LOGINNAME" it displays "Daniel.Williams"). So i know how to get this in to a string ( (setq usernm (getvar "Loginname")) ) Once i have this name (this is where i get stuck) I want to take the first letter of the first name remove all the other letters up until the "full stop" then use the rest of the letters to end up with (D.Williams) and then place this into the a string to put in to my title block. I have been all over the internet and here in the forums but can't find any examples to help me unstand how to do this. If anyone can help me with the code this would be a great help, as i say i'm still learning LISP and want to learn more about language. this would be apperciated. Quote
fixo Posted February 10, 2012 Posted February 10, 2012 (edited) Welcome on board ;; say, after you got it like: (setq ln (getvar "loginname")) ;; and it returned "Daniel.Williams" ;;search for "." within this string: (setq newString (vl-string-subst (substr ln 1 1) (substr ln 1 (vl-string-position (ascii ".") ln) ) ln )) (alert newString) Edited February 10, 2012 by fixo Quote
MSasu Posted February 10, 2012 Posted February 10, 2012 One solution is to split the string by the dot and recompose it by the new rule: ;;; STR2LST ;;; Transforms a string with separator into a list of strings ;;; Author: Gile ;;; Arguments ;;; str = the string ;;; sep = the separator pattern (defun str2lst (str sep / pos) (if (setq pos (vl-string-search sep str)) (cons (substr str 1 pos) (str2lst (substr str (+ (strlen sep) pos 1)) sep) ) (list str) ) ) (setq usernm (getvar "Loginname") usernm (str2lst usernm ".") usernm (strcat (strcase (substr (car usernm) 1 1)) "." (strcase (substr (cadr usernm) 1 1)) (substr (cadr usernm) 2))) Regards, Mircea Quote
dannywi77iams Posted February 10, 2012 Author Posted February 10, 2012 Hi guys Thanks for replying I've tried both of the codes but both are replying with "; error: bad argument type: stringp nil" to be honest i dont understand the "vl" code i'm just getting my head around basic code. could you explain what's happening in the code for me to uderstand whats going on. sorry if i'm coming across stupid, but that what the forums are here for, to help out stupid people like me lol. Quote
pBe Posted February 10, 2012 Posted February 10, 2012 (edited) (setq usernm (getvar "Loginname")) "Daniel.Williams" (strcat (substr usernm 1 1) (last (fnsplitl usernm))) Edited February 10, 2012 by pBe Quote
MSasu Posted February 10, 2012 Posted February 10, 2012 To be able to access VisualLisp extension's function, please add the line below to your codes: (vl-load-com) Regards, Mircea PS. I have it in start-up, so take it for granted and always forget to add it to examples... Quote
dannywi77iams Posted February 10, 2012 Author Posted February 10, 2012 (edited) (defun c:l2t () (setq usernm (getvar "Loginname"));"Daniel.Williams" (strcat (substr usernm 1 1) (last (fnsplitl usernm))) );end This is what I'm after after runing the code i end up with (D.Williams) But when I list (!usernm) it comes up with (Daniel.Williams) how do I place the finished result in to a string. Thanks pBe Edited February 10, 2012 by dannywi77iams Quote
dannywi77iams Posted February 10, 2012 Author Posted February 10, 2012 (edited) I think i have it (defun c:l2t () (setq usernm (getvar "Loginname"));"Daniel.Williams" (setq tilnm (strcat (substr usernm 1 1) (last (fnsplitl usernm)))) );end Edited February 10, 2012 by dannywi77iams Quote
dannywi77iams Posted February 10, 2012 Author Posted February 10, 2012 The command "fnsplitl" is not listed in the lisp help, where can I find information on commands that are not listed. Sorry and with the "strcat" command you have used numbers the lisp help doesn't explain using numbers. Could you explain why and what they do or point me in the direction for this information. Thank again Quote
fixo Posted February 10, 2012 Posted February 10, 2012 Try also edited code of mine above, I just casually changed a variable name Quote
MSasu Posted February 10, 2012 Posted February 10, 2012 Sorry and with the "strcat" command you have used numbers the lisp help doesn't explain using numbers. Could you explain why and what they do or point me in the direction for this information. The STRCAT function will join together two or more strings - to join numbers please check the RTOS and ITOA functions. Regards, Mircea Quote
MSasu Posted February 10, 2012 Posted February 10, 2012 The command "fnsplitl" is not listed in the lisp help, where can I find information on commands that are not listed. There are some undocumented features of AutoCAD; the FNSPLITL may translate as "File Name SPLIT in List": (fnsplitl "C:\\Folder\\File.Ext") ;this will return ("C:\\Folder\\" "File" ".Ext") Regards, Mircea Quote
pBe Posted February 10, 2012 Posted February 10, 2012 The numbers are for substr not for strcat Danny As for fnsplitl from jtbworld.com Quote
dannywi77iams Posted February 10, 2012 Author Posted February 10, 2012 (defun c:l2t () (setq usernm (getvar "Loginname"));"Daniel.Williams" (setq tilnm (strcat (substr usernm 1 1) (last (fnsplitl usernm)))) (setq tilnm (strcase tilnm)) );end This is to change all the character to uppercase as all of our drawings are required to have uppercase text in the title block. Quote
Lee Mac Posted February 10, 2012 Posted February 10, 2012 One more method for you: (setq user (getvar 'LOGINNAME)) (if (setq pos (vl-string-position 46 user)) (strcase (strcat (substr user 1 1) (substr user (1+ pos)))) (strcase user) ) Since: (last (fnsplitl "Lee Mac")) = "" So (setq usernm "Lee Mac") (strcat (substr usernm 1 1) (last (fnsplitl usernm))) = "L" Nice thinking outside the box though pBe Quote
pBe Posted February 10, 2012 Posted February 10, 2012 Nice thinking outside the box though pBe Thanks Lee, pretty sure any of you guys would've have thought of it as well. Got lucky is all. Cheers Quote
Lee Mac Posted February 10, 2012 Posted February 10, 2012 A couple more: (setq user (strcase (getvar 'LOGINNAME))) (if (wcmatch user "*`.*") (setq user (vl-string->list user) user (vl-list->string (cons (car user) (member 46 user))) ) user ) (setq user (strcase (getvar 'LOGINNAME))) (if (wcmatch user "*`.*") (strcat (chr (ascii user)) (while (/= 46 (ascii user)) (setq user (substr user 2)) ) ) user ) Quote
pBe Posted February 10, 2012 Posted February 10, 2012 A couple more: Since you're in turbo mode.. write one for (vlax-get-or-create-object "VBScript.RegExp") for the fun of it Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.