Skip to content

Commit

Permalink
Merge pull request #1210 from ra3xdh/944_splibcomp_library
Browse files Browse the repository at this point in the history
Create Library from Spice library device
  • Loading branch information
ra3xdh authored Jan 19, 2025
2 parents d609a25 + 2261b6c commit a09b9c6
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 124 deletions.
1 change: 1 addition & 0 deletions qucs/components/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Component : public Element {
virtual QString getProbeVariable(bool isXyce = false);
virtual QString getSpiceModel();
virtual QString getSpiceLibrary() { return QString(); }
virtual QStringList getSpiceLibraryFiles() { return QStringList(); }
virtual QString getNgspiceBeforeSim(QString sim, int lvl=0);
virtual QString getNgspiceAfterSim(QString sim, int lvl=0);
virtual QString getVAvariables() {return QString();};
Expand Down
44 changes: 21 additions & 23 deletions qucs/components/libcomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,30 +385,28 @@ QString LibComp::cdl_netlist()
return spice_netlist(spicecompat::CDL);
}

QStringList LibComp::getAttachedIFS()
QString LibComp::getSpiceLibrary()
{
QString content;
QStringList includes,attach,ifs_lst;
ifs_lst.clear();

int r = loadSection("Spice",content,&includes,&attach);
if (r<0) return ifs_lst;
for (const QString& file : attach) {
if (file.endsWith(".ifs")) ifs_lst.append(getSubcircuitFile()+'/'+file);
}
return ifs_lst;
}
QStringList files;
QString content;
QStringList includes,attach;

QStringList LibComp::getAttachedMOD()
{
QString content;
QStringList includes,attach,mod_lst;
mod_lst.clear();

int r = loadSection("Spice",content,&includes,&attach);
if (r<0) return mod_lst;
for (const QString& file : attach) {
if (file.endsWith(".mod")) mod_lst.append(getSubcircuitFile()+'/'+file);
int r = loadSection("Spice",content,&includes,&attach);
if (r<0) {
return QString();
}
for (const auto &file : attach) {
if (file.endsWith(".cir", Qt::CaseInsensitive) ||
file.endsWith(".ckt", Qt::CaseInsensitive) ||
file.endsWith(".lib", Qt::CaseInsensitive) ||
file.endsWith(".sp", Qt::CaseInsensitive)) {
files.append(getSubcircuitFile()+'/'+file);
}
return mod_lst;
}

QString s;
for (const auto &file: files) { // for netlist
s += QStringLiteral(".INCLUDE \"%1\"\n").arg(file);
}
return s;
}
3 changes: 1 addition & 2 deletions qucs/components/libcomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class LibComp : public MultiViewComponent {

bool createSubNetlist(QTextStream *, QStringList&, int type=1);
QString getSubcircuitFile();
QStringList getAttachedMOD(); // LibComp can reference cfunc.mod and ifspec.ifs
QStringList getAttachedIFS(); // These should be compiled before SPICE simulation
QString getSpiceLibrary();

protected:
QString netlist();
Expand Down
17 changes: 17 additions & 0 deletions qucs/dialogs/librarydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ LibraryDialog::LibraryDialog(QWidget *parent)
hCheck->addStretch();
connect(checkDescr, SIGNAL(stateChanged(int)), this, SLOT(slotCheckDescrChanged(int)));

checkAnalogLib = new QCheckBox(tr("Analog models only"));
checkAnalogLib->setChecked(true);
selectSubcktLayout->addWidget(checkAnalogLib);

// ...........................................................
QGridLayout *gridButts = new QGridLayout();
selectSubcktLayout->addLayout(gridButts);
Expand Down Expand Up @@ -508,10 +512,22 @@ void LibraryDialog::slotSave()
}
kern->createSubNetlsit(ts,true);
intoStream(Stream, tmp, "Spice");

QStringList libs = kern->collectSpiceLibraryFiles(Doc);
QStringList copiedFiles;
for (QString &file: libs) {
QString ofile = file;
intoFile(file, ofile, copiedFiles);
}
if (!copiedFiles.isEmpty()) {
Stream << "<SpiceAttach \"" << copiedFiles.join("\" \"")
<< "\">\n";
}
delete kern;
QucsSettings.DefaultSimulator = sim;
//}

if (!checkAnalogLib->isChecked()) {
// save verilog model
tmp.truncate(0);
Doc->setIsVerilog(true);
Expand Down Expand Up @@ -586,6 +602,7 @@ void LibraryDialog::slotSave()
else {
ErrText->insertPlainText("\n");
}
}

Stream << " <Symbol>\n";
Doc->createSubcircuitSymbol();
Expand Down
1 change: 1 addition & 0 deletions qucs/dialogs/librarydialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private slots:
QStringList SelectedNames;
QStringList Descriptions;
QCheckBox *checkDescr;
QCheckBox *checkAnalogLib;

QFile LibFile;
QDir LibDir;
Expand Down
27 changes: 27 additions & 0 deletions qucs/extsimkernels/abstractspicekernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1595,3 +1595,30 @@ QString AbstractSpiceKernel::collectSpiceLibs(Schematic* sch)
return collected_spicelib.join("");
}


