pttr Posted March 27, 2024 Posted March 27, 2024 (edited) Hi! Trying to modify this code to change Fields to Text inside a block with multiple instances. https://forums.augi.com/showthread.php?72534-Convert-Field-to-Text-within-Block-Globally I can get it to work for one, but some of the others change the content of the text to 'Model'. I'm guessing I missed some iteration check, but I'm not sure (defun c:FTT (/ ss n bn an ad s) (setq ss (ssget "_X" '((0 . "INSERT") (2 . "myblock")))) ; Get selection set of blocks named Myblock (if ss (progn (setq n 0) (while (< n (sslength ss)) (setq bn (ssname ss n)) (setq an (entnext bn)) (while (and an (setq ad (entget an)) (= "ATTRIB" (cdr (assoc 0 ad))) ) (setq s (cdr (assoc 1 ad))) (if (not (equal s nil)) ; Check if s is not nil (progn (setq ad (subst (vl-list* 1 "") (assoc 1 ad) ad)) (entmod ad) (setq ad (subst (vl-list* 1 s) (assoc 1 ad) ad)) (entmod ad)) (princ "\nAttribute value is nil. Skipping substitution.") ) (entupd an) (setq an (entnext an)) ) ;_ end while (setq n (1+ n)) ) ;_ end while (princ "\nField links removed from blocks named Myblock.") ) (princ "\nNo blocks named Myblock found.") ) (setq ss nil) (gc) (princ) ) Edited March 28, 2024 by pttr Quote
BIGAL Posted March 28, 2024 Posted March 28, 2024 Read it again and fix up (setq ss (ssget "_X" '((0 . "INSERT") (2 . "myblock")))) ; Get selection set of blocks named "Regionservic" For the get value of field. https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/extract-field-expression/td-p/3364401https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/extract-field-expression/td-p/3364401 Quote
pttr Posted March 28, 2024 Author Posted March 28, 2024 Hmm okay, I found something that might be a problem that i can´t get my head around I think the problem is modifying the block in different layouts. rewrote from scratch and I´m not sure how i iterate different layouts to make the changes. To clarify this code works, but only in one layout (defun c:FTT (/ ss n bn an ad s ) (setq ss (ssget "_X" '((0 . "INSERT")(2 . "Myblock")))) (setq n 0) ;Initialize counter ;; Step through selection set one entity at a time (while (< n (sslength ss)) (setq bn (ssname ss n)) ;Get the nth entity in the selection set (setq an (entnext bn)) ;Get the next enity after bn ;; Step through each next entity until it is not an attribute (while (and an ;Check if entity is found (setq ad (entget an)) ;Get data (= "ATTRIB" (cdr (assoc 0 ad))) ;Check if attribute ) ;_ end of and (setq s (cdr (assoc 1 ad))) ;Get text value (setq ad (subst (vl-list* 1 "") (assoc 1 ad) ad)) ;Clear the attribute's value (entmod ad) ;Modify the entity (setq ad (subst (vl-list* 1 s) (assoc 1 ad) ad)) ;Set the attribute's value back to only the text (entmod ad) ;Modify the entity (entupd an) ;Update screen to show change (setq an (entnext an)) ;Get next entity ) ;_ end of while (setq n (1+ n)) ;Increment counter ) ;_ end of while (setq ss nil) ;Clear selection set (gc) ;Clear unused memory (princ) ) ;_ end of defun Found this, but didn´t manage to implement Quote
pttr Posted March 28, 2024 Author Posted March 28, 2024 This is how far i got on my own but I still get "Model" istead of intended text sometimes (defun c:FTT (/ ss n bn an ad s) (setq layoutOld (getvar "CTAB")) (foreach layoutCrt (layoutlist) ;do som here: (setq ss (ssget "_X" (list '(2 . "myblock") (cons 410 layoutCrt)))) (setq n 0) ;Initialize counter (setq bn (ssname ss n)) ;Get the nth entity in the selection set (setq an (entnext bn)) ;Get the next enity after bn (while (and an ;Check if entity is found (setq ad (entget an)) ;Get data (= "ATTRIB" (cdr (assoc 0 ad))) ;Check if attribute ) ;_ end of and (setq s (cdr (assoc 1 ad))) ;Get text value ;(princ"---INW---") (princ s) (setq ad (subst (vl-list* 1 "") (assoc 1 ad) ad)) ;Clear the attribute's value (entmod ad) ;Modify the entity (setq ad (subst (vl-list* 1 s) (assoc 1 ad) ad)) ;Set the attribute's value back to only the text (entmod ad) ;Modify the entity (entupd an) ;Update screen to show change (setq an (entnext an)) ;Get next entity ) ;_ end of while );_end LayoutCrt (setvar "CTAB" layoutOld) ) Quote
Danielm103 Posted March 28, 2024 Posted March 28, 2024 Note: other APIs (ARX or .NET), have a function convertFieldToText https://help.autodesk.com/view/OARX/2024/ENU/?guid=OARX-RefGuide-AcDbText__convertFieldToText I posted a sample with python here https://www.cadtutor.net/forum/topic/82756-convert-fields-in-blocks-to-test-with-python/ Quote
BIGAL Posted March 28, 2024 Posted March 28, 2024 (edited) Are you sure this is working (setq ss (ssget "_X" (list '(2 . "myblock") (cons 410 layoutCrt)))) what is your block name I doubt its "myblock" I usually wrap a ssget in an IF to make sure it exists. (if (= ss nil) (alert "selection set has not been made skipping program") (progn ............. ) ) Edited March 28, 2024 by BIGAL Quote
pttr Posted April 2, 2024 Author Posted April 2, 2024 (edited) On 3/29/2024 at 12:28 AM, BIGAL said: Are you sure this is working (setq ss (ssget "_X" (list '(2 . "myblock") (cons 410 layoutCrt)))) what is your block name I doubt its "myblock" I usually wrap a ssget in an IF to make sure it exists. (if (= ss nil) (alert "selection set has not been made skipping program") (progn ............. ) ) Yeah every file i will use this Lisp on has the block and "myblock" is an example, but it does (sort of) work! Nice, the if statement might add a layer of security but i dubt it will fix the issue. The file has about 15 layouts with 1 block in each layout Edited April 2, 2024 by pttr Quote
pttr Posted April 2, 2024 Author Posted April 2, 2024 Ah it´s solved! Problem is the layouts have to be "Read/Regenerated" before I can make changes to them. Used this https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/activate-each-layout-one-by-one-in-all-open-drawing/m-p/8050459/highlight/true#M369795 Quote
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.