Add attribute if not exist in a particular block and make it invisible

Add attribute if not exist in a particular block and make it invisible

j.balasubramaniyam
Participant Participant
1,294 Views
18 Replies
Message 1 of 19

Add attribute if not exist in a particular block and make it invisible

j.balasubramaniyam
Participant
Participant
Hi, I wanted to add an attribute PRICE in all my blocks in a drawing but also wanted to check if that attribute already exists so that it can skip that task for that particular block. I tried to modify this code found on cadtutor forum but not working. Also if someone can make that added attribute invisible it would be better help for me. 
defun c:add$ ( / ss i blk blks def AttObj) (and (setq ss (ssget '((0 . "INSERT")))) (setq i (sslength ss)) (while (> i 0) (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i))))))) (if (not (vl-position blk blks))(setq blks (cons blk blks))) ) ) (foreach blk blks (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk)) (if (/= "" (attributeTag-p (car (entsel)) "SHIPLOOSE"))
(prince)
)
(setq AttObj (vla-addattribute def 8 acattributemodelockposition "Enter Item #" (vlax-3D-point 72 84) "PRICE\U+0020" "100" ) ) (vlax-put AttObj 'Alignment acAlignmentmiddle) ;; 4 (command "_.attsync" "_N" blk) ) (princ) ) (vl-load-com) (princ)

 

0 Likes
Accepted solutions (2)
1,295 Views
18 Replies
Replies (18)
Message 2 of 19

pbejse
Mentor
Mentor

@j.balasubramaniyam wrote:
...
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks 
(vla-get-activedocument (vlax-get-acad-object))) blk)) (if (/= "" (attributeTag-p (car (entsel)) "SHIPLOOSE"))
(prince); <----
)
...

This prince ? 

pbejse_0-1628153339424.png

 

Message 3 of 19

j.balasubramaniyam
Participant
Participant

Apologies. that is typo error

0 Likes
Message 4 of 19

pbejse
Mentor
Mentor

@j.balasubramaniyam wrote:

Apologies. that is typo error


 

I know, I'm just messing with you.  😊

