Jump to content

insert fields in texts with autolisp


Manuelmfec

Recommended Posts

Hello dear autocad users, today I come to you for help, I have to perform a lisp routine that extracts the length of a polyline and deposits it in a text in the form of a field so that it is updated as I modify the polyline, I have tried several times but without any success since my knowledge in autolisp is too basic, I ask please help me to reach the result described above and please inform me where I can obtain a pdf to learn autolisp from the Basic to advanced, I thank you very much for your attention

Link to comment
Share on other sites

(defun C:LENGHTPOL (/ )
 (setq pol (ssget "_+.:E:S" '((0 . "LWPOLYLINE,POLYLINE"))))
 (if (not pol) 
  (vl-exit-with-error "")
 )
 (setq Ptx (getpoint "\nSelect text point: ")
       stringtx (strcat 
                    "%<\\AcObjProp Object(%<\\_ObjId "
                    (itoa (vla-get-ObjectID (vlax-ename->vla-object (ssname pol 0))))
                    ">%).Length \\f \"%lu2%pr2\">%"
                )
       vltx (entmakex
             (list
               (cons 0 "TEXT")
               (cons 7 (getvar "TEXTSTYLE"))
               (cons 10 Ptx)
               (cons 1 stringtx)
	       (cons 40 2)
             )
            )
 )
 (vl-cmdf "_updatefield" vltx "")
 (princ)
)

 

Edited by confutatis
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

3 hours ago, confutatis said:

 

Hello friend, thank you very much you are my hero, it works for me to perfection. Excuse my ignorance, is that the autolisp with the fields really confuse me, I have a question, if I want to put 3 decimal places, how could I do it?, Normally I use (RTOS VARIABLE 2 3), but with the field I don't know how to proceed.

Link to comment
Share on other sites

@Manuelmfec - try this. See if you can spot the difference.

 

(defun C:LENGHTPOL (/ pol Ptx stringtx vltx)
 (setq pol (ssget "_+.:E:S" '((0 . "LWPOLYLINE,POLYLINE"))))
 (if (not pol) 
  (vl-exit-with-error "")
 )
 (setq Ptx (getpoint "\nSelect text point: ")
       stringtx (strcat 
                    "%<\\AcObjProp Object(%<\\_ObjId "
                    (itoa (vla-get-ObjectID (vlax-ename->vla-object (ssname pol 0))))
                    ">%).Length \\f \"%lu2%pr3\">%"
                )
       vltx (vlax-ename->vla-object 
                    (entmakex
                        (list
                            (cons 0 "TEXT")
                            (cons 7 (getvar "TEXTSTYLE"))
                            (cons 10 Ptx)
                            (cons 1 stringtx)
			    (cons 40 2)
                        )
                    )
            )
 )
 (vl-cmdf "_updatefield" (vlax-vla-object->ename vltx) "")
 (princ)
)

 

Link to comment
Share on other sites

33 minutes ago, rkmcswain said:

@Manuelmfec - prueba esto. Vea si puede detectar la diferencia.

 


 

I have already noticed, everything is different when it comes to the fields, I will study this routine to learn more, I thank you very much for your attention and for sharing your knowledge.

  • Like 1
Link to comment
Share on other sites

Fields is a whole world unto itself, I am still far from absolute mastery. Then, like all things you have to try and try again, I discovered how to insert formulas, how to highlight entities associated with fields, how to update fields dynamically, without the direct use of the REGEN and UPDATEFIELD commands. It's all a becoming...

Link to comment
Share on other sites

16 hours ago, confutatis said:

(defun C:LENGHTPOL (/ pol Ptx stringtx vltx)
 (setq pol (ssget "_+.:E:S" '((0 . "LWPOLYLINE,POLYLINE"))))
 (if (not pol) 
  (vl-exit-with-error "")
 )
 (setq Ptx (getpoint "\nSelect text point: ")
       stringtx (strcat 
                    "%<\\AcObjProp Object(%<\\_ObjId "
                    (itoa (vla-get-ObjectID (vlax-ename->vla-object (ssname pol 0))))
                    ">%).Length \\f \"%lu2%pr2\">%"
                )
       vltx (vlax-ename->vla-object 
                    (entmakex
                        (list
                            (cons 0 "TEXT")
                            (cons 7 (getvar "TEXTSTYLE"))
                            (cons 10 Ptx)
                            (cons 1 stringtx)
			    (cons 40 2)
                        )
                    )
            )
 )
 (vl-cmdf "_updatefield" (vlax-vla-object->ename vltx) "")
 (princ)
)

 

 

I noticed that the object is changed to a vla-object in the vltx variable.

Just to be change it back to an ename a few lines below.

Guess this would be a small efficiency improvement 🙂

  • Agree 1
Link to comment
Share on other sites

You are right, good point. A typo in several copy-paste. I have changed the post.

Edited by confutatis
Link to comment
Share on other sites

Gracias a sus ayudas he logrado llegar hasta aquí, es una pequeña rutina adaptable, ( se puede usar para  extraer longitud y áreas de polilíneas incluso coordenadas de bloques), haciendo solo pequeñas modificaciones en el código, sin embargo, cuando quise extraer el contenido del campo de un bloque dinámico no me funciono ( me salió "#####") quisiera que por favor me digan que me salió mal, adjunto código y una imagen del bloque dinámico

(defun c:3xtr ()

(setq conjunto (ssget "_+.:E:S" '((0 . "INSERT")))); OR "LWPOLYLINE, POLYLINE" IF IT IS FOR LENGTH OR AREA
(setq pt1(getpoint "\n Punto del texto: "))

  
(setq stringtx (strcat "%<\\AcObjProp Object(%<\\_ObjId "
		       (itoa (vla-get-ObjectID (vlax-ename->vla-object (ssname conjunto 0))))
		       ">%).TextString \\f \"%tc1\">%")); LINE 4, EXTRACT THE CONTENT OF THE FIELD OF A BLOCK, NOT WORKING WELL, IS FOR BLOCKS

(COMMAND "_TEXT" "_J" "_M" PT1 1.8 0 stringtx)

(princ)

)


;(setq stringtx (strcat "%<\\AcObjProp Object(%<\\_ObjId "
                       ;(itoa (vla-get-ObjectID (vlax-ename->vla-object (ssname conjunto 0))))
                       ;">%).Area \\f \"%lu2%pr3\">%")); LINE 4, EXTRA AREA, WORKS WELL FOR POLYLINES

;(setq stringtx (strcat "%<\\AcObjProp Object(%<\\_ObjId "
                       ;(itoa (vla-get-ObjectID (vlax-ename->vla-object (ssname conjunto 0))))
                       ;">%).Length \\f \"%lu2%pr3\">%")); LINE 4, EXTRA LENGTH, WORKS WELL FOR POLYLINES

(el campo que quiero extraer es el que dice "lago")

text field.JPG

EXTRACION DE DATOS DINAMICOS.dwg

Link to comment
Share on other sites

You have extracted the field of the block, but you should have extracted the field of the block attribute, which obviously has a different ID from the block.

 

(defun c:3xtr ()
 (setq conjunto (ssget "_+.:E:S" '((0 . "INSERT")))); OR "LWPOLYLINE, POLYLINE" IF IT IS FOR LENGTH OR AREA
 (setq attr (car (safearray-value (variant-value (vla-getattributes (vlax-ename->vla-object (ssname conjunto 0)))))))
 ;;; (setq attr (car (vlax-invoke (vlax-ename->vla-object (ssname conjunto 0)) 'getattributes))) it can also be written like this
 (setq pt1(getpoint "\n Punto del texto: "))
 (setq stringtx (strcat "%<\\AcObjProp Object(%<\\_ObjId "
		       (itoa (vla-get-ObjectID attr))
		       ">%).TextString \\f \"%tc1\">%")
 ); LINE 4, EXTRACT THE CONTENT OF THE FIELD OF A ATTRIBUTE BLOCK
 (command "_TEXT" "_J" "_M" PT1 1.8 0 stringtx)
 (princ)
)

 

EXTRACION DE DATOS DINAMICOS.dwg

Edited by confutatis
Link to comment
Share on other sites

On 22/7/2021 at 9:11, confutatis said:

 

Hello friends, good morning.

I want to ask you a query, I have noticed that in the routine the expression that extracts the length has these "\\" signs different from the one normally inserted with the field process "\", what I want to know is how to do it in the correct way In other expressions, yesterday I modified and tried to use the following expression but it didn't work for me:

 

%<\\AcExpr (%<\\_FldPtr ########>%+%<\\_FldPtr ########>%+%<\\_FldPtr ########>%) \\f \"%lu2%pr3\">%

 

Could you look at my expression and modify it if necessary and tell me how to distribute or add these "\" signs. please

 

 

 

 

 

 

Link to comment
Share on other sites

The right expression should have a syntax like this:

"%<\\AcExpr (%<\\AcObjProp Object(%<\\_ObjId 2132378152672>%).length>%+%<\\AcObjProp Object(%<\\_ObjId 2132378138672>%).length>%) \\f \"%lu2%pr2\" >%"

 

This is the relative command for summing the lengths of polylines:

(defun C:SUMLENGTH () 
 (if (setq ss (ssget '((0 . "LWPOLYLINE,POLYLINE"))))
  (progn
   (setq lst
	   (strcat
	    "%<\\AcExpr \("
	    (vl-string-right-trim
	     "+"
	     (apply 'strcat
		    (mapcar
		     '(lambda (elem)
		       (strcat
			"%<\\AcObjProp Object(%<\\_ObjId "
			(itoa elem)
			">%).length>%+"
		       )
		      )
		     (mapcar
		      'vla-get-objectid
		      (mapcar
		       'vlax-ename->vla-object
		       (vl-remove-if
			'listp
			(mapcar 'cadr (ssnamex ss))
		       )
		      )
		     )
		    )
	     )
	    )
	    "\) \\f \"%lu2%pr2\" >%"
	   )
   )
     (setq p1 (vlax-3D-Point (getpoint "\nText Position:")))
     (setq nuovotesto (vla-addMText
                       (if (zerop (vla-get-activespace (vla-get-activedocument (vlax-get-acad-object))))
                        (vla-get-paperspace (vla-get-activedocument (vlax-get-acad-object)))
                        (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
                       )
                       p1
                       0.0
                       lst
                      )
     )	 
  )
 )
 (princ)
)

 

\ is the sign to be put before special characters such as ( or ", so that they are read as strings.

Link to comment
Share on other sites

On 30/7/2021 at 22:26, confutatis said:

La expresión correcta debería tener una sintaxis como esta:


 

Este es el comando relativo para sumar las longitudes de polilíneas:


 

\ es el signo que se coloca antes de los caracteres especiales como (o ", para que se lean como cadenas.

Muchas gracias amigo, me ha funcionado de maravilla 

Link to comment
Share on other sites

On 30/7/2021 at 22:26, confutatis said:

La expresión correcta debería tener una sintaxis como esta:


 

Este es el comando relativo para sumar las longitudes de polilíneas:


 

\ es el signo que se coloca antes de los caracteres especiales como (o ", para que se lean como cadenas.

Amigo, donde puedo aprender sobre sobre este tipo de funciones: vla-get-activedocument, vlax-3D-Point, vla-get-modelspace y relacionadas.

por favor

Link to comment
Share on other sites

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