QStringList AbstractSpiceKernel::collectSpiceLibraryFiles(Schematic *sch)
{
QStringList collected_spicelib;
for(Component *pc = sch->a_DocComps.first(); pc != 0; pc = sch->a_DocComps.next()) {
QStringList new_libs;
if (pc->Model == "Sub") {
Schematic *sub = new Schematic(nullptr, ((Subcircuit *)pc)->getSubcircuitFile());
if(!sub->loadDocument()) // load document if possible
{
delete sub;
continue;
}
new_libs = collectSpiceLibraryFiles(sub);
delete sub;
} else {
new_libs = pc->getSpiceLibraryFiles();
}
for (const auto&lib: new_libs) {
if (!collected_spicelib.contains(lib)) {
collected_spicelib.append(lib);
}
}
}
return collected_spicelib;
}

1 change: 1 addition & 0 deletions qucs/extsimkernels/abstractspicekernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class AbstractSpiceKernel : public QObject
virtual void SaveNetlist(QString filename);
virtual bool waitEndOfSimulation();
void setConsole(QPlainTextEdit *console) { a_console = console; }
QStringList collectSpiceLibraryFiles(Schematic *sch);
static QString collectSpiceLibs(Schematic* sch);

signals:
Expand Down
11 changes: 0 additions & 11 deletions qucs/mouseactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,17 +918,6 @@ void MouseActions::rightPressMenu(Schematic *Doc, QMouseEvent *Event, float fX,
SLOT(slotSaveDiagramToGraphicsFile()));
ComponentMenu->addAction(actExport);
}
/*if (focusElement->Type & isComponent) {
Component *pc = (Component *)focusElement;
if (pc->Model == "EDD") {
QAction *actEDDtoIFS = new QAction(QObject::tr("Create XSPICE IFS"), QucsMain);
QObject::connect(actEDDtoIFS,SIGNAL(triggered(bool)),QucsMain,SLOT(slotEDDtoIFS()));
ComponentMenu->addAction(actEDDtoIFS);
QAction *actEDDtoMOD = new QAction(QObject::tr("Create XSPICE MOD"), QucsMain);
QObject::connect(actEDDtoMOD,SIGNAL(triggered(bool)),QucsMain,SLOT(slotEDDtoMOD()));
ComponentMenu->addAction(actEDDtoMOD);
}
}*/
}
break;
}
Expand Down
88 changes: 0 additions & 88 deletions qucs/qucs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3645,94 +3645,6 @@ void QucsApp::slotBuildVAModule()

}


