Change Text Height and Text Style based on Layer Name

Change Text Height and Text Style based on Layer Name

janu319
Advocate Advocate
1,978 Views
10 Replies
Message 1 of 11

Change Text Height and Text Style based on Layer Name

janu319
Advocate
Advocate

Hallo,

 

I'm looking for lisp that will change the Text Height and Text Style of all the Text/MText on a particular layer. 

I tried to find in the old posts but could not find any. 

 

Layer NameText StyleText Height
Layer1Style12,25
Layer2style22
Layer3style32,25
Layer4style21,5

 

Text height set to 0 for all the text styles in the settings. 

 

Thanks in advance.

Janu

0 Likes
Accepted solutions (2)
1,979 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

Here is a function and example of usage.

Make sure that the layer is not locked.

 

(defun :TextHeightByLayer (layer style height / s i)
  (if (setq s (ssget "_A" (list '(0 . "*TEXT")(cons 7 style)(cons 8 layer))))
    (repeat (setq i (sslength s))
      (setpropertyvalue (ssname s (setq i (1- i))) "Height" height))))

 

(:TextHeightByLayer "Layer1" "TextStyle1" 2.25)

 

Or is you want to make all at once, make another function...

(defun c:ChangeAllTexts () 

(:TextHeightByLayer "Layer1" "TextStyle1" 2.25)

...

(princ)

)

 

 

0 Likes
Message 3 of 11

janu319
Advocate
Advocate

Thank you for the quick response, but after loading the code, I got NIL response. 

There is a layer "Layer1" and also a text style "Text Style1" in my drawing. Please see attached image. 

Am I doing something wrong? 

 

 
 

 

0 Likes
Message 4 of 11

ВeekeeCZ
Consultant
Consultant

Looks just about right.

It does not change a style at all. You cannot have a style of one name with different properties for different layer.

It simply changes the text's property.

0 Likes
Message 5 of 11

janu319
Advocate
Advocate

But there is no change in Text properties as well. Text height also not changed. 

0 Likes
Message 6 of 11

ВeekeeCZ
Consultant
Consultant

Then you're doing something wrong.

0 Likes
Message 7 of 11

janu319
Advocate
Advocate

 I just copy paste the code you provided in to the command prompt, and nothing happened in the drawing. Please see the attached screen shot.  

I'm using autoCAD 2018, in case if that matters, I think not. 

0 Likes
Message 8 of 11

ВeekeeCZ
Consultant
Consultant
Accepted solution

Ok, I see your point. I didn't understand your goal about style right.

 

(defun :TextHeightByLayer (layer style height / s i d)
  (if (and (or (setq d (tblobjname  "STYLE" style))
	       (prompt (strcat "Error: TextStyle '" style "' not found in the drawing!")))
	   )
    (if (setq s (ssget "_A" (list '(0 . "*TEXT") (cons 8 layer))))
      (repeat (setq i (sslength s))
	(setpropertyvalue (ssname s (setq i (1- i))) "TextStyleID" d)
	(setpropertyvalue (ssname s i) "Height" height))))
  (if s (princ (strcat (itoa (sslength s)) " texts found.")))
  (princ))

 

Message 9 of 11

CodeDing
Advisor
Advisor
Accepted solution

@janu319 ,

 

This code is not working because there was some misinterpretation between @ВeekeeCZ and you.

Bee's code takes text on a LAYER and STYLE then changes HEIGHT.

You are looking for a code that takes LAYER then changes STYLE and HEIGHT.

So, when you run Bee's code, it does not find any text with the style of "TextStyle1" (thank you for the picture btw).

 

I made some edits and I think this code is what you're looking for. I have not tested yet, but should work.

 

(defun SetStyleHeightByLayer (lyr sty hgt / ssT cnt e)
  (if (and (setq ssT (ssget "_A" (list '(0 . "*TEXT") (cons 8 lyr))))
           (setq sty (tblobjname "STYLE" sty)))
    (repeat (setq cnt (sslength ssT))
      (setq e (ssname ssT (setq cnt (1- cnt))))
      (setpropertyvalue e "TextStyleId" sty)
      (setpropertyvalue e "Height" hgt) t ;<--this 't' is return value
    );repeat
  ;else
    (prompt "\nNothing performed. Check for text and text style. Then try again.")
  );if
);defun

 

 

EDIT:

Looks like BeekeeCZ just posted update! Sorry, not trying to intrude. Now you should have 2 codes to approach with!

 

Best,

~DD

Message 10 of 11

janu319
Advocate
Advocate

Thank you, this is what exactly I'm looking for. 

0 Likes
Message 11 of 11

janu319
Advocate
Advocate

Thank you for your time, both codes are working fine. 🙂

0 Likes