From 4e90f88ab76ed0c1f8e19df291e904fb1616d4a5 Mon Sep 17 00:00:00 2001 From: Greg Landrum Date: Thu, 20 Sep 2012 04:12:24 +0000 Subject: [PATCH] get dashes working in the cairo drawing; additional improvements to the drawing --- Code/Demos/RDKit/Draw/MolDrawing.h | 15 ++++++------ Code/Demos/RDKit/Draw/demo.cpp | 37 +++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Code/Demos/RDKit/Draw/MolDrawing.h b/Code/Demos/RDKit/Draw/MolDrawing.h index 56ee4801b..b9f6de554 100644 --- a/Code/Demos/RDKit/Draw/MolDrawing.h +++ b/Code/Demos/RDKit/Draw/MolDrawing.h @@ -137,7 +137,7 @@ namespace RDKit { int atnum1 = mol[*bAts]->getAtomicNum(); int atnum2 = mol.getAtomWithIdx(a2Idx)->getAtomicNum(); - if( !mol.getRingInfo()->numBondRings(bond->getIdx()) && !bond->getBondType()==Bond::AROMATIC ) { + if( !mol.getRingInfo()->numBondRings(bond->getIdx()) && bond->getBondType()!=Bond::AROMATIC ) { // acyclic bonds RDGeom::Point2D obv=a2-a1; RDGeom::Point2D perp=obv; @@ -169,7 +169,8 @@ namespace RDKit { dotsPerAngstrom*(a2.y-perp.y) ); } - } else if( bond->getBondType()==Bond::SINGLE || bond->getBondType()==Bond::TRIPLE ) { + } + if( bond->getBondType()==Bond::SINGLE || bond->getBondType()==Bond::TRIPLE ) { DrawLine( res , atnum1 , atnum2 , lineWidth , false , dotsPerAngstrom*(a1.x) , dotsPerAngstrom*(a1.y) , @@ -238,12 +239,10 @@ namespace RDKit { } } } - double massDiff=fabs(PeriodicTable::getTable()->getAtomicWeight(mol[*bAts]->getAtomicNum()) - - mol[*bAts]->getMass()); - static double massTol=0.001; + int isotope = mol[*bAts]->getIsotope(); if(mol[*bAts]->getAtomicNum()!=6 || mol[*bAts]->getFormalCharge()!=0 || - massDiff>massTol ){ + isotope ){ res.push_back(ATOM); res.push_back(mol[*bAts]->getAtomicNum()); res.push_back(static_cast(dotsPerAngstrom*a1.x)); @@ -254,8 +253,8 @@ namespace RDKit { leftToRight=false; } - if(massDiff>massTol){ - symbol = boost::lexical_cast(int(mol[*bAts]->getMass()+.1))+symbol; + if(isotope){ + symbol = boost::lexical_cast(isotope)+symbol; } if(mol[*bAts]->getAtomicNum()!=6){ int nHs=mol[*bAts]->getTotalNumHs(); diff --git a/Code/Demos/RDKit/Draw/demo.cpp b/Code/Demos/RDKit/Draw/demo.cpp index 121a3c193..0dd1dc52b 100644 --- a/Code/Demos/RDKit/Draw/demo.cpp +++ b/Code/Demos/RDKit/Draw/demo.cpp @@ -1,6 +1,6 @@ -// $Id: sample.cpp 793 2008-08-17 14:33:30Z glandrum $ +// $Id$ // -// Copyright (C) 2009 Greg Landrum +// Copyright (C) 2009-2012 Greg Landrum // This file is part of the RDKit. // The contents are covered by the terms of the BSD license // which is included in the file license.txt, found at the root @@ -176,6 +176,12 @@ void drawLineCairo(std::vector::const_iterator &pos, int width=*pos++; cairo_set_line_width(cr,width*10); int dashed=*pos++; + if(dashed){ + double dashes[]={20.0,20.0}; + cairo_set_dash(cr,dashes,sizeof(dashes)/sizeof(dashes[0]),0); + } else { + cairo_set_dash(cr,0,0,0); + } int an1=*pos++; int an2=*pos++; int xp1 = *pos++; @@ -256,10 +262,16 @@ void MolToCairo(const ROMol &mol,cairo_t *cr,int width,int height, int fontSize=14,int maxDotsPerAngstrom=30){ PRECONDITION(cr,"no context"); PRECONDITION(width>0 && height>0,"bad dimensions"); - RWMol cp(mol); - RDKit::MolOps::Kekulize(cp); - RDDepict::compute2DCoords(cp); - std::vector drawing=RDKit::Drawing::DrawMol(cp); + RWMol *cp = new RWMol(mol); + try{ + RDKit::MolOps::Kekulize(*cp); + } catch (...) { + delete cp; + cp = new RDKit::RWMol(mol); + } + RDDepict::compute2DCoords(*cp); + std::vector drawing=RDKit::Drawing::DrawMol(*cp); + delete cp; cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); cairo_rectangle(cr,0,0,width,height); @@ -381,6 +393,19 @@ void DrawDemo(){ cairo_surface_destroy (surface); delete mol; } + { + RWMol *mol=SmilesToMol("Nccc(CCO)n",0,false); + mol->updatePropertyCache(); + cairo_surface_t *surface = + cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300); + cairo_t *cr = cairo_create (surface); + MolToCairo(*mol,cr,300,300); + + cairo_destroy (cr); + cairo_surface_write_to_png (surface, "mol4.png"); + cairo_surface_destroy (surface); + delete mol; + } #endif }