[GIS] How to enable the snapping for a layer with the tolerance value with python programming

pyqgisqgis

I have a vector polygon and vector polyline layer. I want to enable the snapping for both the layer with tolerance value? How can i do that using python programming. I have tried this code but getting the following error:

snapper = QgsMapCanvasSnapper(canvas) 
snapper.setSnapSettingsForLayer(aLayer,True,SnapToVertexAndSegment,pixels,0.0001‌​,True) 

Traceback (most recent call last): File "", line 1, in AttributeError: 'QgsMapCanvasSnapper' object has no attribute 'setSnapSettingsForLayer'

Can anyone help me in setting the snapping options to the currentLayer using python programming?

Best Answer

You must define it first:

result3 = QgsVectorLayer("LineString", "ligne", "memory") 
#it's to create your layer

ligneid=result3.id()
# it allows you to have the idvalue for your layer

QgsProject.instance().setSnapSettingsForLayer(ligneid,True,2,1,1000,True)
# it defines the snapping options ligneid : the id of your layer, True : to enable the layer snapping, 2 : options (0: on vertex, 1 on segment, 2: vertex+segment), 1: pixel (0: type of unit on map), 1000 : tolerance, true : avoidIntersection)