C# graph/circuit editor, phase 6: export to C++ solver

Hello,
finally, after a long period of inactivity, due to job commitments, a new small step has been accomplished.

Although an easier calculation of the pneumatic/hydraulic circuit could be performed inside the C# program, I preferred assigning the task to an external C++ program for some reasons.

First, C++ has a more complete operator overloading than C# (see Matrix class post). Second, it’s faster than C#. Third, it’s an opportunity to refresh modern C++ programming (from C++11 up to C++20).

The C# program exports all the meaningful data into a text file (this is not the Save command, which uses JsonSerializer from System.Text.Json library), then makes a System call to launch the C++ program, with arguments.

private bool CalcolaCircuito()
	{
	bool ok;
	string filename;
	
	ok = EsportaCircuito(out filename);
	if(ok)
		{
		StringBuilder solverArg = new StringBuilder();
		solverArg.Append(filename);

		ProcessStartInfo pi = new ProcessStartInfo();

		pi.UseShellExecute = true;
		pi.FileName = solverExe;
		pi.Arguments = solverArg.ToString();

		Process p = new Process();

		p.StartInfo = pi;
		p.Exited += new EventHandler(AfterExeExit);
		p.EnableRaisingEvents = true;

		p.Start();
		}

	return ok;
	}

Then the C++ program retrieves the argument list and uses it to analyze and import data into the Circuito class.




// Solver.cpp : Questo file contiene la funzione 'main', in cui inizia e termina l'esecuzione del programma.


#include <iostream>				// Per cout e i/o
#include <sstream>				// Per getline()
#include <windows.h>			// Per Sleep()

#include "DataRW.h"
#include "Circuito.h"

#pragma region USING
// Namespace necessari (evitare: using namespace std;)
using std::cout;
using std::endl;
using std::getchar;
using std::string;
using std::ifstream;
using std::getline;
using std::tuple;
using namespace Circ;
#pragma endregion

//class Circuito;



int main(int argc, char *argv[])
	{

	Circuito circuito;

	string txt;
	DataRW *drw = (DataRW*) nullptr;
	
	bool ok = false;

    cout << "SOLVER\n";
	cout << "Argomenti: " << argc << endl;

	if (argc > 0)										// Mostra gli argomenti
		{
		for (int i = 0; i < argc; i++)
			{
			cout << " [" << i << "]\t" << argv[i] << endl;
			}
		}

	if ((argc > 1) && (strlen(argv[1]) > 1))			// Se ci sono almeno due argomenti ed il secondo non è nullo...
		{
		drw = new DataRW(argv[1], circuito.Import, &circuito);	// ...alloca un oggetto DataRW per la lettura file, passando il circuito in reference
		}
	
	if(drw != (DataRW*)nullptr)						// Legge i dati
		{
		ok = drw->ReadData();
		}

	Sleep(500);										// Pausa (per debug)

	if (drw != (DataRW*)nullptr)					// Dealloca oggetto DataRW
		{
		delete drw;
		}

	if(ok)
	{
		cout << endl << circuito.ToString() << endl;

		if(!circuito.CheckPrCircuito())
		{
			cout << endl << "CheckPrCircuito() non superato: errori." << endl;
			ok = false;
		}
		else
		{
			cout << endl << "CheckPrCircuito() ok." << endl;
		}
		
		if (!circuito.CheckPrNodi())
		{
			cout << endl << "CheckPrNodi() non superato: errori." << endl;
			ok = false;
		}
		else
		{
			cout << endl << "CheckPrNodi() ok." << endl ;
		}

		if (!circuito.CheckPrRami())
		{
			cout << endl << "CheckPrRami() non superato: errori." << endl;
			ok = false;
		}
		else
		{
			cout << endl << "CheckPrRami() ok." << endl;
		}
		
	}
	else
	{
		cout << endl << "Errore nella lettura del file" << endl;
	}

	
	//#ifdef _DEBUG
	cout << endl << "<enter> per chiudere" << endl;
	char x = getchar();
	//#endif // DEBUG

	return 0;
	}

Properties are stored in maps, using the property name as the search key. Of course, for calculation, data will be transferred into vector<> containers, faster than maps, and Circuito will be deallocated.

This is the outcome of the two programs working together:

I would strongly suggest the wonderful ‘A Tour of C++‘, third edition (by Bjarne Stroustrup, the creator of C++) for its conciseness: a must-to-have reading, ideal for programmers who want to stay updated with modern C++ features.

Best regards and Merry Christmas.