+ All Categories
Home > Documents > Nick's Autolisp Utilities

Nick's Autolisp Utilities

Date post: 01-Nov-2014
Category:
Upload: jessiejames57
View: 35 times
Download: 2 times
Share this document with a friend
Description:
Just a small collection of AutoLisp routines which might be of interest to someone learning to customise Autocad. I used them in an architectural environment but they could be used in most situations - or you could just adapt them for your own purposes.
Popular Tags:
12
Nick's AutoLisp Tools - Brief Description The following is just a small collection of some of the Autolisp utilities I used in various drawing offices over the years. Some of the functions may well be built into the latest versions of AutoCAD but it's sometimes easier to just use a couple of keyboard clicks. Anyway, have fun and perhaps you could update some of the routines to do something more relevant to your particular field. sbe set by entity obe off by entity lbe lock by entity ce draws cross section arcs through circle pdxf returns a list of values (object name, type, starting point etc.) ftm converts length in feet to metres mtf converts mm to feet par `print area' - useful for floor areas, draw a closed polyline around the room, select it and the areas will be printed in both metric & imperial units. Note: I haven't used this for a while but I seem to remember it would throw a wobbly if your text style had a fixed height - meant to update the code but never got around to it. Otherwise it should work fine. so Setting out utility. Using Autocad `points' to indicate known positions on a site, pick and the position will be printed as NSEW coordinates. (Millimetres) som Same as above but units in Metres ls Swaps all entities on selected layer to a newly selected layer. mt `move to' move single entity to selected target layer. el `erase layer' delete all entities on selected layer. (Note: layer is not deleted, only entities on layer) ins Draw insulation - enter thickness, start point, end point and will automatically draw roof or wall insulation. Requires block nbins.dwg to be present in your support directory (see image below for details of block required)
Transcript
Page 1: Nick's Autolisp Utilities

Nick's AutoLisp Tools - Brief Description

The following is just a small collection of some of the Autolisp utilities I used in various drawing offices over the

years. Some of the functions may well be built into the latest versions of AutoCAD but it's sometimes easier to just

use a couple of keyboard clicks. Anyway, have fun and perhaps you could update some of the routines to do

something more relevant to your particular field.

sbe set by entity

obe off by entity

lbe lock by entity

ce draws cross section arcs through circle

pdxf returns a list of values (object name, type, starting point etc.)

ftm converts length in feet to metres

mtf converts mm to feet

par `print area' - useful for floor areas, draw a closed polyline around the room, select it and the areas will be

printed in both metric & imperial units. Note: I haven't used this for a while but I seem to remember it would

throw a wobbly if your text style had a fixed height - meant to update the code but never got around to it.

Otherwise it should work fine.

so Setting out utility. Using Autocad `points' to indicate known positions on a site, pick and the position will be

printed as NSEW coordinates. (Millimetres)

som Same as above but units in Metres

ls Swaps all entities on selected layer to a newly selected layer.

mt `move to' move single entity to selected target layer.

el `erase layer' delete all entities on selected layer. (Note: layer is not deleted, only entities on layer)

ins Draw insulation - enter thickness, start point, end point and will automatically draw roof or wall insulation.

Requires block nbins.dwg to be present in your support directory (see image below for details of block

required)

Page 2: Nick's Autolisp Utilities

eb `Erase block' select a block to erase ALL insertions of this block

