TheSwamp
Code Red => .NET => Topic started by: Bobby C. Jones 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.
<?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>
-
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 :-)
-
' 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?
-
Nice idea Bobby,
Here's one that follows on from yours. You would certainly be wanting to open objects once inside a transaction....
<?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.
-
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 ..
<?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>
<?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 :-)
-
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
-
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.
<?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>
-
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.
<?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>
-
http://msdn.microsoft.com/msdnmag/issues/06/04/AdvancedBasics/default.aspx
IntelliSense Code Snippets -- Advanced Basics -- MSDN Magazine Article April 2006