Author Topic: Code Snippets  (Read 6004 times)

0 Members and 1 Guest are viewing this topic.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Code Snippets
« on: March 17, 2006, 11:48:57 AM »
Has anyone played with these yet?  I had a few minutes this morning to devote to learning something new and this is what I came up with.  It's a C# snippet that inserts a

using(Transaction....)

code snippet.  I'm definitely going to look into these a bit more.

Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Using Transaction</Title>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>TransactionVariableName</ID>
          <ToolTip>Replace with the name of your Transaction variable</ToolTip>
          <Default>trans</Default>
        </Literal>
        <Object>
          <ID>database</ID>
          <Type>Autodesk.AutoCAD.DatabaseServices.Transaction</Type>
          <ToolTip>Replace with the database object where the transaction is taking place</ToolTip>
          <Default>HostApplicationServices.WorkingDatabase</Default>
        </Object>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
          using(Transaction $TransactionVariableName$ = $database$.TransactionManager.StartTransaction())
          {
           
            $TransactionVariableName$.Commit();
          }
          ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Bobby C. Jones

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #1 on: March 17, 2006, 10:56:51 PM »
Hi Bobby,
I've been playing with them for a while,
' cause I'm lazy and my memory isn't what it used to be  :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Code Snippets
« Reply #2 on: March 19, 2006, 08:28:44 AM »
' cause I'm lazy and my memory isn't what it used to be  :-)

Aha!  I see that we are two birds of a feather :-)

I've been playing with them for a while,

I've about convinced myself that I like these better than some reusable 'Helper' type classes; although I am still debating with myself.  Do you have any advice for a snippet newbie?
Bobby C. Jones

Glenn R

  • Guest
Re: Code Snippets
« Reply #3 on: March 19, 2006, 09:56:00 PM »
Nice idea Bobby,

Here's one that follows on from yours. You would certainly be wanting to open objects once inside a transaction....

Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Open AutoCAD Object using transaction</Title>
      <Shortcut>oao</Shortcut>
      <Description>Opens an Autocad object for read through a transaction</Description>
      <Author>Glenn Ryan</Author>
      <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Object>
          <ID>databaseobjecttype</ID>
          <Type>Autodesk.AutoCAD.DatabaseServices</Type>
          <ToolTip>Replace with the database object you wish to open</ToolTip>
          <Default>Autodesk.AutoCAD.DatabaseServices.Line</Default>
        </Object>
        <Literal>
  <ID>ObjectVariableName</ID>
  <ToolTip>Replace with the variable name for the object</ToolTip>
  <Default>line</Default>
        </Literal>
        <Literal>
  <ID>TransactionVariableName</ID>
  <ToolTip>Replace with the name of your Transaction variable</ToolTip>
  <Default>trans</Default>
        </Literal>
        <Literal>
  <ID>objectid</ID>
  <ToolTip>Replace with the name of your object id variable</ToolTip>
  <Default>objectId</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
          $databaseobjecttype$ $ObjectVariableName$ = $TransactionVariableName$.GetObject($objectid$, OpenMode.ForRead, false) as $databaseobjecttype$;
          ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Improvements are welcome.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #4 on: March 20, 2006, 12:02:20 AM »
Having an established naming system for the regularly used variables is important

eg ;

db
ed
tm
tr
bt
btr

etc, etc