ssl To select all entities on selected layer. Use `Previous' to reselect objects (or save the selection for use later)

ssll Similar to above but first you select an entity to set a layer filter then any selection you make will only accept

elements on that layer. Good for filtering out lots of stuff in a busy drawing.

sa Select a line to use as reference for snap angle. To return to normal, either select a horizontal line or type in

sa0

pt polyline tool. Checks to see if polyline is open or closed. Just had to change the code to LWPOLYLINE as I did

this many years ago on R12 when it was still POLYLINE - hope it works okay.

lct Tells you how many elements there are in the drawing on a selected layer

cc Car park bay counting. You will need to create a block called CP - the routine simply counts up how many car

park bays are in the drawing. Could of course be adapted to count up anything in your drawing - either

modify the code or make sure you rename the block to be counted to CP. (Will still say how many car parking

spaces there are unless you modify the code)

aa draws connected arcs - I did this to help me when I was producing a 2d database of a company's carved

products - lots of gargoyles and similar weird shapes.

ss Select text to make style current.

The following pages contain the Autolisp code - simply cut the text and paste into a text file then rename the file

with a .lsp extension - eg. myutilities.lsp

Then either drag and drop the file into an open Autocad drawing or, if you want to use the utilities in every drawing

session, go to `Load Application' on the Tools menu and add the file to the `Startup Suite'

Once loaded, simply type the command name (as listed in the left margin) and follow the on screen instructions if

necessary.

Page 3: Nick's Autolisp Utilities

;Nick's Autolisp tools - have fun!

(defun c:sbe(/ a l msg)

(setq cl(getvar "clayer"))

(setq a(car(entsel)))

(setq a(entget a))

(setq l(cdr(assoc 8 a)))

(if(= cl l)(progn(alert "That layer is current already")(quit)))

(command "layer" "s" l "")

(setq msg "Switched to Layer ")

(setq msg(strcat msg l))

(princ msg)

(princ)

)

(defun c:obe(/ a l msg)

(setq cl(getvar "clayer"))

(setq a(car(entsel)))

(setq a(entget a))

(setq l(cdr(assoc 8 a)))

(if(= cl l)(progn(alert "You cannot freeze the current\nLayer - try again!")(quit)))

(command "layer" "f" l "")

(setq msg "Frozen Layer ")

(setq msg(strcat msg l))

(princ msg)

(princ)

)

(defun c:Lbe(/ a l msg)

(setq a(car(entsel)))

(setq a(entget a))

(setq l(cdr(assoc 8 a)))

(command "layer" "lo" l "")

(setq msg "Locked Layer ")

(setq msg(strcat msg l))

(princ msg)

(princ)

)

(defun c:ce ()

(cend)

)

;(princ "\nNBCEND loaded: type CE to use command ")

;(princ)

(defun cend ( / c ce cca cc cra cr p1x p1ya p1yb p1y p1 mpd mpy mp p2 mp2)

Page 4: Nick's Autolisp Utilities

(setq orient(getvar "ucsname"))

(SETQ C (ENTSEL "\nSelect circle "))

(setq ce (entget (car c)))

(setq cca (assoc 10 ce))

(setq cc (cdr cca))

(setq cra (assoc 40 ce))

(setq cr (cdr cra))

(setq p1x (car cc))

(setq p1ya (cdr cc))

(setq p1yb (car p1ya))

(setq p1y (+ p1yb cr))

(setq p1 (list p1x p1y))

(setq mpd (/ cr 2))

(setq mpy (- p1y mpd))

(setq mp (list p1x mpy))

(command "arc" "c" mp p1 cc)

(setq p2 (list p1x (- p1y (* 2 cr))))

(setq mp2 (list p1x (- mpy cr)))

(command "arc" "c" mp2 p2 cc)

(princ)

)

(defun c:pdxf ()

(setq ent (entsel))

(setq entl(entget(car ent)))

(setq ct 0)

(textpage)

(princ "\nResults from entget of selected entity: ")

(repeat (length entl)

(print (nth ct entl))

(setq ct (+ 1 ct))

)

(princ)

)

(defun c:ftm ();(/ distf distm disxt)

(setq distf (getdist "\nEnter length in ft: "))

(setq distm (* distf 0.3048))

(setq distm(rtos distm))

(setq distxt(strcat distm " metres"))

(terpri)

(princ distxt)

(princ)

)

