File size: 15,469 Bytes
703f5e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

  <!-- Meta -->
  <meta name="Description" content="KLayout layout viewer and editor project page" /> 
  <meta name="Keywords" content="KLayout OASIS GDS2 viewer editor layout semiconductor mask chip design" /> 
  <meta name="abstract" content="KLayout is a free layout viewer and editor for several formats commonly used in the semiconductor industry to transfer layout data" />
  <meta name="URL" content="https://www.klayout.de/" />
  <meta name="Author" content="Matthias Koefferlein" />
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <title>KLayout Layout Viewer And Editor</title>

  <!-- General -->
  <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.klayout.de/news.xml"/>
  <link rel="stylesheet" type="text/css" href="https://www.klayout.de/format.css?v2.0"/>
  <link rel="shortcut icon" href="https://www.klayout.de/favicon.ico"/>

  <!-- Fonts -->
  <link rel="stylesheet" type="text/css" href="https://www.klayout.de/fonts.css"/>

  <!-- jQuery -->
  <script src="https://www.klayout.de/jquery-3.4.1.min.js"></script>

  <!-- bootstrap -->
  <link href="https://www.klayout.de/bootstrap-4.3.1-dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://www.klayout.de/bootstrap-4.3.1-dist/js/bootstrap.min.js"></script>

  <link href="/generated-doc.css" rel="stylesheet">
<link href="/search.css" rel="stylesheet">
<script src="/klayout-search.js"></script>

</head>

<body>

<div class="klayout-header">

  <div class="container mb-5">

    <nav class="navbar navbar-dark navbar-expand-lg">

      <a class="navbar-brand" href="https://www.klayout.de"><img src="https://www.klayout.de/klayout.png" srcset="https://www.klayout.de/klayout-x2.png 2x" alt="KLayout"></a>

      <div class="collapse navbar-collapse" id="navbarToggleExternalContent">

          <ul class="navbar-nav ml-auto mr-auto">

            <li class="nav-item dropdown active mr-4">
              <a class="nav-link dropdown-toggle klayout-menu" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                About
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                <a class="dropdown-item" href="https://www.klayout.de/intro.html">KLayout Project</a>
                <a class="dropdown-item" href="https://www.klayout.de/doc.html">Documentation</a>
                <a class="dropdown-item" href="https://www.klayout.de/galleries.html">Galleries</a>
                <a class="dropdown-item" href="https://www.klayout.de/credits.html">Credits</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" href="https://www.klayout.de/contact.html">Contact</a>
                <a class="dropdown-item" href="https://www.klayout.de/impressum.html">Impressum / Imprint</a>
                <a class="dropdown-item" href="https://www.klayout.de/privacy_policy.html">Datenschutz / Privacy Policy</a>
              </div>
            </li>
            <li class="nav-item active mr-4">
              <a class="nav-link klayout-menu" href="https://www.klayout.de/build.html">Get KLayout</a>
            </li>
            <li class="nav-item active mr-4">
              <a class="nav-link klayout-menu" href="https://sami.klayout.org">Package Index</a>
            </li>
            <li class="nav-item active mr-5">
              <a class="nav-link klayout-menu" href="https://www.klayout.de/forum">Forum</a>
            </li>
            <li class="nav-item active">
              <span class="nav-link klayout-menu">
                <a href="https://f-si.org"><img src="https://www.klayout.de/f-si_logo_32h_w.png" border="0" title="Free Silicon Foundation" alt="F-Si"/></a>
                <a class="ml-4" href="https://github.com/KLayout"><img src="https://www.klayout.de/GitHub-Mark-32px-white.png" border="0" title="KLayout on GitHub" alt="GitHub"/></a>
              </span>
            </li>

          </ul>

      </div>

      <!-- collapse button -->
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

    </nav>

  </div>

</div>

<div class="container">





<form class="search">
  <div class="input-group">
    <input id="search-text" class="form-control" type="text" onkeydown="klayoutSearchTextChanged()">
    <div class="input-group-append">
      <button class="btn btn-secondary" type="submit" id="search-button" onclick="klayoutSearchClicked(5); event.returnValue = false; return false">Search</button>
    </div>
  </div>
</form>

<div id="search-result">
  <div class="search-results-close-box" onclick="klayoutSearchCloseClicked()"></div>
  <div id="search-results-container">
  </div>
</div>

