-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqucsator.js
96 lines (86 loc) · 2.81 KB
/
qucsator.js
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
var inGroupsOf = function(ary, n) {
var grouped = [];
for(i in ary) {
// if (i % 3 == 0) { grouped[Math.floor(i / 3)] = []; }
if (!grouped[Math.floor(i / 3)]) { grouped[Math.floor(i / 3)] = []; }
grouped[Math.floor(i / 3)][i % 3] = ary[i];
}
return grouped;
}
var qucsate = function(netlist, callback, type) {
type = type || 'qucs';
var data = {};
data[type || 'qucs'] = netlist;
$.ajax({
async: false,
url: qucsate.serverUrl,
data: data,
success: qucsate.parser(callback)});
};
//
// Generate a parser, which is a function that parses the qucs data format
// and passes the data to the callback
//
qucsate.parser = function(callback) {
return(function(data) {
var results = {};
// for jsonp we simply put the whole string into the 'result' property of the json object
if ( data.result ) { data = data.result; }
var chunks = data.split("\n")
chunks = inGroupsOf(chunks.slice(1, chunks.length - 1), 3);
for (var i in chunks) {
var key = /<indep (.+)\./.exec(chunks[i][0]);
key = key && key[1];
if(key) {
results[key] = parseFloat(chunks[i][1]);
}
}
callback(results);
});
};
//
// make qucs netlists from breadboard objects
//
qucsate.makeNetlist = function(board) {
var netlist = '# QUCS Netlist\n';
$.each(board.components, function(name, component) {
var line = '';
// Convert the connections object to an array of qucs node names
var nodes = [];
$.each(component.connections, function(i, hole){
nodes.push(hole.strip.name);
});
switch (component.kind) {
case "resistor":
if (!(nodes.length == 2 && component.resistance && component.UID)) { return; }
line = 'R:' + component.UID + ' ';
line = line + nodes.join(' ');
line = line + ' R="' + component.resistance + ' Ohm"' ;
break;
case "wire":
if (!(nodes.length == 2 && component.UID)) { return; }
line = 'TLIN:' + component.UID + ' ';
line = line + nodes.join(' ');
line = line + ' Z="0 Ohm" L="1 mm" Alpha="0 dB"' ;
break;
case "battery":
if (!(nodes.length == 2 && component.voltage && component.UID)) { return; }
line = 'Vdc:' + component.UID + ' ';
line = line + nodes.join(' ');
line = line + ' U="' + component.voltage + ' V"' ;
break;
case "vprobe":
if (!(nodes.length == 2 && component.UID)) { return; }
line = 'VProbe:' + component.UID + ' ';
line = line + nodes.join(' ');
break;
case "iprobe":
if (!(nodes.length == 2 && component.UID)) { return; }
line = 'IProbe:' + component.UID + ' ';
line = line + nodes.join(' ');
break;
}
netlist = netlist + "\n" + line;
});
return netlist + "\n.DC:DC1";
}