StevJ Posted October 16, 2022 Posted October 16, 2022 Just spent most of today wrestling with and trying to find examples of: Extracting single character From Drawing Name. I needed the 7th character of the Autocad drawing name to use as a conditional. Per standards, it will always be either the letter A or the number 0 (zero). IF 7th character is A, run program 1. ELSE run program 2. As simple as that. But I couldn't find examples of or figure how to extract a drawing name character. Then I thought, maybe, I could put spaces between the drawing name characters. That would make it a list and I could use: (nth 6 '(0 1 2 3 4 5 A 7 )) after removing the ".dwg" with: (setq dname (getvar "DWGNAME")); get drawing name (setq strleg (strlen dname)) ; get the length of it (setq strleg1 (- strleg 4)) ; hack off the last 4 characters (setq drawname (substr dname 1 strleg1)) ; what's left? But I couldn't find instructions or an example of making that happen, either. Then I remembered an old program to display the outline of named views. Just coincidentally, our one Model Space named view is the drawing name without the .DWG. The program finds the named view using: (setq vname (strcase (dxf 2 tbdata))) ;extract view name (if (wcmatch vname "??????A*"); ? = any single character (OutlineTheBorder)) so after some thievery and a quick and easy modification to: (if (wcmatch (getvar "DWGNAME") "??????A*") ;IF this (PROGRAM-1) ; THEN do this (PROGRAM-2) ; ELSE do this ) (There won't always be a named view, but there will always be DWGNAME.) Works like a charm. So in the end: 1. I didn't need to extract from the drawing name, although I really wanted to use that method. 2. I didn't need to convert the drawing name to a list, if that's even possible, to use the nth thingy. That would have been a neat, but convoluted and unconventional way to do it. 3. After trying too hard most of the day, the answer turned out to be simple and something I already had examples of. All I needed was wcmatch. Just wanted to share, and hope this is helpful info. Thanks for listening, Steve 2 Quote
mhupp Posted October 16, 2022 Posted October 16, 2022 (edited) The struggle was real for me starting out. what helped me was being a lurker on these forums for a long time. Come here daily and reading over most posts. Then I would test out code posted to see what its doing. After a while things just started to click. The stuff I liked (even if it was a little snippet) I would put into a text file with a description. can't tell you how many times I needing it months later. like this Strips the extension off the filename (setq fn (vl-filename-base (getvar 'dwgname))) Depending on what program-2 does their is a third option for the file name and might want to use a conditional statement. https://www.afralisp.net/autolisp/tutorials/cond-vs-if.php (cond ((wcmatch (getvar "DWGNAME") "??????A*") ;IF this A ;((eq (vl-string-position (ascii "A") (getvar "DWGNAME") 6) 6) ;is the 7th position char A in this string returns 6 if true (PROGRAM-1) ; THEN do this ) ((wcmatch (getvar "DWGNAME") "??????#*") ;IF this # (PROGRAM-2) ; THEN do this ) (t ;if nither option 1 or option 2 are true do this (alert "Drawing name doesn't match Standards") ) ) --edit need (vl-load-com) at the start of your lisp for visual lisp code to work Edited October 16, 2022 by mhupp 1 Quote
Isaac26a Posted October 16, 2022 Posted October 16, 2022 Yes sometimes we over think things and do things that are not necessary, I'm glad you found a solution after all, but maybe this could be easier (if (= (substr (getvar 'dwgname) 7 1) "a") ;;; Then ... Maybe you could have asked there are so many great people here that can help you and with so many post we keep learning a lot. 1 Quote
Jonathan Handojo Posted October 16, 2022 Posted October 16, 2022 1 hour ago, mhupp said: The stuff I liked (even if it was a little snippet) I would put into a text file with a description. can't tell you how many times I needing it months later. like this If you're talking snippets, I recommend using Visual Studio Code. There, you'll just type a few letters of your choice, and everything else appears. You can make an unlimited number of them. It may take a bit of time learning it in the start, but if you are someone who codes a lot, this will make your job easier (because there's a lot more features in there, snippets aside). Quote
mhupp Posted October 16, 2022 Posted October 16, 2022 While Visual Studio Code is nice i find it a bit of an overkill when trying to program a small lisp. Don't get me wrong I use it alot but its mostly to check/clean up code I have done through the BricsCAD encoder. add-on I use https://marketplace.visualstudio.com/items?itemName=Autodesk.autolispext https://marketplace.visualstudio.com/items?itemName=mattn.Lisp https://marketplace.visualstudio.com/items?itemName=spences10.VBA https://marketplace.visualstudio.com/items?itemName=zhuangtongfa.Material-theme Quote
StevJ Posted October 16, 2022 Author Posted October 16, 2022 (edited) Thank you all for your responses and helpful programming suggestions. @mhupp Yes, on further testing after my original post, I did see a need for a 3rd test option, but chose a slightly different way to make the tests, since I was already using IF: (if (wcmatch (getvar "DWGNAME") "??????A*") ;IF A (Program-1) ; THEN do this (progn ; ELSE (if (wcmatch (getvar "DWGNAME") "??????0*");IF 0 (Program-2) ; THEN do this ;;ELSE drawing not to STANDARDs (MsgBox " - Not To STANDARDS - " 512 " Not To STANDARDS" 1) ) ) ) Instead of an ALERT, which requires user action, I chose to use a MSGBOX routine that puts up a temporary notification to the user. No user interruption other than a message box that closes itself and goes away in a second or two. I like the COND version you posted, though. It's easier to read and follow the logic. I'll likely switch to that. @Isaac26a (if (= (substr (getvar 'dwgname) 7 1) "a") That was the kind of test expression I had been searching for initially. Thank you for that. I have lurked here and other Autolisp forums for years, collecting and using bits and pieces of some very creative programming as the need arose. I have asked for help so many times, and been rewarded for the asking by smart folks kind enough to tell me how to fix my code or who decided to just plain rewrite it for me. I thought this time I needed to try harder before giving up. To learn. So I found a method that worked. Maybe not the best or most elegant solution, but I'm delighted to have figured it out and thought I'd share the experience. Thanks again for your suggestions, Steve Edited October 16, 2022 by StevJ spellin errers Quote
BIGAL Posted October 16, 2022 Posted October 16, 2022 An example of message box routine for any one looking for it I use it for a ssget prompt. (defun MsgBox (title options message time / WshShell) (setq WshShell (vlax-create-object "WScript.Shell")) (vlax-invoke WshShell 'Run (strcat "mshta.exe vbscript:close(CreateObject(\"WScript.Shell\").Popup(\"" message "\"," (itoa time) ",\"" title "\"," (itoa options)"))" ) ) (vlax-release-object WshShell) ) (msgbox "Pts on wall" 0 "Select COGO points" 1) 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.