KLayout 0.29.8 (2024-11-26 926dac96c) [master]

API reference - Class Macro

Notation used in Ruby API documentation

Module: lay

Description: A macro class

Sub-classes: Format, Interpreter

This class is provided mainly to support generation of template macros in the DSL interpreter framework provided by MacroInterpreter. The implementation may be enhanced in future versions and provide access to macros stored inside KLayout's macro repository. But it can be used to execute macro code in a consistent way:

path = "path-to-macro.lym"
RBA::Macro::new(path).run()

Using the Macro class with run for executing code will chose the right interpreter and is able to execute DRC and LVS scripts in the proper environment. This also provides an option to execute Ruby code from Python and vice versa.

In this scenario you can pass values to the script using Interpreter#define_variable. The interpreter to choose for DRC and LVS scripts is Interpreter#ruby_interpreter. For passing values back from the script, wrap the variable value into a Value object which can be modified by the called script and read back by the caller.

Public constructors

new Macro ptrnew(string path)Loads the macro from the given file path

Public methods

[const]Macro ptr_const_castReturns a non-const reference to self.
void_createEnsures the C++ object is created
void_destroyExplicitly destroys the object
[const]bool_destroyed?Returns a value indicating whether the object was already destroyed
[const]bool_is_const_object?Returns a value indicating whether the reference is a const reference
void_manageMarks the object as managed by the script side.
void_unmanageMarks the object as no longer owned by the script side.
[const]stringcategoryGets the category tags
voidcategory=(string string)Sets the category tags string
[const]stringdescriptionGets the description text
voiddescription=(string description)Sets the description text
[const]stringdocGets the macro's documentation string
voiddoc=(string doc)Sets the macro's documentation string
[const]stringdsl_interpreterGets the macro's DSL interpreter name (if interpreter is DSLInterpreter)
voiddsl_interpreter=(string dsl_interpreter)Sets the macro's DSL interpreter name (if interpreter is DSLInterpreter)
[const]stringepilogGets the epilog code
voidepilog=(string string)Sets the epilog
[const]Macro::FormatformatGets the macro's storage format
voidformat=(Macro::Format format)Sets the macro's storage format
[const]stringgroup_nameGets the menu group name
voidgroup_name=(string string)Sets the menu group name
[const]Macro::InterpreterinterpreterGets the macro's interpreter
voidinterpreter=(Macro::Interpreter interpreter)Sets the macro's interpreter
[const]stringinterpreter_nameGets the macro interpreter name
voidis_autorun=(bool flag)Sets a flag indicating whether the macro is automatically executed on startup
[const]boolis_autorun?Gets a flag indicating whether the macro is automatically executed on startup
voidis_autorun_early=(bool flag)Sets a flag indicating whether the macro is automatically executed early on startup
[const]boolis_autorun_early?Gets a flag indicating whether the macro is automatically executed early on startup
[const]stringmenu_pathGets the menu path
voidmenu_path=(string string)Sets the menu path
[const]stringnameGets the name of the macro
[const]stringpathGets the path of the macro
[const]stringprologGets the prolog code
voidprolog=(string string)Sets the prolog
[const]intrunExecutes the macro
voidsave_to(string path)Saves the macro to the given file
[const]stringshortcutGets the macro's keyboard shortcut
voidshortcut=(string shortcut)Sets the macro's keyboard shortcut
voidshow_in_menu=(bool flag)Sets a value indicating whether the macro shall be shown in the menu
[const]boolshow_in_menu?Gets a value indicating whether the macro shall be shown in the menu
voidsync_properties_with_textSynchronizes the macro properties with the text
voidsync_text_with_propertiesSynchronizes the macro text with the properties
[const]stringtextGets the macro text
voidtext=(string string)Sets the macro text
[const]stringversionGets the macro's version
voidversion=(string version)Sets the macro's version

Public static methods and constants

