;;;load(strcat( PATH_DIR "jump/jump_path.il"))

PATH_DIR = "~/SHARED/PRODUCTION/SKILL/TOOLS/"

load(strcat( PATH_DIR "common/config.il"))


path_width = 1.96
layer = list("m1" "drawing")

MODE_DEBUG = t

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(generate_via(id angle row col x y)

	niveau_3 = list("12" "13" "21" "31" "23" "32")
	niveau_4 = list("12" "13" "14" "21" "31" "41" "23" "24" "32" "42" "34" "43")
	niveau_5 = list("12" "13" "14" "15" "21" "31" "41" "51" "23" "24" "25" "32" "42" "52" "34" "35" "43" "53" "45" "54")		

	    via_techno = list("m1m2" "m2m3" "m3m4" "m4m5t30")
	   
	    M2_M1 = nth(0 via_techno)
	    M3_M2 = nth(1 via_techno)
	    M4_M3 = nth(2 via_techno)
	    M5_M4 = nth(3 via_techno)	

	    

		nom_via = "M2_M1"
		printf("::: > %s \n" nom_via)	

		cvId = geGetEditCellView()
		tech = techGetTechFile(cvId)
		viaParams = list(list("cutRows" row) list("cutColumns" col))
	 	via1 = techFindViaDefByName(tech "m1m2" )
	 	via2 = techFindViaDefByName(tech "m2m3" )
	 	via3 = techFindViaDefByName(tech "m3m4" )
	 	via4 = techFindViaDefByName(tech "m4m5t30" )

		dbCreateVia(cvId via1 x:y angle viaParams)

)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(distance(xa ya xb yb)

	prog(()
		tmp = (xb - xa) * (xb - xa) + (yb - ya) * (yb - ya)
		d = sqrt(tmp)
		return(d)
	);prog
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(calculer_distance(liste_points x y)

	prog(()

		liste_points = liste_points_obj

		first_point = nth(0 liste_points)
		liste_reverse = reverse(liste_points)
		last_point = nth(0 liste_reverse)
	
		p1_x = car(first_point)
		p1_y = cadr(first_point)
		first_d = distance(p1_x p1_y x y)
	
		p2_x = car(last_point)
		p2_y = cadr(last_point)
		last_d = distance(p2_x p2_y x y)
		
		;;printf(" **** %f  %f  \n" first_d last_d)
		
		tmp = list(first_d last_d first_point last_point)
		return(tmp)
	);
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(classer_distance(retour)

	prog(()
		first_d = nth(0 retour)
		last_d = nth(1 retour)
		first_point = nth(2 retour)
		last_point = nth(3 retour)

		if(first_d > last_d then
			ancre_obj = last_point
		else
			ancre_obj = first_point 
		)

		test = equal( first_point ancre_obj)
		if(test == t then
		   indice_obj = 0	
		else
		   indice_obj = 1	
		)

		return( list(ancre_obj indice_obj) )
	);				
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(jump()

	enterPath( ?prompts list( "Entrer le premier point." "Entrer le point suivant..." )
                          ?doneProc "terminer_jump_path"      
	                  ?wantPoints 100                                    
                          ?pathWidth path_width
	                  ?pathStyle "Truncate"     
		                                     
        	 )

)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(terminer_jump_path(winid done points )

	cv = geGetWindowCellView()

	buffer_segment = '()

	if( done == t then
	    id_path = leCreatePath(cv layer points path_width)
	    buffer_segment = append1(buffer_segment id_path)	
	    traitement_jump(id_path)
	)
	   

; traitement_jump(car(geGetSelSet()))

)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(placer_pont(obj x y)


prog(()

	printf("TRACE \n")

	liste_points_obj = obj~>points
	retour = '()
	retour = calculer_distance(liste_points_obj x y)
	printf(" Calculer distance : %L  \n" retour)
	elements = '()
	elements = classer_distance(retour)
	printf("/* %L    \n" elements )
	ancre_obj = nth(0 elements)
	indice_obj = nth(1 elements)
	
	;;;;;;
	x_ancre = nth(0 ancre_obj)
	y_ancre = nth(1 ancre_obj)
	
	if(sens == "V" then
	   if(y > y_ancre then
		shift = "low"
		y_ancre = y_ancre - 0.63
		y_path = y_ancre - 0.63
	   else
		shift = "high"
		y_ancre = y_ancre + 0.63
		y_path = y_ancre + 0.63
	   )	
	generate_via(obj "R0" 2 1 x_ancre y_ancre)
	retour = '()
	retour = list(x_ancre y_path)
	)

	if(sens == "H" then
	   if(x > x_ancre then
		shift = "left"
		x_ancre = x_ancre - 0.63
		x_path = x_ancre - 0.63
	   else
		shift = "right"
		x_ancre = x_ancre + 0.63
		x_path = x_ancre + 0.63
	   )			
	generate_via(obj "R0" 1 2 x_ancre y_ancre)
	retour = '()
	retour = list(x_path y_ancre)
	)

	return(retour)

   );prog	
	

)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(traitement_jump(id_path)
;procedure(debug_jump()

	cv = geGetWindowCellView()

	proximity_H = 0.0
	proximity_V = 0.0


;	id_path = car(geGetSelSet())
	
	enveloppe = id_path~>bBox
	path_width = id_path~>width

	recouvrement = dbGetTrueOverlaps( cv
					  enveloppe
		    			  list("m1" "drawing")
			   	          0:0 ; niveau d'extraction 
		                        )

	liste_path = setof( x recouvrement (x != id_path) )

	if(liste_path != nil then
		
		obj1 = dbCopyFig( id_path cv ) 
		obj1~>lpp = list("fuserec" "drawing") 
		list_obj = '()

		for(i 0 length(liste_path) - 1
		    obj = dbCopyFig(nth(i liste_path)  cv ) 
		    obj~>lpp = list("aafblk" "drawing")  
		    list_obj = append1(list_obj obj)	
		)

		list_markers = '()
		list_markers = leLayerAnd( cv list("fuserec" "drawing") 
			 		      list("aafblk" "drawing")
					      list("chipsize" "drawing") 
			                 )
	         
		collision = '()
		collision = setof(x cv~>shapes x~>lpp=='("chipsize" "drawing"))

		list_coord_collision = '()

		for(i 0 length(liste_path) - 1
			xinf = car(car( nth(i collision~>bBox) ))
			yinf = cadr(car( nth(i collision~>bBox) ))
			xsup = car(cadr( nth(i collision~>bBox) ))
			ysup = cadr(cadr( nth(i collision~>bBox) ))
			x = (xinf + xsup) / 2.0
			y = (yinf + ysup) / 2.0
			point = '()
			point = list(x y)
			list_coord_collision = append1(list_coord_collision point)

		)

		dbDeleteObject(obj1)
		for(i 0 length(list_obj) - 1
		    dbDeleteObject( nth(i list_obj) )	
		    dbDeleteObject( nth(i list_markers) )	
		)

	) 


	

	points = id_path~>points 
	nb_points = id_path~>nPoints
	nb_seg = nb_points - 1 
	
	segments = '()
	portion = '()

	for(i 0 nb_seg-1
	    points = id_path~>points 	
	    x1 = car(nth(i points))
	    y1 = cadr(nth(i points))
	    x2 = car(nth(i+1 points))
	    y2 = cadr(nth(i+1 points))
	    points = list(x1:y1 x2:y2) 	

	    if(y1 == y2 then
	       sens = "H"
	    )

	    if(x1 == x2 then
		sens = "V"
	    )

	    portion = append1(portion points)	
	    id = leCreatePath(cv list("m3" "drawing") points path_width)
	    for(i 0 length(liste_path) - 1
		test = leIsPointInsideFig(id nth(i list_coord_collision ) )
	    	if( test == t then
			segments = append1(segments list(points i sens) )
	    	)		    

	    )	
	    
	    dbDeleteObject(id)	
	)


	;obj = dbCopyFig(id_path cv ) 
	;obj~>lpp = list("chipsize" "drawing") 

	list_path_shop = '()
	list_segments = '()

	for(i 0 length(segments) - 1
		infos = nth(i segments)
		seg = nth(0 infos)
		indice = nth(1 infos)
		sens = nth(2 infos)

		x = car( nth(i list_coord_collision) )
		y = cadr( nth(i list_coord_collision) )
		
		if( sens == "H" then
			width_obstacle = abs(abs(xsup) - abs(xinf))
			if(width_obstacle  >= 10.0 then
			   x_step = 0.6	
			else
                           x_step = 0.4
			)
			x1 = x - (width_obstacle  / 2.0) - x_step
			x2 = x + (width_obstacle  / 2.0) + x_step
			y1 = y - (path_width / 2.0)
			y2 = y + (path_width / 2.0)
			proximity_H = (width_obstacle  / 2.0) + x_step
			printf("Valeur proximity %f  \n" proximity_H)
		)

		if( sens == "V" then
			width_obstacle  = abs(abs(ysup) - abs(yinf))
			if(width_obstacle  >= 10.0 then
			   y_step = 0.6	
			else
                           y_step = 0.4
			)
			x1 = x - (path_width / 2.0)
			x2 = x + (path_width / 2.0)
			y1 = y + (width_obstacle  / 2.0) + y_step
			y2 = y - (width_obstacle  / 2.0) - y_step
			proximity_V = (width_obstacle  / 2.0) + y_step
			printf("Valeur proximity %f  \n" proximity_V)
		)

		chop_box = list(x1:y1  x1:y2  x2:y2  x2:y1)
		;;dbCreatePolygon(cv list("m3" "drawing") chop_box)

		if(i == 0 then
		   list_path_shop = leChopShape(id_path chop_box t t) 
		   list_segments = list_path_shop
		else

		    for(j 0 length(list_segments) - 1
		        test = leIsPointInsideFig( nth(j list_segments) list(x y)   )
	    	        if( test == t then
			   list_tmp = '() 
			   list_tmp = leChopShape(nth(j list_segments) chop_box t t) 

			   for(k 0 length(list_tmp) - 1
				flag = setof( x list_segments (x == nth(k list_tmp)) )
				if(flag == nil then
				    list_segments = append(list_segments list(nth(k list_tmp)) )
				)
			   )


			)
	    	    )	
	    
		
	    )	

		
	)

	

	proximity_str = "0.670000"

	sprintf(proximity_str, "%f", proximity_H )	
	


	classement = '()

	for(i 0 length(list_coord_collision) - 1

		x = car(nth(i list_coord_collision))
		y = cadr(nth(i list_coord_collision))
		
		for(j 0 length(list_segments) - 1
		    liste_points_obj = nth(j list_segments)~>points
		    retour = '()
		    retour = calculer_distance(liste_points_obj x y)
   	            printf(" Calculer distance : %L  %f  %f \n" retour x y)

		    first_d = abs(nth(0 retour))
		    last_d = abs(nth(1 retour))
		    first_point = nth(2 retour)
		    last_point = nth(3 retour)

		    sprintf(str_first_d, "%f",first_d )	
 		    sprintf(str_last_d, "%f",last_d )

		    if( proximity_str == str_first_d then
		    	
			tmp = '()
			tmp = list( nth(j list_segments) first_point x y )
			classement = append1(classement tmp)
			print("Bingo \n")
		    )

		    if(proximity_str == str_last_d then
			tmp = '()
			tmp = list( nth(j list_segments) last_point x y )
 			classement = append1(classement tmp)
		    )

		) 

	)

	coord_pont = '()
	coord_via = '()

	for(i 0 length(classement) - 1
		infos = nth(i classement)
		xbord = car(nth(1 infos))
		ybord = cadr(nth(1 infos))
		x = nth(2 infos)
		y = nth(3 infos)

		sprintf(xbord_str, "%f",xbord )	
		sprintf(ybord_str, "%f",ybord )	
		sprintf(x_str, "%f",x )	
		sprintf(y_str, "%f",y )	

		if(ybord_str == y_str then
		   sens = "H"
		   if(x > xbord then
		      shift = "left"
		      x_ancre = xbord - 0.63
		      x_path_begin = x_ancre - 0.63
	           else
		      shift = "right"
		      x_ancre = xbord + 0.63
		      x_path_end = x_ancre + 0.63
	           )			
	           generate_via(obj "R0" 1 2 x_ancre y)
		   coord_via = append1(coord_via list(x_ancre y) )
		   test = oddp(i)
		   if(test == t then
		     coord_pont = append1(coord_pont list(x_path_begin:y x_path_end:y) ) 
		     leCreatePath(cv list("m2" "drawing") list(x_path_begin:y x_path_end:y) path_width) 		   )
	    	)

	    	if(xbord_str == x_str then
		   sens = "V"
		   if(y > ybord then
		      shift = "low"
		      y_ancre = ybord - 0.63
		      y_path_begin = y_ancre - 0.63
	           else
		      shift = "high"
		      y_ancre = ybord + 0.63
		      y_path_end = y_ancre + 0.63
	           )	
		   generate_via(obj "R0" 2 1 x y_ancre)
		   coord_via = append1(coord_via list(x y_ancre) )
		   test = oddp(i)
		   if(test == t then
		     coord_pont = append1(coord_pont list(y_path_begin:y y_path_end:y) ) 
		     leCreatePath(cv list("m2" "drawing") list(x:y_path_begin x:y_path_end) path_width)		
		   )
	        )
	)



;	points = list(aaa bbb)
;	id = leCreatePath(cv list("m2" "drawing") points path_width)	

	printf("STOP \n")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(start_jump()

	if(flag_user_custom == nil then
		printf("********************************************** \n")
		printf("[INFOS]: Techno file no selected !. \n")
		printf("********************************************** \n")
	else
		jump()
	)

)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
if(MODE_DEBUG == t then

	fichier_config = strcat( PATH_DIR "config/onc25_techno.conf" )	
	chargement_techno_conf(fichier_config)
	jump();;;debug_jump() ;;;jump()
)

