
PROGRAM _INIT
	(* Insert code here *)
	fRTInfo.enable := 1;
	fRTInfo();
	cycT := fRTInfo.cycle_time/1000;	(*ms*)
	
	(*显示区域相关参数*)
	VISU_NAME:=	'Visu'; 
	

END_PROGRAM

PROGRAM _CYCLIC
	(* Insert code here *)
	
	IF(current_page = 221)THEN//不是曲线显示界面，不执行
		VISU_DRAWBOX_NAME:= 'P_221_CamShow/Default/DrawBox_1';
	END_IF
	
	
	IF GetCamPointsFinish = 0 THEN
		RETURN;	
	END_IF
	
	IF(current_page = 221) THEN//不是曲线显示界面，不执行
		
	ELSE	
		RETURN;
	END_IF	

	
	//防止除零
	IF Master_all < 1THEN
		Master_all :=100;
	END_IF;
			
	IF Slave_all < 1 THEN
		Slave_all := 100;
	END_IF;	

	//精度
	IF(current_page = 221)THEN//送膜轴	
		Resolution := 10;//1mm=10unit	
	END_IF
	
	
	
	(*获得Vis句柄*)
	vcHandle := VA_Setup(1,VISU_NAME);//设置到可视化的连接。

	IF(vcHandle = 0)THEN
		RETURN;
	END_IF

	//刷新画面
	refreshTime := refreshTime + cycT;
	IF(refreshTime < REFRESH_TIME)THEN
		RETURN;
	ELSE                          
		refreshTime := 0;  
	END_IF

   
	//画凸轮曲线
	statusVaccess := VA_Saccess(1,vcHandle);//这是VISAPI中最重要的函数之一。只有使用VA_Saccess才能执行VISAPI函数。
	IF (statusVaccess = 0)THEN
		(* 关联到 DrawBox 控件 *)
		statusAttach := VA_Attach(1,vcHandle,0,ADR(VISU_DRAWBOX_NAME));//建立到可视化项目中页面上创建的DrawBox控件的连接。执行VA_Attach()，告诉PLC需要画图到指定DrawBox控件上
	
		IF(statusAttach = 0)THEN        
			(*获取控件的宽度和高度*)
			DisplayStatusW := VA_GetDisplayInfo(1,vcHandle,1,ADR(width));//显示控件的宽度，像素，该功能可以查询如下信息:max.X像素，max.Y像素，以及每像素的比特数
			
			DisplayStatusH := VA_GetDisplayInfo(1,vcHandle,2,ADR(height));//显示控件的高度，像素
           
			(*清除所有区域值*)
			VA_Rect(1,vcHandle,0,0,width-2,height-2,15,252); //15：白色；51：红色  
				//画图基础
				//1 - 画图函数坐标都是以像素为单位，所以对于大多数应用都需要折算一下scale才可以画图。例如模拟量画图0-32767需要对应到0-drawbox高度	
				//2 - 画图x轴从左向右  y轴从上到下  所以（0，0）表示左上角

			
			
		(*******************************************工程数据与像素换算关系*************************************)
			(*实际范围与显示区域像素范围转换*)
			MaxRangeX     := LIMIT(1, Master_all*Resolution,Master_all*Resolution);(*Master_all单位是mm，MaxRangeX单位是unit*)
			MaxRangeY     := LIMIT(1,Slave_all*Resolution,Slave_all*Resolution);(*500单位是度，MaxRangeY单位是unit*)
			MinRangeY     := 0;

	
			(*比例系数*)
			coordinatePiexlOffsetX      := 60;//坐标系X轴偏移像素值
			coordinatePiexlOffsetY      := 40;//坐标系Y轴偏移像素值
			xFactor        	 			:= LIMIT(0.001,(width-2*coordinatePiexlOffsetX)/MaxRangeX,(width-2*coordinatePiexlOffsetX)/MaxRangeX);//X轴mm与像素换算比例,即像素值=工程值*xFactor
			yFactor         			:= LIMIT(0.001,(height-2*coordinatePiexlOffsetY)/MaxRangeY,(height-2*coordinatePiexlOffsetY)/(MaxRangeY-MinRangeY));//Y轴mm与像素换算比例,即像素值=工程值*yFactor
		
			IF ((REAL_TO_INT(MaxSpeedRatio+1) - REAL_TO_INT(MinSpeedRatio-1)) <> 0 ) THEN
				y1Factor         		:= (height-2*coordinatePiexlOffsetY)/(REAL_TO_INT(MaxSpeedRatio+1) - REAL_TO_INT(MinSpeedRatio-1));//速比与像素换算比例,即像素值=速比*y1Factor
			ELSE
				y1Factor         		:= (height-2*coordinatePiexlOffsetY)/15;//速比与像素换算比例,即像素值=速比*y1Factor	
			END_IF;


			(*坐标划分*)
			coordinateUnitX      := 100; //X坐标刻度，单位是工程值
			coordinateUnitY      := 100;//Y坐标刻度，单位是工程值
			coordinateUnitY1     := 1;//速比Y1坐标刻度，单位是比值
			coordinateUnitNumX   := REAL_TO_UINT(Master_all)/coordinateUnitX;	//X坐标显示的刻度数量
			coordinateUnitNumY   := REAL_TO_UINT(Slave_all)/coordinateUnitY+1;//Y坐标显示的刻度数量

		(*******************************************工程数据与像素换算关系end*************************************)	
		
			
			
			
			(*画横轴*)
			VA_Line(1,vcHandle,coordinatePiexlOffsetX, height-coordinatePiexlOffsetY,   width-coordinatePiexlOffsetX+5, height-coordinatePiexlOffsetY,0  );
			VA_Line(1,vcHandle,coordinatePiexlOffsetX, height-coordinatePiexlOffsetY+1, width-coordinatePiexlOffsetX+5,height-coordinatePiexlOffsetY+1,0 );
 
			(*画纵轴*)
			VA_Line(1,vcHandle,coordinatePiexlOffsetX,   2,coordinatePiexlOffsetX,   height-coordinatePiexlOffsetY,0);
			VA_Line(1,vcHandle,coordinatePiexlOffsetX+1, 2,coordinatePiexlOffsetX+1, height-coordinatePiexlOffsetY,0);

			(*画速比纵轴*)			
			VA_Line(1,vcHandle,width-coordinatePiexlOffsetX+1, 2,width-coordinatePiexlOffsetX+1, height-coordinatePiexlOffsetY,0);
			VA_Line(1,vcHandle,width-coordinatePiexlOffsetX, 2,width-coordinatePiexlOffsetX, height-coordinatePiexlOffsetY,0);
			
			
			(*画横轴的箭头*)
