-
Notifications
You must be signed in to change notification settings - Fork 187
/
klive.py
73 lines (61 loc) · 1.82 KB
/
klive.py
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
"""Stream GDS to Klayout. Requires gdsfactory KLayout integration."""
from __future__ import annotations
import json
import os
import pathlib
import socket
import warnings
from pathlib import Path
from gdsfactory import config
def show(
gds_filename: Path | str,
keep_position: bool = True,
technology: str | None = None,
port: int = 8082,
delete: bool = False,
) -> None:
"""Show GDS in klayout.
Args:
gds_filename: to show.
keep_position: keep position and active layers.
technology: Name of the KLayout technology to use.
port: klayout server port.
delete: deletes file.
"""
if not os.path.isfile(gds_filename):
raise ValueError(f"{gds_filename} does not exist")
gds_filename = pathlib.Path(gds_filename)
data = {
"gds": os.path.abspath(gds_filename),
"keep_position": keep_position,
"technology": technology,
}
data_string = json.dumps(data)
try:
conn = socket.create_connection(("127.0.0.1", port), timeout=0.5)
data = data_string + "\n"
enc_data = data.encode()
conn.sendall(enc_data)
conn.settimeout(5)
except OSError:
warnings.warn(
"Could not connect to klive server. Is klayout open and klive plugin installed?"
)
else:
msg = ""
try:
msg = conn.recv(1024).decode("utf-8")
config.logger.info(f"Message from klive: {msg}")
except OSError:
warnings.warn.warning("klive didn't send data, closing")
finally:
conn.close()
if delete:
Path(gds_filename).unlink()
if __name__ == "__main__":
import gdsfactory as gf
# c = gf.components.mzi()
c = gf.components.straight(length=10)
# gdspath = c.write_gds()
# show(gdspath)
c.show()