Skip to main content
Working from scratch, following simplicity

A LISP for printing a CAD drawing changing colours and texts

In the following article, I described a piece of LISP code that once loaded a DXF or DWG file, enlarges some text, changes the colour of layers and prints a PDF fitting it to the A3 format and orientates it by the drawing dimensions. You can easily adapt it to your purposes.

I wrote it because I was tired to do a lot of repetitive operations when I had to print an output produced by a closed software in use in my Company. With LISP I am able to reduce by fifty times all of the processes, with no errors and reproducing the exact tasks that my mind sometimes forgets.

I usually change the DXF generated by that software only for two reasons:

  1. a better and more readable print;
  2. a PDF with the Text-searchable functionality. That closed software can produce PDF output but without this feature.

In the past, I only open the exported drawing in my CAD software and I printed it right away, as you can see below the output needs some work in order to become a good printed draw.

A LISP for printing a DXF changing colours and texts

In detail every time I load this type of DXF, I have to:

  • enlarge all the texts in a layer (so I can better read them);
  • freeze a layer (useless and with a very bad return in the prints);
  • change the colour of some layers to adapt to my print style;
  • restore all colour properties of objects to BYLAYER;
  • add a text at the bottom with the name of the file itself;
  • create a PDF (assigning the A3 format, my print style and the correct orientation and size in function of the drawing extension).

But now I can make all these operations with a single command defined in the LISP script.

And it is useful when I have 10 or 12 drawings to convert one or two times a day, saving me to do repetitive and boring tasks. To put it more simply, before posting my script I want to share a video comparison: in the top half the LISP execution and at the bottom the same operations made by me for the same file. 3 seconds against 2 minutes and 26 seconds. So the LISP is approx 50 times faster than me!

You can save the following LISP code and adapt it. At the moment I called it Spazi:

CLICK TO SEE THE CODE
(defun c:Spazi (/ ss TxHt LayName Ent EntData sell n cm)
;; zoom extension
(command "zoom" "E")(princ)

(if
(and
;; For the layer name 7 change the text height to 60
;; SOURCE: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/select-objects-based-on-layer-name/m-p/9017917#M389749
(setq LayName "7")
(setq TxHt 60)

(setq ss (ssget "x" (list (cons 0 "TEXT") (cons 8 LayName))))
)
(while (setq Ent (ssname ss 0))
(setq EntData (entget Ent))
(entmod (subst (cons 40 TxHt) (assoc 40 EntData) EntData))
(ssdel Ent ss)
)
)
(print)

(setq sel1 (ssget "_X" '((0 . "*TEXT")(8 . "7"))))
 (if (tblsearch "layer" "(getvar 'LayName)")

(repeat (setq n (sslength sel1));;;count number of entity and go through the each entity
    (print (cdr(assoc 1 (entget (ssname sel1 (setq n (- n 1)))))));;;print text value
    (command "_.chprop" sel1 "" "_color" 1 "");;;change to red color
)
(princ "No entity")
)

;; Change the color of some layers
(command "_.layer" "_color" 10 "0" "")
(command "_.layer" "_color" 4 "2" "")
(command "_.layer" "_color" 1 "3" "")
(command "_.layer" "_color" 2 "4" "")
(command "-layer" "off" 5 "")
(command "_.layer" "_color" 170 "6" "")
(command "_.layer" "_color" 10 "7" "")
(command "_.layer" "_color" 4 "9" "")


;;  taken from ByLayer.lsp [command names: --> BYevery]
;;  To change all properties of objects to BYLAYER and other defaults,
;;  clearing out all entity-specific overrides on such properties.
;;  Works on top-level entities only, not nested ones [e.g. in Blocks].
;;  Kent Cooper, 12 March 2013
;;  SOURCE: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-set-default-color-linetype-line-width-by-layer/m-p/6552240#M344578
  (setq ss (ssget "_X"))
  (repeat (sslength ss)
    (setq
      ent (ssname ss 0)
      edata (entget ent)
      edata (subst '(62 . 256) (assoc 62 edata) edata); color
      edata (subst '(6 . "BYLAYER") (assoc 6 edata) edata); linetype
      edata (subst '(48 . 1.0) (assoc 48 edata) edata); linetype scale
      edata (subst '(370 . -1) (assoc 370 edata) edata); lineweight
      edata (subst '(39 . 0.0) (assoc 39 edata) edata); thickness
    ); setq
    (entmod edata)
    (ssdel ent ss)
  ); repeat

(princ)

;; CreateText is a function to insert a text in a fixed position
;; SOURCE: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/trouble-with-text-coordinates/td-p/9313929
(defun CreateText (txtJust txtHeight insertPt txtString)
 (if (= txtJust "tc") (setq horJust 1
                verJust 3))
 (if (= txtJust "mc") (setq horJust 1
                verJust 2))
 (if (= txtJust "bc") (setq horJust 1
                verJust 1))
 (if (= txtJust "ml") (setq horJust 0
                verJust 3))
 (if (= txtJust "mr") (setq horJust 2
                verJust 2))
 (entmake
  (list
   (cons 0 "TEXT")
   (cons 72 horJust) ;Horizontal text justification,0=Left,1=Center,2=Right,4=Center,5=Fit
   (cons 73 verJust) ;Vertical text justification, 0=Baseline,1=Bottom,2=Middle,3=Top
   (cons 10 insertPt)
   (cons 11 insertPt)
   (cons 40 txtHeight)
   (cons 1 txtString)
  )
 )
)
;; Put the filename at the bottom
(CreateText "ml" 250.0 (getvar "EXTMIN") (vl-filename-base (getvar 'DWGNAME)))

;; Check the orientation looking for the extension of the drawing
(setq
  LL (getvar 'extmin)
  UR (getvar 'extmax)
); setq
(if (> (- (car UR) (car LL)) (- (cadr UR) (cadr LL)))
  (princ "Landscape" (setq orientation L))
  (princ "Portrait" (setq orientation V))
); if

;; How to save a PDF file
;; SOURCE: https://www.theswamp.org/index.php?topic=49301.0
(vl-load-com)
    (setq cm (getvar 'CMDECHO))
    (setvar 'CMDECHO 0)
    (if (> (- (car UR) (car LL)) (- (cadr UR) (cadr LL)))
    (command "_.-plot" "_y"
        (if (= 1 (getvar 'TILEMODE)) "Model" (getvar 'CTAB))
        "Print As PDF.pc3" "A3"
        "_I" "_L" "_N" "_E" "_F" "_C" "_Y" "printStyles.ctb" "_Y"
    )
    (command "_.-plot" "_y"
        (if (= 1 (getvar 'TILEMODE)) "Model" (getvar 'CTAB))
        "Print As PDF.pc3" "A3"
        "_I" "_P" "_N" "_E" "_F" "_C" "_Y" "printStyles.ctb" "_Y"
    )
    ); if
    (if (= 1 (getvar 'TILEMODE))
        (command "_A")
        (command "_N" "_Y")
    )
    (command (strcat (getvar 'DWGPREFIX) (vl-filename-base (getvar 'DWGNAME))) "_Y" "_Y")
    (setvar 'CMDECHO cm)
    (princ)

)

Making of the video

Software used on Windows:

On Linux:

The music in the video: Corncob - Country by Kevin MacLeodDownload from: https://studio.youtube.com/channel/UCQYvnBkLaDwS7yL5KIGqlYg/music rilasciata con licenza Creative Commons 4.0.

TAGS MULTI

Sponsored Links
Pubblicità

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.