//			VA_Line(1,vcHandle,width-10,height-6-coordinatePiexlOffsetY,width-3,height-coordinatePiexlOffsetY,0);
//			VA_Line(1,vcHandle,width-10,height+6-coordinatePiexlOffsetY,width-3,height-coordinatePiexlOffsetY,0);
//			VA_Line(1,vcHandle,width-10,height-7-coordinatePiexlOffsetY,width-2,height-coordinatePiexlOffsetY,0);
//			VA_Line(1,vcHandle,width-10,height+7-coordinatePiexlOffsetY,width-2,height-coordinatePiexlOffsetY,0);

			(*画纵轴的箭头*)		
			VA_Line(1,vcHandle,coordinatePiexlOffsetX-6,10,coordinatePiexlOffsetX,3,0);
			VA_Line(1,vcHandle,coordinatePiexlOffsetX+6,10,coordinatePiexlOffsetX,3,0);
			VA_Line(1,vcHandle,coordinatePiexlOffsetX-7,10,coordinatePiexlOffsetX,2,0);
			VA_Line(1,vcHandle,coordinatePiexlOffsetX+7,10,coordinatePiexlOffsetX,2,0);
		
			(*画坐标原点*)		
//			VA_Textout (1,vcHandle,2,coordinatePiexlOffsetX-5,height-coordinatePiexlOffsetY+1,0,15,'0');
			(*画速比坐标原点*)
