Using Wildcards in the FIND and Replace in AutoCAD

Using Wildcards in the FIND and Replace in AutoCAD


Since my last Blog was about Text features in AutoCAD, I recently got a support request from one of our clients that made us have to stop and scratch our heads for a minute on the capabilities of using the Find/Replace command with wildcards in AutoCAD. It seemed that in some cases the text would not me found and in others we would lose valuable information. So, after several trial and errors we finally were able to get the command to accomplish what the client needed (and after losing a few hair follicles along the way too!). So I thought this was a perfect time to add to last month’s Blog; so I wanted to share my findings with you…


First let me set the stage of what the customer wanted to do.
They had an AutoCAD Structural drawing with Mtext calling out the member sizes and some of them with the quantities if there were multiple beams, obviously to avoid unnecessary duplication and better drawing clarity. The naming convention was standard (i.e. W12X18 [24]) which denotes a Wide-flange beam 12” by 18” and a total count of 24 beams and so on.

Mission: The drawing consisted of hundreds of beam callouts that needed to be modified to only show the quantities and not the beam size. But beams without quantities needed to be left alone.

1

Note: This is a super small portion of the drawing just to show an example.

Solution: Find and Replace with Wildcards. Now the trick only works character by character, so with that said there are differences in the text (example. W8X16 Vs W16X24 has a different number of place holders.) And some of the callouts do not include a quantities so we don’t want to lose those callouts either, so logically we will have to work through several steps to address each scenario, as well as there are certain other things to be aware of to avoid making mistakes that the user could be unaware of.

Operation: In AutoCAD enter the FIND command, the FIND and Replace Dialog if you type in the following bold text in the Find field box.

W?X?? 

The results will find anything that starts with a W and any single character after (The 2nd Character place holder) and the 3rd character has to be an X, then it will accept any characters after, but only the next 2 characters that follow the X will remain if you replace with nothing. (Meaning a voided blank box, not even a space) in the Replace field. Below I created a Matrix Chart  and summary for the client so they can easily; know what to enter in the find and know what the results of the changes would be.

FIND and Replace Results

With FIND, you can use several wild-card characters to broaden your search. Here is a list of the Character’s and their Definitions…

# (Pound)

Matches any numeric digit

 

@ (At)

Matches any alphabetic character

 

. (Period)

Matches any nonalphanumeric character

 

* (Asterisk)

Matches any string and can be used anywhere in the search string

 

? (Question mark)

Matches any single character; for example, ?BC matches ABC, 3BC, and so on

 

~ (Tilde)

Matches anything but the pattern; for example; ~*AB*matches all strings

that don’t contain AB

 

[ ]

Matches any one of the characters enclosed; for example, [AB]C matches

AC and BC

 

[~]

Matches any character not enclosed; for example, [~AB]C matches XC but

not AC

 

[-]

Specifies a range for a single character; for example, [A-G]C matches

AC, BC, and so on to GC, but not HC

 

` (Reverse quote)

Reads the next character literally; for example, `~AB matches ~AB

I hope this is useful information for you. I know that the Find command is one of the commands that most people forget about… So the next time you need to change a lot of text in your AutoCAD drawing, and if you know these tricks, it will drastically speed up the process… Now that is a FIND!!!

How to make AutoCAD Open Faster and Run Faster

How to make AutoCAD Open Faster and Run Faster


A common goal for every AutoCAD user is to get the best performance out of your AutoCAD software. And some of the old tricks still hold true today–they are just done a little bit differently in the newer versions.

So, I thought I would take some time and show everyone how some of some old tricks that help AutoCAD run faster.

Reducing the Amount of Content to Load When Starting AutoCAD

Right-click on the AutoCAD desktop Icon and click Properties.

autocad properties
Click lick in the short and add the switch “/nologo” at the end of the line in the Target field.

Example: “C:\Program Files\Autodesk\AutoCAD 2017\acad.exe” /product ACAD /language “en-US” /nologo

Click Apply at the bottom. (You will need to have administrator permission to do this)

