Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to extract block attributes in a field using LISP

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
Anonymous
3836 Views, 10 Replies

How to extract block attributes in a field using LISP

How to extract block attributes in a field using LISP

10 REPLIES 10
Message 2 of 11
Moshe-A
in reply to: Anonymous

@Anonymous  hi,

 

check this 😀

 

Moshe

 

;; Create the text object in model space
(setq modelSpace (vla-get-ModelSpace doc))
(setq textObj (vla-AddText modelSpace text insertionPoint height))  
;; Return the current text string for the object
(setq text (vla-FieldCode textObj))
(alert (strcat "The FieldCode for the text object equals: " text))

 

Message 3 of 11
Anonymous
in reply to: Moshe-A

it doesn't work. 

commad: (setq text (vla-FieldCode textObj))
; mistake: invalid argument type: VLA-OBJECT nil
commad: (alert (strcat "The FieldCode for the text object equals: " text)
(_> The model is being regenerated.
lisp stops working

I have a block. It has two attributes. The first attribute has TAG - "DATA 1", and its DEFAULT - " BTH 1.". The second attribute has TAG - "Number", and its DEFAULT - "1".
Mode of all verify attributes.I want to get a field in the model and have "BTH 1.1" ("BTH 1." + "1") in it. I need to extract the first argument and the second argument from
the block.
Then add a second argument to the first argument and output the result in the field. I don't know how to do this correctly. Help.

Message 4 of 11
Moshe-A
in reply to: Anonymous

@Anonymous  hi,

 

Got you now, thought you want to extract a field code but you only wants the attribute value

that's pretty easy: 

 

(getproperyvalue) function will retrieve an attribute value and (setpropertyvalue) will set an attribute value

be aware that these function gets into error if the attribute tag is not found\miss spelled 😀

 

enjoy

Moshe

 

Message 5 of 11
dbroad
in reply to: Anonymous

Assuming you know how to program in LISP and in the spirit of teaching you how to fish, the procedure that I use is to create the fields manually and then examine them.  For example, when editing your mtext entity with the current contents "field". Add two object fields.  When selecting the block reference, the choices will either be properties of the block itself or its attributes.  Use the insert field twice, once for each attrbute you want to concatenate together.  The resulting field code could be returned either by using vla-fieldcode or by copying the field for each attribute directly from the field dialog.  For your block reference, it would be:

"%<\\AcObjProp Object(%<\\_ObjId 929430624>%).TextString>%%<\\AcObjProp Object(%<\\_ObjId 929430656>%).TextString>%"

The advantage of using vla-field code is that it returns a lisp formatted text string.  To create a field yourself using LISP, simply create text entity as @Moshe-A  demonstrated and then put the textstring to be the fieldcode.

(vla-put-textsring
  <mtext vla-object>
  (STRCAT "%<\\AcObjProp Object(%<\\_ObjId "
	  (ITOA <objectid integer for first attribute>)
	  ">%).TextString>%%<\\AcObjProp Object(%<\\_ObjId "
	  (ITOA	<objectid integer for second attribute>	">%).TextString>%" )
  )
)

The objectid of a vla-object can be obtained by using (vla-get-objectid <vla-object>)

To get a vla-object from a selection, try (setq object (vlax-ename->vla-object(car(entsel)))).  Add as much error handling as necessary.

Architect, Registered NC, VA, SC, & GA.
Message 6 of 11
Anonymous
in reply to: dbroad

Unfortunately, I don't know how to program and I'm taking my first steps now

Message 7 of 11
Anonymous
in reply to: Anonymous

Now I'm trying to extract the value of the "DATA1" attribute from the block. Far unsuccessfully

Message 8 of 11
Moshe-A
in reply to: Anonymous

@Anonymous ,

 

check this, picking the block is ok

 

(if (setq pick (entsel "\nSelect block: "))

 (progn

  (setq ent (car pick))

  (terpri)

  (princ (getpropertyvalue ent "data1"))

))

 

note: you will end up with an error if tag not found.

 

Moshe

 

Message 9 of 11
Anonymous
in reply to: Moshe-A

Clear. Thanks. Everything works well. Now I want to understand how you can simultaneously extract the values of the "DATA1" and "NUMBER" attributes from this block

Message 10 of 11
Moshe-A
in reply to: Anonymous

@Anonymous  hi,

 


@Anonymous wrote:

Clear. Thanks. Everything works well. Now I want to understand how you can simultaneously extract the values of the "DATA1" and "NUMBER" attributes from this block


simultaneously you mean each one at a step - yes?

check this:

 

(if (setq pick (entsel "\nSelect block: "))

 (progn

  (setq ent (car pick))

  (setq data1 (getpropertyvalue ent "data1"))

  (setq num1 (getpropertyvalue ent "number"))

 (prompt (strcat "\n" data1 "-" num1))

))

 

 

Message 11 of 11
Anonymous
in reply to: Moshe-A

I was hoping that the result would be "B T H 1." and "1". But it turns out nil

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report