Here's a couple of simple ones ..
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>Write a message to the AutoCAD command line</Title>
      <Shortcut>kbMsg</Shortcut>
      <Description>Displays a message on the AutoCAD command line.</Description>
      <Author>Kerry Brown KDUB</Author>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>Editor</ID>
          <ToolTip>AutoCAD Editor class object</ToolTip>
          <Default>ed</Default>         
        </Literal>
        <Literal Editable="true">
          <ID>message</ID>
          <ToolTip>message to be displayed to the AutoCAD command line.</ToolTip>
          <Default>message</Default>         
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[$Editor$.WriteMessage("\n$message$");$end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>Write a Formatted message to the AutoCAD command line</Title>
      <Shortcut>kbMsg1</Shortcut>
      <Description>Write a Formatted message on the AutoCAD command line.</Description>
      <Author>Kerry Brown KDUB</Author>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>Editor</ID>
          <ToolTip>AutoCAD Editor class object</ToolTip>
          <Default>ed</Default>         
        </Literal>
        <Literal Editable="true">
          <ID>message</ID>
          <ToolTip>message to be displayed to the AutoCAD command line.</ToolTip>
          <Default>message</Default>
        </Literal>       
        <Literal Editable="true">
          <ID>variable</ID>
          <ToolTip>variable to be displayed to the AutoCAD command line.</ToolTip>
          <Default>variable</Default>
      </Literal>       
      </Declarations>
      <Code Language="csharp">
        <![CDATA[$Editor$.WriteMessage("\n$message$ {0}", $variable$);$end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

edit:
fixed "bunny ears" .. thanks Glenn :-)
« Last Edit: December 26, 2006, 11:19:36 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Code Snippets
« Reply #5 on: March 20, 2006, 09:24:18 AM »
Kerry,

Nicely done and elegant.

Cheers,
Glenn.

P.S. I think in your 2nd code sample, in the CDATA section, you need to move the righthand set of "bunny's ears"...:D

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Code Snippets
« Reply #6 on: March 21, 2006, 10:11:44 PM »
Thanks for the great examples guys!  I'm certainly going to start building my library of these things.  Here is my re-worked Transaction snippet.  I'm *still* reading up on them.

Code: [Select]
<?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>
        <Keyword>using</Keyword>
        <Keyword>Transactions</Keyword>
      </Keywords>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>TransactionVariableName</ID>
          <ToolTip>Replace with the name of your Transaction variable.</ToolTip>
          <Default>trans</Default>
        </Literal>
        <Object>
          <ID>database</ID>
          <Type>Autodesk.AutoCAD.DatabaseServices.Database</Type>
          <ToolTip>Replace with the database object where the transaction is taking place.</ToolTip>
          <Default>HostApplicationServices.WorkingDatabase</Default>
        </Object>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[using(Transaction $TransactionVariableName$ = $database$.TransactionManager.StartTransaction())
        {
          $selected$
          $end$
          $TransactionVariableName$.Commit();
        }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Bobby C. Jones

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Code Snippets
« Reply #7 on: March 22, 2006, 03:39:55 PM »
Here is a type of snippet that has got me wondering...

It creates an entire procedure.  I started to build a library of helper classes with these sorts of functions.  But I wasn't happy with that.  I didn't want to have to learn another API on top of acad, and adt, and the .net framework.  Not to mention that it would be a library under constant update, just to name a couple of things that were nagging at me.

I'm going to give this method a spin and see how it goes.

Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Get or create a symbol table record.</Title>
      <Author>Bobby C. Jones</Author>
      <Description>
        Creates a function that returns an existing symbol table record, or creates it if it doesn't exist.
      </Description>
      <Shortcut>gc</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Keywords>
        <Keyword>Autocad</Keyword>
        <Keyword>Symbol Table Object</Keyword>
        <Keyword>BlockTableRecord</Keyword>
        <Keyword>LayerTableRecord</Keyword>
      </Keywords>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>SymbolTable</ID>
          <ToolTip>Replace with the proper SymbolTable type name.</ToolTip>
          <Default>Block</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
        internal static ObjectId GetOrCreate$SymbolTable$TableRecord(string $SymbolTable$TableRecordName, Database targetDatabase)
        {
          using (Transaction trans = targetDatabase.TransactionManager.StartTransaction())
          {
            $SymbolTable$Table symbolTable = trans.GetObject(targetDatabase.$SymbolTable$TableId, OpenMode.ForRead) as $SymbolTable$Table;
            if (!symbolTable.Has($SymbolTable$TableRecordName))
            {
              $SymbolTable$TableRecord symbolTableRecord = new $SymbolTable$TableRecord();
              symbolTableRecord.Name = $SymbolTable$TableRecordName;
             
              symbolTable.UpgradeOpen();
              symbolTable.Add(symbolTableRecord);
              symbolTable.DowngradeOpen();
             
              trans.AddNewlyCreatedDBObject(symbolTableRecord, true);
             
              trans.Commit();
            }
            return symbolTable[$SymbolTable$TableRecordName];
          }
        }
        $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Bobby C. Jones

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #8 on: March 25, 2006, 10:49:36 PM »
http://msdn.microsoft.com/msdnmag/issues/06/04/AdvancedBasics/default.aspx
 
IntelliSense Code Snippets -- Advanced Basics --  MSDN Magazine Article April 2006
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.