Visual Studio 2005 Code Snippets for the AutoCAD .NET API – Part 2

Now that you’ve decided that Code Snippets are greatest thing since dual core processors, you’ve learned the basics of creating and managing them, and you’ve learned of some of their more advanced features, let’s look at a few snippets specific to AutoCAD.  One word of warning, all of these examples are for C#.  I’ll see if I can get VB versions coded up and posted; or even better, I’ll be more than happy to post your VB translations of them!

 One of my most used snippets is one for starting a Transaction.  It is an Expansion and a SurroundsWith snippet.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Start a Transaction</Title>
      <Author>Bobby C. Jones</Author>
      <Description>Inserts a "using(Transaction...)" code block.</Description>
      <Shortcut>tr</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
      <Keywords>
        <Keyword>Autocad</Keyword>
      </Keywords>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>database</ID>
          <ToolTip>Replace with the database that owns the transaction.</ToolTip>
          <Default>HostApplicationServices.WorkingDatabase</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
using(Transaction trans = $database$.TransactionManager.StartTransaction())
        {
          $selected$
          $end$
          trans.Commit();
        }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

  Another common snippet is one that opens a Database object.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Open DBObject</Title>
      <Author>Bobby C. Jones</Author>
      <Description>Opens a DBObject using an existing Transaction.</Description>
      <Shortcut>open</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Keywords>
        <Keyword>Autocad</Keyword>
      </Keywords>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>Type</ID>
          <ToolTip>Replace with the type of the object to open.</ToolTip>
          <Function>SimpleTypeName(Autodesk.AutoCAD.DatabaseServices.DBObject)</Function>
        </Literal>
        <Literal>
          <ID>ObjVarName</ID>
          <ToolTip>Replace with the objects variable name.</ToolTip>
          <Default>dbObject</Default>
        </Literal>
        <Literal>
          <ID>TransVarName</ID>
          <ToolTip>Replace with the name of your Transaction variable.</ToolTip>
          <Default>trans</Default>
        </Literal>
        <Literal>
          <ID>ObjectIdToOpen</ID>
          <ToolTip>Replace with the ObjectId to open.</ToolTip>
          <Default>objectID</Default>
        </Literal>
        <Literal>
          <ID>OpenMode</ID>
          <ToolTip>OpenMode for opening the DBObject</ToolTip>
          <Function>SimpleTypeName(Autodesk.AutoCAD.DatabaseServices.OpenMode)</Function>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
        $Type$ $ObjVarName$ = ($Type$)$TransVarName$.GetObject($ObjectIdToOpen$, $OpenMode$);
        $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Here’s a snippet to insert code that asks the user to select an entity, Editor.GetEntity().  I like this one, and snippets that insert the other GetXXX calls, because there can be a lot of setup code for the call and a lot of code to check the return values.  It’s easy code, but involves a lot of typing, unless you use a snippet to insert it of course.

 

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Get Entity</Title>
      <Author>Bobby C. Jones</Author>
      <Description>Allows the user to select a single entity.</Description>
      <Shortcut>GetEnt</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Keywords>
        <Keyword>Autocad</Keyword>
      </Keywords>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>PromptEntityOptions</ID>
          <Function>SimpleTypeName(Autodesk.AutoCAD.EditorInput.PromptEntityOptions)</Function>
        </Literal>
        <Literal Editable="false">
          <ID>PromptEntityResult</ID>
          <Function>SimpleTypeName(Autodesk.AutoCAD.EditorInput.PromptEntityResult)</Function>
        </Literal>
        <Literal Editable="false">
          <ID>PromptStatus</ID>
          <Function>SimpleTypeName(Autodesk.AutoCAD.EditorInput.PromptStatus)</Function>
        </Literal>
        <Literal>
          <ID>Prompt</ID>
          <ToolTip>Prompt to display to the user.</ToolTip>
          <Default>Select an entity</Default>
        </Literal>
        <Literal>
          <ID>editor</ID>
          <ToolTip>The current Editor object.</ToolTip>
          <Default>Application.DocumentManager.MdiActiveDocument.Editor</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
          //Create a new entity selection options object
          $PromptEntityOptions$ entitySelectionOpts = new $PromptEntityOptions$("\n$Prompt$: ");
          $end$
          //Start the selection process
          $PromptEntityResult$ entitySelectionResult = $editor$.GetEntity(entitySelectionOpts);
          //Exit if an entity is not selected
          if (entitySelectionResult.Status != $PromptStatus$.OK)
          {
            return;
          }
          ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

While these examples show inserting small peices of code, there’s nothing wrong with snippets that insert entire procedures, or groups of procedures, or even entire class skeletons.

 

This entry was posted in Customization. Bookmark the permalink.

Leave a comment