I would like to know how to add each object obtained as an entity in the same layer; Not by creating a memory layer at each time in this example.
rb = QgsRubberBand(qgis.utils.iface.mapCanvas())
rb.setColor(Qt.red)
geom = QgsGeometry.fromPolyline(p)
for pnt in p:
rb.addPoint(pnt)
v_layer = QgsVectorLayer("LineString", "cable", "memory")
QgsMapLayerRegistry.instance().addMapLayers([v_layer])
pr = v_layer.dataProvider()
v_layer.startEditing()
v_layer.addAttribute(QgsField("id", QVariant.String))
v_layer.addAttribute(QgsField("type", QVariant.String))
seg = QgsFeature()
geom = rb.asGeometry()
seg.setGeometry(geom)
pr.addFeatures( [seg] )
v_layer.commitChanges()