Anushka Posted July 11, 2019 Posted July 11, 2019 (edited) I'm trying to learn AutoLISP, but it's very different. Some questions: Why does my selection set only work on the first block and does not run on the others? As I would do when selecting the block, it rewrites the desired option: Example: Demo - Select item1 - It writes to the attribute, Demo - select Item1 again and keep typing it and write another Item1 separated by ";" (Item1; Item1) other Item2 (Item1;Â Item1;Â Item2) ... Â (defun c:DEMO (/ a) (initget "Item1 Item2 item3 Item4") (setq a (cond ((getkword "\n[Item1/Item2/Item3/Item4] <Item1>:")) ("Item1"))) (cond ((= a "Item1") (SFSA "tag" (strcat a))) ((= a "Item3") (SFSA "tag" (strcat a))) ((= a "Item3") (SFSA "tag" (strcat a))) ((= a "Item4") (SFSA "tag" (strcat a))) ) (princ) ) ;****; (defun SFSA (tag value / ss i) (setq ss (ssget '((0 . "INSERT")))) (setq i 0) (repeat (sslength ss) (setq ss (ssname ss i)) (LM:vl-SetAttributeValue (vlax-ename->vla-object ss) "tag" "value") (setq i (1+ i)) ) ) ;****; ;; Set Attribute Value - Lee Mac ;; Sets the value of the first attribute with the given tag found within the block, if present. ;; blk - [vla] VLA Block Reference Object ;; tag - [str] Attribute TagString ;; val - [str] Attribute Value ;; Returns: [str] Attribute value if successful, else nil. (defun LM:vl-setattributevalue ( blk tag val ) (setq tag (strcase tag)) (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (progn (vla-put-textstring att val) val) ) ) (vlax-invoke blk 'getattributes) ) ) Â Edited July 11, 2019 by Anushka Quote
Emmanuel Delay Posted July 11, 2019 Posted July 11, 2019 >> Why does my selection set only work on the first block and does not run on the others?  Because of this: (setq ss (ssname ss i))  What you're doing, is overwriting ss. ss used to be your whole selection, but the instant you execute this line of code ss now means the first block. You should put this in another variable. for example: (setq blk (ssname ss i)) (LM:vl-SetAttributeValue (vlax-ename->vla-object blk) "tag" "value") Also ... "tag" "value", this is weird. This means the value "value" gets set. What you want is (LM:vl-SetAttributeValue (vlax-ename->vla-object blk) tag value) This means the value "ITEM1", or "ITEM2" ... gets set.  Also, DEMO doesn't do what you want it to do. But I'll stop here, for now. 1 Quote
Anushka Posted July 11, 2019 Author Posted July 11, 2019 @Emmanuel Delay  still does not work on all blocks. (defun c:Teste (/ tagname blockname) (setq tagname "REL" blockname "Teste") (setq ss (ssget '((0 . "INSERT")(66 . 1)))) (setq i 0) (repeat (sslength ss) (setq blk (ssname ss i)) (LM:vl-SetAttributeValue (vlax-ename->vla-object blk) tagname blockname) (setq i (1+ 1)) ) (princ) )  Quote
dlanorh Posted July 11, 2019 Posted July 11, 2019 (edited) 3 hours ago, Anushka said: @Emmanuel Delay  still does not work on all blocks. (defun c:Teste (/ tagname blockname) (setq tagname "REL" blockname "Teste") (setq ss (ssget '((0 . "INSERT")(66 . 1)))) (setq i 0) (repeat (sslength ss) (setq blk (ssname ss i)) (LM:vl-SetAttributeValue (vlax-ename->vla-object blk) tagname blockname) (setq i (1+ 1)) ) (princ) )   You are not incrementing "i" properly. (1+ 1) will always = 2. Use (setq i (1+ i)) or try  (defun c:Teste (/ tagname blockname) (setq tagname "REL" blockname "Teste") (setq ss (ssget '((0 . "INSERT")(66 . 1)))) (repeat (setq i (sslength ss)) (setq blk (ssname ss (setq i (1- i)))) (LM:vl-SetAttributeValue (vlax-ename->vla-object blk) tagname blockname) ) (princ) )  Edited July 11, 2019 by dlanorh 1 Quote
Lee Mac Posted July 11, 2019 Posted July 11, 2019 (edited) 3 hours ago, Anushka said: @Emmanuel Delay  still does not work on all blocks. (setq i (1+ 1))   The above expression will evaluate to 2 for every iteration, and hence the code will modify the block at selection set index 0 and 2 only.  Instead, either use the code provided by @dlanorh or change the above expression to: (setq i (1+ i))  Edited July 11, 2019 by Lee Mac 1 Quote
Anushka Posted July 11, 2019 Author Posted July 11, 2019 (edited) 43 minutes ago, dlanorh said:  Você não está incrementando "i" corretamente. (1+ 1) será sempre = 2. Use (setq i (1+ i)) ou tente     Thank you guys. You are very good, just another small help, how do I rewrite the value of choice and menten the added, Choose I1 and it will add I1, choose I2 It will contain I1 and add I2 (I1; I2) ... ;;***********************************************************************************************;; ;blockV - STR (defun Read:ATT (blockV / blk) (setq ss (ssget '((0 . "INSERT")(66 . 1)))) (repeat (setq i (sslength ss)) (setq blk (ssname ss (setq i (1- i)))) (LM:vl-SetAttributeValue (vlax-ename->vla-object blk) "REL" blockV) ) (princ) ) ;;***********************************************************************************************;; (defun c:+ATT (/ a) (initget "i1 i2 i3 i4") (while (setq a (cond ((getkword "\n[i1/i2/i3/i4] <i1>:")) ("i1"))) (cond ((= a "i1") (Read:ATT (strcat a))) ((= a "i2") (Read:ATT (strcat a))) ((= a "i3") (Read:ATT (strcat a))) ((= a "i4") (Read:ATT (strcat a))) ) ) (princ) )  Edited July 11, 2019 by Anushka Quote
dlanorh Posted July 11, 2019 Posted July 11, 2019 (edited) I'm not entirely sure what you are trying to achieve, but try this  ;blockV - STR (defun Read:ATT (blockV / blk) (setq ss (ssget '((0 . "INSERT")(66 . 1)))) (repeat (setq i (sslength ss)) (setq blk (ssname ss (setq i (1- i)))) (LM:vl-SetAttributeValue (vlax-ename->vla-object blk) "REL" blockV) ) (princ) ) (defun c:+ATT (/ lst idx a) (setq str "i1" lst (list "i2" "i3" "i4") idx 0 ) (initget "i1 i2 i3 i4") (setq a (cond ((getkword "\n[i1/i2/i3/i4] <i1>:")) ("i1"))) (cond ( (= a "i2") (setq idx 1)) ( (= a "i3") (setq idx 2)) ( (= a "i4") (setq idx 3)) ) (setq a "i1") (if (not (zerop idx)) (repeat idx (setq a (strcat a ";" (car lst)) lst (cdr lst) ) ) ) ;(Read:ATT a) (princ) )  Edited July 11, 2019 by dlanorh 1 Quote
Anushka Posted July 12, 2019 Author Posted July 12, 2019 @dlanorh For some reason, it's not working. When calling the command again, keep the value of the previous tag and Add a new option with the desired option. (c: + ATT >> Select an option (Example I1) >> Select Blocks >> And program letter I1) Â When powering on again (c: + ATT >>) Select an option (Example I2) >> Display the Blocks >> And the program type I1; I2 Â Again (c: + ATT >> Select an option (Example I1) >> Select Blocks >> And program write I1; I2; I1 ... I do not know if I'm being very clear, let alone how to achieve this Quote
Roy_043 Posted July 12, 2019 Posted July 12, 2019 I think the OP wants to append to the existing attribute value. 1 Quote
Anushka Posted July 12, 2019 Author Posted July 12, 2019 18 minutes ago, Roy_043 said: I think the OP wants to append to the existing attribute value. Â exactly @Roy_043 I'm trying to use the Get Attribute Values lee-mac function to store and then write the new one, but I do not know if it is the best method and I do not understand well. Quote
dlanorh Posted July 12, 2019 Posted July 12, 2019 (edited) 1 hour ago, Roy_043 said: I think the OP wants to append to the existing attribute value. Â Thanks @Roy_043 I hadn't thought about that, but it explains a the read function with a set properties. Edited July 12, 2019 by dlanorh Quote
dlanorh Posted July 12, 2019 Posted July 12, 2019 1 hour ago, Anushka said: Â exactly @Roy_043 I'm trying to use the Get Attribute Values lee-mac function to store and then write the new one, but I do not know if it is the best method and I do not understand well. Â I will provide an answer when I can successfully log in next time. The Forum is having problem, but you must use both of Lee-Mac's functions, get and set. 1 Quote
dlanorh Posted July 12, 2019 Posted July 12, 2019 OK. Try these  (vl-load-com) (defun LM:vl-getattributevalue ( blk tag ) (setq tag (strcase tag)) (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes)) ) (defun LM:vl-setattributevalue ( blk tag val ) (setq tag (strcase tag)) (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (progn (vla-put-textstring att val) val) ) ) (vlax-invoke blk 'getattributes) ) ) (defun c:+ATT (/ blk c_val ss i a) (setq ss (ssget ":L" '((0 . "INSERT")(66 . 1)))) (repeat (setq i (sslength ss)) (setq blk (vlax-ename->vla-object (ssname ss (setq i (1- i)))) c_val (LM:vl-getattributevalue blk "REL") );end_setq (initget "i1 i2 i3 i4") (setq a (cond ((getkword "\n[i1/i2/i3/i4] <i1>:")) ("i1"))) (cond ( (= a "i1") (setq a (strcat c_val ";" a))) ( (= a "i2") (setq a (strcat c_val ";" a))) ( (= a "i3") (setq a (strcat c_val ";" a))) ( (= a "i4") (setq a (strcat c_val ";" a))) );end_cond (LM:vl-setattributevalue blk "REL" a) );end_repeat ;(Read:ATT a) (princ) );end_defun (defun c:+ATT2 (/ blk c_val ss i a) (initget "i1 i2 i3 i4") (setq a (cond ((getkword "\n[i1/i2/i3/i4] <i1>:")) ("i1")) ss (ssget ":L" '((0 . "INSERT")(66 . 1))) );end_setq (repeat (setq i (sslength ss)) (setq blk (vlax-ename->vla-object (ssname ss (setq i (1- i)))) c_val (LM:vl-getattributevalue blk "REL") );end_setq (LM:vl-setattributevalue blk "REL" (strcat c_val ";" a)) );end_repeat ;(Read:ATT a) (princ) );end_defun I have removed your read:att function and moved the selection set selection into the main program  There are two main programs c:+ATT and c:+ATT2. Both require a selection set of blocks on unlocked layers, and there are NO checks that any of the block has the required attribute.  c:+ATT This creates the selection set and loops through each block asking for a new value for the required attribute.  c:+ATT2 This asks for the value then creates the selection set and sets the required attribute in every block to the same value   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.