//			VA_Textout (1,vcHandle,2,width-coordinatePiexlOffsetX+10,height-coordinatePiexlOffsetY-15,0,15,'0');
			
			

										
			(*画横轴的刻度*)		
			FOR i:=0 TO coordinateUnitNumX DO	
				VA_Line(1,vcHandle,(coordinatePiexlOffsetX+ REAL_TO_UINT( coordinateUnitX*Resolution*i*xFactor)),height-coordinatePiexlOffsetY+5,(coordinatePiexlOffsetX+REAL_TO_UINT(coordinateUnitX*Resolution*i*xFactor)),height-coordinatePiexlOffsetY,0);
				VA_Line(1,vcHandle,(1+coordinatePiexlOffsetX+REAL_TO_UINT(coordinateUnitX*Resolution*i*xFactor)),height-coordinatePiexlOffsetY+5,(1+coordinatePiexlOffsetX+REAL_TO_UINT(coordinateUnitX*Resolution*i*xFactor)),height-coordinatePiexlOffsetY,0);
				ftoa(coordinateUnitX*i,ADR(stringCSX));
				IF i = 0 THEN
					VA_Textout (1,vcHandle,2,(coordinatePiexlOffsetX-5+REAL_TO_UINT(coordinateUnitX*Resolution*i*xFactor)),height-coordinatePiexlOffsetY+7,0,15,stringCSX);
				ELSE
					VA_Textout (1,vcHandle,2,(coordinatePiexlOffsetX-20+REAL_TO_UINT(coordinateUnitX*Resolution*i*xFactor)),height-coordinatePiexlOffsetY+7,0,15,stringCSX);	
				END_IF;
			END_FOR	
			
			(*画纵轴的刻度*)	
			IF (current_page = 221) THEN//送膜轴	
				coordinateUnitNumMinY := 1;
				SpeedRatioOffsetY := 127;//速比纵轴偏移像素
				CuterOffsetY := 0;			
			END_IF;

			FOR i:=coordinateUnitNumMinY TO coordinateUnitNumY DO			
				VA_Line(1,vcHandle,coordinatePiexlOffsetX-5,(height-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY*Resolution*i*yFactor))-CuterOffsetY,coordinatePiexlOffsetX,height-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY*Resolution*i*yFactor)-CuterOffsetY,0);
				VA_Line(1,vcHandle,coordinatePiexlOffsetX-5,(height-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY*Resolution*i*yFactor)-1)-CuterOffsetY,coordinatePiexlOffsetX,(height-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY*Resolution*i*yFactor)-1)-CuterOffsetY,0);
				ftoa(coordinateUnitY*i,ADR(stringCSY));
			
				IF i < 0 OR i>= 10 THEN
					VA_Textout (1,vcHandle,2,coordinatePiexlOffsetX-50,(height-12-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY*Resolution*i*yFactor))-CuterOffsetY,0,15,stringCSY);	
				ELSIF i >= 0 AND i <10 THEN	
					VA_Textout (1,vcHandle,2,coordinatePiexlOffsetX-40,(height-12-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY*Resolution*i*yFactor))-CuterOffsetY,0,15,stringCSY);		
				END_IF
							
					
