Recording Code
Click the Record Code button in the Code section of the Method Editor ribbon to record a sequence of operations that you perform using the model tree, as shown in the figure below.
Certain operations in the application tree can also be recorded, including methods used to modify the user interface while the application is running such as changing the color of a text label.
Clicking the Record Code button will add code to an existing method. To record a new method, click the Record Method button. This button is also available in the Main section of the Home tab of the Form Editor or Method Editor ribbon.
In addition, you can click the Record Method button in the Developer tab of the Model Builder ribbon.
While recording code, the COMSOL Desktop windows are surrounded by a red frame:
To stop recording code, click one of the Stop Recording buttons in the ribbon of either the Model Builder or the Application Builder.
The previous section on Data Access demonstrated how to set the values for the Heat transfer coefficient and External temperature properties in the busbar tutorial model. Alternatively, you can achieve this using the Record Code feature. To generate similar code using this method, follow these steps:
In the Model Builder window, click Record Method, or with the Method Editor open, click Record Code.
Change the value of the Heat transfer coefficient to 5.
Change the value of the External temperature to 300[K].
Click Stop Recording.
The resulting code is listed below:
model.component("comp1").physics("ht").feature("hf1").set("h", 5);
model.component("comp1").physics("ht").feature("hf1").set("Text", "300[K]");
Note that in File > Preferences you can enable the use of with() statements in order to make the code more compact. For more information on the use of with(), see The With Statement.
To generate code corresponding to changes to the application object, use Record Code or Record Method, then go to the Form Editor and, for example, change the appearance of a form object. The following code corresponds to changing the color of a text label from the default Inherit to Blue:
app.form("form1").formObject("textlabel1").set("foreground", "blue");
Recording code for operations in the Form Editor is useful for tasks such as generating code to enable or disable various form objects. This allows for controlling their status dynamically using a Boolean variable, for example, through a checkbox.
For more information on modifying the model object and the application object, see the Application Programming Guide.
Use the tools for recording code to quickly learn how to interact with the model object or the application object. The autogenerated code shows you the names of properties, parameters, and variables. Use strings and string-number conversions to assign new parameter values in model properties. By using Data Access while recording, you can, for example, extract a parameter value using get, process its value in a method, and set it back into the model object using set. For more information on Data Access, see Data Access in the Method Editor.