properties dialog box

Click OK to exit the properties dialog box.

autocad properties shortcut
Setting the Priority in the Task Manager

Launch AutoCAD so the executable displays in the Task Manager.

Right-click on the Window’s start icon and open the Task Manager.

 

autocad task manager

Click on the Details Tab.

Right-click on the ACAD.EXE file.

Click the Set Priority.

Select Realtime option.

Close the Task Manager by clicking the X in the upper-right corner of the dialog.

autocad task manager x
Setting the Hardware Acceleration

Open AutoCAD and check the Hardware Acceleration icon to see if it is ON.

hardware acceleration off vs hardware acceleration on

 

(If OFF, follow the steps below to turn it ON)

graphics performance

Right-click on the Hardware Acceleration icon and pick the [Graphics Performance] shortcut menu.

graphics performance shortcut menu

Other ways you can test to see if you are getting the best performance is to test some of the settings by turning them on and off.

For example: Open the Hardware Acceleration the same way you did in the last Item. You can also view the Tuner Log.

tuner log
Additional Notes from the AutoCAD Help file:

Graphics performance tuning examines your graphics card and 3D display driver and determines whether to use software or hardware acceleration.

Features that might not work properly on your system are turned off. Some features might work with your hardware, but are not recommended because the graphics card or 3D graphics display driver that you are using did not pass the Autodesk certification process or was not tested. Features can be enabled at your own risk.

The tuner log reports which graphics card and 3D display driver were identified on your system and whether hardware effects are enabled. The Effects section displays the current state of the supported hardware effects of the current display driver.

The log file displays information about the current application driver, as well as available application drivers and supported effects. Use this information to determine whether it’s better to use an application driver for your graphics card or the software driver.

To display mapped object shadows in viewports and to use per-pixel lighting, hardware acceleration is required.

When Uncompressed Textures is turned on, the amount of video memory required to open a drawing that contains materials that utilize images is increased.

By using this effect, it may decrease the time to load the images the first time that they are accessed, but the downside is it can increase the amount of video memory necessary to display the drawing. Also, there is a reduction in the quality of the images when they are displayed.

Finally, always make sure that your Hardware (including Graphics Card) is tested and Certified by Autodesk.

And when it comes to RAM, more is always better!

Remember, the recommend amount is just to run the software, not your business!

Send all IMAGES, OLE’s and PDF’s to back in one click


;;; Send all IMAGES, OLE's and PDF's to back in one click
;;; Created by Willie https://www.theswamp.org/index.php?action=profile;u=1474
;;; Saved from here: https://www.theswamp.org/index.php?topic=52967.0
;;; Slightly modified by Igal Averbuh 2017 (added option for PDF's and OLE's)

