// add(category, (molids), (atomids)) static PyObject *label_add(PyObject *self, PyObject *args) { char *type; PyObject *molids, *atomids; int i; if (!PyArg_ParseTuple(args, (char *)"sO!O!:label.add", &type, &PyTuple_Type, &molids, &PyTuple_Type, &atomids)) return NULL; VMDApp *app = get_vmdapp(); MoleculeList *mlist = app->moleculeList; int cat = app->geometryList->geom_list_index(type); if (cat < 0) { PyErr_SetString(PyExc_ValueError, (char *)"Unknown label category"); return NULL; } int numitems; if (PyTuple_Size(molids) == 1 && PyTuple_Size(atomids) == 1) numitems = 1; else if (PyTuple_Size(molids) == 2 && PyTuple_Size(atomids) == 2) numitems = 2; else if (PyTuple_Size(molids) == 3 && PyTuple_Size(atomids) == 3) numitems = 3; else if (PyTuple_Size(molids) == 4 && PyTuple_Size(atomids) == 4) numitems = 4; else { PyErr_SetString(PyExc_TypeError, (char *)"label.add: 2nd and 3rd arguments" " must be tuples of size 1, 2, 3, or 4"); return NULL; } int m[4], a[4]; for (i=0; i<numitems; i++) { m[i] = PyInt_AsLong(PyTuple_GET_ITEM(molids, i)); a[i] = PyInt_AsLong(PyTuple_GET_ITEM(atomids, i)); if (PyErr_Occurred()) return NULL; Molecule *mol = mlist->mol_from_id(m[i]); if (!mol) { PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id"); return NULL; } if (a[i] < 0 || a[i] >= mol->nAtoms) { PyErr_SetString(PyExc_ValueError, (char *)"Invalid atom id"); return NULL; } } // Add the label, but don't toggle the on/off status. int ind = app->label_add(type, numitems, m, a, NULL, 0.0f, 0); if (ind < 0) { Py_INCREF(Py_None); return Py_None; } // Get the dict corresponding to this label. The dict we return // corresponds to either the label we just created, or to an existing // label whose state we have not changed. This makes it safe to use // label.add to get a proxy to a VMD label. GeomListPtr glist = app->geometryList->geom_list(cat); return geom2dict((*glist)[ind]); }
// helper function to get a molecule. Raises a ValueError exception on error static MoleculeGraphics *mol_from_id(int id) { VMDApp *app = get_vmdapp(); MoleculeList *mlist = app->moleculeList; Molecule *mol = mlist->mol_from_id(id); if (!mol) { PyErr_SetString(PyExc_ValueError, (char *)"Invalid graphics molecule"); return NULL; } return mol->moleculeGraphics(); }