Monday, October 15, 2007

How to Overcome the FIND Command Limitations

We know very well that the FIND command is doing a pretty good service by helping us to find various types of text objects inside the drawing such as single line text, multiline text, block attribute values, table text, dimension annotation text and hyperlink text. It will be too much to ask for, if we wish to add something else in the list. Here is a tricky way to find the other categories of text such as block attribute definition, RText etc. which can not be located using the FIND command.

Open the Quick Select dialog box using the QSELECT command. Select the 'Attribute' entry in the Object type drop down list. Next, select the 'Tag' entry inside the properties list. Now choose the appropriate operator and provide the attribute tag name in the 'Value' Text box. The specific attibute definition will be selected on clicking OK button. Type ZOOM (normal shortcut - z) and Object (Normal Shortcut - o) to zoom to the selected attribute definition.

Use the same procedure with Object type 'RText' and Property 'Contents' to locate an RText object.

Tuesday, October 9, 2007

Using Additional Options Inside Standard AutoCAD Commands

Are we going to edit the standard AutoCAD commands? No way. Rather we will look how to achieve a similar functionality in a tricky way. We are going to make use of AutoLISP's special ability to work transparently in between commands. Let us see how to utilise this to draw a circle with circumference option.

Add the following lisp routine to your acadDoc.lsp file. If the acaddoc.lsp file does not exist, create a new one.

(defun c:C#() (/ (getreal "\nEnter circumference : ") (* 2 pi)))

Now we can use this circumference option inside the circle command in the following way.

Command: Circle
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]:
Pick center point
Specify radius of circle or [Diameter] <5.5>: 'C#
Enter circumference : 100

Don't forget to provide the option as transparent using apostrophe ('). It will draw a circle with given circumference. Note that you can use any convenient option name in place of C#. I used the special character '#' in order to distinguish it from the standard command shortcuts. Here goes another sample for creating a circle with 'Area' option. Follow the same procedure you did for the previous option.

(defun c:A#() (sqrt (/ (getreal "\nEnter circle area : ") pi)))

And use it in this way

Command: Circle
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]:
Pick center point
Specify radius of circle or [Diameter] <6.8>: 'A#
Enter area : 500

If you come up with any other options, feel free to share it here.

Sunday, October 7, 2007

Quick Tip - Move Text Inside Edit Attributes Dialog

This tip is some what similar to the Move Text Inside Edit Text Dialog tip posted earlier. Here you can drag selected text from one textbox to other using the cursor. Again, I have used two images to simplify the explanation.

The image before moving the text

And this is what I achieved using the cursor

Tuesday, October 2, 2007

Renaming an Active Drawing from the Command Prompt

Is it really possible to rename an opened file? Normally the operating system will not allow you to do that. But it is REALLY possible to do it inside AutoCAD by adapting a tricky way as explained below. Here are the steps involved in the process.

  1. Get the new drawing name from the user

  2. Check any drawing is existing in the current folder with the name provided.

  3. If not, save and close the current drawing and copy it in the new drawing name.

  4. After verification, delete the old file.

  5. Open the copied file in AutoCAD

Though it seems like a long procedure, practically it all finish within a moment. The user never get the feeling of all those background processing. Users are encouraged to set system variable ISAVEBAK to 1 in order to eliminate the chances of possible data loss due to any unexpected errors. The following is an implimentation of above logic in AutoCAD VBA

'<-- Active Drawing Renaming Utility -->
'<-- Renames the current drawing from the command prompt. -->
'<-- By Mohamed Haris ( -->
Sub RenameOnline()
On Error GoTo ErrDet
Dim objFS, objFL As Object
Dim strFilePath As String, strNewFileName As String, strNewFileFullName As String
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFL = objFS.getfile(ThisDrawing.FullName)
strFilePath = ThisDrawing.Path
strNewFileName = ThisDrawing.Utility.GetString(True, vbCr & "Enter new file name to rename: ")
strNewFileFullName = strFilePath & "\" & strNewFileName & ".dwg"
'*** Check for a valid windows filename
If Not FileNameIsValid(strNewFileName) Then
MsgBox "Please provide a valid name to rename....", vbExclamation
GoTo ClearMem
ElseIf objFS.fileExists(strNewFileFullName) Then
MsgBox "A file with new filename already exists...!!!" & vbCrLf & _
"Please check the existing filenames...", vbExclamation
GoTo ClearMem
objFL.Copy (strNewFileFullName), False
End If
'*** Make sure that the file has been copied before deleting the old one
If objFS.fileExists(strNewFileFullName) Then
ThisDrawing.Close True
Application.Documents.Open strNewFileFullName
MsgBox "Couldn't rename the file. Please try again...!!!", vbExclamation
End If
If Err.Number <> 0 Then
MsgBox "An error occured while renaming the file....!!!" & vbCrLf & vbCrLf & _
"Error Description: " & vbCrLf & Err.Description, vbExclamation
End If
Set objFL = Nothing
Set objFS = Nothing
End Sub

'*** NOTE: A regular expression could serve better for this purpose
Function FileNameIsValid(ByVal strFileName As String) As Boolean
Dim i As Integer
If Trim(strFileName) = "" Then
FileNameIsValid = False
Exit Function
End If
arInvalidChars = Array("\", "/", ":", "*", "?", """", "<", ">", "")
For i = 0 To UBound(arInvalidChars) - 1
If InStr(1, strFileName, arInvalidChars(i), vbTextCompare) <> 0 Then
FileNameIsValid = False
Exit Function
End If
FileNameIsValid = True
End Function

Normally I don't provide the new file name in the command prompt. Instead, there is another VBA routine which takes the the drawing name attribute value from the title blocks and provide it as the input for the RenameOnline utitlity. You could also implement this logic in any of your favourite programming languages.