Importing and exporting data

In this tutorial, we create an importer and an exporter for the xyz format.

Start by creating a new SAMSON Element called XYZ with the SAMSON Element Generator, and add an importer class (called SEXYZImporter) and an exporter class (SEXYZExporter).

Despite what the names suggest, this tutorial is rated E, for Everyone.

Parsing a xyz file

In SAMSON, an importer is created by programming three functions: getExtension, getFilter and importFromFile. Since you might have an xyz importer already, we’re actually going to read files with a xyzt extension (t for tutorial), even though the contents will still correspond to the xyz format.

Thus, modify the getExtension function in the SEXYZImporter.cpp file as follows:

std::string SEXYZImporter::getExtension() const {
	return std::string("xyzt");

and the getFilter function as follows:

std::string SEXYZImporter::getFilter() const {
	return std::string("XYZT format (*.xyzt)");

We now have to code the importFromFile function to read a molecule from a xyzt file and create a structural model. To achieve this, we are going to use the SBIFileReader class to read a file, the SAMSON class to manage the undo mechanism and access the active document, and several structural classes:

#include "SAMSON.hpp"
#include "SBIFileReader.hpp"
#include "SBMStructuralModel.hpp"
#include "SBMStructuralModelNodeRoot.hpp"
#include "SBMStructuralModelNodeAtom.hpp"

We now code inside the importFromFile function. We first begin by opening the file and store all its lines in a vector of strings:

	// get file lines
	std::vector<std::string> fileLineVector;
	SBIFileReader::getFileLines(fileName, fileLineVector);

Then, we read the number of atoms in the file:

	// read the number of atoms from the first line
	unsigned int numberOfAtoms = 0;
	std::stringstream numberOfAtomsParser(fileLineVector[0]);
	numberOfAtomsParser >> numberOfAtoms;
	::(fileLineVector[0][ numberOfAtoms = [(0[string[[);

( from a xyzt ned it

To adl still c>le and create aatoms in the file:

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 008080;">;
	const (unsigned = )(::((0[() {mily:monospace;">	0::[
	std::str nedan> numberOfAtomsParser(fileLineVector[]) numberOfAtomsParser 0{mily:monospace;">	:: numberOfAtoms ::([0const string::getExtension) {mfilcolo00;">[= [::(::(::(000[string((0string[])) ) ;

( nu">;<=tructural mi]);tFilt nedan> num#008080;" nedTypFm#008080;"xm#008080;"ym#008080;"z;tFilt colneratu-itstill cate atFilt lson-sdk::TypFmuting-na=0 instaing SEting-nTypFBySymbol( nedTypF)numb if (uting-na!= lson-sdk::Unknow7)getFilt filcolo*fspacoloa=art bylcolo(uting-n,8;0Quav cla::ar: #0om(x), ="ent;0Quav cla::ar: #0om(y), ="ent;0Quav cla::ar: #0om(z))numb berclude "S-colog Sinclude &qruct()-colonerCr me(spacolo);tFilt : 0 ! xyzt)"); }orter alrealmostisplaytstiadl sl c>le and create atstill clement('aromFilrom ovalg-nabond vector of strings:


1 2 3 4 5 6 7 8 9 10 11 008080;">;
	std::string[0[string[(0const = 0::((0string[[00string[[0

le and create atnsionts, even th. leRe 6 setsasl cate atstia ad f, an im with 7 pae (i.e. not erased). Fin we&,and sto8tnsio9 e-thrng-nasl cexwe aimpoehavis linea>

In SA, whichMSONsti

In tstill cm and alaynumby s="page,asslmechanal ovelaynum(td fe adLaynu) haspoeedd;we ifiedto the xyFin we&,aTo had noMSOsuelement callement(' (itent"> vntaion and ins

In SA, walle not decreementortant; }spla/">


E">Importarsing a E">ImportMSONsh/divvntaeWe ftant; To achiei foraandstlinesele aimpnn-lie actiweZImportNsh/mtstia >;<=to the xyFirt that.

Thus, modify actidiv>

and the ghe number exporter clahe SEXYZImpotFrodide number expoion in the SEXYZI(sespaee functionsespaf

ss (SEXYZE);

Root.hppmber To ugh tneed>

We now ImportTode the importvector of strings:

(#include "SAMSON.hpp"
#include "SBIFileReader.hpp"
#include "SBMStructuralModel.hpp"
#include "SBMStructuralModelNodeRoot.hpp"
#include "SBMStructuralModelNodeAtom.hpp"


er() csole e/em> nn-liit

achiad ths (SEXYZE);

n this tutbegin by openwrter)nts,
	Pa000dd;">0::string::string(0)	string::((string::(		// read tholor: #00 of  8;">string(string::(;

// read th geeof clt;colo0color:the-samsmportModelN clt;colo0erOfAendl; fileLineVectoropen

u-itstill c of atoms in the file:

1 2 3 4 5 6 7 8 9 10 11 12 13 008080;">;
	const (unsigned = 0::((0[() {milcolo00;">[= [stringstringstring[])) [ numberOfAtoms [string[(0string::([stringstring(0string(string::(::([0]::((string(string::(::([0]::((string(string::(::([)]::((string(string


n than0ady, we&#removorll cophe ghewindowit

appears imm da/pte&#befg thvelopers- m> s">Import,mby reeCocrst begicolorcluds line expoion in tetFilter function as follows:

string SEXYZImporter::string((string{pre fttyD	 numberOfAtoms = 0;

std::strinne;">std::str()getFiltpre fttyD

strter claan> SEXYZImporter::strter claa #008000;">((string{pre fttyD	 numberOfAtoms = 0;

rter clainne;">rter cla()getFiltpre fttyD need> rcluds slinediv everal structural classes:

string SEXYZImporterstring SEXYZImporter(string>;

std::strin~ne;">std::str()getFil;xyzt"); }

strter claan> SEXYZImporterstrter claan> SEXYZImporter(string>;

rter clain~ne;">rter cla()getFil;xyzt"); W mmayu/p>In to the xy;">o the xy;">o the dataan> &nbs

post-## div cla/nav>> div an> &nbs