Skip to content

Simple Structure Checker Button

/*
* Simple Structure Checker Button
* 
* Runs the structure checker against the currently selected row
* Checks the bond length
* for more checkers available see:
* https://chemaxon.com/jchem/doc/dev/java/api/chemaxon/checkers/package-frame.html
*
* Usage:
* 1. Edit the name of the Structure field
* 2. Run button script
*
* @author David Pech <dpech@chemaxon.com>
*/
import com.im.df.api.*
import com.im.df.api.support.SelectionDescription
import com.im.ijc.core.api.util.IJCCoreUtils
import chemaxon.checkers.*
import chemaxon.struc.Molecule
import com.im.df.api.chem.MarvinStructure
init = { widget ->
}
destroy = { widget ->
}
evaluate = { widget ->
    def ety = dataTree.rootVertex.entity // assumes you have reference to the data tree
    def molFld = ety.fields.items.find { it.name == 'Structure' } // find the structure field
    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 selected IDs
    if (ids.size == 1) {
        Map rows = rootVS.getData(ids, DFEnvironmentRO.DEV_NULL) // get the data
        Map row = rows[ids[0]] // get the first and only row
        MarvinStructure mol = row[molFld.id] // Get the Structure. Its a com.im.df.api.chem.MarvinStructure instance
        Molecule cxnMol = mol.getNative() // obtain the chemaxon.struc.Molecule instance

        // Structure checker
        StructureChecker checker = new BondLengthChecker() // initialize new checker (see Documentation for more checkers)
        def result = checker.check(cxnMol) // run the checker and obtain instance of StructureCheckerResults (returns null if molecule is fine)
        if (result != null ) {
            def error = result.getDescription() // get the error description form StructureCheckerResults instance
            println error
        } else {
            println "Molecule is fine"
        }


    } else {
        println "bad selection"
    }
}
on_change = { widget, button ->

}