Retrieving data from text entity

Retrieving data from text entity

Anonymous
Not applicable
3,778 Views
16 Replies
Message 1 of 17

Retrieving data from text entity

Anonymous
Not applicable

Hey guys, I'm VERY new to LISP but would like to implement some scripts to streamline my workflow a bit. Currently trying to figure out a command that will allow me to select a text entity and set the value typed in that entity as a variable. I've been trying to use

 

(defun c:NEWBLOCK()
(prompt "/nSelect DWG Number: ")
(setq DN (entsel))

 

From here I'm not sure how to collect the value inside a text entity (single OR multi-line) 

Maybe "entsel" is ineffective for this but I've spent too much time trying to figure this out.

Also been looking into the getvar command.

 

Thanks in advance!

0 Likes
Accepted solutions (1)
3,779 Views
16 Replies
Replies (16)
Message 2 of 17

hak_vz
Advisor
Advisor

 

(defun textval ( / e ent)
(setq e (car (entsel "\nSelect text >")))
(setq ent (entget e))
(cond 
	((wcmatch (cdr (assoc 0 ent)) "*TEXT")
		(setq val (cdr (assoc 1 ent)))
	)
)
val
)

You can select TEXT or MTEXT entity. Function stores text string into global variable named val.

To call function you can use

(textval) ;Stores text string to variable val
(setq a (textval)) ; stores text string to variables a and val

To call function directly from console you can create c: function

(defun c:textval nil (textval)); now you can simply type textval to console

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 3 of 17

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

.... to select a text entity and set the value typed in that entity as a variable. ....


There are several ways you can do that.  Two ways that work the same for either Text or Mtext:

 

(setq textcontent (cdr (assoc 1 (entget (car (entsel "\nSelect DWG Number: "))))))

 

or:

 

(vl-load-com); if needed

(setq textcontent (vla-get-TextString (vlax-ename->vla-object (car (entsel "\nSelect DWG Number: ")))))

 

or [with the distadvantage that you can't use exactly the same code for either Text or Mtext]:

 

for plain Text:

(getpropertyvalue (car (entsel "\nSelect DWG Number: ")) "TextString")

but for Mtext:

(getpropertyvalue (car (entsel "\nSelect DWG Number: ")) "Text")

 

All of those rely on your selecting the right kind of thing, but can be enhanced to verify that, as @hak_vz's suggestion does [it uses the approach in my first one for the pulling of the content].

Kent Cooper, AIA
Message 4 of 17

hak_vz
Advisor
Advisor

@Kent1CooperHere we have beginner in autolisp world. Let he starts with vanilla autolisp and learn vl-functions later, when he grasp the basics. For a start functions like various CAR CDR combinations, ASSOC, MEMBER, ENTGET, ENTSEL and similar are a must to have. This is my opinion, someone may disagree.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 5 of 17

Anonymous
Not applicable

all of these seem to work for what I'm trying to build but @Kent1Cooper first sol'n seems very elegant:

 

(setq textcontent (cdr (assoc 1 (entget (car (entsel "\nSelect DWG Number: "))))))

 

If either of you have time, could you try to walk me through the logic of this declaration? I've never seen textcontent before, not sure what the syntax for that is.  *EDIT, textcontent is of course the variable name but why have a CDR and what does making the array associative do?

I see what you're saying @hak_vz and I agree I should try to grasp the basics, been taking apart the logic of your solution as well.

0 Likes
Message 6 of 17

hak_vz
Advisor
Advisor

@Anonymous wrote:

all of these seem to work for what I'm trying to build but @Kent1Cooper first sol'n seems very elegant:

 

(setq textcontent (cdr (assoc 1 (entget (car (entsel "\nSelect DWG Number: "))))))

 


Execution of commands is from right to left, so you can take parts of this expression and run it in console, just copy, past and hit enter. So with text entity we  have

 

Command: (entsel "\nSelect DWG Number: ")
Select DWG Number: (<Entity name: 25147bf3440> (1024.89 433.124 0.0))

 

Here is a list that contains two entries, entity name and pickpoint where we clicked on entity.

Functions car, cadr adn varios combination access diferent entity in a list. Car fetches first, cadr second. Or you can use command (NTH num list) starting from 0.

 

Command: (car (entsel "\nSelect DWG Number: "))
Select DWG Number: <Entity name: 25147bf3440>

 

To read definition  of this entity we use command entget

 

(entget(car (entsel "\nSelect DWG Number: ")))
Select DWG Number: ((-1 . <Entity name: 25147bf3440>) (0 . "TEXT") (330 . <Entity name: 25147bf81f0>) (5 . "274") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDbText") (10 1019.87 433.069 0.0) (40 . 2.5) (1 . "aaa") (50 . 0.0) (41 . 1.0) (51 . 0.0) (7 . "Standard") (71 . 0) (72 . 0) (11 0.0 0.0 0.0) (210 0.0 0.0 1.0) (100 . "AcDbText") (73 . 0))

 

Text value is stored in association list (1 . "aaa"). To fetch it we use (cdr (assoc keyvalue list))

And at last this value is stored in variable textcontent  using command setq.

For a key values for some entity you can look in autocad help - search for DXF ENTITIES Section

Under -> Developer Documentation -> Reference guide you have almost everything you need to start learning autolisp. Google will help you a lot. There is a lot of code in this forum. Check AFRALISP 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 7 of 17

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... walk me through the logic of this declaration? ....


I suggest you explore the AutoLisp Reference.  In that code line, every word that immediately follows a left parenthesis is an AutoLisp function name.  You can look them up, and it will tell you what they do, what arguments they require, whether some arguments are optional, what they return, and often some things about their relationships to certain other functions, with examples.

Kent Cooper, AIA
Message 8 of 17

ronjonp
Advisor
Advisor

@hak_vz wrote:

@Kent1CooperHere we have beginner in autolisp world. Let he starts with vanilla autolisp and learn vl-functions later, when he grasp the basics. For a start functions like various CAR CDR combinations, ASSOC, MEMBER, ENTGET, ENTSEL and similar are a must to have. This is my opinion, someone may disagree.


I'd disagree, IMO the vla functions are more readable.  Skimming DFX data takes a bit of practice ( not to undermine the value it's just not as readable, especially for a beginner ).

 

 

(defun c:foo (/ e o); < - localized variables
  (if (and ;; 'AND' ensures that all the conditions are met, if you pass vlax-ename->vla-object OR entget we'll chuck a wobbly
	   (setq e (car (entsel "\nPick something with text: ")))
	   ;; Convert to vla-object
	   (setq o (vlax-ename->vla-object e))
	   ;; See if it has a textstring property
	   (vlax-property-available-p o 'textstring)
      )
    ;; All conditions above are valid alert the text string value
    (alert (vla-get-textstring o))
    ;; Check if we have an object
    (alert (if o
	     ;; We have an object, message for picked item that does not have the textstring property
	     (strcat (vla-get-objectname o) " selected does not have a textstring property!")
	     ;; Message for missed pick
	     "You missed your pick!"
	   )
    )
  )
  (princ)
);; Load VL functions
(vl-load-com)

 

 

 

Message 9 of 17

Anonymous
Not applicable

is the Association list standard for all types of entities? I would assume a leader or dimension would have different association lists correct? would there be a legend of similar? how did u find out 1 . "aaa" is where the text value is stored? would other information like text color and size be in this association list?

0 Likes
Message 10 of 17

ronjonp
Advisor
Advisor

@Anonymous wrote:

is the Association list standard for all types of entities? I would assume a leader or dimension would have different association lists correct? would there be a legend of similar? how did u find out 1 . "aaa" is where the text value is stored? would other information like text color and size be in this association list?


Some of the codes store similar data ( color linetype rotation etc ) . The lists will vary for each entity selected.

Here's some reference:

Entities Section

Message 11 of 17

hak_vz
Advisor
Advisor

@ronjonp 

Yes, vl-functions are more readable and versatile, but you have to know how to apply them, how to handle errors, check if property or method is available and so on. They often require creating an array or value of particular data type. Luckily for vl objects you can always use vlax-dump-object. For reading key values one can create helper function. There is large group of common key values, entity type is always 0, layer 8 .... Learning autlisp / visualisp is not easy, but is definitively rewarding.

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 12 of 17

ronjonp
Advisor
Advisor

@hak_vz wrote:

@ronjonp 

Yes, vl-functions are more readable and versatile, but you have to know how to apply them, how to handle errors, check if property or method is available and so on. They often require creating an array or value of particular data type. Luckily for vl objects you can always use vlax-dump-object. For reading key values one can create helper function. There is large group of common key values, entity type is always 0, layer 8 .... Learning autlisp / visualisp is not easy, but is definitively rewarding.

 

 


The order in which one learns lisp will vary by person. Providing more options to achieve the same goal is a win win IMO ... agreed, learning LISP is very rewarding. 🍻

Message 13 of 17

hak_vz
Advisor
Advisor

@Anonymous wrote:

 how did u find out 1 . "aaa" is where the text value is stored? would other information like text color and size be in this association list?


Yes. There are common group entities codes, 0 entitiy type, 8 layer, 62 color number (indexed colors) 0...256, 5 linetype .....

And there are entity specific codes. For text entity 1 is string value, 10 insertions point, 40 text height, 50 rotation....

Yeu have it all explained in developers guide. There is also pdf documentation you can install or read online.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 14 of 17

CodeDing
Advisor
Advisor

@Anonymous ,

 

Here are 2 functions that I almost ALWAYS use when writing code. I have them load into every drawing, so I can easily look at entities and their properties. I recommend you save them and give them a try:

 

 

(defun c:EG ( / )
  (entget (car (entsel)))
);defun

 

^^^ EG (short for entget in this case), will have you select an entity then in the command history you can see all of the entity definition data. This knowledge, along with understanding the DXF codes (that ronjonp posted) is the main premise for all entities in AutoCAD. Once you realize that objects are essentially just Lists that we can manipulate, then the sky is the limit. [You will use functions like entget, entmodsubst, assoc, cdr to primarily manipulate objects this way]

 

 

(defun c:DP ( / )
  (dumpallproperties (car (entsel)))
);defun

 

^^^ DP (short for Dump Properties in this case), will have you select an entity then in the command history you can see all of the entity definition data in a prettier fashion essentially. It's easier for you to see & understand what properties you are looking at since they're labeled, and thus can be easier to change also. [You will use the functions getpropertyvalue & setpropertyvalue to manipulate objects this way]

 

Best,

~DD

0 Likes
Message 15 of 17

pedrohmc1
Community Visitor
Community Visitor

Greetings @CodeDing , @ronjonp , @hak_vz 

Please, can someone help me understand why this code doesn't work:

Spoiler
(defun c:create-text ()
  (setq texto (getstring "\nIngrese el texto: "))
  (setq pt (getpoint "\nEspecifique la ubicación del texto: "))
  (setq height 2.5)
  (setq width (* (length text) height))
  
  ; Crear un objeto de texto y agregarlo al dibujo
  (setq text-entity (entmake (list
                              (cons 0 "TEXT")
                              (cons 8 "0") ; Capa por defecto (ajustar según sea necesario)
                              (cons 10 pt) ; Punto de inserción
                              (cons 40 height) ; Altura del texto
                              (cons 1 texto) ; Contenido del texto
                              (cons 50 0) ; Ángulo de rotación (ajustar según sea necesario)
                              )))
  (entupd text-entity)

  (alert "Texto insertado en el dibujo.")
)

(c:create-text)

 

0 Likes
Message 16 of 17

CodeDing
Advisor
Advisor

@pedrohmc1 ,

 

Try this:

(defun c:create-text ( / texto pt height text-entity)
  (setq texto (getstring "\nIngrese el texto: "))
  (initget 1)
  (setq pt (getpoint "\nEspecifique la ubicación del texto: "))
  (setq height 2.5)
  ; Crear un objeto de texto y agregarlo al dibujo
  (setq text-entity
    (entmakex (list (cons 0 "TEXT")
                   (cons 8 "0") ; Capa por defecto (ajustar según sea necesario)
                   (cons 10 pt) ; Punto de inserción
                   (cons 40 height) ; Altura del texto
                   (cons 1 texto) ; Contenido del texto
                   (cons 50 0) ; Ángulo de rotación (ajustar según sea necesario)
    ))
  )
  (alert "Texto insertado en el dibujo.")
)

(c:create-text)

 

- added local variables

- forced "getpoint" with the (initget 1) function

- removed "width" variable, not being used.

- changed "entmake" to "entmakex" function (to return entity name)

- removed "entupd" call, not necessary

 

Best,

~DD

0 Likes
Message 17 of 17

pedrohmc1
Community Visitor
Community Visitor

Much better, thank you very much

 

How would the code change if instead of using 'text' I used 'mtext'?

I modified it like this, but it doesn't work for me either:

(defun c:create-mtext ( / texto pt width text-entity)
  (setq texto (getstring "\nIngrese el texto: "))
  (initget 1)
  (setq pt (getpoint "\nEspecifique la ubicación del texto: "))
  (setq width 100.0) ; Ancho del área de texto
  ; Crear un objeto de mtext y agregarlo al dibujo
  (setq text-entity
    (entmakex (list (cons 0 "MTEXT")
                   (cons 8 "0") ; Capa por defecto (ajustar según sea necesario)
                   (cons 10 pt) ; Punto de inserción
                   (cons 41 width) ; Ancho del área de texto
                   (cons 1 texto) ; Contenido del texto
                   (cons 50 0) ; Ángulo de rotación (ajustar según sea necesario)
                   (cons 71 1) ; Justificación del texto (1 = Izquierda, ajustar según sea necesario)
                   (cons 72 1) ; Alineación del texto (1 = Izquierda, ajustar según sea necesario)
    ))
  )
  (alert "Texto insertado en el dibujo.")
)

(c:create-mtext)

Thank you in advance for your answer.

0 Likes