Author Topic: Anyone using Code snippets ?  (Read 2271 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • class keyThumper<T>:ILazy<T>
Anyone using Code snippets ?
« on: June 28, 2017, 12:16:35 AM »
Anyone using Code snippets in VS ?
kcGetEntity_Test.snippet
file is attached at bottom of post
Code - C#: [Select]
  1. PromptEntityOptions $options$ = new PromptEntityOptions("\n$message$");
  2. $selected$ $end$
  3. PromptEntityResult $result$ = $editor$.GetEntity($options$);
  4. if ( $result$.Status != PromptStatus.OK )
  5. {
  6.     switch($result$.Status)
  7.     {
  8.         case PromptStatus.Cancel:
  9.             $editor$.WriteMessage("\n$cancelMsg$");
  10.             return;
  11.             break;         
  12.         case PromptStatus.Error:
  13.             $editor$.WriteMessage("\n$errorMsg$");
  14.             return;
  15.             break;         
  16.         default:
  17.             return;
  18.             break;         
  19.     }
  20. }
  21.  
  22.  





« Last Edit: June 28, 2017, 12:19:46 AM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Anyone using Code snippets ?
« Reply #1 on: June 28, 2017, 01:28:27 AM »
Hi,

I extensively use code snippets since a while.
Here're two examples (French prompts and comments):

docdbed
Code - XML: [Select]
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>Documment actif, base de donnée, éditeur</Title>
  6.       <Author>gile</Author>
  7.       <Shortcut>docdbed</Shortcut>
  8.       <Description>Instancie le document actif sa base de donnée et son éditeur</Description>
  9.       <SnippetTypes>
  10.         <SnippetType>SurroundsWith</SnippetType>
  11.         <SnippetType>Expansion</SnippetType>
  12.       </SnippetTypes>
  13.     </Header>
  14.     <Snippet>
  15.       <Declarations>
  16.         <Literal>
  17.           <ID>application</ID>
  18.           <ToolTip>Remplacer par l'identificateur de l'application AutoCAD</ToolTip>
  19.           <Default>Application</Default>
  20.         </Literal>
  21.         <Literal>
  22.           <ID>document</ID>
  23.           <ToolTip>Remplacer par l'identificateur pour le document</ToolTip>
  24.           <Default>doc</Default>
  25.         </Literal>
  26.         <Literal>
  27.           <ID>database</ID>
  28.           <ToolTip>Remplacer par l'identificateur pour la base de données</ToolTip>
  29.           <Default>db</Default>
  30.         </Literal>
  31.         <Literal>
  32.           <ID>editor</ID>
  33.           <ToolTip>Remplacer par l'identificateur pour l'éditeur</ToolTip>
  34.           <Default>ed</Default>
  35.         </Literal>
  36.       </Declarations>
  37.       <Code Language="CSharp">
  38.         <![CDATA[var $document$ = $application$.DocumentManager.MdiActiveDocument;
  39.            var $database$ = $document$.Database;
  40.            var $editor$ = $document$.Editor;$end$]]>
  41.       </Code>
  42.     </Snippet>
  43.   </CodeSnippet>
  44. </CodeSnippets>

Code - C#: [Select]
  1.             var doc = Application.DocumentManager.MdiActiveDocument;
  2.             var db = doc.Database;
  3.             var ed = doc.Editor;

entsel
Code - XML: [Select]
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>Sélectionner une entité</Title>
  6.       <Author>gile</Author>
  7.       <Shortcut>entsel</Shortcut>
  8.       <Description>Invite l'utilisateur à sélectionner un objet</Description>
  9.       <SnippetTypes>
  10.         <SnippetType>SurroundsWith</SnippetType>
  11.         <SnippetType>Expansion</SnippetType>
  12.       </SnippetTypes>
  13.     </Header>
  14.     <Snippet>
  15.       <Declarations>
  16.         <Literal>
  17.           <ID>ppo</ID>
  18.           <ToolTip>Remplacer par l'identificateur du PromptEntityOptions</ToolTip>
  19.           <Default>entOpts</Default>
  20.         </Literal>
  21.         <Literal>
  22.           <ID>typeName</ID>
  23.           <ToolTip>Remplacer par le type de l'entité à filtrer</ToolTip>
  24.           <Default>Polyline</Default>
  25.         </Literal>
  26.         <Literal>
  27.           <ID>exactMatch</ID>
  28.           <ToolTip>Remplacer par le la valeur indiquant si le type est strict</ToolTip>
  29.           <Default>true</Default>
  30.         </Literal>
  31.         <Literal>
  32.           <ID>per</ID>
  33.           <ToolTip>Remplacer par l'identificateur du PromptEntityResult</ToolTip>
  34.           <Default>entRes</Default>
  35.         </Literal>
  36.         <Literal>
  37.           <ID>promptMsg</ID>
  38.           <ToolTip>Remplacer par le message d'invite</ToolTip>
  39.           <Default>Sélectionnez une polyligne</Default>
  40.         </Literal>
  41.         <Literal>
  42.           <ID>rejectMsg</ID>
  43.           <ToolTip>Remplacer par le message de rejet</ToolTip>
  44.           <Default>L'objet séléctionné n'est pas une polyligne</Default>
  45.         </Literal>
  46.       </Declarations>
  47.       <Code Language="CSharp">
  48.         <![CDATA[var $ppo$ = new PromptEntityOptions("\n$promptMsg$: ");
  49.            $ppo$.SetRejectMessage("\n$rejectMsg$.");
  50.            $ppo$.AddAllowedClass(typeof($typeName$), $exactMatch$);
  51.            var $per$ = ed.GetEntity($ppo$);
  52.            if ($per$.Status != PromptStatus.OK) return;$end$]]>
  53.       </Code>
  54.     </Snippet>
  55.   </CodeSnippet>
  56. </CodeSnippets>

Code - C#: [Select]
  1.             var entOpts = new PromptEntityOptions("\nSélectionnez une polyligne: ");
  2.             entOpts.SetRejectMessage("\nL'objet séléctionné n'est pas une polyligne.");
  3.             entOpts.AddAllowedClass(typeof(Polyline), true);
  4.             var entRes = ed.GetEntity(entOpts);
  5.             if (entRes.Status != PromptStatus.OK) return;
« Last Edit: June 28, 2017, 01:31:45 AM by gile »
Speaking English as a French Frog

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • class keyThumper<T>:ILazy<T>
Re: Anyone using Code snippets ?
« Reply #2 on: June 28, 2017, 01:41:24 AM »

Thanks Gilles.
I'm thinking about resurrecting some I used in VS2010 :)

I have some similar to the 2 you posted.
I have use kc as a prefix to all mine for AutoCAD ... I think it was to allow me to type kc for the name and all mine showed in the list ... it's been a long time since I've used them.

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • class keyThumper<T>:ILazy<T>
Re: Anyone using Code snippets ?
« Reply #3 on: June 28, 2017, 02:11:44 AM »
Here's one I found really useful

SurroundWithBraces.snippet
kc_braces

Code - XML: [Select]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>braces</Title>
  6.       <Shortcut>kc_braces</Shortcut>
  7.       <Description>Surround selected block of code with braces</Description>
  8.       <Author>kdub</Author>
  9.       <SnippetTypes>
  10.         <SnippetType>Expansion</SnippetType>
  11.         <SnippetType>SurroundsWith</SnippetType>
  12.       </SnippetTypes>
  13.     </Header>
  14.     <Snippet>
  15.       <Code Language="csharp"><![CDATA[{        
  16.   $selected$ $end$    
  17. }]]></Code>
  18.     </Snippet>
  19.   </CodeSnippet>
  20. </CodeSnippets>
  21.  
  22.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Anyone using Code snippets ?
« Reply #4 on: June 28, 2017, 07:11:18 AM »
I use Resharper Templates all the time.  They are similar to code snippets but are just more powerful and easier to create.  Here is an example of a file template that I use in VB. 


Code - Text: [Select]
  1. #If BricsCAD Then
  2.     Imports Teigha.Runtime
  3.     Imports Teigha.DatabaseServices
  4.     Imports Bricscad.ApplicationServices
  5.     Imports Bricscad.EditorInput
  6.     Imports Teigha.Geometry
  7.     Imports CadRx = Teigha.Runtime
  8.     Imports CadAs = Bricscad.ApplicationServices
  9.     Imports CadApp = Bricscad.ApplicationServices.Application
  10.     Imports CadBr = Teigha.BoundaryRepresentation
  11.     Imports CadDb = Teigha.DatabaseServices
  12.     Imports CadGe = Teigha.Geometry
  13.     Imports CadEd = Bricscad.EditorInput
  14.     Imports CadGi = Teigha.GraphicsInterface
  15.     Imports CadClr = Teigha.Colors
  16.     Imports CadWnd = Bricscad.Windows
  17.  
  18. #ElseIf AutoCAD Then
  19.     Imports Autodesk.AutoCAD.Runtime
  20.     Imports Autodesk.AutoCAD.ApplicationServices
  21.     Imports Autodesk.AutoCAD.DatabaseServices
  22.     Imports Autodesk.AutoCAD.Geometry
  23.     Imports Autodesk.AutoCAD.EditorInput
  24.     Imports Autodesk.AutoCAD.GraphicsInterface
  25.     Imports Autodesk.AutoCAD.Colors
  26.     Imports Autodesk.AutoCAD.Windows
  27.     Imports CadRx = Autodesk.AutoCAD.Runtime
  28.     Imports CadApp = Autodesk.AutoCAD.ApplicationServices.Application
  29.     Imports CadAs = Autodesk.AutoCAD.ApplicationServices       
  30.     Imports CadBr = Autodesk.AutoCAD.BoundaryRepresentation
  31.     Imports CadDb = Autodesk.AutoCAD.DatabaseServices
  32.     Imports CadGe = Autodesk.AutoCAD.Geometry
  33.     Imports CadEd = Autodesk.AutoCAD.EditorInput
  34.     Imports CadGi = Autodesk.AutoCAD.GraphicsInterface
  35.     Imports CadClr = Autodesk.AutoCAD.Colors
  36.     Imports CadWnd = Autodesk.AutoCAD.Windows
  37. #End If
  38.  
  39. Namespace $NAMESPACE$
  40.  
  41.     Public Class $CLASS$
  42.       $END$
  43.     End Class
  44. End Namespace

And a simple snippet to start a transaction.

Code - Text: [Select]
  1. Using tr As Transaction = db.TransactionManager.StartTransaction()
  2.         $END$
  3. End Using

And another to use the top transaction.

Code - Text: [Select]
  1. Using tr As Transaction = db.TransactionManager.TopTransaction
  2.         $END$
  3. End Using
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • class keyThumper<T>:ILazy<T>
Re: Anyone using Code snippets ?
« Reply #5 on: June 28, 2017, 07:18:53 AM »
Hi Keith,
I use Resharper as well, I agree that the Templates are excellent.
I hadn't considered it replacing my code snippets, but I will have a good think about that.

Thanks for the thought.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Re: Anyone using Code snippets ?
« Reply #6 on: June 28, 2017, 10:52:39 AM »
I use resharper templates as well. The only autocad one I've created is for transactions, using Jeff H's LockedTransaction class and a static 'Active' class for document, editor and database
Code: [Select]
using (LockedTransaction lTr = Active.Document.TransactionManager.StartLockedTransaction())
  {
    $END$
    lTr.Commit();
  }

Looks like
Code - C#: [Select]
  1. using (LockedTransaction lTr = Active.Document.TransactionManager.StartLockedTransaction())
  2. {
  3.  
  4.   lTr.Commit();
  5. }
  6.  


Great thread idea though, I should use more, and have some great ideas already, including figuring out .TopTransaction, so I stop spamming the database with unnecessary commits.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Anyone using Code snippets ?
« Reply #7 on: June 29, 2017, 12:34:16 AM »
One of my fave snippets
<Code Language="CSharp">
        <![CDATA[if ($PromptResult$.Status != PromptStatus.OK) return;               
        $end$]]>
and

    <![CDATA[       
          Document doc=acadApp.DocumentManager.MdiActiveDocument;
          Editor ed=doc.Editor;
          Database db=doc.Database;       
        $end$]]>