(defun c:ins ();(th pt1 pt2 d blk arrd arrn arrnx sa sa2 sang sang2)

Page 5: Nick's Autolisp Utilities

(graphscr)

(if (= th nil)(setq th 50))

(princ "\nThickness for Insulation? <")(princ th)(princ "> ")

(setq thx(getdist))

(if (/= thx nil)(setq th thx))

;(setq th(getdist "\nEnter thickness of insulation "))

(setq pt1 (getpoint "\nEnter start point of insulation "))

(setq pt2 (getpoint pt1 "\nEnter finish point of Insulation "))

(setq d (distance pt1 pt2))

(setq blk "nbins")

(setq arrd (/ th 2))

(setq arrn (/ d arrd))

(setq arrnx (fix arrn))

(setq sa (angle pt1 pt2))

(setq sa2 (angle pt2 pt1))

(setq sang (rtd sa))

(setq sang2 (rtd sa2))

(if (< (car pt1)(car pt2))(setq ip pt1)(setq ip pt2))

(if (equal ip pt1)(setq ra sang)(setq ra sang2))

(if (equal ip pt2)(setvar "snapang" sa2)(setvar "snapang" sa))

(command "insert" blk ip th th ra)

(command "array" "l" "" "r" "1" arrnx arrd)

(setvar "snapang" 0)

(princ)

)

(defun rtd (sa)

(/ (* sa 180.0) pi)

)

(defun dtr (a)

(* pi (/ a 180.0))

)

(defun c:mtf ();(/ dist distm distf distxt)

(setq dist (getdist "\nEnter length in mm <or pick>: "))

(setq distm (/ dist 1000))

(setq distf (/ distm 0.3048))

(setq distf(rtos distf))

(setq distxt(strcat distf " ft."))

(terpri)

(princ distxt)

(princ)

)

(defun C:PAR ();(/ ent ary arym aryf am af th ins txtht)

Page 6: Nick's Autolisp Utilities

(graphscr)

(setvar "cmdecho" 0)

(setq ent (entsel "\nPick Pline defining area: "))

(command ".area" "e" ent)

(setq ary (getvar "area"))

(setq arym (/ ary 1000000.0))

(setq aryf (* arym 10.764))

(setq arym (rtos arym 2 2))

(setq aryf (rtos aryf 2 1))

(setq am(strcat arym " sq.m."))

(setq af(strcat aryf " sq.ft."))

(if(= th nil)(setq th 150))

(setq ins(getpoint "\nPick text start point "))

(princ "\nHeight for text? <")(princ th)(princ "> ")

(setq txtht(getdist))

(if (/= txtht nil)(setq th txtht))

(command "text" ins th "0" am)

(command "text" "" af)

(princ)

)

(defun c:so ();(/ soce so_e so_n e_w txt_e n_s tx_n)

(if (= th nil)(setq th 150))

(setq soce (entget(car(entsel "\nPick setting out marker: "))))

;(setq soce (entget(entlast)))

(setq so_e(cadr(assoc 10 soce)))

(setq so_n(caddr(assoc 10 soce)))

(setq so_e(rtos so_e 2 1))

(setq so_n(rtos so_n 2 1))

(setq e_w(substr so_e 1 1))

(if( = e_w "-")

(setq txt_e(strcat (substr so_e 2) "W"))

(setq txt_e(strcat so_e "E"))

)

(setq n_s(substr so_n 1 1))

(if( = n_s "-")

(setq txt_n(strcat (substr so_n 2) "S"))

(setq txt_n(strcat so_n "N"))

)

(setq ins(getpoint "\nPick text start point: "))

(princ "\nHeight for text? <")(princ th)(princ "> ")

(setq txtht(getdist))

(if (/= txtht nil)(setq th txtht))

Page 7: Nick's Autolisp Utilities

(command "text" ins th "0" txt_e )

(command "text" "" txt_n)

(princ)

)

