Using Snippets

Snippets are existing lines of code that can quickly and easily be inserted in the command line, saving you from repetitive typing. A snippet can contain editable fields, which are automatically selected for modification when the snippet is inserted. When a field is selected, you can type a new value to override the default value or let the default value remain. Pressing the Tab key moves the cursor to the next editable field.

When nothing is entered in the command line, pressing Ctrl + space opens a list of suggestions displaying all variables and snippets. When you scroll down to the snippets, you can see those predefined for standard Python commands as well as those in the Snippets panel.

 

If you know the name of the snippet, you can begin typing its name to find it in the list of suggestions. The predefined Snippet Template provides code for swapping two variables.

 

When you insert a snippet in the command line, all editable fields can easily be seen.

 

For example, in the Snippet Template snippet, the first editable field (foo) is highlighted.

 

Typing something changes the foo value to whatever you type (value1)​. Notice that both foo values change to value1 in one operation.

 

Pressing the Tab key moves the cursor to the next editable field, causing bar to be highlighted.

 

Typing something changes the bar value to whatever you type (value2)​. Notice that both bar values change to value2 in one operation.

 

Pressing the Tab key again finalizes the code.

To define an editable field, you must use the following notation: ${#:default_value} where # is the index of the field. For the first editable field, the value is 1. To reference an editable field that is already defined, you simply need to specify its index (${#}), as shown in the following figure for the Geometry snippet.

 

For the previous example for swapping two variables, you can define the following snippet:

temp = ${1:foo}
${1} = ${2:bar}
${2} = temp

Creating and Managing Snippets

From the Snippets pane, you can easily insert a snippet in the command line by clicking it. This pane displays all personal snippets, which are either those supplied by ANSYS for ACT-specific commands or those that you create. It does not display snippets for basic Python commands because these snippets cannot be removed. For example, the Snippets pane displays the supplied Snippet Template for swapping two variables, but it does not display the lambda snippet for this basic Python command.


Note:  When typing in the command line, autocompletion displays all snippets in the Snippet pane as well as the snippets for basic Python commands.


To make creating and managing snippets easy, the Snippets pane provides toolbar icons and a context menu.

Toolbar Icons:

  1. Add a new snippet.

  2. Add a new snippet folder.

  3. Import an existing snippets collection.

  4. Export the current snippets collection.

  5. Show hidden icons; only displays when the ACT Console window is not wide enough to display all icons in the toolbar.

Context Menu:

Creating Snippets and Folders

You can create snippets and folders using any of the following methods:

Once the snippet or folder is created, the Snippet Properties dialog box opens so that you can specify properties. A snippet has two properties, Caption and Value. A folder has only Caption.

After specifying the properties for the snippet or folder, you click Apply to save your changes or Cancel to discard the newly created snippet or folder.

Viewing and Editing Snippet or Folder Properties

To view or edit the properties of an existing snippet or folder in the Snippets pane, right-click it and selecting Edit to open the Snippet Properties dialog box. When finished, click Apply to save changes or Cancel to exit without saving changes.

Deleting Snippets and Folders

You can delete a snippet or folder from the Snippets pane by right-clicking it and selecting Delete.


Caution:  If you delete a folder, all of its contents are also deleted. Deleting a snippet from the Snippets pane also removes it from the list of suggestions for autocompletion.


Organizing Snippets and Folders

When creating a snippet or folder, you can determine its location by using a particular creation method, as described in Creating Snippets and Folders. To organize existing snippets and folders in the Snippets pane, you can drag them to the desired location in the hierarchical tree structure.

By default, the Snippets pane contains a Helpers folder and an Examples folder. The Helpers folder contains snippets for frequently used commands. The Examples folder contains snippets for use as templates. For example, the AddPressure snippet provides an example of how to create a pressure on the first face of the first body for the first part.

Importing and Exporting a Snippets Collection

You can import or export a collection of snippets as an XML file.

The following figure shows the XML file that results from the export of a snippets collection.

 


Release 18.2 - © ANSYS, Inc. All rights reserved.