[static,const]Macro::InterpreterDSLInterpreterA domain-specific interpreter (DSL)
[static,const]Macro::FormatMacroFormatThe macro has macro (XML) format
[static,const]Macro::InterpreterNoneNo specific interpreter
[static,const]Macro::FormatPlainTextFormatThe macro has plain text format
[static,const]Macro::FormatPlainTextWithHashAnnotationsFormatThe macro has plain text format with special pseudo-comment annotations
[static,const]Macro::InterpreterPythonThe interpreter is Python
[static,const]Macro::InterpreterRubyThe interpreter is Ruby
[static,const]Macro::InterpreterTextPlain text
Macro ptrmacro_by_path(string path)Finds the macro by installation path
intreal_line(string path,
int line)
Gets the real line number for an include-encoded path and line number
stringreal_path(string path,
int line)
Gets the real path for an include-encoded path and line number

Deprecated methods (protected, public, static, non-static and constructors)

voidcreateUse of this method is deprecated. Use _create instead
voiddestroyUse of this method is deprecated. Use _destroy instead
[const]booldestroyed?Use of this method is deprecated. Use _destroyed? instead
[const]boolis_const_object?Use of this method is deprecated. Use _is_const_object? instead

Detailed description

DSLInterpreter

Signature: [static,const] Macro::Interpreter DSLInterpreter

Description: A domain-specific interpreter (DSL)

Python specific notes:
The object exposes a readable attribute 'DSLInterpreter'. This is the getter.

MacroFormat

Signature: [static,const] Macro::Format MacroFormat

Description: The macro has macro (XML) format

Python specific notes:
The object exposes a readable attribute 'MacroFormat'. This is the getter.

None

Signature: [static,const] Macro::Interpreter None

Description: No specific interpreter

Python specific notes:
This member is available as 'None_' in Python.
The object exposes a readable attribute 'None_'. This is the getter.

PlainTextFormat

Signature: [static,const] Macro::Format PlainTextFormat

Description: The macro has plain text format

Python specific notes:
The object exposes a readable attribute 'PlainTextFormat'. This is the getter.

PlainTextWithHashAnnotationsFormat

Signature: [static,const] Macro::Format PlainTextWithHashAnnotationsFormat

Description: The macro has plain text format with special pseudo-comment annotations

Python specific notes:
The object exposes a readable attribute 'PlainTextWithHashAnnotationsFormat'. This is the getter.

Python

Signature: [static,const] Macro::Interpreter Python

Description: The interpreter is Python

Python specific notes:
The object exposes a readable attribute 'Python'. This is the getter.

Ruby

Signature: [static,const] Macro::Interpreter Ruby

Description: The interpreter is Ruby

Python specific notes:
The object exposes a readable attribute 'Ruby'. This is the getter.

Text

Signature: [static,const] Macro::Interpreter Text

Description: Plain text

Python specific notes:
The object exposes a readable attribute 'Text'. This is the getter.

_const_cast

Signature: [const] Macro ptr _const_cast

Description: Returns a non-const reference to self.

Basically, this method allows turning a const object reference to a non-const one. This method is provided as last resort to remove the constness from an object. Usually there is a good reason for a const object reference, so using this method may have undesired side effects.

This method has been introduced in version 0.29.6.

_create

Signature: void _create

Description: Ensures the C++ object is created

Use this method to ensure the C++ object is created, for example to ensure that resources are allocated. Usually C++ objects are created on demand and not necessarily when the script object is created.

_destroy

Signature: void _destroy

Description: Explicitly destroys the object

Explicitly destroys the object on C++ side if it was owned by the script interpreter. Subsequent access to this object will throw an exception. If the object is not owned by the script, this method will do nothing.

_destroyed?

Signature: [const] bool _destroyed?

Description: Returns a value indicating whether the object was already destroyed

This method returns true, if the object was destroyed, either explicitly or by the C++ side. The latter may happen, if the object is owned by a C++ object which got destroyed itself.