<div class='klayout-generated-doc'><p class="navigator"><b>KLayout Documentation (Qt 5): </b><a href="../index.html">Main Index</a> » <a href="index.html">Programming scripts</a> » Using Python</p><h1>Using Python</h1><a name="k_1"></a><a name="k_2"></a><a name="k_3"></a><a name="k_4"></a><ul><li><a href="#h2-47">Writing Macros in Python</a></li><li><a href="#h2-121">Python PCells</a></li><li><a href="#h2-138">Python Implementation Notes</a></li></ul><p>
  KLayout does not come with one integrated interpreter. Instead Python and Ruby can <b>both</b> be used together. 
  So it is possible to write one script in Ruby and another one in Python. Just pick your favorite language. 
  Scripts written in different languages share the same KLayout data structures.
  Naturally they cannot directly share variables or language-specific data. But you can, for example, 
  implement PCells in Python and Ruby and use those different PCells in the same layout at the same time.
  Depending on the type of PCell, KLayout will either execute Python or Ruby code.
  </p><p>
  Python macros are loaded into KLayout using either &quot;.py&quot; files or &quot;.lym&quot; files with the interpreter 
  set to &quot;Python&quot;. To create Python macros, a new tab is available in the Macro Development IDE. 
  When creating macros in the &quot;Python&quot; tab, they will use the Python interpreter. Macros created 
  in the &quot;Ruby&quot; tab will use the Ruby interpreter. Files loaded by &quot;import&quot; need to be in plain
  text format and use the &quot;.py&quot; suffix. The macro folder is called &quot;pymacros&quot; for a clean separation 
  between the two macro worlds. Technically, both Ruby and Python macros are .lym files with
  a different interpreter specified in these files.
  </p><p>
  The Python macro folder is in the &quot;sys.path&quot; search path so it is possible to install modules
  there. To install libraries globally use &quot;<i>inst_path</i>/lib/python/Lib&quot; and &quot;<i>inst_path</i>/lib/python/DLLs&quot; on 
  Windows. <i>inst_path</i> is the installation path (where klayout.exe is located). On Linux, the 
  installation will share the Python interpreter with the system and modules installed there
  will be available for KLayout too.
  </p><p>
  &quot;$PYTHONHOME&quot; is not supported to prevent interference with other Python consumers. Instead, KLayout will
  read the Python path from &quot;$KLAYOUT_PYTHONPATH&quot; (for Python &gt;= 3.x).
  </p><a name="h2-47"></a><h2>Writing Macros in Python</h2><p>
  A good way is to start with the samples provided when creating new macros on the Python tab.
  The samples are available at the end of the template list. There is a sample for a PCell
  implementation, a sample for a Qt dialog, a sample for using Qt's .ui files in Python
  macros and one sample turning KLayout into a HTTP server using a Python macro.
  </p><p>
  Apart from a few specialities and the different language of course, Python macros do not look much different from Ruby 
  macros. Ruby's &quot;RBA&quot; namespace is &quot;pya&quot; for Python (lowercase to conform with PEP-8). The
  class and methods names are the same with very few exceptions and the documentation can be
  used for Python too. Where necessary, a special remark is made regarding the Python implementation.
  </p><p>
  Here is a basic Python Macro. It creates a layout with a single cell and single layer and
  puts one rectangle on that layer:
  </p><pre>
# Python version:

import pya

layout = pya.Layout()
top = layout.create_cell(&quot;TOP&quot;)
l1 = layout.layer(1, 0)
top.shapes(l1).insert(pya.Box(0, 0, 1000, 2000))

layout.write(&quot;t.gds&quot;)
</pre><p>
  Here is the Ruby variant to demonstrate the similarity:
  </p><pre>
# Ruby version:

layout = RBA::Layout::new()
top = layout.create_cell(&quot;TOP&quot;)
l1 = layout.layer(1, 0)
top.shapes(l1).insert(RBA::Box::new(0, 0, 1000, 2000))

