public static IChemModel emptyModel() {
		final IChemModel chemModel = DefaultChemObjectBuilder.getInstance()
				.newInstance(IChemModel.class);
		chemModel.setMoleculeSet(chemModel.getBuilder().newInstance(
				IAtomContainerSet.class));
		chemModel.getMoleculeSet().addAtomContainer(
				chemModel.getBuilder().newInstance(IAtomContainer.class));
		return chemModel;
	}
	private static void removeDuplicateAtomContainers(final IChemModel chemModel) {
		// we remove molecules which are in AtomContainerSet as well as in a
		// reaction
		final IReactionSet reactionSet = chemModel.getReactionSet();
		final IAtomContainerSet moleculeSet = chemModel.getMoleculeSet();
		if (reactionSet != null && moleculeSet != null) {
			final List<IAtomContainer> aclist = ReactionSetManipulator
					.getAllAtomContainers(reactionSet);
			for (int i = moleculeSet.getAtomContainerCount() - 1; i >= 0; i--) {
				for (int k = 0; k < aclist.size(); k++) {
					final String label = moleculeSet.getAtomContainer(i)
							.getID();
					if (aclist.get(k).getID().equals(label)) {
						chemModel.getMoleculeSet().removeAtomContainer(i);
						break;
					}
				}
			}
		}
	}
示例#3
0
 private IChemModel readChemModel(IChemModel chemModel) throws CDKException {
   IAtomContainerSet setOfMolecules = chemModel.getMoleculeSet();
   if (setOfMolecules == null) {
     setOfMolecules = chemModel.getBuilder().newInstance(IAtomContainerSet.class);
   }
   IAtomContainer m = readAtomContainer(chemModel.getBuilder().newInstance(IAtomContainer.class));
   if (m != null && m instanceof IAtomContainer) {
     setOfMolecules.addAtomContainer((IAtomContainer) m);
   }
   chemModel.setMoleculeSet(setOfMolecules);
   return chemModel;
 }
  private IMolecule checkForXMoleculeFile(IChemFile chemFile, int numberOfMolecules) {
    Assert.assertNotNull(chemFile);

    Assert.assertEquals(chemFile.getChemSequenceCount(), 1);
    org.openscience.cdk.interfaces.IChemSequence seq = chemFile.getChemSequence(0);
    Assert.assertNotNull(seq);

    Assert.assertEquals(seq.getChemModelCount(), 1);
    org.openscience.cdk.interfaces.IChemModel model = seq.getChemModel(0);
    Assert.assertNotNull(model);

    org.openscience.cdk.interfaces.IMoleculeSet moleculeSet = model.getMoleculeSet();
    Assert.assertNotNull(moleculeSet);

    Assert.assertEquals(moleculeSet.getMoleculeCount(), numberOfMolecules);
    IMolecule mol = null;
    for (int i = 0; i < numberOfMolecules; i++) {
      mol = moleculeSet.getMolecule(i);
      Assert.assertNotNull(mol);
    }
    return mol;
  }
	private static void removeEmptyAtomContainers(final IChemModel chemModel) {
		final IAtomContainerSet moleculeSet = chemModel.getMoleculeSet();
		if (moleculeSet != null && moleculeSet.getAtomContainerCount() == 0) {
			chemModel.setMoleculeSet(null);
		}
	}
	/**
	 * Inserts a molecule into the current set, usually from Combobox or Insert
	 * field, with possible shifting of the existing set.
	 * 
	 * @param chemPaintPanel
	 * @param molecule
	 * @param generateCoordinates
	 * @param shiftPanel
	 * @throws CDKException
	 */
	public static void generateModel(
			final AbstractJChemPaintPanel chemPaintPanel,
			IAtomContainer molecule, final boolean generateCoordinates,
			final boolean shiftPasted) throws CDKException {
		if (molecule == null) {
			return;
		}

		final IChemModel chemModel = chemPaintPanel.getChemModel();
		IAtomContainerSet moleculeSet = chemModel.getMoleculeSet();
		if (moleculeSet == null) {
			moleculeSet = new AtomContainerSet();
		}

		// On copy & paste on top of an existing drawn structure, prevent the
		// pasted section to be drawn exactly on top or to far away from the
		// original by shifting it to a fixed position next to it.

		if (shiftPasted) {
			double maxXCurr = Double.NEGATIVE_INFINITY;
			double minXPaste = Double.POSITIVE_INFINITY;

			for (final IAtomContainer atc : moleculeSet.atomContainers()) {
				// Detect the right border of the current structure..
				for (final IAtom atom : atc.atoms()) {
					if (atom.getPoint2d().x > maxXCurr) {
						maxXCurr = atom.getPoint2d().x;
					}
				}
				// Detect the left border of the pasted structure..
				for (final IAtom atom : molecule.atoms()) {
					if (atom.getPoint2d().x < minXPaste) {
						minXPaste = atom.getPoint2d().x;
					}
				}
			}

			if (maxXCurr != Double.NEGATIVE_INFINITY
					&& minXPaste != Double.POSITIVE_INFINITY) {
				// Shift the pasted structure to be nicely next to the existing
				// one.
				final int MARGIN = 1;
				final double SHIFT = maxXCurr - minXPaste;
				for (final IAtom atom : molecule.atoms()) {
					atom.setPoint2d(new Point2d(atom.getPoint2d().x + MARGIN
							+ SHIFT, atom.getPoint2d().y));
				}
			}
		}

		if (generateCoordinates) {
			// now generate 2D coordinates
			final StructureDiagramGenerator sdg = new StructureDiagramGenerator();
			sdg.setTemplateHandler(new TemplateHandler(moleculeSet.getBuilder()));
			try {
				sdg.setMolecule(molecule);
				sdg.generateCoordinates(new Vector2d(0, 1));
				molecule = sdg.getMolecule();
			} catch (final Exception exc) {
				JOptionPane.showMessageDialog(chemPaintPanel,
						GT._("Structure could not be generated"));
				throw new CDKException("Cannot depict structure");
			}
		}

		if (moleculeSet.getAtomContainer(0).getAtomCount() == 0) {
			moleculeSet.getAtomContainer(0).add(molecule);
		} else {
			moleculeSet.addAtomContainer(molecule);
		}

		final IUndoRedoFactory undoRedoFactory = chemPaintPanel.get2DHub()
				.getUndoRedoFactory();
		final UndoRedoHandler undoRedoHandler = chemPaintPanel.get2DHub()
				.getUndoRedoHandler();

		if (undoRedoFactory != null) {
			final IUndoRedoable undoredo = undoRedoFactory
					.getAddAtomsAndBondsEdit(chemPaintPanel.get2DHub()
							.getIChemModel(), molecule, null, "Paste",
							chemPaintPanel.get2DHub());
			undoRedoHandler.postEdit(undoredo);
		}

		chemPaintPanel.getChemModel().setMoleculeSet(moleculeSet);
		chemPaintPanel.updateUndoRedoControls();
		chemPaintPanel.get2DHub().updateView();
	}