Dim XA As Double
Dim XB As Double
Dim YA As Double
Dim YB As Double
Dim SAL1 As String
Dim SAL2 As String
Dim SAL3 As String
Dim SAL4 As String
Dim SAL5 As String
Dim arch As String
Dim puntos As Integer
Dim TEXTO As String
Dim I As Integer
Dim M As Integer
Dim P As Integer

arch=FileName()

If arch="No File Loaded." Then
	message"Debe abrir un archivo antes"
Else
	
	
	Code "G0 X0 Y0"
	While IsMoving () 
	Wend
	Code "G0 Z0"
	While IsMoving () 
	Wend
	puntos=InputBox("Ingrese puntos de matriz")
	
	XA=MinX ()
	XB=MaxX ()
	YA=MinY ()
	YB=MaxY ()
	
	Message "Comenzando proceso de autonivelado"
	Code "G90 G21 S20000 G17"
	While IsMoving () 
	Wend
	MSGBOX "Coloque los cables de sondeo"

	Code "G0 Z2"
	TEXTO=("G1 X" & XA & " Y" & YA & " F2000")
	
	Code TEXTO
	Code "G0 Z1"
	Code "G31 Z-5 F40"
	While IsMoving () 
	Wend
	Code "G92 Z0"
	Code "G0 Z1"
	Code "G31 Z-5 F20"
	Code "G92 Z0"
	Code "G0 Z1"

	While IsMoving () 
	Wend
	M=1
	Y=YA
	For I=0 To PUNTOS
	   	If P=20 Then
	   		I=0
	   		P=10
	   		M=M+1
	 	End If
		
		X=(((XB-XA)/(PUNTOS-1))*I)+XA
		TEXTO=("G1 X" & X & " Y" & Y & " F2000")
		
		While IsMoving () 
		Wend
		Code "G0 Z1"
		Code TEXTO
		Code "G31 Z-5 F40"
		
		While IsMoving () 
		Wend
		If XB=X Then
				If X=XB AND Y=YB Then
				 		I=PUNTOS
			 	End If
	  			
				 Y=(((YB-YA)/(PUNTOS-1))*M)+YA
	 			 P=20
		End If

	Next I
	
	Code "M41"
	Code "G0 Z5"
	TEXTO=("G1 X" & XA & " Y" & YA & " F2000")
	Code TEXTO
	Code "G0 Z0"
	MSGBOX "Retire los cables de sondeo, Autonivelado Finalizado"
	Message"Autonivelado Finalizado"

End If