private void encode(XMLEncoder out, String xyzFileName) throws Exception {

		setProgressMessage("Writing states ......");
		ModelState state = new ModelState();
		state.setXyzFileName(xyzFileName);
		state.setRotation(view.getViewer().getCurrentOrientation());
		state.setNavigationMode(view.getViewer().getNavigationMode());
		state.setPerspectiveDepth(view.getViewer().getPerspectiveDepth());
		int cameraAtom = view.getCameraAtom();
		if (cameraAtom >= 0) {
			state.setCameraAtom(view.getCameraAtom());
		}
		else {
			Point3f cp = view.getViewer().getCameraPosition();
			state.setCameraPosition(cp.x + " " + cp.y + " " + cp.z);
		}
		state.setFrameInterval(model.getFrameInterval());
		state.setViewRefreshInterval(model.getViewRefreshInterval());
		state.setInitScript(model.getInitializationScript());
		state.setZDepthMagnification(view.getViewer().getZDepthMagnification());
		state.setShowAtomIndex(view.getShowAtomIndex());
		state.setShowGlassSimulationBox(view.getShowGlassSimulationBox());
		state.setShowClock(view.getShowClock());
		state.setShowAxes(view.areAxesShown());
		state.setAxisStyle(view.getViewer().getAxisStyle());
		state.setShowEnergizer(view.getShowEnergizer());
		state.setKeShading(view.getKeShading());
		state.setShowCharge(view.getShowCharge());
		state.setFullSizeUnbondedAtoms(view.getFullSizeUnbondedAtoms());
		state.setShowVdwLines(view.getShowVdwLines());
		state.setVdwLinesRatio(view.getVdwLinesRatio());
		state.setVelocityVectorScalingFactor(view.getVelocityVectorScalingFactor());
		state.setMoleculeStyle(view.getMolecularStyle());
		state.setTimeStep(model.getTimeStep());
		state.setLength(model.getLength());
		state.setWidth(model.getWidth());
		state.setHeight(model.getHeight());
		state.setHeatBath(model.getHeatBath());
		state.setX1Mass(model.getElementMass("X1"));
		state.setX2Mass(model.getElementMass("X2"));
		state.setX3Mass(model.getElementMass("X3"));
		state.setX4Mass(model.getElementMass("X4"));
		state.setX1Sigma(model.getElementSigma("X1"));
		state.setX2Sigma(model.getElementSigma("X2"));
		state.setX3Sigma(model.getElementSigma("X3"));
		state.setX4Sigma(model.getElementSigma("X4"));
		state.setX1Epsilon(model.getElementEpsilon("X1"));
		state.setX2Epsilon(model.getElementEpsilon("X2"));
		state.setX3Epsilon(model.getElementEpsilon("X3"));
		state.setX4Epsilon(model.getElementEpsilon("X4"));
		state.setX1Argb(view.getElementArgb("X1"));
		state.setX2Argb(view.getElementArgb("X2"));
		state.setX3Argb(view.getElementArgb("X3"));
		state.setX4Argb(view.getElementArgb("X4"));
		if (model.getGField() != null) {
			if (!model.getGField().isAlwaysDown())
				state.setGFieldDirection(model.getGField().getDirection());
			state.setGravitationalAcceleration(model.getGField().getIntensity());
		}
		if (model.getBField() != null) {
			state.setBFieldDirection(model.getBField().getDirection());
			state.setBFieldIntensity(model.getBField().getIntensity());
		}
		if (model.getEField() != null) {
			state.setEFieldDirection(model.getEField().getDirection());
			state.setEFieldIntensity(model.getEField().getIntensity());
		}
		int n = model.getObstacleCount();
		if (n > 0) {
			for (int i = 0; i < n; i++) {
				state.addObstacle(model.getObstacle(i));
			}
		}
		if (view.getFillMode() != FillMode.getNoFillMode()) {
			if (view.getFillMode() instanceof FillMode.ImageFill) {
				FillMode.ImageFill imgFillMode = (FillMode.ImageFill) view.getFillMode();
				imgFillMode.setURL(FileUtilities.getFileName(imgFillMode.getURL()));
			}
			state.setFillMode(view.getFillMode());
		}

		if (view.getDrawList() != null && !view.getDrawList().isEmpty()) {
			List<Object> drawList = new ArrayList<Object>();
			Object o;
			for (DrawingElement e : view.getDrawList()) {
				o = DrawingElementStateFactory.createState(e);
				if (o != null)
					drawList.add(o);
			}
			state.setDrawList(drawList);
		}

		String s = null;
		if (view.getVelocityBitSet().cardinality() > 0) {
			s = view.getVelocityBitSet().toString().replaceAll(",", "");
			state.setVelocitySelection(s.substring(1, s.length() - 1));
		}
		if (view.getTrajectoryBitSet().cardinality() > 0) {
			s = view.getTrajectoryBitSet().toString().replaceAll(",", "");
			state.setTrajectorySelection(s.substring(1, s.length() - 1));
		}
		if (view.getTranslucentBitSet().cardinality() > 0) {
			s = view.getTranslucentBitSet().toString().replaceAll(",", "");
			state.setTranslucentSelection(s.substring(1, s.length() - 1));
		}
		s = model.getAtomPropertySelection(Atom.UNMOVABLE);
		if (s != null && !s.equals("")) {
			state.setUnmovableSelection(s);
		}
		s = model.getAtomPropertySelection(Atom.INVISIBLE);
		if (s != null && !s.equals("")) {
			state.setInvisibleSelection(s);
		}

		if (model.getJob() != null)
			state.addTasks(model.getJob().getCustomTasks());
		out.writeObject(state);

	}