-
Notifications
You must be signed in to change notification settings - Fork 912
Expand file tree
/
Copy pathgetspice.cpp
More file actions
112 lines (102 loc) · 3.82 KB
/
getspice.cpp
File metadata and controls
112 lines (102 loc) · 3.82 KB
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
/*******************************************************************
skw
Part of the Fritzing project - http://fritzing.org
Copyright (c) 2023 Fritzing
Fritzing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Fritzing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fritzing. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/
#include "getspice.h"
#include "../debugdialog.h"
#include "../connectors/connectoritem.h"
#include "../items/propertydef.h"
#include "../model/modelpart.h"
#include "../utils/textutils.h"
QString GetSpice::getSpice(ItemBase * itemBase, const QList< QList<class ConnectorItem *>* >& netList) {
static QRegularExpression curlies("\\{([^\\{\\}]*)\\}");
QString spice = itemBase->spice();
int pos = 0;
while (true) {
QRegularExpressionMatch match;
pos = spice.indexOf(curlies, pos, &match);
if (pos < 0) break;
QString token = match.captured(1).toLower();
QString replacement;
if (token == "instancetitle") {
replacement = itemBase->instanceTitle();
if (pos > 0 && replacement.at(0).toLower() == spice.at(pos - 1).toLower()) {
// if the type letter is repeated
replacement = replacement.mid(1);
}
replacement.replace(" ", "_");
}
else if (token.startsWith("net ")) {
QString cname = token.mid(4).trimmed();
Q_FOREACH (ConnectorItem * ci, itemBase->cachedConnectorItems()) {
if (ci->connectorSharedID().toLower() == cname) {
int ix = -1;
Q_FOREACH (QList<ConnectorItem *> * net, netList) {
ix++;
if (net->contains(ci)) break;
}
replacement = QString::number(ix);
break;
}
}
}
else {
//Find the symbol of this property
QString symbol;
QHash<PropertyDef *, QString> propertyDefs;
PropertyDefMaster::initPropertyDefs(itemBase->modelPart(), propertyDefs);
foreach (PropertyDef * propertyDef, propertyDefs.keys()) {
if (token.compare(propertyDef->name, Qt::CaseInsensitive) == 0) {
symbol = propertyDef->symbol;
break;
}
}
//Find the value of the property
QVariant variant = itemBase->modelPart()->localProp(token);
if (variant.isNull()) {
auto prop = itemBase->modelPart()->properties();
replacement = prop.value(token, "");
QList<QString> knownTokens;
knownTokens << "inductance" << "resistance" << "current" << "tolerance" << "power" << "capacitance" << "voltage";
if(replacement.isEmpty()) {
if (prop.contains(token) || knownTokens.contains(token)) {
// Property exists but is empty. Or it is one of a few known tokens. Just assume zero.
replacement = "0";
} else {
//Leave it, probably is a brace expresion for the spice simulator
replacement = match.captured(0);
continue;
}
}
}
else {
replacement = variant.toString();
}
//Remove the symbol, if any. It is not mandatory:
//(Ngspice ignores letters immediately following a number that are not scale factors)
if (!symbol.isEmpty()) {
replacement.replace(symbol, "");
}
//Ngspice does not differenciate from m and M prefixes, u shuld be used for micro
replacement.replace("M", "Meg");
replacement.replace(TextUtils::MicroSymbol, "u");
replacement.replace(TextUtils::AltMicroSymbol, "u");
replacement.replace("μ", "u");
replacement.replace("Ω", "");
}
spice.replace(pos, match.captured(0).size(), replacement);
DebugDialog::debug("spice " + spice);
}
return spice;
}