Jump to content

Recommended Posts

Posted

I need to apologise first, the issue seems to be so trivial that I should have is solved in a few minutes. Yet, I keep fighting for the last 2 h without any good results.

 

I need to set attribute value in an existing dynamic block. I started with "how can I read the attributes" and failed badly. After abusing an AI system I got the code:

(defun c:get_dba (/ ss blkRef vlaBlock attArray att)
  (setq ss (ssget '((0 . "INSERT"))))
  (if ss
    (progn
      (setq blkRef (ssname ss 0)) 
      (setq vlaBlock (vlax-ename->vla-object blkRef)) 
      
      (setq attArray (vla-getattributes vlaBlock))
      
      ;; Loop through the attributes and print their names and values
      (foreach att attArray ;looping through the array that way doesn't work
        (princ (strcat "\nAttribute Name: " (vla-get-TagString att))) 
        (princ (strcat " | Value: " (vla-get-TextString att))) 
      )
    )
    (princ "\nNo block selected.")
  )
  (princ)
)

I need to find i.e. TYPE and set it to AAA. The watch tree for attArray:

image.thumb.png.669a9fcd4cbabc37340860ea988cc4bb.png

 

Any hints how can I:

1. Make the code to read the attributes

2. Set the attribute to AAA

 

I'm OK with the old school LISP, but the VL stuff beats me...

Posted

I hate spending time correcting AI-generated code, but the first thing that sticks out is that the following:

(setq attArray (vla-getattributes vlaBlock))

 

Will return a safearray variant, which cannot be iterated directly using foreach.

 

Instead, you can use:

(setq attArray (vlax-invoke vlaBlock 'getattributes))

 

Which will return the data using native data types, i.e. a list.

 

You can find more examples here.

  • Like 2
Posted (edited)

Thank you Lee Mac!! I'm not sure if the AI is doing more good or bad stuff. I used to dig deep, learn, write code, debug, done. Now, ask AI, clean the rubbish, ask again, clean more rubbish... Sometimes it works and sometimes it's sending you straight into a ditch, but in a way that is not easily recognisable unless you're an expert.

 

Gone testing, I'll report back.

 

Edit: your suggestion was 10/10

Attribute Name: FUNC_DESIG | Value: XV
Attribute Name: SEQ_NO | Value: XXXX
Attribute Name: BMS | Value:
Attribute Name: INSTR-TYP-DETAIL-NO | Value: 1
Attribute Name: FUNCTION | Value: ########
Attribute Name: TAG | Value:
Attribute Name: SIZE | Value:
Attribute Name: SYSTEM | Value:
Attribute Name: NOTE | Value:
Attribute Name: TYPE | Value: fghfh
Attribute Name: VENDOR | Value:
Attribute Name: DWG | Value:

I hope I can handle setting them to a new value....

Edit2:

Just in case someone has the same problem. This sets the attribute vale based on attribute name

(if (eq (vla-get-TagString att) "TYPE")(vla-put-TextString att "AAA"))

 

Edited by pefi
report back
Posted

Just a comment when you get the attributes as a list you can edit any attribute by its position in the list so don't need a Tag name search. (nth 9 atts) is "TYPE" the 1st item in a list is (nth 0 .Sometimes its easier to do it this way.

  • Like 1
Posted
  On 3/26/2025 at 11:15 PM, BIGAL said:

Just a comment when you get the attributes as a list you can edit any attribute by its position in the list so don't need a Tag name search. (nth 9 atts) is "TYPE" the 1st item in a list is (nth 0 .Sometimes its easier to do it this way.

Expand  

 

Personally I wouldn't rely on the attribute order unless absolutely necessary (i.e. when faced with duplicate attribute tags).

  • Like 1
Posted (edited)

Thank you again! I need something that will survive block definition modification and I have no control over the drawings, but I can be sure there will be no attribute duplicates. We're in the process of changing the block definitions (adding more attributes) and the drawings are a data source for some data extract software, so duplicates are not going to happen.

Edited by pefi

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