Jump to content

Define the Attribute and, if necessary, add it again with ";"


Recommended Posts

Posted (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 by Anushka
Posted

>> 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.

  • Like 1
Posted

@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)
 )

 

Posted (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 by dlanorh
  • Like 1
Posted (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 by Lee Mac
  • Like 1
Posted (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 by Anushka
Posted (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 by dlanorh
  • Like 1
Posted

@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

Posted

I think the OP wants to append to the existing attribute value.

  • Like 1
Posted
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.

Posted (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. :beer:

Edited by dlanorh
Posted
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.

  • Like 1
Posted

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

 

 

  • Thanks 1

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...