(defun c:som ();(/ soce so_e so_n e_w txt_e n_s txt_n ins txth)

(if (= th nil)(setq th 150))

(setq soce (entget(car(entsel "\nPick setting out marker: "))))

(setq so_e(cadr(assoc 10 soce)))

(setq so_e(/ so_e 1000))

(setq so_n(caddr(assoc 10 soce)))

(setq so_n(/ so_n 1000))

(setq so_e(rtos so_e 2 3))

(setq so_n(rtos so_n 2 3))

(setq e_w(substr so_e 1 1))

(if( = e_w "-")

(setq txt_e(strcat (substr so_e 2) "W"))

(setq txt_e(strcat so_e "E"))

)

(setq n_s(substr so_n 1 1))

(if( = n_s "-")

(setq txt_n(strcat (substr so_n 2) "S"))

(setq txt_n(strcat so_n "N"))

)

(setq ins(getpoint "\nPick text start point: "))

(princ "\nHeight for text? <")(princ th)(princ "> ")

(setq txtht(getdist))

(if (/= txtht nil)(setq th txtht))

(command "text" ins th "0" txt_e )

(command "text" "" txt_n)

(princ)

)

(defun c:ls()

(setq l(entsel "\nPick entity on old layer "))

(setq l(car l))

(setq l(entget l))

(setq ln(cdr(assoc 8 l)))

(setq ss(ssget"X"(list(cons 8 ln))))

(setq tl(entsel "\nPick entity on target layer "))

(setq tl(car tl))

(setq tl(entget tl))

(setq tln(cdr(assoc 8 tl)))

(command "chprop" ss "" "la" tln "")

(setq m1 "All entities on ")

(setq m1(strcat m1 ln))

Page 8: Nick's Autolisp Utilities

(setq m2 " moved to ")

(setq m2 (strcat m2 tln))

(setq msg (strcat m1 m2))

(terpri)

(princ msg)

(princ)

)

(defun c:mt(/ e en ol warn e2 en2 tl)

(setq e(car(entsel "\nPick entity to switch ")))

(setq en(entget e))

(setq ol(cdr(assoc 8 en)))

(setq warn(tblsearch "layer" ol))

(setq warn(cdr(assoc 70 warn)))

(if(= warn 68)

(progn

(alert "You have selected an object on\na locked layer - try again!")(quit)

)

)

(setq e2(car(entsel "\nPick entity on target layer ")))

(setq en2(entget e2))

(setq tl(cdr(assoc 8 en2)))

(setq warn(tblsearch "layer" tl))

(setq warn(cdr(assoc 70 warn)))

(if(= warn 68)

(progn

(alert "You cannot move an object to\na locked layer - try again!")(quit)

)

)

(command "chprop" e "" "la" tl "")

(PRINC)

)

(defun c:el(/ e la ss)

(setq e(car(entsel)))

(setq la(entget e))

(setq la(cdr(assoc 8 la)))

(setq ss(ssget"X"(list(cons 8 la))))

(command "erase" ss "")

(setq ss nil)

(princ)

)

(defun c:eb(/ blk e)

(setq blk(car(entsel "\nPick block type to delete ")))

Page 9: Nick's Autolisp Utilities

(setq e(entget blk))

(setq blk(cdr(assoc 2 e)))

(setq ss(ssget"X"(list(cons 0 "insert")(cons 2 blk))))

(command "erase" ss "")

(setq ss nil)

(princ)

)

(defun c:ssl(/ l le)

(setq l(car(entsel "\nPick entity on target layer")))

(setq le(entget l))

(setq l(cdr(assoc 8 le)))

(setq ssl(ssget"X"(list(cons 8 l))))

(princ)

)

(defun c:ssb(/ l le)

(setq l(car(entsel "\nPick entity on target layer")))

(setq le(entget l))

(setq l(cdr(assoc 2 le)))

(setq ssb(ssget"X"(list(cons 0 "insert")(cons 2 l))))

(princ)

)

