ILoveMadoka Posted March 24, 2023 Share Posted March 24, 2023 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.. Quote Link to comment Share on other sites More sharing options...
rlx Posted March 24, 2023 Share Posted March 24, 2023 where do you actualy put MyStyle to the object? Quote Link to comment Share on other sites More sharing options...
ILoveMadoka Posted March 24, 2023 Author Share Posted March 24, 2023 I do not know how to do that... Quote Link to comment Share on other sites More sharing options...
ILoveMadoka Posted March 24, 2023 Author Share Posted March 24, 2023 (edited) 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 March 24, 2023 by ILoveMadoka Quote Link to comment Share on other sites More sharing options...
rlx Posted March 24, 2023 Share Posted March 24, 2023 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) ) Quote Link to comment Share on other sites More sharing options...
rlx Posted March 24, 2023 Share Posted March 24, 2023 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) ) Quote Link to comment Share on other sites More sharing options...
ILoveMadoka Posted March 24, 2023 Author Share Posted March 24, 2023 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.. Quote Link to comment Share on other sites More sharing options...
rlx Posted March 24, 2023 Share Posted March 24, 2023 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 Quote Link to comment Share on other sites More sharing options...
ILoveMadoka Posted March 27, 2023 Author Share Posted March 27, 2023 Thank you again! Very appreciated. Quote Link to comment Share on other sites More sharing options...
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.