-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.py
423 lines (279 loc) · 13.3 KB
/
draw.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
from pathlib import Path
import glob, os
import sys
import numpy as np
coord = lambda x: np.floor(float(x)/50*100)/100
WIRES = {}
tikzset_defs = set()
TIKZSET = """\\tikzset{
partial ellipse/.style args={#1:#2:#3}{
insert path={+ (#1:#3) arc (#1:#2:#3)}
}
}"""
TIKZCODE = ""
unitvec = lambda vec: vec / np.linalg.norm(vec)
angle = lambda v: np.degrees(np.arctan2(v[1], v[0]))
node_degree = {}
def cleanup_text(text):
text = text.replace("_", "\\_")
return text
def rotate_vec(vec, deg):
theta = np.deg2rad(deg)
rot = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
return np.dot(rot, vec)
def name_of_component(component):
return component.replace("/", "-")
def tikz_add(s):
global TIKZCODE
TIKZCODE += s + "\n"
def add_symbol(symfile):
global TIKZSET
tikzset_defs.add(symfile)
# \tikzset{ pics/res.asy/.style args={#1/#2}{
# code={
# \draw (0.32,1.76) to (0.32,1.92);\draw (0.0,1.6) to (0.32,1.76);\draw (0.64,1.28) to (0.0,1.6);\draw (0.0,0.96) to (0.64,1.28);\draw (0.64,0.64) to (0.0,0.96);\draw (0.32,0.32) to (0.32,0.48);\draw (0.32,0.48) to (0.64,0.64);\draw (0.72,0.8) node {#1};\draw (0.72,1.52) node {#2};
# }
# }
# }
with open(symfile, "r") as f:
TIKZSET += "\\tikzset{ pics/" + name_of_component(symfile) + "/.style args={#1*^*#2}{code={"
for i in f.read().split("\n"):
cmd = i.split(" ")
if cmd[0] == "LINE":
coords = [coord(i) for i in cmd[2:6]]
style = cmd[1]
TIKZSET += f"\draw ({coords[0]},{coords[1]}) to ({coords[2]},{coords[3]});\n"
elif cmd[0] == "CIRCLE":
coords = [coord(i) for i in cmd[2:6]]
style = cmd[1]
pos = np.array(coords[0:2])
size = abs(pos - coords[2:4])/2
pos += size
TIKZSET += f"\draw ({pos[0]},{pos[1]}) ellipse ({size[0]} and {size[1]});\n"
elif cmd[0] == "ARC":
coords = np.array([coord(i) for i in cmd[2:10]])
pos = coords[0:2]
size = abs(pos - coords[2:4])/2
center = (pos + coords[2:4])/2
theta_i = angle(coords[6:8]-center)
theta_f = angle(coords[4:6]-center)
TIKZSET += f"\draw ({center[0]},{center[1]}) [partial ellipse={theta_i}:{theta_f}:{size[0]} and {size[1]}];\n"
elif cmd[0] == "WINDOW":
# WINDOW 0 24 16 Left 2
# WINDOW 1 36 80 Left 2
type = cmd[1]
pos = [coord(i) for i in cmd[2:4]]
alignment = cmd[4]
fontsize = cmd[5]
text = ""
if type=="0":
text = "#1"
elif type=="3":
text = "#2"
TIKZSET += f"\\draw ({pos[0]},{pos[1]}) node[yscale=-1] {{{text}}};\n"
TIKZSET += "}}}\n"
def draw_symbol(symfile, loc, angle, data0, data3):
global TIKZCODE
if data0==None: data0 = ""
if data3==None: data3 = ""
data0, data3 = cleanup_text(data0), cleanup_text(data3)
if symfile in tikzset_defs:
# place in circ
pass
else:
add_symbol(symfile)
tikz_add(f"\path[rotate around ={'{'}{angle}:({loc[0]}, {loc[1]}){'}'}, anchor=west, transform shape] ({loc[0]}, {loc[1]}) pic{{{name_of_component(symfile)}={data0}*^*{data3}}};")
def incremenet_node_degree(p):
try:
node_degree[p] += 1
except:
node_degree[p] = 0
def text_window(text, pos, angle, align_vec=[0, 1]):
if NO_COMMENTS_AND_COMMANDS == True: return
global TIKZCODE
align_cmd = ""
align_vec = rotate_vec(align_vec, angle)
if angle == 90 or angle == 270:
align_vec = rotate_vec(align_vec, -90)
if np.allclose(align_vec, [0, 1]):
align_cmd = "[below]"
elif np.allclose(align_vec, [0, -1]):
align_cmd = "[above]"
elif np.allclose(align_vec, [1, 0]):
align_cmd = "[left]"
elif np.allclose(align_vec, [-1, 0]):
align_cmd = "[right]"
TIKZCODE += f"\\draw ({pos[0]},{pos[1]}) node {align_cmd} {{{cleanup_text(text)}}};\n"
def parse_circuit(circuit, component_name, component_value, local_dir):
global TIKZCODE
circuit_split = circuit.split("\n")
k=-1
for i in circuit_split:
k+=1
cmd = i.split(" ")
if "WIRE" in cmd[0]:
# print(i)
coords = [coord(i) for i in cmd[1:5]]
tikz_add(f"\\draw ({coords[0]},{coords[1]}) to ({coords[2]},{coords[3]});")
WIRES[(coords[0],coords[1])] = (coords[2],coords[3])
WIRES[(coords[2],coords[3])] = (coords[0],coords[1])
incremenet_node_degree((coords[0],coords[1]))
incremenet_node_degree((coords[2],coords[3]))
elif "FLAG" in cmd[0]:
# tikz_add("%" + i)
coords = [coord(i) for i in cmd[1:3]]
node_type = cmd[3]
if node_type == "0":
draw_symbol("gnd.asy", coords, 0, "", "")
# print(f"({coords[0]},{coords[1]}) to ({coords[0]},{coords[1]}) {node_type}")
elif "TEXT" in cmd[0]:
pos = [coord(i) for i in cmd[1:3]]
if cmd[3] == "VBottom": cmd[3] = [0, -1]
elif cmd[3] == "VTop": cmd[3] = [0, 1]
elif cmd[3] == "Left": cmd[3] = [-1, 0]
elif cmd[3] == "Right": cmd[3] = [1, 0]
text = ' '.join(cmd[5:]) #[1:] # the [1:] removes the [; or !] char at the beginning of comment
# TODO: cmd 4 is size (and add support for symbol window and symbol compiler!)
text_window(text, pos, 0, align_vec=cmd[3])
elif "SYMBOL" in cmd[0]:
# if sys.platform == "darwin":
# os.chdir(os.path.expanduser('~') + "/Library/Application Support/LTspice/lib/sym")
# elif sys.platform == "linux" or sys.platform == "linux2":
# os.chdir(os.path.expanduser('~') + "/Documents/LTspiceXVII/lib/sym")
# elif sys.platform == "win32":
# os.chdir(os.path.expanduser('~') + "/Documents/LTspiceXVII/lib/sym")
coords = np.array( [coord(i) for i in cmd[2:4]] )
angle = int(cmd[4][1:])
# check if symbol
path = Path(local_dir + "/" + cmd[1]+".asy")
# try:
# symfile = glob.glob(cmd[1].lower()+".asy")[0]
# except:
# symfile = glob.glob(local_dir + "/" + cmd[1].lower()+".asy")[0]
if not path.is_file():
path = Path(cmd[1]+".asy")
klocal = 0
data0 = ""
data3 = ""
window0, window3 = None, None
while klocal<100:
klocal+=1
cmd = circuit_split[k+klocal].split(" ")
if cmd[0]=="SYMATTR":
if cmd[1] == "InstName" and component_name:
if data0!=None:
data0 = ' '.join(cmd[2:])
else:
pos = np.array([coord(window0[i]) for i in [0, 1]])
pos = rotate_vec(pos, angle)+coords
if window0[2] == "VBottom": window0[2] = [0, -1]
elif window0[2] == "VTop": window0[2] = [0, 1]
elif window0[2] == "Left": window0[2] = [-1, 0]
elif window0[2] == "Right": window0[2] = [1, 0]
text_window( ' '.join(cmd[2:]), pos, angle, align_vec=window0[2])
elif cmd[1] == "Value" and component_value:
if data3!=None:
data3 = ' '.join(cmd[2:])
else:
pos = [coord(window3[i]) for i in [0, 1]]
pos = rotate_vec(pos, angle)+coords
if window3[2] == "VBottom": window3[2] = [0, -1]
elif window3[2] == "VTop": window3[2] = [0, 1]
elif window3[2] == "Left": window3[2] = [-1, 0]
elif window3[2] == "Right": window3[2] = [1, 0]
text_window( ' '.join(cmd[2:]), pos, angle, align_vec=window3[2])
elif cmd[0]=="WINDOW":
if cmd[1] == "0":
window0 = cmd[2:]
data0 = None
elif cmd[1] == "3":
window3 = cmd[2:]
data3 = None
elif cmd[0] not in {"WINDOW"}:
break
draw_symbol(symfile, coords, angle, data0, data3)
# if "Voltage" in cmd[1]:
# print(f"({coords[0]},{coords[1]}) to [V,v<=$V_1$,rotate={angle}] ({coords[0]},{coords[1]})")
tikz_add(";")
def post_process_circuit():
global TIKZCODE
for node, degree in node_degree.items():
if degree > 1:
draw_symbol("node.asy", node, 0, "", "")
def circ_to_latex(circuit, component_name=False, component_value=False, local_dir=".", entire_page=False):
res = ""
add_symbol("gnd.asy")
add_symbol("node.asy")
parse_circuit(circuit, component_name, component_value, local_dir)
post_process_circuit()
if entire_page: res += "\documentclass[tikz,border=5mm]{standalone}\n"
res += TIKZSET
if entire_page: res += "\n\\begin{document}"
res += "\\begin{tikzpicture}[yscale = -1]\n"
res += TIKZCODE
res += "\n\end{tikzpicture}"
if entire_page: res += "\end{document}"
return res
def detect_encoding(file_path, expected_str: str = '') -> str:
"""
Simple strategy to detect file encoding. If an expected_str is given the function will scan through the possible
encodings and return a match.
If an expected string is not given, it will use the second character is null, high chances are that this file has an
'utf_16_le' encoding, otherwise it is assuming that it is 'utf-8'.
:param file_path: path to the filename
:type file_path: str
:param expected_str: text which the file should start with
:type expected_str: str
:return: detected encoding
:rtype: str
"""
for encoding in ('utf-8', 'utf-16-le', 'cp1252', 'cp1250', 'shift-jis'):
try:
with open(file_path, 'r', encoding=encoding) as f:
lines = f.readlines()
f.seek(0)
except UnicodeDecodeError:
# This encoding didn't work, let's try again
continue
else:
if expected_str:
if not expected_str in lines[0]:
# File did not start with expected string
# Try again with a different encoding (This is unlikely to resolve the issue)
continue
if encoding == 'utf-8' and lines[0][1] == '\x00':
continue
return encoding
else:
raise UnicodeError("Unable to detect log file encoding")
def open_file(file):
with open(file, "rb") as f:
return decode_data(f.read()), os.path.dirname(os.path.realpath(file))
def decode_data(data):
return data.decode(detect_encoding(sys.argv[1], "Version"))
# data = data.replace(b'\x00', b'')
try:
circuit = data.decode('utf-16-le')
print(circuit)
except:
try:
circuit = data[1:].decode('utf-8')
except:
circuit = data[2:].decode('utf-8')
return circuit
# def try_to_read(file):
if __name__=="__main__":
try:
import pyperclip
clipboard = True
except:
clipboard = False
print("If you want the result copied to your clipboard, run [pip install pyperclip]")
circuit, dir = open_file(sys.argv[1])
NO_COMMENTS_AND_COMMANDS = True
res = circ_to_latex(circuit, component_name=False, component_value=False, local_dir=dir, entire_page=True)
if clipboard:
pyperclip.copy(res)
print("latex code copied to clipboard")
else:
print("\n\n\n", res)