Export selection to file
/** * Export selected rows button** Usage:* 1. edit the settings* 2. perform a selection of rows* 3. execute button script** Script will open a "save dialog" for you, where you will need to specify* the filename, including the file extension.** Please use file extensions supported by MolExporter https://docs.chemaxon.com/display/docs/Molecule+Formats** @author David Pech <[email protected]>* improvements of "Export data to SD file" example written by Tim Dudgeon* https://docs.chemaxon.com/display/docs/Simple+SDF+Exporter*/import com.im.df.api.*import com.im.commons.progress.*import chemaxon.struc.Moleculeimport chemaxon.formats.MolExporter;import javax.swing.filechooser.FileFilterimport javax.swing.JFileChooser // -------------------------------- settings -------------------------------------// define what fields to exportdef FIELDS_FROM_PARENT = [ 'Formula', 'Donors', 'Acceptors']// define how the fields are going to be renamed (optional)def FIELD_NAMES = ['Donors':'My Lovely Donors', 'Acceptors':'My Ugly Acceptors']// define the name of filed containing structuredef STRUCTURE_FIELD = 'Structure'// ------------------- probably not needed to edit anything below ----------------init = { widget ->}destroy = { widget ->}evaluate = { widget ->    // get to root entity    def ety = dataTree.rootVertex.entity // assumes you have reference to the data tree    // get the ID field    def fldId = ety.idField    println "Found ID field ${fldId.id}"    // get the Structure field    def molFld = ety.fields.items.find { it.name == STRUCTURE_FIELD }     println "Found MOL field ${molFld.id}"    // Get resultsets    def rs = ety.schema.dataProvider.getDefaultResultSet(dataTree, false, DFEnvironmentRO.DEV_NULL) // find the ResultSet    def rootVS = rs.getVertexState(dataTree.rootVertex) // obtain the VertexState    List ids = rootVS.getSelectedRowsIds() // get the IDs of the selection    // if you want to export all rows always, change the previous line to    // Lists ids = rootVS.ids    println "Found ${ids.size} lines to export"    // data fields from parent    def fieldsFromParent = [ ]    FIELDS_FROM_PARENT.each { name ->        def fld = ety.fields.items.find { it.name == name }        if (fld) {            fieldsFromParent << fld            println "Found parent field ${fld.id} for $name"        } else {            println "WARNING: field $name not found"        }    }        // lets choose location where to save the file (file format should be supported by https://docs.chemaxon.com/display/docs/Molecule+Formats)    def saveFileDialog = new JFileChooser(    dialogTitle: "Choose save location",    fileSelectionMode: JFileChooser.FILES_ONLY)    saveFileDialog.showSaveDialog()    // get the full path where to save the file to    def FILE_NAME = saveFileDialog.getSelectedFile().getAbsolutePath()    // get the extension of the filename     def EXTENSION = FILE_NAME.lastIndexOf('.').with {it != -1 ? FILE_NAME.substring(it+1):''}    // lets read the data    def good = 0    def bad = 0    // and initialize exporter    // if you need simpler solution, then FILE_NAME and EXTENSION can be hardcoded    // and saveFileDialog above can be removed    // coud be used like this instead:    // def exporter = new MolExporter('C:\path\to\filename.sdf','sdf')    def exporter = new MolExporter(FILE_NAME, EXTENSION)    try {    ids.each { id ->        try {            def data = rootVS.getData([id], DFEnvironmentRO.DEV_NULL)            // get the mol            def mol = data[id][molFld.id]            // get the other fields            def values = [ : ]            fieldsFromParent.each {                values.put(it, data[id][it.id])            }            println "Exporting ID $id"            def expMol            // work with a clone so we don't alter the original            if (!mol || !mol.native ) {                expMol = new Molecule()            } else {                expMol = mol.native.cloneMolecule()            }            values.each { k,v ->                if (v != null) {                    def pName = (FIELD_NAMES[k.name] == null ? k.name : FIELD_NAMES[k.name])                    expMol.setProperty(pName, v.toString())                }            }            exporter.write(expMol)            good++        } catch (Exception exc) {            println "EROROR Failed to load ID $id ${exc.toString()}"            bad++        }    }    } finally {        exporter.flush()        exporter.close()    }    println "Finished exporting data to file $FILE_NAME"    println "good: $good bad: $bad" }on_change = { widget, button ->    }