Manuelmfec Posted July 22, 2021 Share Posted July 22, 2021 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 Quote Link to comment Share on other sites More sharing options...
confutatis Posted July 22, 2021 Share Posted July 22, 2021 (edited) (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 July 24, 2021 by confutatis 1 1 Quote Link to comment Share on other sites More sharing options...
Manuelmfec Posted July 22, 2021 Author Share Posted July 22, 2021 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. Quote Link to comment Share on other sites More sharing options...
rkmcswain Posted July 22, 2021 Share Posted July 22, 2021 @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) ) Quote Link to comment Share on other sites More sharing options...
Manuelmfec Posted July 22, 2021 Author Share Posted July 22, 2021 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. 1 Quote Link to comment Share on other sites More sharing options...
confutatis Posted July 23, 2021 Share Posted July 23, 2021 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... Quote Link to comment Share on other sites More sharing options...
dexus Posted July 23, 2021 Share Posted July 23, 2021 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 1 Quote Link to comment Share on other sites More sharing options...
confutatis Posted July 23, 2021 Share Posted July 23, 2021 (edited) You are right, good point. A typo in several copy-paste. I have changed the post. Edited July 23, 2021 by confutatis Quote Link to comment Share on other sites More sharing options...
Manuelmfec Posted July 23, 2021 Author Share Posted July 23, 2021 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") EXTRACION DE DATOS DINAMICOS.dwg Quote Link to comment Share on other sites More sharing options...
confutatis Posted July 24, 2021 Share Posted July 24, 2021 (edited) 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 July 24, 2021 by confutatis Quote Link to comment Share on other sites More sharing options...
Manuelmfec Posted July 24, 2021 Author Share Posted July 24, 2021 36 minutes ago, confutatis said: Ha extraído el campo del bloque, pero debería haber extraído el campo del atributo del bloque, que obviamente tiene un ID diferente al del bloque. EXTRACION DE DATOS DINAMICOS.dwg 945.78 kB · 0 descargas It has worked for me, I thank you again 1 Quote Link to comment Share on other sites More sharing options...
Manuelmfec Posted July 30, 2021 Author Share Posted July 30, 2021 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 Quote Link to comment Share on other sites More sharing options...
confutatis Posted July 31, 2021 Share Posted July 31, 2021 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. Quote Link to comment Share on other sites More sharing options...
Manuelmfec Posted August 2, 2021 Author Share Posted August 2, 2021 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 Quote Link to comment Share on other sites More sharing options...
Manuelmfec Posted August 2, 2021 Author Share Posted August 2, 2021 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.