29 #include <qstringlist.h>
30 #include <qmessagebox.h>
31 #include <qtextstream.h>
34 #include <qfileinfo.h>
48 Props.append(
new Property(
"Sim",
"yes",
false, QString(
"x")));
49 Props.append(
new Property(
"Preprocessor",
"none",
false, QString(
"x")));
71 Name = QObject::tr(
"SPICE netlist");
72 BitmapFile = (
char *)
"spicefile";
86 int fHeight = metrics.lineSpacing();
89 QString tmp, PortNames =
Props.at(1)->Value;
90 if(!PortNames.isEmpty()) No = PortNames.contains(
',') + 1;
93 int h = 30*((No-1)/2) + 15;
102 tmp = QObject::tr(
"sim");
103 w = metrics.width(tmp);
104 Texts.append(
new Text(w/-2, 0, tmp, Qt::red));
106 tmp = QObject::tr(
"spice");
107 w = metrics.width(tmp);
116 tmp = PortNames.section(
',', i, i).mid(4);
117 w = metrics.width(tmp);
118 Texts.append(
new Text(-20-w, y-fHeight-2, tmp));
124 tmp = PortNames.section(
',', i, i).mid(4);
125 Texts.append(
new Text( 20, y-fHeight-2, tmp));
131 Lines.append(
new Line( 0, h, 0,h+15,QPen(QPen::darkBlue,2)));
140 ty =
y1 - fHeight - 4;
141 if(
Props.first()->display)
ty -= fHeight;
148 if(
Props.at(1)->Value.isEmpty())
151 QString
s =
"Sub:"+
Name;
153 s +=
" "+pp->Connection->Name;
164 QString FileName =
Props.getFirst()->Value;
172 QString FileName =
Props.first()->Value;
173 if(FileName.isEmpty()) {
174 ErrText += QObject::tr(
"ERROR: No file name in SPICE component \"%1\".").
182 SpiceFile.setName(FileName);
183 if(!SpiceFile.open(IO_ReadOnly)) {
184 ErrText += QObject::tr(
"ERROR: Cannot open SPICE file \"%1\".").
189 QString ConvName = SpiceFile.name() +
".lst";
190 ConvFile.setName(ConvName);
191 QFileInfo Info(ConvName);
194 if(changed || !ConvFile.exists() ||
195 (lastLoaded.isValid() && lastLoaded < Info.lastModified())) {
196 if(!ConvFile.open(IO_WriteOnly)) {
198 QObject::tr(
"ERROR: Cannot save converted SPICE file \"%1\".").
199 arg(FileName +
".lst");
203 filstream =
new QTextStream(&ConvFile);
204 QString SpiceName = SpiceFile.name();
205 bool ret = recreateSubNetlist(&SpiceName, &FileName);
212 if(!ConvFile.open(IO_ReadOnly)) {
214 QObject::tr(
"ERROR: Cannot open converted SPICE file \"%1\".").
215 arg(FileName +
".lst");
218 QByteArray FileContent = ConvFile.readAll();
220 stream->writeRawBytes(FileContent.data(), FileContent.size());
225 bool SpiceFile::recreateSubNetlist(QString *
SpiceFile, QString *FileName)
234 if(
Props.at(1)->Value !=
"")
235 makeSubcircuit =
true;
237 makeSubcircuit =
false;
238 if(
Props.at(2)->Value ==
"yes")
244 QString preprocessor =
Props.at(3)->Value;
245 if (preprocessor !=
"none") {
249 QString interpreter =
"tinyperl.exe";
251 QString interpreter =
"perl";
253 if (preprocessor ==
"ps2sp") {
255 }
else if (preprocessor ==
"spicepp") {
256 script =
"spicepp.pl";
257 }
else if (preprocessor ==
"spiceprm") {
262 SpicePrep =
new QProcess(
this);
263 SpicePrep->addArgument(interpreter);
264 SpicePrep->addArgument(script);
265 SpicePrep->addArgument(*SpiceFile);
268 QString PrepName = *SpiceFile +
".pre";
271 SpicePrep->addArgument(PrepName);
272 connect(SpicePrep, SIGNAL(readyReadStdout()), SLOT(slotSkipOut()));
273 connect(SpicePrep, SIGNAL(readyReadStderr()), SLOT(slotGetPrepErr()));
275 connect(SpicePrep, SIGNAL(readyReadStdout()), SLOT(slotGetPrepOut()));
276 connect(SpicePrep, SIGNAL(readyReadStderr()), SLOT(slotGetPrepErr()));
279 QMessageBox *MBox =
new QMessageBox(QObject::tr(
"Info"),
280 QObject::tr(
"Preprocessing SPICE file \"%1\".").
arg(*SpiceFile),
281 QMessageBox::NoIcon, QMessageBox::Abort,
282 QMessageBox::NoButton, QMessageBox::NoButton, 0, 0,
true,
283 Qt::WStyle_DialogBorder | Qt::WDestructiveClose);
284 connect(SpicePrep, SIGNAL(processExited()), MBox, SLOT(close()));
287 PrepFile.setName(PrepName);
288 if(!PrepFile.open(IO_WriteOnly)) {
290 QObject::tr(
"ERROR: Cannot save preprocessed SPICE file \"%1\".").
294 prestream =
new QTextStream(&PrepFile);
297 if(!SpicePrep->start()) {
298 ErrText += QObject::tr(
"ERROR: Cannot execute \"%1\".").
299 arg(interpreter +
" " + script +
"\".");
306 SpicePrep->closeStdin();
314 *SpiceFile = PrepName;
320 if(makeSubcircuit) com <<
"-g" <<
"_ref";
321 com <<
"-if" <<
"spice" <<
"-of" <<
"qucs";
328 QString PortNames =
Props.at(1)->Value;
329 PortNames.replace(
',',
' ');
330 NetText += PortNames;
331 if(makeSubcircuit) NetText +=
" _ref";
336 QucsConv =
new QProcess(
this);
337 QucsConv->setArguments(com);
338 connect(QucsConv, SIGNAL(readyReadStdout()), SLOT(slotGetNetlist()));
339 connect(QucsConv, SIGNAL(readyReadStderr()), SLOT(slotGetError()));
340 connect(QucsConv, SIGNAL(processExited()), SLOT(slotExited()));
341 if(!QucsConv->start()) {
342 ErrText += QObject::tr(
"ERROR: Cannot start QucsConv!");
345 (*outstream) << NetText;
346 (*filstream) << NetText;
347 QucsConv->closeStdin();
350 QMessageBox *MBox =
new QMessageBox(QObject::tr(
"Info"),
351 QObject::tr(
"Converting SPICE file \"%1\".").
arg(*SpiceFile),
352 QMessageBox::NoIcon, QMessageBox::Abort,
353 QMessageBox::NoButton, QMessageBox::NoButton, 0, 0,
true,
354 Qt::WStyle_DialogBorder | Qt::WDestructiveClose);
355 connect(QucsConv, SIGNAL(processExited()), MBox, SLOT(close()));
360 lastLoaded = QDateTime::currentDateTime();
365 void SpiceFile::slotSkipErr()
367 SpicePrep->readStderr();
371 void SpiceFile::slotSkipOut()
373 SpicePrep->readStdout();
377 void SpiceFile::slotGetPrepErr()
379 ErrText += QString(SpicePrep->readStderr());
383 void SpiceFile::slotGetPrepOut()
385 (*prestream) << QString(SpicePrep->readStdout());
389 void SpiceFile::slotGetError()
391 ErrText += QString(QucsConv->readStderr());
395 void SpiceFile::slotGetNetlist()
399 NetLine += QString(QucsConv->readStdout());
401 while((i = NetLine.find(
'\n')) >= 0) {
403 NetLine.remove(0, i+1);
404 s = s.stripWhiteSpace();
407 }
else if(s.isEmpty()) {
409 }
else if(s.at(0) ==
'.') {
410 if(s.left(5) !=
".Def:") {
411 if(insertSim) SimText += s +
"\n";
419 (*outstream) << s <<
"\n";
420 (*filstream) << s <<
"\n";
425 void SpiceFile::slotExited()
427 if (!QucsConv->normalExit()) {
432 (*outstream) <<
".Def:End\n\n";
433 (*filstream) <<
".Def:End\n\n";
435 if(!SimText.isEmpty()) {
436 (*outstream) << SimText;
437 (*filstream) << SimText;