Jump to content

What is wrong with my code..?


ILoveMadoka

Recommended Posts

I know other routines abound but was attempting my own..

Can someone please point out what is wrong (and the fix to my code if possible)?

 

Its my attempt to write code that does this:

 

      Change all selected text/mtext to MyTextStyle

 

(defun c:ChangeTextStyle ()
;;Change all selected text/mtext to MyTextStyle
  (setq ss (ssget)) ;Prompt the user to select text and mtext objects
  (if ss ;If the user selected something
    (progn
      (setq n (sslength ss)) ;Get the number of selected objects
      (setq i 0)
      (while (< i n) ;Loop through all selected objects
        (setq obj (ssname ss i)) ;Get the object name
        (setq objType (cdr (assoc 0 (entget obj)))) ;Get the object type
        (if (or (equal objType "TEXT") (equal objType "MTEXT")) ;If the object is text or mtext
          (progn
            (setq textStyleObj (vla-item (vla-get-TextStyleTable (vla-get-ActiveDocument (vlax-get-acad-object))) "MyTextStyle")) ;Get the text style object
            (vla-put-TextStyle (vlax-ename->vla-object obj) textStyleObj) ;Set the text style of the object
          )
        )
        (setq i (1+ i)) ;Increment the counter
      )
      (princ "\nSelected text and mtext objects have been changed to MyTextStyle.") ;success message
    )
    (princ "\nNothing selected. Please try again.") ;error message
  )
  (princ)
)


Thx..

Link to comment
Share on other sites

my attempt is here..
 

(setq textStyleObj (vla-item (vla-get-TextStyleTable (vla-get-ActiveDocument (vlax-get-acad-object))) "MyTextStyle")) ;Get the text style object
            (vla-put-TextStyle (vlax-ename->vla-object obj) textStyleObj) ;Set the text style of the object

 

Edited by ILoveMadoka
Link to comment
Share on other sites

totaly untested on the toilet so I wouldn't be surpriced if it doesn't work 😄

(defun c:ChangeTextStyle ( / ss i MyTextStyle obj ) (vl-load-com)
  (if (and (setq ss (ssget '((0 . "*TEXT")))) (setq i 0)
           (setq MyTextStyle (vla-item (vla-get-TextStyles (vla-get-ActiveDocument (vlax-get-acad-object))) "MyTextStyle")))
    (progn (while (setq obj (ssname ss i)) (vla-put-TextStyle (vlax-ename->vla-object obj) MyTextStyle)(setq i (1+ i)))
      (princ "\nSelected text and mtext objects have been changed to MyTextStyle."))
    (princ "\nNothing selected. Please try again.")
  )
  (princ)
)

 

Link to comment
Share on other sites

I'm a 💩 , this should work better 

 

(defun c:t1 ( / ss i MyTextStyle obj ) (vl-load-com)
  (if (and (setq ss (ssget '((0 . "*TEXT")))) (setq i 0) (tblobjname "style" "MyTextStyle"))
    (progn
      (while (setq ent (ssname ss i))
        (if (vlax-property-available-p (setq obj (vlax-ename->vla-object ent)) "StyleName")
          (vla-put-stylename obj "MyTextStyle"))
        (setq i (1+ i))
      )
      (princ "\nSelected text and mtext objects have been changed to MyTextStyle.")
    )
    (princ "\nNothing selected. Please try again.")
  )
  (princ)
)

 

 

Link to comment
Share on other sites

That works great..

Where was I wrong?

Looks like you wrote an entirely new proggie..

Was I even close??

 

Thank you...

 

Tryin to learn here..

Link to comment
Share on other sites

I think it was an excellent attempt!

 

only here :

(if (or (equal objType "TEXT") (equal objType "MTEXT")) ;If the object is text or mtext
  (progn
    (setq textStyleObj (vla-item (vla-get-TextStyleTable (vla-get-ActiveDocument (vlax-get-acad-object))) "MyTextStyle")) ;Get the text style object
    (vla-put-TextStyle (vlax-ename->vla-object obj) textStyleObj) ;Set the text style of the object
  )
 )

 

you use vla-put-TextStyle and it should have been vla-put-StyleName (but don't worry , after I finished my 💩 and went back to my machine and tested it and found it not working I google'd how to change text style and found I needed StyleName 😁

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