_is_const_object?

Signature: [const] bool _is_const_object?

Description: Returns a value indicating whether the reference is a const reference

This method returns true, if self is a const reference. In that case, only const methods may be called on self.

_manage

Signature: void _manage

Description: Marks the object as managed by the script side.

After calling this method on an object, the script side will be responsible for the management of the object. This method may be called if an object is returned from a C++ function and the object is known not to be owned by any C++ instance. If necessary, the script side may delete the object if the script's reference is no longer required.

Usually it's not required to call this method. It has been introduced in version 0.24.

_unmanage

Signature: void _unmanage

Description: Marks the object as no longer owned by the script side.

Calling this method will make this object no longer owned by the script's memory management. Instead, the object must be managed in some other way. Usually this method may be called if it is known that some C++ object holds and manages this object. Technically speaking, this method will turn the script's reference into a weak reference. After the script engine decides to delete the reference, the object itself will still exist. If the object is not managed otherwise, memory leaks will occur.

Usually it's not required to call this method. It has been introduced in version 0.24.

category

Signature: [const] string category

Description: Gets the category tags

The category tags string indicates to which categories a macro will belong to. This string is only used for templates currently and is a comma-separated list of category names.

Python specific notes:
The object exposes a readable attribute 'category'. This is the getter.

category=

Signature: void category= (string string)

Description: Sets the category tags string

See category for details.

Python specific notes:
The object exposes a writable attribute 'category'. This is the setter.

create

Signature: void create

Description: Ensures the C++ object is created

Use of this method is deprecated. Use _create instead

Use this method to ensure the C++ object is created, for example to ensure that resources are allocated. Usually C++ objects are created on demand and not necessarily when the script object is created.

description

Signature: [const] string description

Description: Gets the description text

The description text of a macro will appear in the macro list. If used as a macro template, the description text can have the format "Group;;Description". In that case, the macro will appear in a group with title "Group".

Python specific notes:
The object exposes a readable attribute 'description'. This is the getter.

description=

Signature: void description= (string description)

Description: Sets the description text

description:The description text.

See description for details.

Python specific notes:
The object exposes a writable attribute 'description'. This is the setter.

destroy

Signature: void destroy

Description: Explicitly destroys the object

Use of this method is deprecated. Use _destroy instead

Explicitly destroys the object on C++ side if it was owned by the script interpreter. Subsequent access to this object will throw an exception. If the object is not owned by the script, this method will do nothing.

destroyed?

Signature: [const] bool destroyed?

Description: Returns a value indicating whether the object was already destroyed

Use of this method is deprecated. Use _destroyed? instead

This method returns true, if the object was destroyed, either explicitly or by the C++ side. The latter may happen, if the object is owned by a C++ object which got destroyed itself.

doc

Signature: [const] string doc

Description: Gets the macro's documentation string

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a readable attribute 'doc'. This is the getter.

doc=

Signature: void doc= (string doc)

Description: Sets the macro's documentation string

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a writable attribute 'doc'. This is the setter.

dsl_interpreter

Signature: [const] string dsl_interpreter

Description: Gets the macro's DSL interpreter name (if interpreter is DSLInterpreter)

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a readable attribute 'dsl_interpreter'. This is the getter.

dsl_interpreter=

Signature: void dsl_interpreter= (string dsl_interpreter)

Description: Sets the macro's DSL interpreter name (if interpreter is DSLInterpreter)

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a writable attribute 'dsl_interpreter'. This is the setter.

epilog

Signature: [const] string epilog

Description: Gets the epilog code

The epilog is executed after the actual code is executed. Interpretation depends on the implementation of the DSL interpreter for DSL macros.

Python specific notes:
The object exposes a readable attribute 'epilog'. This is the getter.

epilog=

Signature: void epilog= (string string)

Description: Sets the epilog

See epilog for details.

