AutoLISP: Make a New Linetype with Text

It seems that this blog has gotten a lot of interest since I started posting AutoLISP code.

Here is great routine that creates a new LineType with text. If you have ever tried creating a linetype with text on your own, you know how frustrating it can be. This Lisp routine makes this task effortless and will make you look like a “wizard” in the eyes of the other drafters.

In the animated picture, the linetype dropdown list would not record when I recorded this. I don’t know why… What I was showing you is that before the routine, there was no linetype called “Greg” and then after the routine, I made the linetype “Greg” current before drawing the lines.

In the example I did the following:

  • MAKELT <enter> to start
  • GREG <enter> This is the text for the new linetype. This routine works great for up to about 6 text characters. If you use more than about six characters you may have to manually adjust the spacing so that the line does not overlap your text.
  • <enter> to accept the default text height of 1
  • I then set the linetype “Greg” current and drew the lines.

 

 

 

 

 

 

 

 

 

 

 

 


;;; AUTHOR

;;; 2010 Ron Perez

;;; Updated original code posted to Cadalyst http://cadtips.cadalyst.com/2d-operations/create-custom-linetype

;;; User can specify text height now

;;; Gap that text is placed in is much more accurate vertical and horizontal

;;; Text in linetype now uses the current textstyle (getvar 'textstyle)

;;; Assembled linetype definition is printed to command line

;;; Checks for existing linetype and prompts user to redefine

;;; Variables dashlen, txtgap, txthgt can be modified in code for user preference ... Maybe someday I'll create a front end for this :P

;;; *Known Issues* Current textstyle that has a fixed height creates wonky linetypes, code does not catch invalid characters for linetype names

(defun c:makelt

(/ dashlen exprt file fn ltdef str strw txtgap txthgt txtstyle rjp-txtwdth _rtos)

(vl-load-com)

(defun rjp-txtwdth (str hgt style / d e pts)

;;Returns textstring width, gap from insertion point to start of text, and height

(if	(setq e	(entmakex (list	'(0 . "TEXT")

'(100 . "AcDbEntity")

'(100 . "AcDbText")

'(8 . "rjp-tmptxtlayer")

'(10 0. 0. 0.)

(cons 40 hgt)

(cons 1 str)

(cons 7 style)

)

)

)

(progn (setq pts (textbox (entget e)))

(setq d (distance (car pts) (list (caadr pts) (cadar pts))))

(entdel e)

)

)

(list d (caar pts) (- (cadadr pts) (abs (cadar pts))))

)

(defun _rtos (real) (rtos real 2 6))

;;Set these numbers to preference

(setq dashlen 0.25)

(setq txtgap 0.025)

(setq txthgt 0.1)

;;-------------------------------

(setq txtstyle (getvar 'textstyle))

(if (and (setq str (getstring t "\nEnter string to use in linetype: "))

(not (zerop (strlen str)))

(setq txthgt

(cond

((getreal (strcat "\nEnter height of text [<" (rtos txthgt 2 3) ">]: "))

)

(txthgt)

)

)

(setq strw (rjp-txtwdth str txthgt txtstyle))

(setq file

(strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) "_mylt.lin")

)

(setq fn (open file "w"))

(setq exprt (getvar 'expert))

)

(progn

(setq ltdef (strcat "\n*"

str

", ---"

str

"---\n"

"A,"

(_rtos dashlen)

",-"

(_rtos (abs (- txtgap (cadr strw))))

",[\""

str

"\","

txtstyle

",S="

(_rtos txthgt)

",R=.0,X=-.0,Y=-"

(_rtos (* (caddr strw) 0.5))

"],-"

(_rtos (+ txtgap (car strw) (cadr strw)))

)

)

(write-line ltdef fn)

(close fn)

(setvar 'expert 5)

(cond ((not (tblsearch "ltype" str))

(command "._-linetype" "load" "*" file "")

(princ ltdef)

(princ (strcat "\nLinetype " str " loaded..."))

)

((and (not (initget 1 "Y N"))

(eq (getkword "\nLinetype exists... Reload it? [Y/N]") "Y")

)

(command "._-linetype" "load" "*" file "")

(vla-regen (vla-get-activedocument (vlax-get-acad-object)) acactiveviewport)

(princ ltdef)

(princ (strcat "\nLinetype " str " reloaded..."))

)

)

(setvar 'expert exprt)

(vl-file-delete file)

)

)

(princ)

)

Advertisement

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Customization, Text, TIPS, Uncategorized. Bookmark the permalink.

4 Responses to AutoLISP: Make a New Linetype with Text

  1. Autumn Friday says:

    I have loaded the lisp routing and it appears to be working, however, the text is offset from my line. Do you know how to fix this?

  2. Hitesh Prajapati says:

    I tried this auto lisp & its work perfect as i need.
    Only one problem I face, when create text line type and draw line in right to left direction (Reverse), line type text is flip.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s