Generating Image using MolExporter exactly as MSKETCH panel

User 956e0ca334

23-05-2013 15:55:28

I wonder if you could help me at all.  I am using marvin applets for our new database system and need to create a page button that will fire an event to create an set of images etc from a backing bean. ( I need  to record  the image  details in the database and fire some other methods.)



I have pretty much got all this working well.  I  pick up the XML from MSketch on the page and pass it to a hidden field with jquery, with the SP option so I get all the annotations.  



However my issue is, if the user changes a view option like say Implicting hydrogens when viewing a molecule, then they click from the menu in the applet "SAVE IMAGE", the image is saved with their choice of implicit hydrogens, however when I create it from xml file and MolExporter.exportToBinFormat.  I can not pass which view options were selected so it will create the image with the default view options rather than those that were selected in the sketch panel at the time.  



Is there any way I can create an image in a backing bean where I can ensure than I am using the save view options as have been selected   by the user in the  MSketch applet?


 


(Or  maybe I could fire the SAVE AS IMAGE option from the applet itself with javascript, but can't see any way to do this! )



Thanks
______________________________________________________________


The Jquery I am using to pick up the information from the applet (structureString is the name of the hidden field):



$("#myForm\\:structureString").val(document.MSketch.getMol('mrv:SP'));

Then in the backing bean I am using the following:
    public void saveImage(){
        try {
            InputStream is = new ByteArrayInputStream(
structureStringValue.getBytes());
    MolImporter importer = new MolImporter(is);    
Molecule mol = importer.read();
           byte[] moldata = MolExporter.exportToBinFormat(mol, "png:w300,h300,b32,#00ffff00");             
              File destinationFile = new File("C:\\temp\\myTest.png");
            OutputStream out = new FileOutputStream(destinationFile);
            out.write(moldata);
            out.close();
.......
        } catch (Exception e) {
.......
}
    }

ChemAxon 2c555f5717

27-05-2013 16:40:34

Dear Megan Eisenbrsun!


   This gona be complicated:


   You can read GUI propoerties from MolImporter with the getGlobalProperties() function. (If they were saved to the molecule with open(format, guiProperties) then convert(mol) then close() methods.) You can use those properties to pass to ImageExportUtil.mergeProperties(yourSetting, readProperties, imageSize) which gives you a propertiesObject, that can be given to ImageExportUtil.createImageExportParameter(propertiesObject, format, MDocument) which returns the formatString that contains enough information to use with MolExporter.exportToBinFormat(MDocument, formatString).


   To sum it up: 



  1. save GUI settings to MRV to pass it to server. (This is done autmatically if the Save / Load GUI settings is set)

  2. read that information from MRV on server.

  3. convert the information to an image format parameter string.

  4. use the image format parameter with MolExporter.


   I hope it helped you.


Regards:
Balázs 

User 956e0ca334

29-05-2013 08:59:32

Dear Balázs,


Thanks for your reply.  I am afraid I am still having no luck.  In fact using this method I am now losing the text box annotations too!  The string from the applet document.MSketch.getMol('mrv:SP'); did not seem to be bring back all the properties , so I tried saving out to a marvin fielwith the gui setting saved (file is attached).   You can see the properties are listed in the xml of the marvin file.  However when I try to generate the images with the methods you suggest using the file, they come out as using the defaults with no text boxes and none of the users selections such as implicit hydrogens.  Is there something I am doing the is incorrect?


Thanks


Megan


           File file = new File("c:\\temp\\test.mrv");
InputStream inMRV = new BufferedInputStream(new FileInputStream(file));
MolImporter importerMRV = new MolImporter(inMRV);


                MPropertyContainer guiprops = importerMRV.getGlobalProperties();
            UserSettings settings = new UserSettings();

            Rectangle rectSize = new Rectangle( 200, 175);


            Properties props = ImageExportUtil.mergeProperties(settings, guiprops, rectSize);


            chemaxon.struc.MDocument doc = new MDocument(mol);
            String formatString = ImageExportUtil.createImageExporterParameter(props, "png", doc);

            byte[] moldataPNG = MolExporter.exportToBinFormat(mol, formatString);
            File destinationFilePNG = new File("C:\\temp\\test.png");


 
            OutputStream outPNG = new FileOutputStream(destinationFilePNG);
            outPNG.write(moldataPNG);
            outPNG.close();

ChemAxon 5433b8e56b

29-05-2013 11:07:33

Hi Megan,


there is a not really advertised thing on this API part, I assume we should extend the API documentation with this information.
The MarvinSketch applet also uses this API to create the image files, with one difference, but let me explain the background of this problem first.


Some of the settings is global for the viewer, and sketcher. Some of them are specific for the sketcher, and for the viewer. This API has to respect this crazy thing, and it has to decide whether it is called from a sketcher or a viewer, because in both cases it gets the UserSettings object of the application, and the MPropertyContainer in most cases is null, we introduced a property in the UserSettings object which indicates that whether it is coming from a sketcher or a viewer.


The MarvinSketch application sets this property, and if it is not there, then the ImageExportUtil class assumes that it is called from a viewer. And as you see it uses the properties belongs to the viewer settings saved onto the mrv also from sketcher.


So if you would like to force the sketcher settings to be used, you should add one line of code after the creating of the UserSettings object, this: settings.setProperty("isSketcher", "true");


I know it is not the best solution, but currently this is how you can achieve to use the sketcher settings.


Please note that you can not force the saving of GUI properties into the mrv format from the server side in an applet.


I hope this helps.


Regards,
Istvan 

User 956e0ca334

29-05-2013 14:28:11

That is great, almost there! I now have my selections appearing as they should for the molecular view.


 


Sorry to be difficult  but unfortunately the textbox annotations  are not appearing using this method i.e. this MtextBox tag, is not being read:


 


 <MTextBox id="o2" toption="NOROT" fontScale="10.0" halign="LEFT" valign="TOP" autoSize="false"> <Field name="text"><![CDATA[this is a test ]]></Field> <MPoint x="-7.864999771118164" y="7.644999980926514"/> <MPoint x="-0.550000011920929" y="7.644999980926514"/> <MPoint x="-0.550000011920929" y="6.159999847412109"/> <MPoint x="-7.864999771118164" y="6.159999847412109"/> </MTextBox>


Is there another setting I need to change?


Thanks so much for your help.


 


Megan

ChemAxon 5433b8e56b

30-05-2013 12:16:56

Hi Megan,


I do not really see in your code snippet, where the mol variable get its value, but I assume you are using on of the MolImporter methods that returns a Molecule object, instead of doing so, I suggest to use the readDoc(MDocument, Molecule) method with null as the parameter values, the returned MDocument will contain the textbox object.


All of the graphical objects are stored only in the MDocument, so if there are such in the structure file, you can not get them imported when you importing only the Molecule.


I hope this helps, when I have tested it it was working for me.


Regards,
Istvan 

User 956e0ca334

03-06-2013 08:29:56

Thanks for help Istvan!