;;;C:LINELEGEND ;;;Function to label a selection set of lines with user supplied text ;;;Autoscales text to plot at current value of DIMTXT ;;;Text may be placed above, below or centered on lines. ;;;Works on a selection set of lines only, not polylines. ;;;If the text is centered on the lines, the lines are broken around the text. ;;;Calls TRIM to break the lines, so if the text is longer than the line, ;;;the line will not be broken, but text will be written over the line segment. ;;;Uses current value of TEXTSTYLE for text style. ;;;Option to use a predefined layer or current layer - see below ;;; ;;;Copyright © 2001 by Herman Mayfarth ;;;Provided as is and with all faults. No warranty of any kind. ;;;Permission granted to freely use and redistribute without fee, ;;;provided copyright notice and disclaimer of warranty remain intact. ;;;Author: h_mayfarth@tktn.com http://www.tktn.com ;;; ;;;Created:5 July 1999 ;;;Version:1.1 9 Feb. 00 ;;;Added option for Above/Below lines ;;;Changed text style from DIMTXSTY to TEXTSTYLE ;;;Version 1.2 2 Aug 01 ;;;Added option to center text on lines & break lines around text ;;;No error handler. ;;;Written for and tested with AutoCAD R14.01 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; GLOBAL variable for line labels ;; change to suit your needs ;; comment out the following ONE line to place text on the current layer (setq lbl_layer "LINE_LEGEND") (if (not lbl_layer) (setq lbl_layer (getvar "CLAYER"))) (defun C:LINELEGEND( / ss1 ss2 i n pt1 pt2 midpt langle sstyle sf txtht txtsty ename gap legend inspt elist vpht curvpt txtloc vert) ;;start an undo group (command "_.UNDO" "BEGIN") ;; handle nonsense case of zero dimscale in tiled space (if(and(= 0 (getvar "DIMSCALE"))(= 1 (getvar "TILEMODE"))) (progn((alert " Zero Dimension Scale\n Set Tilemode = 0 or Use a Non-Zero Dimension Scale")(exit)))) ;; calculate text scale factor (setq curvpt (getvar "CVPORT")) (cond ((= 1 (getvar "TILEMODE")) (setq sf (getvar "DIMSCALE")));tiled space ((= 1 curvpt) (setq sf 1.0));in paper space ;; in a viewport (T (setq ss2 (ssget "X" (list '(0 . "VIEWPORT") (cons 69 curvpt)));select vport vpht (cdr(assoc 41 (entget(ssname ss2 0))));set vp height sf (/ (getvar "VIEWSIZE") vpht));calc scale factor );end default );end cond ;; calculate text height (setq txtht (* (getvar "DIMTXT") sf)) ;; get current text style (setq txtsty (getvar "TEXTSTYLE")) (setq sstyle (tblsearch "style" txtsty)) (setq gap (getvar "DIMGAP")) ;;prompt user for text location (initget "Above Below Center") (setq txtloc (getkword "\nText Location: Above Below
: ")) (if (null txtloc) (setq txtloc "Center")) ;;get label text (while (or(not legend)(= legend "")) (setq legend (getstring T "\nEnter Text Label: ")) ) ;; get lines to label (princ "\nSelect Lines to Label:") (setq ss1 (ssget '((0 . "LINE"))) n (sslength ss1)) (princ (strcat "\n" (itoa n) " lines selected.")) ;; main processing loop (setq i 0) ;initialize loop counter (while (< i n) (setq ename (ssname ss1 i) elist (entget ename) pt1 (cdr (assoc 10 elist)) pt2 (cdr (assoc 11 elist)) midpt (mapcar '(lambda (x y) (/ (+ x y) 2.0)) pt1 pt2) langle (min (angle pt1 pt2) (angle pt2 pt1)) ) (if (> langle (/ pi 2)) (setq langle (+ langle pi)));langle 2nd quadrant (cond ((= txtloc "Above") (setq inspt (polar midpt (+ langle (/ pi 2)) (* gap sf)) vert 1 ) ) ((= txtloc "Below") (setq inspt (polar midpt (- langle (/ pi 2)) (* gap sf)) vert 3 ) ) ((= txtloc "Center") (setq inspt midpt vert 2 ) ) );cond ;;entmake the text (entmake (list '(0 . "TEXT") (cons 1 legend) ;text string (cons 7 txtsty) ;text style (cons 8 lbl_layer) ;layer (cons 10 inspt) ;insertion point (cons 11 inspt) ;req'd for 72 & 73 group (cons 40 txtht) ;text height (assoc 41 sstyle) ;width factor (cons 50 langle) ;text insertion angle (cons 51 (cdr(assoc 50 sstyle)));obliquing angle (assoc 71 sstyle) ;text generation flags '(72 . 1) ;horiz. alignment (cons 73 vert) ;vertical alignment ) ) ;;if the text is centered, break the line (if (= vert 2) (command "_.TRIM" (entlast) "" (list ename (trans midpt 0 1)) "") ) (setq i (1+ i)) );end main ;;terminate undo group (command "_.UNDO" "END") (princ) );end LINELEGEND ;;load prompts (princ "\nLINELGND v1.2 Copyright © 2001 Herman Mayfarth") (princ "\nType LINELEGEND to label lines with text") (princ)