Jump to content

Attribute Tag Change position (Text alignment X or Y)


Recommended Posts

Posted

Hello Everyone, 

 

Hoping somebody could help me out. 

I have ask an AI(free version chatgpt) to create an LSP code to change specific block name and attribute tag.

It has choices as shown the code below.

 

(defun c:EditBlockAttribute ()
  (setq blkName (getstring T "\nEnter block name: ")
        attrTag (strcase (getstring T "\nEnter attribute tag: "))
        choice (getint "\nSelect option: 1-Change Text Height, 2-Change Layer, 3-Change Position: ")))
  
  (setq filter (list (cons 0 "INSERT") (cons 2 blkName)))
  (setq ss (ssget "X" filter)) ;; Select all instances of the specified block
  
  (if ss
    (progn
      (setq i 0)
      (while (< i (sslength ss))
        (setq ent (ssname ss i))
        (setq blkObj (vlax-ename->vla-object ent))
        (setq blkEnts (vlax-invoke blkObj 'GetAttributes)) ;; Get block attributes
        
        (foreach obj blkEnts
          (if (equal (strcase (vlax-get obj 'TagString)) attrTag)
            (EditAttribute obj choice)
          )
        )
        (vlax-release-object blkObj)
        (setq i (1+ i))
      )
      (princ "\nAttribute updated successfully.")
    )
    (princ "\nNo matching blocks found."))
  (princ)
)

(defun EditAttribute (obj choice)
  (cond
    ((= choice 1)
     (setq newHeight (getreal "\nEnter new text height: "))
     (vlax-put obj 'Height newHeight)
    )
    ((= choice 2)
     (setq newLayer (getstring T "\nEnter new layer name: "))
     (vlax-put obj 'Layer newLayer)
    )
    ((= choice 3)
     (setq moveDist (getreal "\nEnter move distance (use positive for right, negative for left): ")))
     (if (vlax-property-available-p obj 'InsertionPoint)
       (progn
         (setq insPt (vlax-get obj 'InsertionPoint))
         (setq newPt (list (+ (car insPt) moveDist) (cadr insPt) (caddr insPt)))
         (vlax-put obj 'InsertionPoint newPt)
       )
       (princ "\nError: Attribute does not have an insertion point.")
     )
    )
    (t (princ "\nInvalid option selected."))
  )
)

 

Unfortunately, it didn't push through. I don't know what happen or how to fix this code. 

Need help

Posted

I've just skimmed through your code. But there's one thing you need to know: when you specify the name of the target layer for the block, it must exist. If it doesn't, your code won't work.

Posted
1 minute ago, GLAVCVS said:

I've just skimmed through your code. But there's one thing you need to know: when you specify the name of the target layer for the block, it must exist. If it doesn't, your code won't work.

 

Hi GLAVCVS thank you for noticing my post.  yes it is existing in the drawing, but it didn't push through. even I input the value for the option 3. 

Posted

Hi

 

here's the cad file

 

i guess, i got a little bit of dizzy. It is now moving but it ask me twice when I choose the option 3.

Supposed to be only once and the command terminated after I enter the desired movement.

SAMPLEFILE.dwg

Posted

Your code had several badly placed parentheses.
You should check your code with the debugger.
More things: your code makes a selection of all the blocks that match the specified filters. And it requests parameters for each block to be modified, but without displaying it on the screen.
I think it is necessary to add a call to the 'zoom' command to move the graphic window to the position of the next block to be modified.
So I have added that code. But I am not sure if your version of AutoCAD will correctly understand my calls to the 'zoom' command.
You should check it yourself

 

(defun c:EditBlockAttribute ()
  (setq	blkName	(getstring T "\nEnter block name: ")
	attrTag	(strcase (getstring T "\nEnter attribute tag: "))
	choice	(getint
		  "\nSelect option: 1-Change Text Height, 2-Change Layer, 3-Change Position: "
		)
  )


  (setq filter (list (cons 0 "INSERT") (cons 2 blkName)))
  (setq ss (ssget "X" filter))
  ;; Select all instances of the specified block

  (if ss
    (progn
      (setq i 0)
      (while (< i (sslength ss))
	(setq ent (ssname ss i))
	(setq blkObj (vlax-ename->vla-object ent))
	(setq blkEnts (vlax-invoke blkObj 'GetAttributes))
	;; Get block attributes

	(VL-CMDF "_zoom" "c" (cdr (assoc 10 (entget ent))) "30")
	
	(foreach obj blkEnts
	  (if (equal (strcase (vlax-get obj 'TagString)) attrTag)
	    (EditAttribute obj choice)
	  )
	)
	(vlax-release-object blkObj)
	(setq i (1+ i))
      )
      (princ "\nAttribute updated successfully.")
    )
    (princ "\nNo matching blocks found.")
  )
  (princ)
)


(defun EditAttribute (obj choice)
  (cond
    ((= choice 1)
     (setq newHeight (getreal "\nEnter new text height: "))
     (vlax-put obj 'Height newHeight)
    )
    ((= choice 2)
     (setq newLayer (getstring T "\nEnter new layer name: "))
     (vlax-put obj 'Layer newLayer)
    )
    ((= choice 3)
     (setq moveDist
	    (getreal
	      "\nEnter move distance (use positive for right, negative for left): "
	    )
     )
;;;    )
     (if
       (vlax-property-available-p obj 'InsertionPoint)
	(progn
	  (setq insPt (vlax-get obj 'InsertionPoint))
	  (setq	newPt (list (+ (car insPt) moveDist)
			    (cadr insPt)
			    (caddr insPt)
		      )
	  )
	  (vlax-put obj 'InsertionPoint newPt)
	)
	(princ
	  "\nError: Attribute does not have an insertion point."
	)
     )
    )
    (t (princ "\nInvalid option selected."))
  )
)

 

Posted

@GLAVCVS

 

It work, this is what i'm looking for.

 

Thank you very much sir. I will take note the comments above, It really help me 

 

once again thank you.

Posted

As mentioned above, "Your code had several badly placed parentheses." if you use Notepad++ to write code it has a  parentheses checker built in and its free. It also has a addon ActiveX that allows you run the code in you are writing. 

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