Python specific notes:
The object exposes a writable attribute 'epilog'. This is the setter.

format

Signature: [const] Macro::Format format

Description: Gets the macro's storage format

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a readable attribute 'format'. This is the getter.

format=

Signature: void format= (Macro::Format format)

Description: Sets the macro's storage format

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a writable attribute 'format'. This is the setter.

group_name

Signature: [const] string group_name

Description: Gets the menu group name

If a group name is specified and show_in_menu? is true, the macro will appear in a separate group (separated by a separator) together with other macros sharing the same group.

Python specific notes:
The object exposes a readable attribute 'group_name'. This is the getter.

group_name=

Signature: void group_name= (string string)

Description: Sets the menu group name

See group_name for details.

Python specific notes:
The object exposes a writable attribute 'group_name'. This is the setter.

interpreter

Signature: [const] Macro::Interpreter interpreter

Description: Gets the macro's interpreter

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a readable attribute 'interpreter'. This is the getter.

interpreter=

Signature: void interpreter= (Macro::Interpreter interpreter)

Description: Sets the macro's interpreter

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a writable attribute 'interpreter'. This is the setter.

interpreter_name

Signature: [const] string interpreter_name

Description: Gets the macro interpreter name

This is the string version of interpreter.

This method has been introduced in version 0.27.5.

is_autorun=

Signature: void is_autorun= (bool flag)

Description: Sets a flag indicating whether the macro is automatically executed on startup

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a writable attribute 'is_autorun'. This is the setter.

is_autorun?

Signature: [const] bool is_autorun?

Description: Gets a flag indicating whether the macro is automatically executed on startup

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a readable attribute 'is_autorun'. This is the getter.

is_autorun_early=

Signature: void is_autorun_early= (bool flag)

Description: Sets a flag indicating whether the macro is automatically executed early on startup

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a writable attribute 'is_autorun_early'. This is the setter.

is_autorun_early?

Signature: [const] bool is_autorun_early?

Description: Gets a flag indicating whether the macro is automatically executed early on startup

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a readable attribute 'is_autorun_early'. This is the getter.

is_const_object?

Signature: [const] bool is_const_object?

Description: Returns a value indicating whether the reference is a const reference

Use of this method is deprecated. Use _is_const_object? instead

This method returns true, if self is a const reference. In that case, only const methods may be called on self.

macro_by_path

Signature: [static] Macro ptr macro_by_path (string path)

Description: Finds the macro by installation path

Returns nil if no macro with this path can be found.

This method has been added in version 0.26.

menu_path

Signature: [const] string menu_path

Description: Gets the menu path

If a menu path is specified and show_in_menu? is true, the macro will appear in the menu at the specified position.

Python specific notes:
The object exposes a readable attribute 'menu_path'. This is the getter.

menu_path=

Signature: void menu_path= (string string)

Description: Sets the menu path

See menu_path for details.

Python specific notes:
The object exposes a writable attribute 'menu_path'. This is the setter.

name

Signature: [const] string name

Description: Gets the name of the macro

This attribute has been added in version 0.25.

new

Signature: [static] new Macro ptr new (string path)

Description: Loads the macro from the given file path

This constructor has been introduced in version 0.27.5.

Python specific notes:
This method is the default initializer of the object.

path

Signature: [const] string path

Description: Gets the path of the macro

The path is the path where the macro is stored, starting with an abstract group identifier. The path is used to identify the macro in the debugger for example.

prolog

Signature: [const] string prolog

Description: Gets the prolog code

The prolog is executed before the actual code is executed. Interpretation depends on the implementation of the DSL interpreter for DSL macros.

Python specific notes:
The object exposes a readable attribute 'prolog'. This is the getter.

prolog=

Signature: void prolog= (string string)

Description: Sets the prolog

See prolog for details.

Python specific notes:
The object exposes a writable attribute 'prolog'. This is the setter.

real_line