(defun c:sa()

(setq ln(car(entsel "\nPick line to align with snapangle ")))

(setq lne(entget ln))

(setq pt1(cdr(assoc 10 lne)))

(setq pt2(cdr(assoc 11 lne)))

(setq ang(angle pt1 pt2))

(setq ang(rtd ang))

(command "snapang" ang)

(setq m1 "New Snapangle set to ")

(setq m2(rtos ang))

(setq msg(strcat m1 m2))

(terpri)

(princ msg)

(princ)

)

(defun c:sa0 ()

(command "snapang" 0)

(princ)

)

Page 10: Nick's Autolisp Utilities

(defun c:ssll(/ l le pt1 pt2 ssl)

(setq l(car(entsel "\nPick object on layer to use as filter ")))

(setq le(entget l))

(setq l(cdr (assoc 8 le)))

(setq msg1 "You have selected ")

(setq msg (strcat msg1 l))

(alert msg)

(setq pt1 (getpoint "\nPick first point"))

(setq pt2(getcorner pt1 "\nPick other corner"))

(setq ssl(ssget "C" pt1 pt2 (list(cons 8 l))))

(princ)

)

(defun c:pt()

(setq p(car(entsel "\nPick Polyline to check")))

(setq pe(entget p))

(setq test(cdr(assoc 0 pe)))

(setq test(strcase test))

(if

(/= "LWPOLYLINE" test)

(progn(alert "Entity selected is\nNOT a polyline\nTry again!")(quit)

)

)

(setq co(cdr(assoc 70 pe)))

(if

(= 1 co)(alert "This is a CLOSED Polyline")(alert "This is an OPEN Polyline")

)

(princ)

)

(defun c:lct (/ ln no msg)

(setq ln(getstring "\nEnter name of layer to investigate <RETURN to select>..\n"))

(if

(= "" ln)

(progn

(setq ln(car(entsel "\nPick object on target layer.. ")))

(setq lne(entget ln))

(setq ln(cdr(assoc 8 lne)))

)

)

(setq ssl(ssget"X"(list(cons 8 ln))))

(if

(= nil ssl)

Page 11: Nick's Autolisp Utilities

(progn

(alert "There are no entities on that layer")(quit)

)

)

(setq no (sslength ssl))

(setq msg " entities found on layer")

(setq no(rtos no))

(setq msg(strcat no msg))

(setq ln(strcat " " ln))

(setq msg(strcat msg ln))

(setq msg(strcat msg " <sset named SSL> "))

(terpri)

(princ msg)

(princ)

)

(defun c:cc()

(SETQ A(TBLSEARCH "BLOCK" "CP"))

(IF

(= NIL A)

(PROGN

(ALERT "NO CAR PARKING BAYS FOUND IN DRAWING")

(QUIT)

)

)

(setq ss(ssget"X"(list(cons 0 "insert")(cons 2 "cp"))))

(setq a(sslength ss))

(setq a(RTOS a 2 0))

(setq msg "Number of parking spaces = ")

(setq msg(strcat msg a))

(terpri)

(alert msg)

(setq ss nil)

(princ)

)

(defun c:aa()

(setq p1 (getpoint "\nPick first point..."))

(setq p2 (getpoint "\nPick next point..."))

(while p2

(command "arc" p1 "e" p2 "d" pause)

(setq p1 p2)

Page 12: Nick's Autolisp Utilities

(setq p2 (getpoint "\nNext....<RETURN TO QUIT>"))

(PRINC)

)

)

(defun c:ss (/ txt txt1 e_txt st)

(setq txt (car(entsel "\nPick text style to make current ..")))

(setq txt1(entget txt))

(setq e_txt(cdr (assoc 0 txt1)))

(setq e_txt(strcase e_txt t))

(if(/= e_txt "text")

(progn

(alert "You haven't selected text\nTry again!")

(exit)

);end progn

);end if

(setq st(cdr (assoc 7 txt1)))

(setvar "textstyle" st )

(princ)

)


Recommended