Using toBinFormat(string) always output blank image

User 22c88daf92

22-10-2012 07:43:07

hello!


We use method http://www.chemaxon.com/marvin/examples/beans/sketch-images/index.html to create image from a mol string (code as follow),   but sometimes the output png image is blank. and when we run the same program again, the png image will create successfully. 


And the method toBinFormat(string) is show as deprecated, Which is the new method?


 thanks!


java code :


import chemaxon.marvin.beans.MSketchPane;


import org.springframework.util.FileCopyUtils;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;


public class ChemaxonUtil {


public void mol2ImageChemaxon(String molStr,String imagePath, String type, int width,int height) throws IOException{
     if(molStr!=null)
     {
             String scale="80";
             MSketchPane sketchPane= new MSketchPane();
             sketchPane.setMol(molStr);
             sketchPane.getMol("mol");
                 
             String format="png:w"+width+",h"+height+",maxscale"+scale+",transbg,#ffffffff";


              if(type.equals("jpeg"))        
                    format = "jpeg:w"+width+",h"+height+",maxscale"+scale;           
                 
              byte[] imageData = sketchPane.getMol().toBinFormat(format); // Is this method be deprecated?


              FileOutputStream fos=null;
              try {
                           fos = new FileOutputStream(imagePath);
                           fos.write(imageData);
                           fos.close();
                    } catch (FileNotFoundException e) {
                               e.printStackTrace();
               } 
      
         }
    }
}

ChemAxon 5433b8e56b

23-10-2012 13:45:59

Hi,


the new method is MolExporter.exportToBinFormat(Molecule, String) method.


Currently within a few try I could not reproduce the issue, can you give me a molecule with you have problems frequently? Are there any exceptions thrown when the issue occurs?


Regards,
Istvan

User 22c88daf92

24-10-2012 01:17:08










ifajth wrote:

Hi,


the new method is MolExporter.exportToBinFormat(Molecule, String) method.


Currently within a few try I could not reproduce the issue, can you give me a molecule with you have problems frequently? Are there any exceptions thrown when the issue occurs?


Regards,
Istvan



hello,


I submit a mol file example in the Attachment of this reply. and there was not  any exceptions thrown when the issue occurs.


We find that this problem also can be solved by changing this line in my java example:


sketchPane.getMol("cml");


thanks!



ChemAxon 2c555f5717

25-10-2012 04:47:29

 


Hi!


   You have run into a parallel issue of MSketchPane. MSketchPane loads molecules in a background thread, and you want to use the molecule you have set, before it has arrived to the canvas. The canvas fires a PropertyChangeEvent when a molecule is set to the canvas. You can register a PropertyChangeListener to get notification about it. You can even synchronise the two threads through this event.


   If you would like to get more information about the events of MSketchPane, than please look at this page.


   Here you can see some example code, that you can paste to your code: 


 public void mol2ImageChemaxon(String molStr, String imagePath, String type, 
int width, int height) throws IOException {
if (molStr != null) {
String scale = "80";
MSketchPane sketchPane = new MSketchPane();
registerHaandler(sketchPane);
sketchPane.setMol(molStr);
waitLoader(sketchPane);
sketchPane.getMol("mol");

String format = "png:w" + width + ",h" + height + ",maxscale" +
scale + ",transbg,#ffffff";

if (type.equals("jpeg")) format = "jpeg:w" + width + ",h" + height +
",maxscale" + scale;

byte[] imageData = sketchPane.getMol().toBinFormat(format);

FileOutputStream fos = null;
try {
fos = new FileOutputStream(imagePath);
fos.write(imageData);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}
}

private void waitLoader(final MSketchPane sketchPane) {
synchronized (sketchPane) {
try{
sketchPane.wait();
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}

private void registerHaandler(final MSketchPane sketchPane) {
sketchPane.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent evt) {
if("mol".equals(evt.getPropertyName())) {
synchronized (sketchPane) {
sketchPane.notifyAll();
}
}
}
});
}

Please accept this workaround temporary. We are working on a final solution.

ChemAxon 2c555f5717

25-10-2012 06:01:01

Technically none of my test have failed but, theoretically it is better to put the set method into the same synchronization block with the wait() statement, like this:



(...)

MSketchPane sketchPane = new MSketchPane();
registerHaandler(sketchPane);

synchronized (sketchPane) {
sketchPane.setMol(molStr);
waitLoader(sketchPane);
}

String format = "png:w" + width + ",h" + height + ",maxscale" + scale
+ ",transbg,#ffffff";

(...)