Jump to content

error in the lisp


CADWORKER

Recommended Posts

(Defun c:Tx2 (/ *error* lay ent pnt pnt2)
        (while (setq txt (car (ssget "\nSelect text: ")))
        (setq pnt (trans (cdr (assoc 10 (entget txt))) 0 1))
    (setpropertyvalue txt "Height" 2)
)
        (*error* "end")
  )

 

Hi, I had a lisp to select each text to change the height, but when I replaced the entget to ssget its not working.

How to fix this. Please let me know or any other lisp code for doing this.

Thanks.

Link to comment
Share on other sites

entsel returns the entity name and the point of our curser. (entity<asfasdf> (x, y))

ssget returns a list of entity names and the last point selected with the mouse.

 

The code would work but for only the first entity in the selection set. You need to step thought the entire list of entity names for it to work properly. I like using the foreach function.

 

Code untested but should work.

(defun c:TH2 (/ ss pnt)
  (while (setq ss (ssget ":L" '((0 . "TEXT") ))) ;Selects only text on unlocked layers
    (foreach txt (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
      (setq pnt (trans (cdr (assoc 10 (entget txt))) 0 1))
      (setpropertyvalue txt "Height" 2)
    )
  )
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

(defun c:tx2 ( / ss ssl index ent obj )
  (vl-load-com)                                   ; Loads Visual LISP extensions to AutoLISP
  (princ "\n Select Texts to Change Height to 2") ; ssget doesn't take small talk as an argument, 
                                                  ; so if there's anything you want to say to the user, write it separately at the front.
  (setq ss (ssget ":L" '((0 . "TEXT"))))          ; Insert TEXTs from among those selected by the user into the variable ss. ":L" is an option to exclude locked layers.
  (setq ssl (sslength ss))                        ; Find out how many objects in ss.
  (setq index 0)                                  ; To use it within repeat, first declare the index as 0.
  (repeat ssl                                     ; Repeat as many times as ss.
    (setq ent (ssname ss index))                  ; get only 1 item from ss into ent with ssname. 
                                                  ; When extracting something from a list (nth index list), note that the order is different.
    (setq obj (vlax-ename->vla-object ent))       ; You can directly edit the ent list by subst, assoc, and cons or something.
                                                  ; but since it is hard for starter to memorize the list numbers what is that property. so convert to object.
    (vlax-put-property obj 'height 2)             ; Set the height value to 2 in this obj.
    (setq index (+ index 1))                      ; Add 1 to index, place it back in index, and end the turn. After this, 
                                                  ; it goes up again to (setq ent (ssname ss index)) by repeat.
  )
  (princ)                                         ; to silence the parrot-like repetition of the last value.
)

 

 

I don't know what setpropertyvalue or pnt pnt2 are for,

but if you only change the text height to 2,

 

Since mhupp already gave a good answer with foreach, I will write with repeat.

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

17 hours ago, exceed said:

I don't know what setpropertyvalue or pnt pnt2 are for.

 

Yeah don't know what pnt was for either just left it in. But for setpropertyvalue its a Autocad Function that sets values of an entity. you can feed it enames as well with out having to convert over to vla-objects. You can also use getpropertyvalue to pull things like attributes strings from blocks. with just one line of code.

  • Like 1
Link to comment
Share on other sites

On 9/20/2023 at 7:21 PM, mhupp said:

entsel returns the entity name and the point of our curser. (entity<asfasdf> (x, y))

ssget returns a list of entity names and the last point selected with the mouse.

 

The code would work but for only the first entity in the selection set. You need to step thought the entire list of entity names for it to work properly. I like using the foreach function.

 

Code untested but should work.

(defun c:TH2 (/ ss pnt)
  (while (setq ss (ssget ":L" '((0 . "TEXT") ))) ;Selects only text on unlocked layers
    (foreach txt (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
      (setq pnt (trans (cdr (assoc 10 (entget txt))) 0 1))
      (setpropertyvalue txt "Height" 2)
    )
  )
  (princ)
)

 

Can we include adjusting the text width to 0.8?

Link to comment
Share on other sites

2 hours ago, CADWORKER said:

Can we include adjusting the text width to 0.8?

(defun c:TH2 (/ ss pnt)
  (while (setq ss (ssget ":L" '((0 . "TEXT") ))) ;Selects only text on unlocked layers
    (foreach txt (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
      (setq pnt (trans (cdr (assoc 10 (entget txt))) 0 1))
      (setpropertyvalue txt "Height" 2) 
      (setpropertyvalue txt "WIDTHFACTOR" 0.8) ;  ADD THIS LINE 
    )
  )
  (princ)
)

 

And for another routine 

 

(vlax-put-property obj 'height 2)             ; Set the height value to 2 in this obj.
    (vlax-put-property obj 'WIDTHFACTOR 0.8)      ; Set the WIDTH value to .8 in this obj.    <<<<    ADD THIS LINE

 

Edited by asos2000
  • Like 2
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...