In this tutorial, we will create an exporter for the following XYZ format: The 1st line: the number of atoms in the file. The 2nd line: arbitraty header line. Next lines contain information on atoms in the following format (coordinates are in angstroms): element_symbol x-coordinate y-coordinate z-coordinate.
Example of the format:
Note: SAMSON already has an XYZ Exporter which is installed by default, that's why we are actually going to read files with a exyz extension, even though the contents will still correspond to the xyz format.
The source code for an Extension created in this tutorial can be found at https://github.com/1A-OneAngstrom/SAMSON-Developer-Tutorials.
We will start by creating a new SAMSON Extension called EXYZ thanks to the SAMSON Extension generator (please, refer to the SAMSON Extension generator tutorial for a reminder on how to use it):
SEEXYZExporter
);The SAMSON Extension generator generates an exporter class (derived from the SBIFileExporter class) and a GUI class that implements the user interface of the exporter and is derived from the SBGFileExporter class.
In SAMSON, an exporter is created by programming three functions in the exporter class:
Let's open the SEEXYZExporter.cpp file and implement these functions.
Modify the getExtension
function to return the extension of the exporter (exyz) as follows:
Modify the getFilter
function to return the filter of the exporter as follows (it should be in the Qt format):
We now have to code the exportToFile
function to export atoms into a exyz file. To achieve this, we are going to use fstream
to create and write into a file, the SAMSON class to get atoms from the active document, and several structural classes. For that, include the following headers in the file:
Let's now code inside the exportToFile
function:
First, we find all atoms in the nodes that were passed to the exportToFile
function:
Recall that SBNode::IsType(SBNode::Atom)
is a predicate, passed to the getNodes
function, that returns true
for nodes that are atoms.
If the user saves data via File > Save or File > Save as... it will export all atoms from the active document, if the user saves data via File > Save selection as... it will export all atoms from the selection in the active document.
Then, we create the file and write its header:
In this tutorial, for simplicity we do not check for errors during the creation and writing of the file.
And we then write the description of each atom to the file:
Finally, we close the file and we signal that everything went fine (at least we believe so, since we didn’t test for errors in this tutorial):
That is it. You can now export into exyz format.
If you want for the Exporter to get some parameters/options from the user, please check out to the Importer tutorial: Getting parameters, if not then you can simply disable the options window by replacing the constructor and destructor of SEEXYZExporter
as follows: