Jump to content

Fields inside a block to text


pttr

Recommended Posts

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 by pttr
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 by BIGAL
Link to comment
Share on other sites

Posted (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 by pttr
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...