Signature: [static] int real_line (string path, int line)

Description: Gets the real line number for an include-encoded path and line number

When using KLayout's include scheme based on '# %include ...', __FILE__ and __LINE__ (Ruby) will not have the proper values but encoded file names. This method allows retrieving the real line number by using

# Ruby
real_line = RBA::Macro::real_line(__FILE__, __LINE__)

# Python
real_line = pya::Macro::real_line(__file__, __line__)

This substitution is not required for top-level macros as KLayout's interpreter will automatically use this function instead of __FILE__. Call this function when you need __FILE__ from files included through the languages mechanisms such as 'require' or 'load' where this substitution does not happen.

For Python there is no equivalent for __LINE__, so you always have to use:

# Pythonimport inspect
real_line = pya.Macro.real_line(__file__, inspect.currentframe().f_back.f_lineno)

This feature has been introduced in version 0.27.

real_path

Signature: [static] string real_path (string path, int line)

Description: Gets the real path for an include-encoded path and line number

When using KLayout's include scheme based on '# %include ...', __FILE__ and __LINE__ (Ruby) will not have the proper values but encoded file names. This method allows retrieving the real file by using

# Ruby
real_file = RBA::Macro::real_path(__FILE__, __LINE__)

This substitution is not required for top-level macros as KLayout's interpreter will automatically use this function instead of __FILE__. Call this function when you need __FILE__ from files included through the languages mechanisms such as 'require' or 'load' where this substitution does not happen.

For Python there is no equivalent for __LINE__, so you always have to use:

# Pythonimport inspect
real_file = pya.Macro.real_path(__file__, inspect.currentframe().f_back.f_lineno)

This feature has been introduced in version 0.27.

run

Signature: [const] int run

Description: Executes the macro

This method has been introduced in version 0.27.5.

save_to

Signature: void save_to (string path)

Description: Saves the macro to the given file

This method has been introduced in version 0.27.5.

shortcut

Signature: [const] string shortcut

Description: Gets the macro's keyboard shortcut

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a readable attribute 'shortcut'. This is the getter.

shortcut=

Signature: void shortcut= (string shortcut)

Description: Sets the macro's keyboard shortcut

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a writable attribute 'shortcut'. This is the setter.

show_in_menu=

Signature: void show_in_menu= (bool flag)

Description: Sets a value indicating whether the macro shall be shown in the menu

Python specific notes:
The object exposes a writable attribute 'show_in_menu'. This is the setter.

show_in_menu?

Signature: [const] bool show_in_menu?

Description: Gets a value indicating whether the macro shall be shown in the menu

Python specific notes:
The object exposes a readable attribute 'show_in_menu'. This is the getter.

sync_properties_with_text

Signature: void sync_properties_with_text

Description: Synchronizes the macro properties with the text

This method performs the reverse process of sync_text_with_properties.

This method has been introduced in version 0.27.5.

sync_text_with_properties

Signature: void sync_text_with_properties

Description: Synchronizes the macro text with the properties

This method applies to PlainTextWithHashAnnotationsFormat format. The macro text will be enhanced with pseudo-comments reflecting the macro properties. This way, the macro properties can be stored in plain files.

This method has been introduced in version 0.27.5.

text

Signature: [const] string text

Description: Gets the macro text

The text is the code executed by the macro interpreter. Depending on the DSL interpreter, the text can be any kind of code.

Python specific notes:
The object exposes a readable attribute 'text'. This is the getter.

text=

Signature: void text= (string string)

Description: Sets the macro text

See text for details.

Python specific notes:
The object exposes a writable attribute 'text'. This is the setter.

version

Signature: [const] string version

Description: Gets the macro's version

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a readable attribute 'version'. This is the getter.

version=

Signature: void version= (string version)

Description: Sets the macro's version

This method has been introduced in version 0.27.5.

Python specific notes:
The object exposes a writable attribute 'version'. This is the setter.