(defun c:add$ ( / ss i blk blks def AttObj prince)
  (and
       (setq ss (ssget '((0 . "INSERT"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (GetPropertyValue  (ssname ss (setq i (1- i))) "BlockTableRecord/Name" ))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
	 blks
       )
       (foreach blk blks
	(and
	  (Vlax-for obj (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
		 (setq prince 
			  (and
			    (eq (vla-get-ObjectName obj) "AcDbAttributeDefinition")
			    (eq (vla-get-tagstring obj) "PRICE ")
			    )
		       )
	     (null prince)
	  )
	(setq AttObj (vla-addattribute def  8 acattributemodelockposition
	      "Enter Item #" (vlax-3D-point 72 84) "PRICE\U+0020"
	      "100"
	    )
	 )
        (null (vlax-put AttObj 'Alignment acAlignmentmiddle))
        (command "_.attsync" "_N" blk)
	  )
	 )
       )
(princ)
)

HTH

 

0 Likes
Message 5 of 19

j.balasubramaniyam
Participant
Participant

Yes I am prince only waiting for Princess. Regarding the LISP, it does not ask to select objects in the original version but the LISP you modified asking to select the Objects. Can you please look at it? Also please make that particular attribute invisible. I am having difficult time to find a solution.

0 Likes
Message 6 of 19

pbejse
Mentor
Mentor

@j.balasubramaniyam wrote:

Regarding the LISP, it does not ask to select objects in the original version but the LISP you modified asking to select the Objects.

.... Also please make that particular attribute invisible. 


 

Wrong! your posts shows this as the original

 

(setq ss (ssget '((0 . "INSERT"))))

 

You can however change it to

 

(setq ss (ssget  "X" '((0 . "INSERT"))))

 

And this 

 

(setq AttObj (vla-addattribute def  8  acattributemodelockposition
	      "Enter Item #" (vlax-3D-point 72 84) "PRICE\U+0020"
	      "100"
	    )
	 )

 

to this for "invisible" 

(setq AttObj (vla-addattribute def  8  acAttributeModeInvisible
	      "Enter Item #" (vlax-3D-point 72 84) "PRICE\U+0020"
	      "100"
	    )
	 )

 

0 Likes
Message 7 of 19

j.balasubramaniyam
Participant
Participant

Hi, your code is working well interms of Making the attribute invisible. But still it add the attribute eventhough it exists already. could you please help me once again? thanks for the support you provided till now

0 Likes
Message 8 of 19

pbejse
Mentor
Mentor

If there's anything else after this you need to post a sample drawing 

 

This

(eq (vla-get-tagstring obj) "PRICE ")

To

(wcmatch (vla-get-tagstring obj) "PRICE*")

Or better yet, go to where you copied the code at post #1 and request it there.

0 Likes
Message 9 of 19

j.balasubramaniyam
Participant
Participant

Hi, code looks perfect for me if null prince () called it has to skip the remaining steps and has to run null (vlax-put AttObj 'Alignment acAlignmentmiddle). But not happening I believe. Someminor changes to be done somewhere. I will post the dwg if possible as I am autocad only in my office laptop. Thank you so much for your support

0 Likes
Message 10 of 19

pbejse
Mentor
Mentor

@j.balasubramaniyam wrote:

Hi, code looks perfect for me if null prince () called it has to skip the remaining steps and has to run null (vlax-put AttObj 'Alignment acAlignmentmiddle). But not happening I believe. Someminor changes to be done somewhere. I will post the dwg if possible as I am autocad only in my office laptop. Thank you so much for your support


Q: But why male models?

A: Are you serious? I just.. I just told you that a moment ago.

pbejse_1-1628239733465.png

 

 

0 Likes
Message 11 of 19

j.balasubramaniyam
Participant
Participant

🤣🤣🤣...  Did you get a chance to look at this? You can try this code for a block in any of your drawing and help me out right? I am just trying every option in the code but nothing working

0 Likes
Message 12 of 19

pbejse
Mentor
Mentor

Asked and answered

pbejse_0-1628240647548.png

 

0 Likes
Message 13 of 19

j.balasubramaniyam
Participant
Participant

Yes you are answered it is working fine except it still adding the attribute to every block even if it exists. I could not able to find a solution. 

0 Likes
Message 14 of 19

j.balasubramaniyam
Participant
Participant

Yes you are answered it is working fine except it still adding the attribute to every block even if it exists. I could not able to find a solution. 

0 Likes
Message 15 of 19

pbejse
Mentor
Mentor
Accepted solution

@j.balasubramaniyam wrote:

Yes you are answered it is working fine except it still adding the attribute to every block even if it exists. I could not able to find a solution. 


man from the river.png

0 Likes
Message 16 of 19

j.balasubramaniyam
Participant
Participant

Hi pbejse,  Today i checked in my personal PC & the same code which was not working in my official PC works very well and adding only attribues if not exists. I am really wondering how it is possible. Attaching the dwg & code used for your reference.  Thank you so much for your help. If possible could you please brainstorm & tell why the issue happened in my official PC only?

I am accepting your solution anyway.

Thanks & Regards,

Bala

0 Likes
Message 17 of 19

pbejse
Mentor
Mentor
Accepted solution

@j.balasubramaniyam wrote:

.... tell why the issue happened in my official PC only?


 

If you are wondering why it works on some but not all, its because of the  tag name:
- If the block already have a "PRICE" tag, and not as the result the program
  the TAG name will probably just "PRICE" and not equal to "PRICE " < take note of the space as the program creates

 

When you encounter this problem, please Check the existing TAG name.

 

For clarity:
"PRICE\U+0020" <-- IF you dont have a any reason for adding the extra space might as well use "PRICE" on the code

  (setq AttObj (vla-addattribute def  8 acAttributeModeInvisible
		      "Enter Item #" (vlax-3D-point 72 84) "PRICE"
		      "100"					
		    )
		 )

Below is the code modified so not to confused you with statement can understand it better.

 

 

(defun c:add$ ( / ss i blk blks def AttObj prince)
  (if
    	(and
	       (setq ss (ssget  "X" '((0 . "INSERT"))))
	       (setq i (sslength ss))
	       (while (> i 0)
	          (setq blk (GetPropertyValue  (ssname ss (setq i (1- i))) "BlockTableRecord/Name" ))
	          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
		 blks
	       )
	)
    
       (foreach blk blks
	  (Vlax-for obj (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
	    ;;;	If this is not null that means "PRICE" tag already exist	    ;;
		 (setq prince
			(and
			    (eq (vla-get-ObjectName obj) "AcDbAttributeDefinition")
			    (eq (vla-get-tagstring obj) "PRICE")
			    )
		       )
	    )
	    
	 (if (=  prince nil) ; <-- Confirms the current block has no attribute definitions named "PRICE"
	   (progn								
		(setq AttObj (vla-addattribute def  8 acAttributeModeInvisible	;<-- Add attribute definition
		      "Enter Item #" (vlax-3D-point 72 84) "PRICE"		;<-- Enter Item # as prompt
		      "100"							;<-- "100" as default value
		    )
		 )
        	(vlax-put AttObj 'Alignment acAlignmentmiddle)			
        	(command "_.attsync" "_N" blk)
	  	)
	   )
	 )
       )
(princ)
)

 

 

HTH

0 Likes
Message 18 of 19

j.balasubramaniyam
Participant
Participant

You are absolutely correct. Though I found this issue before reading the thread, your explanation & knowledge on LISP is awesome. Thank you so much for the help

0 Likes
Message 19 of 19

john.uhden
Mentor
Mentor

@j.balasubramaniyam 

You are quite fortunate that @pbejse took you under his wing.  He is one of the best around here, plus his sense of humor is thoroughly enjoyable, not to mention his patience. (I'm still laughing about Prince).

John F. Uhden

0 Likes