layout.write(&quot;t.gds&quot;)
</pre><p>
  Of course, not everything can be translated that easily between Ruby and Python. The
  details are given below. Usually however, it's straightforward to translate Ruby into
  Python.
  </p><p>
  There is no clear advantage of one language over the other. The Python community
  is somewhat stronger, but performance-wise, Ruby is better.
  In KLayout, the debugger support for Python is slighly better, since the guts
  of the interpreter are better documented for Python.
  </p><p>
  Apart from that, Python and Ruby coexist remarkably well and it is amazing, how easy it
  it to extend the interfaces from Ruby to Python: not counting the different in the memory 
  management model (mark and sweep garbage collector in Ruby, reference counting in Python),
  the concepts are very similar. 
  </p><p>
  Please read the Python specific notes below before you start. Some things need to be
  considered when going from Ruby to Python.
  </p><a name="h2-121"></a><h2>Python PCells</h2><p>
  Please have a look at the PCell sample available in the templates. Pick the PCell sample
  after you have created a new Python macro.
  </p><p>
  PCell implementation in Python is very similar to Ruby. 
  </p><p>
  Python macros are &quot;.lym&quot; files that are placed into the &quot;pymacro&quot; subfolder in the KLayout path.
  Python libraries can be put into the &quot;python&quot; subfolder. This subfolder is included into the 
  &quot;sys.path&quot; variable, so macros can load libraries simply by using &quot;import&quot;.
  </p><a name="h2-138"></a><h2>Python Implementation Notes</h2><ul><li><b>KLayout module:</b>
    KLayout's module is &quot;pya&quot; (lowercase conforming to PEP 8). 
    </li><li><b>Reserved names:</b>
    Some methods with reserved names are not available (i.e. &quot;exec&quot;, &quot;in&quot;).
    Some of these methods have been renamed and their original name is still available
    in Ruby, but use is deprecated.
    Where this was not possible, they are available with an appended underscore. 
    For example: &quot;QDialog.exec&quot; is available as &quot;QDialog.exec_&quot;. That is the same scheme PyQt uses.
    </li><li><b>Assignment methods (attribute setters):</b>
    Assignment methods (i.e. &quot;Box#left=&quot; are available as attributes. If there is a
    read accessor method too, the attribute can be read and written. For example:

    <pre>
box = pya.Box()
box.left = 10
box.right = box.right + 100
</pre><p>
    If the translation is ambiguous (i.e. because there is more than one getter or setter, 
    the setter will be translated to a method &quot;set_x(value)&quot; where &quot;x&quot; is the attribute name.
    </p></li><li><b>Predicate getters:</b>
    Question-mark names for predicates are translated to non-question-marker names:

    <pre>
# Ruby: 
  edges.is_empty?

# Python:
  edges.is_empty()
</pre></li><li><b>Constants:</b>
    Constants (upper-case static variable) are made available as static attributes.
    </li><li><b>Arrays:</b>
    Arrays will be represented as lists, but on assignment, they accept tuples as
    well.
    </li><li><b>Boolean values:</b>
    Boolean values are True and False.
    </li><li><b>No protected methods:</b>
    Protected methods are not supported - methods are public always.
    </li><li><b>&quot;nil&quot; value:</b> The Python equivalent to Ruby's &quot;nil&quot; is &quot;None&quot;.</li><li><b>Iterators:</b>
    Iterator binding:

    <pre>
edges = pya.Edges()
...
for edge in edges.each():
  ...
</pre><p>
    If there is an iterator named &quot;each&quot;, it will become the default iterator:
    </p><pre>
for edge in edges:
  ...
</pre></li><li><b>Keyword arguments:</b><p>Most methods support keyword arguments, for example:</p><pre># a 45 degree rotation
t = pya.CplxTrans(rot = 45)</pre><p>
    Exceptions are some built-in methods like &quot;assign&quot;. Keyword arguments can be used
    when the non-optional arguments are specified either as positional or other keyword
    arguments.
    </p></li><li><b>Standard protocols:</b><p>&quot;x.to_s()&quot; is available as &quot;str(x)&quot; too.</p><p>&quot;x.size()&quot; is available as &quot;len(x)&quot; too.</p><p>If there is a &quot;[]&quot; operator and a &quot;size&quot; method, the object implements
    the sequence protocol too.</p></li><li><b>Operators:</b><p>
    Operators are made available through Python operators. For example
    </p><ul><li>&quot;+&quot;  will be available as   &quot;+&quot; (&quot;__add__&quot;)</li><li>&quot;&amp;&quot;  will be available as   &quot;and&quot; (&quot;__and__&quot;)</li><li>&quot;|&quot;  will be available as  &quot;or&quot; (&quot;__or__&quot;)</li><li>&quot;==&quot; will be available as  &quot;==&quot; (&quot;__eq__&quot;)</li></ul></li><li><b>Deep copies:</b>
    Deep copies of pya objects can be made with dup()

    <pre>
box = pya.Box(10, 20, 110, 220)
copy_box = box.dup()
</pre></li><li><b>Events (signals):</b>

    Events can be bound to lambdas or functions:

    <pre>
action.on_triggered( lambda: action.text += &quot;X&quot; )
</pre>

    or to function:

    <pre>
def f():
  print &quot;triggered&quot;

action.on_triggered(f)
</pre><p>
    Events have to match precisely - exactly the number of arguments have to be declared.
    </p></li><li><b>sys.settrace:</b>
    Using &quot;sys.settrace&quot; will disable the debugger support permanently.
    </li><li><b>Instance attributes can't reimplement virtual methods:</b>
    This is a limitation driven by the need to avoid cyclic references.
    Instance-bound methods require a reference to the instance and that 
    will create a cycle with the reimplementation callable object which
    is held by the class itself.
    </li><li><p><b>Tips when developing own modules:</b></p><ul><li>The &quot;python&quot; subfolders of the KLayout path are added to sys.path, so modules can be put as plain .py files
          and imported with &quot;import module&quot;.</li><li>Or: modules can be put into folders inside &quot;python&quot; using an &quot;__init__.py&quot; file to indicate
          the folder is a module.</li><li>Use &quot;reload(module)&quot; on the console to refresh the module cache if changes have been applied.</li></ul></li></ul></div>
</div>
</body>
</html>