PyObject *scriptFigureOpen(PyObject *self, PyObject *args) {
	PyObject *pyFigure;
	int preserveRotation=false;
	if (!PyArg_ParseTuple(args, "O|i", &pyFigure, &preserveRotation))
		return NULL;

	struct figureData *figure=figureFromPython(pyFigure);
	if (figure) {
		if (preserveRotation && (figure->dim==-1)) {
			figure->dim=figureData.dim;
			figure->count=safeCalloc(figure->dim+1, sizeof(int));
		}
		figureOpen(figure, preserveRotation);
	}
	if (!PyErr_Occurred()) {
		if (preserveRotation) {
			scriptEventsPerform(&scriptEventsModified);
		} else {
			scriptEventsPerform(&scriptEventsNew);
			scriptEventsPerform(&scriptEventsOpen, NULL);
		}
	}
	if (PyErr_Occurred())
		return NULL;
	return Py_None;
}
PyObject *scriptVertexAdd(PyObject *self, PyObject *args) {
    PyObject *pyCoords=Py_None;
    if (!PyArg_ParseTuple(args, "|O", &pyCoords))
        return NULL;

    if (figureData.dim<0) {
        scriptThrowException("There is no space yet, use new or open");
        return NULL;
    }

    GLdouble coords[figureData.dim];
    if (!coordsFromPython(pyCoords, coords))
        return NULL;

    if (!safeCheckPos(coords, figureData.dim)) {
        scriptThrowException("Wrong position");
        return NULL;
    }

    int index=figureVertexAdd(coords);

    scriptEventsPerform(&scriptEventsModified);

    return Py_BuildValue("i", index);
}
void consoleCmdNew(int dim) {
	if ((dim<0) || (dim>safeMaxDim)) {
		scriptThrowException("Wrong parameters");
		return;
	}
	consoleCmdVertexDeselect();
	figureNew(dim);
	drawerSetDim(dim);
	scriptEventsPerform(&scriptEventsNew);
}
void consoleCmdWrite(char *path) {
	if (figureData.dim<0)
		scriptThrowException("Nothing opened");
	else {
		char *expr=scriptFigureToPythonExpr(&figureData);
		FILE *f=fopen(utilExpandPath(path), "w");
		if (!f) {
			scriptThrowException("File cannot be opened for writing");
			return;
		}
		fprintf(f, "import gf\ngf.figureOpen(%s)\n", expr);
		fclose(f);
		scriptEventsPerform(&scriptEventsWrite, path);
	}
}
PyObject *scriptVertexRm(PyObject *self, PyObject *args) {
    int index;
    if (!PyArg_ParseTuple(args, "i", &index))
        return NULL;

    if ((figureData.dim<0) || (index<0) || (index>=figureData.count[0])) {
        scriptThrowException("Out of bounds");
        return NULL;
    }

    figureVertexRm(index);

    scriptEventsPerform(&scriptEventsModified);

    return Py_None;
}
PyObject *scriptVertexSetPos(PyObject *self, PyObject *args) {
    PyObject *pyCoords;
    int index;
    if (!PyArg_ParseTuple(args, "iO", &index, &pyCoords))
        return NULL;

    if ((figureData.dim<0) || (index<0) || (index>=figureData.count[0])) {
        scriptThrowException("Out of bounds");
        return NULL;
    }

    GLdouble coords[figureData.dim];
    if (!coordsFromPython(pyCoords, coords))
        return NULL;

    if (!safeCheckPos(coords, figureData.dim)) {
        scriptThrowException("Wrong position");
        return NULL;
    }

    figureVertexMove(index, coords);
    scriptEventsPerform(&scriptEventsModified);
    return Py_None;
}