(defun c:HAB (/)
(setvar "cmdecho" 0)
(setq SS_IMAGE nil)
(setq SS_IMAGE (ssget "X" '((0 . "IMAGE,OLE2FRAME,PDFUNDERLAY"))))
(if (= SS_IMAGE nil)
(princ "\nNo images in drawing")

(progn
(command "draworder" SS_IMAGE "" "back" )
(setvar "cmdecho" 1)

(princ "\n *** All IMAGES, OLE's and PDF's moved to back !! ***")

);progn
);if
(princ)
);defun
(c:hab)

Delete all dimensions in the all blocks


(defun c:dim (/ ss i sn name lst)
(vl-load-com)
;;; ------ Tharwat 15. June. 2012 ----- ;;;
;;; codes to delete all dimensions entities in the ;;;
;;; all blocks ;;;
(if (not acdoc)
(setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
)
(if (setq ss (ssget "X" '((0 . "INSERT"))))
(repeat (setq i (sslength ss))
(setq sn (ssname ss (setq i (1- i))))
(if (not (member (setq name (cdr (assoc 2 (entget sn)))) lst))
(progn
(setq lst (cons name lst))
(vlax-for each (vla-item (vla-get-blocks acdoc) name)
(if (eq (vla-get-objectname each) "AcDbRotatedDimension")
(vla-delete each))
)
)
)
)
(princ)
)
(if ss (vla-regen acdoc AcAllviewports))
(princ)
)
(c:dim)

Delete all dimensions in the selected blocks

 
(defun c:dim (/ ss i sn name lst)
(vl-load-com)
;;; ------ Tharwat 15. June. 2012 ----- ;;;
;;; codes to delete all dimensions entities in the ;;;
;;; all blocks ;;;
(if (not acdoc)
(setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
)
(if (setq ss (ssget "_:L" '((0 . "INSERT"))))
(repeat (setq i (sslength ss))
(setq sn (ssname ss (setq i (1- i))))
(if (not (member (setq name (cdr (assoc 2 (entget sn)))) lst))
(progn
(setq lst (cons name lst))
(vlax-for each (vla-item (vla-get-blocks acdoc) name)
(if (eq (vla-get-objectname each) "AcDbRotatedDimension")
(vla-delete each))
)
)
)
)
(princ)
)
(if ss (vla-regen acdoc AcAllviewports))
(princ)
)
(c:dim)

 

Good AutoLISP Programming Techniques

Good AutoLISP Programming Techniques

By Rachid Khouri on May 30, 2017 04:56 pm

When creating AutoLISP routines, the programmer should keep in mind not only correct syntax, but also a well-structured program that is easy to comprehend, forgiving of mistakes, easy to debug, and easy to edit.

Formatting

You can format manually while you type. But Visual LISP includes helpful formatting tools. They automatically insert tabs and spaces where needed to give you added clarity and help you track parentheses. You can either Format edit window or Format selection.

AutoLISP ignores spaces and carriage returns, so use them to make the routine more understandable to the programmer.

Unformatted routine:

(defun C:TRIANGLE ()
(setq PT1 (getpoint “Pick a Point: “)
PT2 (getpoint “Pick another point: “)
PT3 (list (car PT1) (cadr PT2)))
(command “line” PT1 PT2 PT3 “c”))

Formatted routine:

You can set the formatting options under Tools>Environment Options> Visual LISP Format Options.

Commenting

Comments help you and anyone else reading the file to understand the code. Use a semi-colon in front of any line to add comments to a file. Anything on a line to the right of a semi-colon is ignored.

  • At the beginning of the routine be sure to include a comment that states the filename, what the function does, the author, the date, and the version.
  • You may also want to break longer functions up into stages and explain what each stage does.
  • While just one semi-colon will do the job, some people prefer two or three for visibility. Visual LISP automatically adds three semicolons in front of any comment starting at a blank line that is not indented, two semicolons when the new line is indented and one when tabbed over to a column location or after a closing parenthesis.
  • To automatically add comments after a closing parenthesis on a separate line, check the “Insert form-closing comment” box in the dialog under Tools>Environment Options> Visual LISP Format Options.

Comment Block/Uncomment Block

To use these tools, simply select the lines you want to comment or uncomment and press the button. Comment Block adds semicolons at the start of the selected lines; Uncomment Block removes the semicolons.

For example, you may want to test just a portion of your routine in AutoCAD. You can highlight the undesired portion and comment it out with the click of a button. When you are ready to test this section as well, highlight it again and press the Uncomment Block tool.

Commenting Example

;;;This program draws a triangle based on the two
;;;endpoints of the hypotenuse.
;;;June 14, 2012

(defun C:TRIANGLE ()
(setq PT1 (getpoint “Pick a Point: “)
          PT2 (getpoint “Pick another point: “)
          PT3 (list
                         (car PT1)
                         (cadr PT2) )
                   ;end list
) ;end setq
(command “line” PT1 PT2 PT3 “c”)
) ;end defun

Naming

  • Use symbol names that make sense. Use a name that relates to the value such as, PT1, ReferencePoint, SCALE_FACTOR, etc.
  • Visual LISP will complete a function or symbol name if you type the first few letters and the press Ctrl + Spacebar. Repeat if you don’t come up with the right name at first.
  • Symbols are not case sensitive but capitalization can help you differentiate between symbols and functions when you are not in Visual LISP.
  • Remember, don’t use PI, T, or any function name such as ANGLE as a symbol name.
  • Give understandable file names for your LISP routines. For example, not DS.LSP but DATESTAMP.LSP.

Prompting

Use error control such as the (iniget) and (getkword) functions. Expect the unexpected input. Make the routine as easy to use as possible.

Write prompts that look like AutoCAD’s. This makes a smoother interface between your LISP routine and the main package. For example: “Choose a color: Red or [Blue/Yellow/Green]: ” Don’t forget that AutoCAD 2000 has a new prompt style.

Setting Local Variables

Another good clarity practice is to keep the track of the variables you use as symbols and make sure you do not leave them active in AutoCAD if they will impact another routine. This is called localizing the variables.

All variables in use in a session of AutoCAD are stored in the atom list. This also includes all the AutoLISP function names.

Explanation: the (defun) function options

A variable can be declared exclusive to a given function. These “local” variables are declared in the same parentheses as the argument list of the (defun):

(defun function_name ([arguments / local_variables])

The optional arguments are the variables that you will want permanently assigned in a drawing session. These may be symbol information that you will use in another AutoLISP routine. Or they are the ones you must supply for the function to work.

The optional local_variables set aside the name and value of local variables. They will not be added to the atom-list and will only be retained in memory while the function is executing.

  • Arguments and local variables are separated by a forward slash “/”.
  • Local variables remain in memory only while the routine is executing. This allows the use of longer and clearer variable names.
  • You may need to exit and restart AutoCAD to remove any existing variables.

Explanation: the (atoms-family) function

The (atoms-family) function returns a list of all the symbols currently defined, called the atom list. This way you can know what symbols have been defined in the drawing session.

(atoms-family format [symbol])

The format argument specifies whether the symbols will be returned as symbols or strings: 0 returns a list of symbols and 1 returns list of symbols converted to strings.

The optional symbol argument is used to check whether each symbol in the symbol list exists in the current editing session. If the symbol exists, the argument returns a symbol name; otherwise it returns nil.

Command: (atoms-family 1 ‘(“CAR”))
(“CAR”)

Example: Localized Variables

;;;MATRIX.LSP
;;;C:MATRIX – A function to create a rectangular array
;;; by total row and column distance.
(defun c:MATRIX ( / ROW-NUMBER COL-NUMBER STARTPT TOTAL-ROW
TOTAL-COL ROW-DIST COL-DIST SS1)
(command “graphscr”)
(initget (+ 1 2 4)
(setq ROW-NUMBER (getint “Number of rows (—): “))
(initget (+ 1 2 4)
(setq COL-NUMBER (getint “Number of columns (|||): “))
(setq STARTPT (getpoint “Starting point: “))
(initget (+ 1 2)
(setq TOTAL-ROW (getdist STARTPT “\nTotal row distance: “))
(initget (+ 1 2)
(setq TOTAL-COL (getdist STARTPT “\nTotal column distance: “))
(setq ROW-DIST (/ TOTAL-ROW (1- ROW-NUMBER)))
(setq COL-DIST (/ TOTAL-COL (1- COL-NUMBER)))
(setq SS1 (ssget))
(command “array” SS1 “” “r” ROW-NUMBER COL-NUMBER ROW-DIST COL-DIST) (princ)
); close defun

About Rachid Khouri

R. Khouri is a CAD Engineer, CAD manager, Corporate trainer and Educator he has worked with big industries and create training institution in Quebec (Canada) and delivers training to corporate clients in AutoCAD. He is Currently Autodesk Developper and author authorized .You can get more on AutoCAD and Autolisp/Visual lisp from TutoCAD

Dimension Text Style Assign to All


;;;; Dimension Text Style Assign to All
;;; Created by Kent1Cooper
;;; Saved from: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-change-textstyles-in-all-dimensions-and-leaders-in-an/td-p/7061539

(defun C:DS
(/ tblentry styname data)

(command "-style" "Igal" "arial.ttf" "" "" "0" "" "")

(while (setq tblentry (tblnext "dimstyle" (not tblentry))); step through Dim.Style entries
(setq
styname (cdr (assoc 2 tblentry)); Style name
data (entget (tblobjname "dimstyle" styname)); its entity data
); setq
(entmod
(subst
(cons 340 (tblobjname "STYLE" "Igal")); assign Text Style to it
(assoc 340 data)
data
); subst
); entmod
); while
(command "_.dimoverride" "DIMTXSTY" "Igal" "" "_all" ""); force it on already-drawn ones
(princ)
); defun
(c:ds)

Copy First Layout Multiple Times, Number Incrementally and Sort Alphabetical


;; Copy First Layout Multiple Times and Number Incrementally
;; Saved from: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/copy-first-layout-multiple-times-and-number-incrementally/td-p/7030955
;; Created by Ranjit.Singh
;; Slightly modified by Igal Averbuh 2017 (added layout sort function)

(defun C:TabSort (/ cnt doc lay)
(vl-load-com)
(setq cnt 1
doc (vla-get-activedocument (vlax-get-acad-object))
)
(foreach lay (acad_strlsort (vl-remove "Model" (layoutlist)))
(vla-put-taborder (vla-item (vla-get-layouts doc) lay) cnt)
(setq cnt (1+ cnt))
)
(princ)
)

(defun c:cll (/ a adoc curpos curtab i n)
(setvar "tilemode" 0)
(and (= 0 (getvar 'tilemode))
(setq i (getint "\nEnter Starting Layout number: ")
curtab (substr (getvar 'ctab) 1 (- (strlen (getvar 'ctab)) 2))
n (getint "\nHow many copies of this tab: "))
(repeat n
(command "._layout" "_copy" "" (strcat curtab (if (= 1 (strlen (setq a (itoa (+ (1- n) i)))))
(strcat "0" a)
a)))
(setq i (1- i)))))

(defun c:cl (/)
(c:cll)
(C:TabSort)
)
(c:cl)

Unexplode Polylines


;;; Unexplode Polylines
;;; Based on Lee Mak routines saved from: http://www.cadtutor.net/forum/showthread.php?92452-convert-lines-to-polyline-(where-endpoints-coincide)
;;; Combined by Igal Averbuh 2017

;;--------------------=={ Chain Selection }==-----------------;;
;; ;;
;; Prompts the user to select an object and generates a ;;
;; selection chain of all objects sharing endpoints with ;;
;; objects in the accumulative selection. ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright © 2012 - http://www.lee-mac.com ;;
;;------------------------------------------------------------;;

(defun c:pj ( / *error* sel val var )

(defun *error* ( msg )
(mapcar '(lambda ( a b ) (if b (setvar a b))) var val)
(LM:endundo (LM:acdoc))
(if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
(princ (strcat "\nError: " msg))
)
(princ)
)

(LM:startundo (LM:acdoc))
(if
(setq sel
(LM:ssget "\nPolyline was Unexploded "
'( "_:L"
(
(-4 . "<OR")
(0 . "LINE,ARC")
(-4 . "")
(-4 . "OR>")
)
)
)
)
(progn
(setq var '(cmdecho peditaccept)
val (mapcar 'getvar var)
)
(mapcar '(lambda ( a b c ) (if a (setvar b c))) val var '(0 1))
(command "_.pedit" "_m" sel "" "_j" "" "")
)
)
(*error* nil)
(princ)
)

(defun c:ccp ( / en fl in l1 l2 s1 s2 sf vl )
(setq sf
(list
'(-4 . "<OR")
'(0 . "LINE,ARC")
'(-4 . "")
'(-4 . "")
'(-4 . "<AND")
'(0 . "ELLIPSE")
'(-4 . "")
'(-4 . "AND>")
'(-4 . "OR>")
(if (= 1 (getvar 'cvport))
(cons 410 (getvar 'ctab))
'(410 . "Model")
)
)
)
(if (setq s1 (ssget "_X" sf))
(if (setq en (ssget "_+.:E:S" sf))
(progn
(setq s2 (ssadd)
en (ssname en 0)
l1 (list (vlax-curve-getstartpoint en) (vlax-curve-getendpoint en))
)
(repeat (setq in (sslength s1))
(setq en (ssname s1 (setq in (1- in)))
vl (cons (list (vlax-curve-getstartpoint en) (vlax-curve-getendpoint en) en) vl)
)
)
(while
(progn
(foreach v vl
(if (vl-some '(lambda ( p ) (or (equal (car v) p 1e-8) (equal (cadr v) p 1e-8))) l1)
(setq s2 (ssadd (caddr v) s2)
l1 (vl-list* (car v) (cadr v) l1)
fl t
)
(setq l2 (cons v l2))
)
)
fl
)
(setq vl l2 l2 nil fl nil)
)
)
)
(princ "\nNo valid objects found.")
)
(sssetfirst nil s2)
(princ)
)
(vl-load-com) (princ)

(defun c:up ( / )
(c:ccp)
(c:pj)
)
(c:up)

Draw Grass at Elevation


;;; Draw Grass at Elevation
;;; Created by Ronjonp: https://www.theswamp.org/index.php?topic=52989.0
(defun c:gr(/ grass bpt d h intensity p1 p2 tilt top)
;; RJP - 04.28.2017
(defun getrandnum (minnum maxnum / randnum)
;; Getrandnum returns a real number between minNum and maxNum.
;;
;; By: Stig Madsen
(defun randnum (/ modulus multiplier increment random)
;; Randnum.lsp
;; Returns a random number.
;; Written by Paul Furman, 1996.
;; Based on algorithm by Doug Cooper, 1982.
(if (not seed)
(setq seed (getvar "DATE"))
)
(setq modulus 65536
multiplier 25173
increment 13849
seed (rem (+ (* multiplier seed) increment) modulus)
random (/ seed modulus)
)
)
(if (not (< minnum maxnum))
(progn (setq tmp minnum
minnum maxnum
maxnum tmp
)
)
)
(setq random (+ (* (randnum) (- maxnum minnum)) minnum))
)
(defun grass (p1 p2 / w)
(setq w -0.1)
(entmake (list '(0 . "LWPOLYLINE")
'(100 . "AcDbEntity")
'(67 . 0)
'(8 . "Grass")
'(100 . "AcDbPolyline")
'(90 . 4)
'(70 . 128)
'(43 . 0.0)
'(38 . 0.0)
'(39 . 0.0)
(cons 10 p1)
'(40 . 0.0)
'(41 . 0.0)
(cons 42 (getrandnum w (+ w w)))
'(91 . 0)
(cons 10 p2)
'(40 . 0.0)
'(41 . 0.0)
'(42 . 0.1)
'(91 . 0)
(cons 10 p1)
'(40 . 0.0)
'(41 . 0.0)
(cons 42 (getrandnum w (+ w w)))
'(91 . 0)
(cons 10 p2)
'(40 . 0.0)
'(41 . 0.0)
'(42 . -11.5)
'(91 . 0)
)
)
)
(setq intensity 25)
(setq tilt 0.2)
(if (and (or (setq h (getdist "\nEnter maximum grass height : ")) (setq h 0.5))
(setq p1 (getpoint "\nSpecify first point: "))
(setq d (getdist p1 "\nSpecify second point: "))
)
(repeat (fix (/ (* d intensity) h))
(setq bpt (list (getrandnum (car p1) (+ d (car p1))) (cadr p1)))
(setq top (list (getrandnum (car bpt) (+ (getrandnum (- tilt) tilt) (car bpt)))
(getrandnum (cadr bpt) (+ h (cadr bpt)))
)
)
(grass bpt top)
)
)
(princ)
)
(c:gr)