/*void QucsApp::slotBuildXSPICEIfs(int mode)
{
if (!isTextDocument(DocumentTab->currentWidget())) {
Schematic *Sch = (Schematic*)DocumentTab->currentWidget();
QFileInfo inf(Sch->getDocName());
QString msg,ext;
switch(mode) {
case spicecompat::cmgenSUBifs:
case spicecompat::cmgenEDDifs: msg = inf.path()+QDir::separator()+inf.baseName()+".ifs";
ext = "XSPICE IFS (*.ifs)";
break;
case spicecompat::cmgenSUBmod:
case spicecompat::cmgenEDDmod: msg = inf.path()+QDir::separator()+inf.baseName()+".mod";
ext = "XSPICE MOD (*.mod)";
break;
default: break;
}
QString filename = QFileDialog::getSaveFileName(this,tr("Save XSPICE source"),msg,ext);
if (filename.isEmpty()) return;
QFile f(filename);
if (f.open(QIODevice::WriteOnly)) {
QTextStream stream(&f);
CodeModelGen *cmgen = new CodeModelGen;
bool r = false;
switch(mode) {
case spicecompat::cmgenSUBifs: r = cmgen->createIFS(stream,Sch);
case spicecompat::cmgenEDDifs: {
for(Component *pc = Sch->a_DocComps.first(); pc != 0; pc = Sch->a_DocComps.next()) {
if (pc->isSelected) {
r = cmgen->createIFSfromEDD(stream,Sch,pc);
break;
}
}
}
break;
case spicecompat::cmgenEDDmod : {
for(Component *pc = Sch->a_DocComps.first(); pc != 0; pc = Sch->a_DocComps.next()) {
if (pc->isSelected) {
r = cmgen->createMODfromEDD(stream,Sch,pc);
break;
}
}
}
break;
default: r = false;
break;
}
QString errs;
if (!r) errs = tr("Create XSPICE CodeModel"
"Create CodeModel source file failed!"
"Schematic is not subciruit!");
messageDock->reset();
messageDock->msgDock->setWindowTitle(tr("Debug messages dock"));
messageDock->builderTabs->setTabIcon(0,QPixmap());
messageDock->builderTabs->setTabText(0,tr("XSPICE"));
messageDock->builderTabs->setTabIcon(1,QPixmap());
messageDock->admsOutput->
insertPlainText(QStringLiteral("Creating XSPICE source file: %1\n").arg(filename));
errs += cmgen->getLog();
if (errs.isEmpty()) {
messageDock->admsOutput->insertPlainText(tr("Success!\n"));
} else {
messageDock->admsOutput->insertPlainText(errs);
}
messageDock->msgDock->show();
delete cmgen;
f.close();
}
}
}
void QucsApp::slotEDDtoIFS()
{
slotBuildXSPICEIfs(spicecompat::cmgenEDDifs);
}
void QucsApp::slotEDDtoMOD()
{
slotBuildXSPICEIfs(spicecompat::cmgenEDDmod);
}*/

void QucsApp::slotShowModel()
{
DisplayDialog *dlg = new DisplayDialog(this,Symbol->ModelString,
Expand Down
13 changes: 13 additions & 0 deletions qucs/spicecomponents/sp_include.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,16 @@ QString S4Q_Include::getSpiceLibrary()
return s;
}

QStringList S4Q_Include::getSpiceLibraryFiles()
{
QStringList files;
for (Property *pp : Props) {
QString val = pp->Value;
if (!val.isEmpty()) {
val = misc::properAbsFileName(val, containingSchematic);
files.append(val);
}
}
return files;
}

1 change: 1 addition & 0 deletions qucs/spicecomponents/sp_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class S4Q_Include : public Component {
Component* newOne();
static Element* info(QString&, char* &, bool getNewOne=false);
QString getSpiceLibrary();
QStringList getSpiceLibraryFiles();

protected:
QString vhdlCode(int) { return QString(); }
Expand Down
8 changes: 8 additions & 0 deletions qucs/spicecomponents/spicelibcomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,11 @@ QString SpiceLibComp::getSpiceLibrary()
QString s = QStringLiteral(".INCLUDE \"%1\"\n").arg(f);
return s;
}

QStringList SpiceLibComp::getSpiceLibraryFiles()
{
QString f = misc::properAbsFileName(getProperty("File")->Value, containingSchematic);
QStringList files;
files.append(f);
return files;
}
1 change: 1 addition & 0 deletions qucs/spicecomponents/spicelibcomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SpiceLibComp : public MultiViewComponent {
Component* newOne();
static Element* info(QString&, char* &, bool getNewOne=false);
QString getSpiceLibrary();
QStringList getSpiceLibraryFiles();

protected:
QString spice_netlist(spicecompat::SpiceDialect dialect = spicecompat::SPICEDefault);
Expand Down

0 comments on commit a09b9c6

Please sign in to comment.