AutoCAD

Customize AutoCAD through Programming

28 Mar, 2013 By: Andrew G. Roe

Learn how to search for text and modify layers with Visual Studio.NET.


11. Double-click the button to display the Code window. A subroutine is already set up for the button you just double-clicked.

12. Modify the code to read as shown below, inserting the first two lines at the top of the code window and the others to fit within the preset code. (Hint: You can copy and paste the code instead of retyping it!)

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public Class Form1
   ' AutoCAD must be open before running this example.
   Dim AcadApp As AcadApplication
   Dim acadDoc As AcadDocument
   Dim objEntity As AcadEntity
   Dim objLayer As AcadLayer
   Dim objText As AcadText
   Dim objNewLayer As Object
   Dim txtSearch As String
   Dim intTextEntities As Integer
   Dim intMatch As Integer
   Dim blnFound As Boolean

Public Sub Button1_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles Button1.Click
    If txtSearch = Nothing Or objNewLayer = Nothing Then
      MsgBox("You must enter a search string and a layer.")
      Exit Sub
    End If
    intTextEntities = 0
    For Each objEntity In acadDoc.ModelSpace
      Select Case objEntity.ObjectName
         Case "AcDbText", "AcDbMText"
            intTextEntities = intTextEntities + 1
            blnFound = objEntity.textstring.ToString.Contains(txtSearch)
            ' Find string in text.
            If blnFound = True Then
               intMatch = intMatch + 1
              Dim UserResult As DialogResult =
                  MessageBox.Show("Found match. Do you want to change layers?",
                  "Change Layer", MessageBoxButtons.YesNoCancel,
               MessageBoxIcon.Question)
               Select Case UserResult
                  Case DialogResult.Yes
                     objEntity.Layer = objNewLayer
                  Case DialogResult.No
                     ' Do nothing
                  Case DialogResult.Cancel
                     Exit Sub
               End Select
            End If
         End Select
    Next
       MessageBox.Show("Found " & intTextEntities & " text entity(ies) and " & _
intMatch & " match(es) to the search string: " & txtSearch)
   Close()
  End Sub
End Class

Let’s examine what we have so far, as this might look confusing if you’re a novice programmer. With references properly set, the Imports statements are used to specify which libraries, or namespaces, will be accessed by the application. Dim statements declare variables for the AutoCAD Application and Document objects, as well as for the various AutoCAD objects we’ll be accessing. A String variable is declared to store the value for a search string, and Integer variables are declared to store values for the number of entities found and the number of matches found.

The code following the Dim statements might be even more confusing, so let’s look at the highlights to remove some of the mystery. The Public Sub Button1… line identifies the beginning of a subroutine that defines what happens when the user clicks Button 1. An If…Then block ensures that the user enters a search string and selects a layer. A For Each…Next loop searches through all entities in the drawing, and a Select Case structure finds only the text and multi-line text entities (AcDbText and AcDbMText, respectively). Another Select Case structure determines what happens when the user clicks a button in a dialog box. A message box displays the results at the end of the search.

1 2 3 


Add comment

Note: Comments are moderated and will appear live after approval by the site moderator.

AutoCAD Tips!

Lynn Allen

Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!
Follow Lynn on Twitter Follow Lynn on Twitter


Poll
At your company, who has the most say in CAD-related software purchasing?
CAD manager
CAD users
IT personnel
vice-president/department manager
CEO/company owner
Submit Vote