partridgejiang / Kekule.js

A Javascript cheminformatics toolkit.
http://partridgejiang.github.io/Kekule.js
MIT License
250 stars 61 forks source link

Select Atoms/Bonds from script #99

Open cleblond opened 5 years ago

cleblond commented 5 years ago

I see in the docs that we can get the objects selected with var selObjs = composer.getSelection();

How do I do the reverse (i.e. select objects based on their className and id)?

partridgejiang commented 5 years ago

There is no direct way to select objects with id or class in composer currently. To manually create a selection, you have to retrieve the object instances(atoms, bonds, etc.) first, then use method composer.setSelection. For example, the following code will select the objects with id 'a1' and 'a2' in composer:

var chemdoc = composer.getChemObj();
composer.setSelection([chemdoc.getObjById('a1'), chemdoc.getObjById('a2')]);

The next example selects all the child object in composer:

var chemdoc = composer.getChemObj();
var children = chemdoc.getChildren();
composer.setSelection(children);