jr.roberto.santos Posted December 28, 2021 Posted December 28, 2021 hello everyone, I'd like to share an idea based on the Leemac script (Escape Wildcards). is working fine, but I would like help for code optimization. 1: Searches the text according to the wildcard setting 2: selects all texts found 3: If the wildcard is found, it creates the layer and changes the blocks to its layers. 4: Sets the color of the layer 5: converts to block (HDV1_CONVERT) As you can see in the attachment, when I run the script for the first time, the alert appears. Is there any way to accept the alert automatically? Regards ::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;;Autocad Electrical String Match and convert ;;search wildcard text and convert to component (block: HDV1_CONVERT) ;;http://www.lee-mac.com/escapewildcards.html Reference (defun c:CALL_TAG1 ();; START (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "TAG_1\r") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "CALL_TAG2\r") ) (defun c:CALL_TAG2 () (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "TAG_2\r") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "CALL_TAG3\r") ) (defun c:CALL_TAG3 () (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "TAG_3\r") ;;(vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "CALL_TAG4\r") ;;... ) (defun c:TAG_1 (/ ss) ;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ;; Set of variables (setq 1_sequence "@@###`.@@@`.###A") (setq 1_layer "TAG1_LAYER_A") (setq 1_color "0,255,0") (setq 2_sequence "@@###`.@@@`.###B") (setq 2_layer "TAG2_LAYER_B") (setq 2_color "0,0,255") (setq 3_sequence "@@###`.@@@`.###C") (setq 3_layer "TAG3_LAYER_C") (setq 3_color "0,255,255") ;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ (if (setq ss (car (cdr (ssgetfirst)))) (command "._zoom" "1" ss "") (progn (C:SEARCH_BY_WILDCARD1) (C:SendToNewLayer1) (princ) );; progn );; if ) ;#################################################################################### ;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ (defun c:TAG_2 (/ ss) (if (setq ss (car (cdr (ssgetfirst)))) (command "._zoom" "1" ss "") (progn (C:SEARCH_BY_WILDCARD2) (C:SendToNewLayer2) (princ) );; progn );; if ) ;#################################################################################### ;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ (defun c:TAG_3 (/ ss) (if (setq ss (car (cdr (ssgetfirst)))) (command "._zoom" "1" ss "") (progn (C:SEARCH_BY_WILDCARD3) (C:SendToNewLayer3) (princ) );; progn );; if ) ;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ;;http://www.lee-mac.com/escapewildcards.html Reference (vl-load-com) ;;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ (defun c:SEARCH_BY_WILDCARD1 ( / str ) (if (/= "" (setq str 1_sequence)) (sssetfirst nil (ssget "_X" (list '(0 . "TEXT") (cons 1 (LM:escapewildcards str)) ) ) ) ) (princ) ) ;;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;; Escapes wildcard special characters in a supplied string (defun LM:escapewildcards ( str ) (if (wcmatch str str) ; (if (wcmatch str str) ; (strcat "`" (substr str 1 1) (LM:escapewildcards (substr str 2))) (strcat (substr str 1 1) (LM:escapewildcards (substr str 2))) ) str ) ) (defun c:SendToNewLayer1 () (if (setq ss (car (cdr (ssgetfirst)))) (tolayer1 (ssget "_P") ;;selection exclude lock layer 1_layer ) (progn (princ) );; progn );; if ) (defun tolayer1 ( ss lay / i e ) ;;; ss - pickset ;;; lay -layer name (repeat (setq i (sslength ss)) (entmod (subst (cons 8 lay) (assoc 8 (entget (setq e (ssname ss (setq i (1- i)))))) (entget e) ) ) ) (command "-LAYER" "_N" 1_layer"") (command "-LAYER" "color" "Truecolor" 1_color 1_layer"") (command "-LAYER" "set" 1_layer"") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "SEARCH_BY_WILDCARD1\r") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "AETAGSCH _P\r\r\r_qsave\r") ) ;;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ;;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ;;http://www.lee-mac.com/escapewildcards.html Reference (defun c:SEARCH_BY_WILDCARD2 ( / str ) (if (/= "" (setq str 2_sequence)) (sssetfirst nil (ssget "_X" (list '(0 . "TEXT") (cons 1 (LM:escapewildcards str)) ) ) ) ) (princ) ) ;;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;; Escapes wildcard special characters in a supplied string (defun LM:escapewildcards ( str ) (if (wcmatch str str) ; (if (wcmatch str str) ; (strcat "`" (substr str 1 1) (LM:escapewildcards (substr str 2))) (strcat (substr str 1 1) (LM:escapewildcards (substr str 2))) ) str ) ) (defun c:SendToNewLayer2 () (if (setq ss (car (cdr (ssgetfirst)))) (tolayer2 (ssget "_P") ;;selection exclude lock layer 2_layer ) (progn (princ) );; progn );; if ) (defun tolayer2 ( ss lay / i e ) ;;; ss - pickset ;;; lay -layer name (repeat (setq i (sslength ss)) (entmod (subst (cons 8 lay) (assoc 8 (entget (setq e (ssname ss (setq i (1- i)))))) (entget e) ) ) ) (command "-LAYER" "_N" 2_layer"") (command "-LAYER" "color" "Truecolor" 2_color 2_layer"") (command "-LAYER" "set" 2_layer"") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "SEARCH_BY_WILDCARD2\r") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "AETAGSCH _P\r\r\r_qsave\r") ) ;;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ;;;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ;;http://www.lee-mac.com/escapewildcards.html Reference (defun c:SEARCH_BY_WILDCARD3 ( / str ) (if (/= "" (setq str 3_sequence)) (sssetfirst nil (ssget "_X" (list '(0 . "TEXT") (cons 1 (LM:escapewildcards str)) ) ) ) ) (princ) ) ;;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;; Escapes wildcard special characters in a supplied string (defun LM:escapewildcards ( str ) (if (wcmatch str str) ; (if (wcmatch str str) ; (strcat "`" (substr str 1 1) (LM:escapewildcards (substr str 2))) (strcat (substr str 1 1) (LM:escapewildcards (substr str 2))) ) str ) ) (defun c:SendToNewLayer3 () (if (setq ss (car (cdr (ssgetfirst)))) (tolayer3 (ssget "_P") ;;selection exclude lock layer 3_layer ) (progn (princ) );; progn );; if ) (defun tolayer3 ( ss lay / i e ) ;;; ss - pickset ;;; lay -layer name (repeat (setq i (sslength ss)) (entmod (subst (cons 8 lay) (assoc 8 (entget (setq e (ssname ss (setq i (1- i)))))) (entget e) ) ) ) (command "-LAYER" "_N" 3_layer"") (command "-LAYER" "color" "Truecolor" 3_color 3_layer"") (command "-LAYER" "set" 3_layer"") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "SEARCH_BY_WILDCARD3\r") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "AETAGSCH _P\r\r\r_qsave\r") ) SEARCH_AND_CONVERT.zip Quote
mhupp Posted December 29, 2021 Posted December 29, 2021 Please read code posting guidelines pinned at the top of forum. Why not make 3 blocks Tag1,Tag2, & Tag3 with all the text on the correct layers already? Quote
Steven P Posted December 29, 2021 Posted December 29, 2021 What mhupp says about using the code tags, it makes it a little easier to read (code tag - use the '<>' button). Another top tip is to use indents - makes it all easier to read and see which closing brackets align with which opening brackets. Something like this (just a short extract from your code) ;;Autocad Electrical String Match and convert ;;search wildcard text and convert to component (block: HDV1_CONVERT) ;;http://www.lee-mac.com/escapewildcards.html Reference ;####################################################################################; (defun c:CALL_TAG1 ();; START (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "TAG_1\r") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "CALL_TAG2\r") ) ;####################################################################################; (defun c:CALL_TAG2 () (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "TAG_2\r") (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "CALL_TAG3\r") ) ;####################################################################################; (defun c:CALL_TAG3 () (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "TAG_3\r") ;; (vla-sendcommand (vla-get-activedocument(vlax-get-acad-object)) "CALL_TAG4\r") ) ;####################################################################################; (defun c:TAG_1 (/ ss) ;; Set of variables (setq 1_sequence "@@###`.@@@`.###A") (setq 1_layer "TAG1_LAYER_A") (setq 1_color "0,255,0") (setq 2_sequence "@@###`.@@@`.###B") (setq 2_layer "TAG2_LAYER_B") (setq 2_color "0,0,255") (setq 3_sequence "@@###`.@@@`.###C") (setq 3_layer "TAG3_LAYER_C") (setq 3_color "0,255,255") (if (setq ss (car (cdr (ssgetfirst)))) (command "._zoom" "1" ss "") (progn (C:SEARCH_BY_WILDCARD1) (C:SendToNewLayer1) (princ) );; progn );; if ) ;####################################################################################; (defun c:TAG_2 (/ ss) (if (setq ss (car (cdr (ssgetfirst)))) (command "._zoom" "1" ss "") (progn (C:SEARCH_BY_WILDCARD2) (C:SendToNewLayer2) (princ) );; progn );; if ) ;####################################################################################; (defun c:TAG_3 (/ ss) (if (setq ss (car (cdr (ssgetfirst)))) (command "._zoom" "1" ss "") (progn (C:SEARCH_BY_WILDCARD3) (C:SendToNewLayer3) (princ) );; progn );; if ) ;####################################################################################; "Bad argument type: numberp: nil' usually comes about because the code is expecting a number and often has been given a string to use (it doesn't like adding "e + t" but can do "3 + 7' OK. Not quite as simple as that, on the screen you might see the contents of a variable as a number, but those contents might have been defined as a string... and LISP cannot do arithmetic with strings. It can also return the same error if it is trying to process an empty variable, or a value of nil You have a couple of functions that can run directly from the command line, which one do you use to run your functions and which cause the error? You have quite a bit of code there to look through and my CAD is off till next year, but to get you going how much debugging have you done so far? can you identify sort of the area in the code where the error occurs? So.... assuming that a lot of people are having a Christmas and New Year break if you can narrow things down then it will be quicker for them to help. I would normally comment out all the code (use a ; - or a multiple of them so you can spot quickly which you have commented out temporarily), and then put the lines back in, few at a time... and this is where the indents help so for example, you can put back in an 'if' command and spot its closing bracket to put that back again. So then run the code, out lines back in and run again until you find the line giving an error Use princ to return to the command line contents of variables to see if they are what you expect That is how I would go... there might be better ways to go about it Quote
Steven P Posted December 29, 2021 Posted December 29, 2021 So looking at this a little, "AETAGSCH" is an AutoCAD electrical command... so I can't help much here, I don't have that. 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.