Model Center—Modelica Text View
The Modelica Text View is a text editor where the textual representation (Modelica definition) of the class can be viewed and edited. The Modelica Text View allows you to access and edit all attributes of a class. In most situations, the graphical views are easier to work with and provide a better overview of the class. However, the graphical views do not let you edit all aspects of a class. For instance, to edit the equations of a class, the Modelica Text View is used.
The textual representation of the ForceAndTorque model in Modelica.Mechanics.MultiBody.Forces.
Any changes made to the class in the Modelica Text View are applied when saving the class or when switching to a view that accesses the Modelica definition of the class. The changes can also be applied manually by pressing Shift+Return.
The Modelica class definition is checked for syntax errors when applying changes in the Modelica Text View. Syntax errors will appear as clickable links in the Messages view. Clicking a link will highlight the row where the error originates. It is important that the syntax errors are corrected before editing the class (or any other class) in any other view, or you risk losing the parts of the class definition with syntax errors.
The position (line and column) of the text cursor is visible in the status bar when the Modelica Text View is active.
Zooming In and Out
The Modelica text view has zoom in and out capabilities. To zoom in or out, either use the drop-down menu located in the toolbar or press Ctrl and rotate the mouse scroll wheel forward or backward.
To make Modelica code easier to read and write, the text editor highlights elements of the Modelica language in different colors. The highlighting is performed in real time while typing or modifying text in the text editor. The colors and text styles used by the highlighter can be customized in the Options dialog box; see how to customize the Modelica Text View.
To further assist you in reading and writing Modelica code, the editor will highlight matching parentheses, braces and brackets. Whenever you type one of these parentheses or place the text cursor next to an existing parenthesis, the editor will attempt to match it. If a matching parenthesis is found, the parenthesis, as well as the text in between, will be highlighted.
The Modelica Text view supports contextual help. When activated from the context menu or by pressing the F1 key, it shows documentation for the selected text, or the word under the cursor if no selection exists. If no specific documentation page can be derived from the selected text or from the word under the cursor, a general search in Documentation Center is performed. In case there is no selection and no word under the cursor, the documentation for the class associated with the currently open class window is shown.
Both single-line and multiline comments are supported in the text view in System Modeler. The multiline comments, /*comment*/, are most often used to comment out larger sections that cover several lines, whereas the single-line comments, //comment, only comment out the line in which the double slash is inserted (see example following).
An example of multiline and single-line comments.
Any line or block of code can be turned into a comment, or turned back into code if already commented out, using the Toggle Comment command. The command is available in the context menu and can also be activated using the keyboard shortcut Ctrl+/.
For individual lines of code, activate the command when the cursor is anywhere at the line. Multiple lines of code can be addressed by selecting everything on those lines before activating the command. Both of these actions will insert // comments in the beginning of the lines, or remove them if present.
To comment out arbitrary blocks of code, select the code and activate the Toggle Comment command. If the selection does not consist of complete lines of code, matching /* and */ comments will be used to comment out the code. To remove comments of this kind, select the commented out code and activate the command.
Expanding or Collapsing Annotations
To increase readability, each annotation is by default collapsed to a single character, », in the text editor. An annotation can be expanded by clicking the » character, or collapsed by clicking the annotation word. To expand or collapse all annotations, right-click anywhere within the Modelica Text View and choose Expand Annotations or Collapse Annotations from the popup menu. The default behavior can be specified in the Options dialog box; see how to customize the Modelica Text View.
Toggling Word Wrap
By default, lines with text are allowed to extend beyond the right edge of the editor. The behavior can be changed by enabling word wrapping. If enabled, any portion of a line that does not fit the viewable editor area is automatically displayed on the next line, enabling the text to be read without any horizontal scrolling. To enable word wrapping, right-click anywhere within the Modelica Text View and choose Word Wrap. Whether word wrapping should be enabled or disabled can be specified in the Options dialog box; see how to customize the Modelica Text View.
Finding and Replacing Text
The find and replace toolbar allows you to search in the Modelica Text View of a class window and to make text substitutions if you wish. To open the find and replace toolbar, choose Edit ▶ Find.
Searching for parameters in the Modelica Text View.
Enter the text you intend to search for in the Find what text field. The search is performed while you are typing, and if the text is found, it will be selected in the document and all matches become highlighted. Click the Find Next button or press Return to find the next occurrence of the text. The search automatically continues past the end or start of the document into the unsearched portion. If the text is not found at all, the background color of the text field will turn red.
Clicking the Replace button will substitute the first occurrence of the text you search for with the text entered in the Replace with text field. To replace all occurrences of the text, click the Replace All button.
By default, the search is performed from the insertion point and forward in the document. To search backward from the insertion point, select the Search up checkbox. Check the Match case checkbox to restrict the search to look only for occurrences that match the uppercase and lowercase characters you enter. You can also choose to search only for whole words, rather than matching the text as it occurs within words, by checking the Match whole words only box.
Close the find and replace toolbar by clicking the close button or pressing the Esc key.
To get a quick overview of all occurrences of a particular word or a piece of text without using the Find function, simply mark the text, and all other matches will be highlighted automatically.
Marking the word "controlBus" has automatically highlighted all other occurrences of the word.
Finding Component Declarations
While it is possible to find a component declaration by searching in the text for the name of the component, it can be tedious if the component is not declared in the class currently opened—for instance, an inherited component.
To quickly find a component declaration in the Modelica Text View regardless of the class in which it is declared, locate and right-click the component in either the Digram View or the Component Browser. In the drop-drown menu, click Go to Component in Modelica Text View. This takes you to the Modelica Text View of the class in which the component is declared, and the line of the component declaration is selected.
Viewing the Flat Modelica Definition
For a better understanding and overview of a model, it is possible to flatten its class definition. In a flat Modelica definition, all inheritance and component hierarchies have been flattened out, providing a single view of all parameters, variables, constants and equations of the model.
To show the flat Modelica definition for a class, right-click anywhere in the Modelica Text View and choose View Flat Modelica from the popup menu. A separate window is brought up, and once the flattening process is finished, the window will contain the flat Modelica definition of the model.
If any class that is part of the model is changed, a note will be shown at the top of the window informing you that the flat Modelica definition may no longer be up to date. Click the Refresh button next to the message or press F5 to regenerate the flat Modelica definition.
While the flat Modelica definition may at first look like legal Modelica, it is not. It is not possible to build and simulate a flat Modelica definition. It should rather be considered as a way of being able to obtain a more complete view of a model.
As long as access to the Modelica class is not protected (common for encrypted Modelica libraries), the class definition or parts of it can be copied to the clipboard. Select the text you want to copy, right-click anywhere in the view, and choose either Copy or Copy without Annotations from the popup menu. The selected text will be copied to the clipboard both as plain text and as HTML, preserving the formatting and syntax highlighting. If the target that you paste the text into accepts HTML, the formatted and colored version will be pasted, otherwise the plain text version will be. Some applications let you choose which one to use.
You can print the contents of the currently active Modelica Text View by choosing File ▶ Print. By default, a page header including the name of the printed class and the page number is inserted on every page. Also, to the left of each line of code, a line number is inserted. To turn off the header or line numbers, choose File ▶ Page Setup and open the Page Setup dialog box. In the Print Setup tab, clear the checked boxes in the Paper Decorations section.
The Print Setup view of the Page Setup dialog box.
Customizing the Modelica Text View
The text editor of the Modelica Text View may be customized in the Options dialog box. Choose Options from the Tools menu and select Model Center ▶ Modelica Text View in the left column of the dialog box.
Editing the options of the Modelica Text View.
All settings in the dialog box are automatically saved when closing the dialog box and restored the next time the model editor is started.
The sections of the Modelica Text View options are described in detail here.
- Font and Colors The Font and Colors settings allow you to establish a custom font and color scheme for various display items in the Modelica Text View.
- Size. Specifies the point size of the font to use in the Modelica Text View. A point is a traditional measure used by typesetters and is equal to 1/72 of an inch. Changing the font size affects all Display items.
- Display items. Lists the items for which you can modify the appearance. Text is the default display item. As such, properties assigned to Text will be overridden by properties assigned to other display items. For example, if you assign the color blue to Text and the color green to Identifier, all identifiers will appear in green. In this example, Identifier properties override Text properties.
- Item foreground. Specifies the foreground color and text style of the item selected in Display items.
- Expand annotations. If checked, annotations are expanded in the Modelica Text View. If not checked, annotations are collapsed into the » character.
- Show a warning message before deleting collapsed annotations. Specifies whether or not a warning message is shown when a collapsed annotation is deleted in the Modelica Text View.