//				VA_Textout (1,vcHandle,1,width-40,-23+height-coordinatePiexlOffsetY,0,15,'X [m]');
//				VA_Textout (1,vcHandle,1,coordinatePiexlOffsetX-35,0,0,15,'y');
			END_FOR	
			
			
			//位置和速比部分画图		
			FOR i:=0 TO DISP_POINTS-1 DO
				//位置mm
				VA_Rect(1,vcHandle,REAL_TO_UINT(MasterPositions[i]*xFactor)+coordinatePiexlOffsetX ,REAL_TO_UINT(-SlavePositions[i]*yFactor +height-coordinatePiexlOffsetY )-CuterOffsetY-2,2,2,COLOR_BLUE,COLOR_BLUE);	
				//速比
				VA_Rect(1,vcHandle,REAL_TO_UINT(MasterPositions[i]*xFactor)+coordinatePiexlOffsetX ,REAL_TO_UINT(-SlaveSpeed[i]/MasterSpeed[i]*coordinateUnitY1*y1Factor +height-coordinatePiexlOffsetY )- SpeedRatioOffsetY-2,2,2,120,120);	
			END_FOR	
			
			//切割位置
			IF (current_page = 221) THEN
				VA_Rect(1,vcHandle,coordinatePiexlOffsetX ,REAL_TO_UINT(-FilmAutoCut_start_pos*10*yFactor +height-coordinatePiexlOffsetY )-CuterOffsetY-2,width-2*coordinatePiexlOffsetX,2,COLOR_RED,COLOR_RED);	
			END_IF;

			
		    (*画速比纵轴的刻度*)
			FOR i:=REAL_TO_INT(MinSpeedRatio-1) TO REAL_TO_INT(MaxSpeedRatio+1) - REAL_TO_INT(MinSpeedRatio-1) DO				
				VA_Line(1,vcHandle,width-coordinatePiexlOffsetX,(height-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY1*i*y1Factor))-SpeedRatioOffsetY,width-coordinatePiexlOffsetX+5,height-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY1*i*y1Factor)-SpeedRatioOffsetY,0);
				VA_Line(1,vcHandle,width-coordinatePiexlOffsetX,(height-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY1*i*y1Factor)-1)-SpeedRatioOffsetY,width-coordinatePiexlOffsetX+5,(height-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY1*i*y1Factor)-1)-SpeedRatioOffsetY,0);
				ftoa(coordinateUnitY1*i,ADR(stringCSY));
				VA_Textout (1,vcHandle,2,width-coordinatePiexlOffsetX+10,(height-15-coordinatePiexlOffsetY-REAL_TO_UINT(coordinateUnitY1*i*y1Factor))-SpeedRatioOffsetY,0,15,stringCSY);						
			END_FOR	
					
			
			//触控位置显示
			VA_GetTouchAction(1,vcHandle,0,ADR(TouchAction_0));
			IF TouchAction_0.status = 1 THEN
				x_position		:= TouchAction_0.x;
				y_position		:= TouchAction_0.y;
				VA_Line(1,vcHandle,UDINT_TO_UINT(x_position),0,UDINT_TO_UINT(x_position),height,COLOR_BLUE_LIGHT);
				VA_Line(1,vcHandle,0,UDINT_TO_UINT(y_position),width,UDINT_TO_UINT(y_position),COLOR_BLUE_LIGHT);

				IF x_position < coordinatePiexlOffsetX OR x_position > width OR y_position > height  THEN
					x_show := 0;
				ELSE
					x_show := (x_position-coordinatePiexlOffsetX)/(xFactor*Resolution);	
				END_IF;

				
				IF y_position > height-coordinatePiexlOffsetY THEN
					y_show := 0;
				ELSIF y_position < height-coordinatePiexlOffsetY- CuterOffsetY THEN
					y_show := (height-coordinatePiexlOffsetY-CuterOffsetY-y_position)/(yFactor*Resolution);
				ELSIF y_position > height-coordinatePiexlOffsetY- CuterOffsetY THEN
					y_show := UDINT_TO_DINT((height-coordinatePiexlOffsetY-CuterOffsetY)-y_position)/(yFactor*Resolution);
				END_IF;				
				
				IF y_position > height-coordinatePiexlOffsetY THEN
					y1_show := 0;
				ELSIF y_position < height-coordinatePiexlOffsetY-SpeedRatioOffsetY	THEN
					y1_show := (height-y_position-coordinatePiexlOffsetY-SpeedRatioOffsetY)/(coordinateUnitY1*y1Factor);
				ELSIF y_position > height-coordinatePiexlOffsetY-SpeedRatioOffsetY	THEN
					y1_show := UDINT_TO_DINT((height-coordinatePiexlOffsetY-SpeedRatioOffsetY)-y_position)/(coordinateUnitY1*y1Factor);
				END_IF;

			END_IF;
			
			(*断开 DrawBox 控件连接*)
			VA_Detach(1,vcHandle);//删除到DrawBox控件的连接。
		END_IF
	
		VA_Srelease(1,vcHandle);//VA_Srelease进行资源释放
	END_IF
	 
END_PROGRAM

PROGRAM _EXIT
	(* Insert code here *)
	 
END_PROGRAM

