I hate doing things I can make my computer do for me. My adventures in customizing and automating computers. Primarily with AutoCAD and Microsoft Office.

Monday, August 27, 2012

Here are some Text Justification Routines using a core routine.

This link to Justify_Text_Tools.lsp (originally by Ryan Pace) has routines for setting the justification of text/mtext objects.  The included routines are:
(defun c:JTL () (JustifyText "TL")(princ));Justify Text to top left
(defun c:JTR () (JustifyText "TR")(princ));Justify Text to top right
(defun c:JTC () (JustifyText "TC")(princ));Justify Text to top center
(defun c:JML () (JustifyText "ML")(princ));Justify Text to middle left
(defun c:JMR () (JustifyText "MR")(princ));Justify Text to middle right
(defun c:JMC () (JustifyText "MC")(princ));Justify Text to middle center
(defun c:JBL () (JustifyText "BL")(princ));Justify Text to bottom left
(defun c:JBR () (JustifyText "BR")(princ));Justify Text to bottom right
(defun c:JBC () (JustifyText "BC")(princ));Justify Text to bottom center
As you can see, each of the routines calls the JustifyText routine with an argument telling it which justification to use.  This allows us to use the one core routine for multiple tools so if we ever have to update the routines we only have to update the JustifyText sub-routine.
(defun JustifyText(mode / *ERROR* cmdecho)
  (defun *ERROR*(msg)
    (command)
    (command)
    (command)
    (if cmdecho (setvar "cmdecho" cmdecho))
    (princ msg)
  )
  (setq cmdecho (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (cond
    ((null (setq ss1 (vl-catch-all-apply 'ssget (list '((0 . "Text,Mtext,Attdef"))))))
      nil
    )
    ((vl-catch-all-error-p ss1)
      (princ (strcat "\nERROR|" (vl-catch-all-error-message ss1)))
      nil
    )
    ((and
        (= 'PICKSET (type ss1))
        (= 0 (sslength ss1))
      )
      nil
    )
    (T(command "_justifytext" ss1 "" mode))
  )
  (setvar "cmdecho" cmdecho)
  ss1
)

Thursday, August 23, 2012

EZOffset and leveraging a code snippet to create multiple routines

It's been a while.

Since I am emailing tips to my coworkers I thought I would start posting some of the background on those tips.

This is a link to EZOFFSET.LSP which has the following aliases for offsetting specific distances:


;;;  Offsets by sixteenths of an inch  -  OF (the "F" is for fraction") followed by the number of 16ths you want to offset
OF1        offset 1/16"
OF2        offset 2/16"   (1/8")
OF3        offset 3/16"
OF4        offset 4/16"   (1/4")
OF5        offset 5/16"
OF6        offset 6/16"   (3/8")
OF7        offset 7/16"
OF8        offset 8/16"   (1/2")
OF9        offset 9/16"
OF10      offset 10/16"  (5/8")
OF11      offset 11/16"
OF12      offset 12/16"  (3/4")
OF13      offset 13/16"
OF14      offset 14/16"  (7/8")
OF15      offset 15/16"

;;;  Offsets by inches - O followed by the number of inches you want to offset.  This has some variations shown below in red.
O1          offset 1"
O1F4      offset 1 4/16"  (1 1/4")
O1F8      offset 1 8/16"  (1 1/2")
O1F12   offset 1 12/16" (1 3/4")
O2          offset 2"
O3          offset 3"
O3F8      offset 3 8/16" (3 1/2")
O4          offset 4"
O5          offset 5"
O5F8      offset 5 8/16" (5 1/2")
O6          offset 6"
O7          offset 7"
O8          offset 8"
O9          offset 9"
O10        offset 10"
O11        offset 11"
O12        offset 12"
O13        offset 13"
O14        offset 14"
O15        offset 15"
O16        offset 16"
O17        offset 17"
O18        offset 18"
O19        offset 19"
O20        offset 20"
O21        offset 21"
O22        offset 22"
O23        offset 23"
O24        offset 24"
O30        offset 30"
O36        offset 36"
O42        offset 42"
O48        offset 48"
O60        offset 60"
O72        offset 72"
O84        offset 84"
O96        offset 96"
O108      offset 108"  (9')
O120      offset 120"  (10')
O132      offset 132"  (11')
O144      offset 144"  (12')
O156      offset 156"  (13')
O168      offset 168"  (14')
O180      offset 180"  (15')
O240      offset 240"  (20')
O360      offset 360"  (30')
O480      offset 480"  (40')

The meat of the code is thus:

(defun ezoffset(
                dist
                /
                offsetdist
                )
  (setq offsetdist (getvar "offsetdist"))
  (setvar "offsetdist" dist)
  (command "offset" dist)
  (while (wcmatch (getvar "cmdnames") "*offset*")
    (command pause)
    )
  (setvar "offsetdist" offsetdist)
  (princ)
  )
(princ)
)
and can be called, like this:

(defun c:OF1()(ezoffset 0.0625))  ;offset 1/16"
(defun c:OF2()(ezoffset 0.125))   ;offset 2/16"   (1/8")



The naming conventions I have used for these routines are arbitrary, but they work for me.

Let me know if you find these useful.