Author Topic: Custom Snippets  (Read 4950 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Custom Snippets
« on: August 24, 2010, 02:13:17 AM »
This is a quick example and is very easy and fast to set up. I just started setting up some templates  if anyone wants me to post them
This one does not have the reference and Namespace tags for inserting the using statements
When you insert it you type the command method name and it changes the method name and the ent type is a object so you get intellisense  I am sure you all
have used snippets

So if I in visual studo all have to do is right click insert Snippet Type Psr and press Tab twice and get

Code: [Select]
[CommandMethod("cv")]
        public static void cv()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect Viewport");
            peo.SetRejectMessage("\nNot a Viewport");
            peo.AddAllowedClass(typeof(Viewport), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

            }

        }


This is the snippet XML snippet

Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Get Current BlockTable</Title>
      <Description>Add Code To get bt</Description>
      <Shortcut>Per</Shortcut>         
    </Header>
    <Snippet>     
      <Declarations>
        <Literal>
          <ID>CN</ID>
          <ToolTip>Replace with CommandName</ToolTip>
          <Default>CommandName</Default>
        </Literal>
        <Object>
          <ID>ENT</ID>
          <Type>Autodesk.AutoCAD.DatabaseServices.Entity</Type>
          <ToolTip>Replace with a connection object in your application.</ToolTip>
          <Default>dcConnection</Default>
        </Object>
      </Declarations>
        <Code Language="CSharp">
                <![CDATA[
         [CommandMethod("$CN$")]
        public static void $CN$()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect $ENT$");
            peo.SetRejectMessage("\nNot a Viewport");
            peo.AddAllowedClass(typeof($ENT$), true);
            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
           
            }
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

But I thought maybe some of you would want them to set them how you like

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #1 on: August 24, 2010, 03:59:23 AM »
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #2 on: August 24, 2010, 04:21:25 AM »

Declare AutoCAD Document,Editor,DataBase

Shortcut : kacDed   .. and Tab twice
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #3 on: August 24, 2010, 04:24:30 AM »

Write a message to CommandLine

Shortcut : kacMsg .. and Tab twice
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #4 on: August 24, 2010, 04:31:41 AM »

Prompts for entity selection and test

kacGetEntity_Test : tab twice

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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #5 on: August 24, 2010, 04:38:03 AM »

I keep my snippets here :

C:\Users\ .... \Documents\Visual Studio 2010\Code Snippets\AutoCAD\kacDeclare Doc_Ed_Db.snippet
so Visual Studio can find them.


The reason I prefix snippets is to allow automatic listing by intellisence ..

eg type kac and all snippets prefixed with kac are listed

and the resulting code snip inserted

piccys :

« Last Edit: August 24, 2010, 04:46:57 AM by Kerry »
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.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Custom Snippets
« Reply #6 on: August 24, 2010, 04:48:21 AM »
I believe your setup is way better

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #7 on: August 24, 2010, 04:53:38 AM »
not better perhaps :) ...

uses the same snippets.
this is the XML for the WriteMessage

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 CommandLine</Title>
      <Shortcut>kacMsg</Shortcut>
      <Description>Write a message to CommandLine</Description>
      <Author>KerryBrown</Author>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>editor</ID>
          <ToolTip>AcadApp.DocumentManager.MdiActiveDocument.Editor</ToolTip>
          <Default>ed</Default>
          <Function></Function>
        </Literal>
        <Literal Editable="true">
          <ID>message</ID>
          <ToolTip>Message string written to Command Line</ToolTip>
          <Default>message_stuff</Default>
          <Function></Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[$editor$.WriteMessage("\n$message$");$end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Thankfully the snippet XML does not need to be hand coded :-D
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #8 on: August 24, 2010, 06:03:45 AM »

Creating and Using IntelliSense Code Snippets
http://msdn.microsoft.com/en-us/library/ms165392(v=VS.100).aspx

Snippet Designer Docs
http://snippetdesigner.codeplex.com/documentation


How Do I: Use Code Snippets in the Visual Studio IDE?  (video)
http://msdn.microsoft.com/en-us/vstudio/ee625748.aspx
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.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Custom Snippets
« Reply #9 on: August 24, 2010, 10:14:38 AM »
Since I use Line line=tr.getObject(  and Crcle circle=, I was trying to figure out how to do this in a  getobject snippet, but failed.
<![CDATA[$Type$ $ObjVarName$ = tr.GetObject($ObjectIdToOpen$, OpenMode$OpenMode$) as $Type$;$end$]]>
where $ObjVarName$ would =lowercase  $Type$

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #10 on: August 24, 2010, 08:42:34 PM »

Bryce,
I don't recall seeing anything that would allow that sort of dynamic default.
I'll have a look tonight.
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #11 on: August 24, 2010, 08:53:03 PM »
For anyone following along, this is the type of snippet Bryco was referring to ... without the dynamic VarName option.

attached:
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.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Custom Snippets
« Reply #12 on: August 24, 2010, 08:54:38 PM »
Is that available in VS2008

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #13 on: August 24, 2010, 08:57:36 PM »
Is that available in VS2008

Yes, appears so.
http://snippetdesigner.codeplex.com/releases/view/43862

.. not sure if the 2008 version has been updated though.
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Custom Snippets
« Reply #14 on: August 24, 2010, 10:49:12 PM »
< snip >
where $ObjVarName$ would =lowercase  $Type$

Had a look over lunchtime Bryce.
I can't see any way to do that sort of dynamic assignment.
The Function Element is very restricted.

though, the current snippet only requires changes to 2 variables, so it's not too bad as is.
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.