#include "GCMotion.h"
#include "string.h"
#include <windows.h>
#include <math.h>
using namespace System;
using namespace System::Windows::Forms;

/*****************************************************************
* Function Name : 指令返回值
* Description   : 返回错误值 rtn
* Date			: 2022年8月3日
* Auther		: LWH
******************************************************************/
short gc_rtn_error(short rtn)
{
	if (rtn != RTN_CMD_SUCCESS) {
		MessageBox::Show(Convert::ToString(rtn));
		return rtn;		// 指令执行错误
	}
}

/*****************************************************************
* Function Name : 坐标系变换之旋转变换例程
* Description   : 略
* Date			: 2022年8月3日
* Auther		: LWH
******************************************************************/
short gc_example(void)
{
	short rtn;					// 指令返回值
	short devNum;				// 设备序号，从0开始
	short axisNum;				// 轴号，从0开始
	HAND devHandle;				// 设备句柄
	HAND axisHandle;			// 轴句柄
	HAND crdHandle;				// 坐标系句柄
	rtn = NMC_DevOpen(devNum, &devHandle);				//打开控制器
	gc_rtn_error(rtn);
	rtn = NMC_MtOpen(devHandle, axisNum, &axisHandle);	//打开轴,并获得轴句柄
	gc_rtn_error(rtn);
	rtn = NMC_MtSetSvOn(axisHandle);					//打开轴使能(根据实际情况使用)
	gc_rtn_error(rtn);

	/************************* 先建立坐标系(插补),过程略 ***************************/
	short rtn = 0;
	double mmToPluse = 1000;
	double endVel, vel, acc, rotVel;
	endVel = 0;
	vel = 10;
	acc = 1;
	rotVel = 3;
	double m_l = 100;									//圆角矩形的长为mm
	double m_w = 60;									//圆角矩形的宽为mm
	double m_r = 5;										//圆角矩形的倒角半径为mm
	//旋转轴为轴,旋转一圈脉冲数为，旋转中心为(,15000)
	rtn = NMC_CrdSetTransRotate(crdHandle, 3, (2 * 3.1415926) / 10000, 0, 15000);
	gc_rtn_error(rtn);
	rtn = NMC_CrdBufClr(crdHandle);//指令缓冲区清空
	gc_rtn_error(rtn);
	long rotPos = -2500;
	long posArray[4] = { 0,0,0,0 };
	long segNo = 0;
	//插补轨迹第一条得指定旋转变换的轴，这条跟随位移是
	rtn = NMC_CrdBufAxMoveRel(crdHandle, segNo++, 8, posArray, 0, 1);
	gc_rtn_error(rtn);
	rtn = NMC_CrdLineXYZEx(crdHandle, segNo++, 15, posArray, endVel, vel, acc, 0);//运动到原点
	gc_rtn_error(rtn);
	rtn = NMC_CrdBufSetPtpMovePara(crdHandle, segNo++, 3, rotVel, 1, 0);//缓冲区单轴移动参数设置
	gc_rtn_error(rtn);
	posArray[0] = (0.5 * m_l - m_r) * mmToPluse;
	rtn = NMC_CrdLineXYZEx(crdHandle, segNo++, 3, posArray, endVel, vel, acc, 0);// 直线插补
	gc_rtn_error(rtn);
	rtn = NMC_CrdBufAxMoveRel(crdHandle, segNo++, 8, &rotPos, 0, 1);//缓冲区单轴移动(相对位移移动)
	gc_rtn_error(rtn);
	posArray[0] = (0.5 * m_l) * mmToPluse;
	posArray[1] = (m_r)*mmToPluse;
	// XY平面圆弧插补：终点位置、半径、方向
	rtn = NMC_CrdArcRadiusEx(crdHandle, segNo++, posArray, (m_r)*mmToPluse, 1, endVel, vel, acc, 0);
	gc_rtn_error(rtn);
	posArray[0] = (0.5 * m_l) * mmToPluse;
	posArray[1] = (m_w - m_r) * mmToPluse;
	rtn = NMC_CrdLineXYZEx(crdHandle, segNo++, 3, posArray, endVel, vel, acc, 0);// 直线插补
	gc_rtn_error(rtn);
	rtn = NMC_CrdBufAxMoveRel(crdHandle, segNo++, 8, &rotPos, 0, 1);//缓冲区单轴移动(相对位移移动)
	gc_rtn_error(rtn);
	posArray[0] = (0.5 * m_l - m_r) * mmToPluse;
	posArray[1] = (m_w)*mmToPluse;
	// XY平面圆弧插补：终点位置、半径、方向
	rtn = NMC_CrdArcRadiusEx(crdHandle, segNo++, posArray, (m_r)*mmToPluse, 1, endVel, vel, acc, 0);
	gc_rtn_error(rtn);
	posArray[0] = (-0.5 * m_l + m_r) * mmToPluse;
	posArray[1] = (m_w)*mmToPluse;
	rtn = NMC_CrdLineXYZEx(crdHandle, segNo++, 3, posArray, endVel, vel, acc, 0); // 直线插补
	gc_rtn_error(rtn);
	rtn = NMC_CrdBufAxMoveRel(crdHandle, segNo++, 8, &rotPos, 0, 1); // 缓冲区单轴移动(相对位移移动)
	gc_rtn_error(rtn);
	posArray[0] = (-0.5 * m_l) * mmToPluse;
	posArray[1] = (m_w - m_r) * mmToPluse;
	//  XY平面圆弧插补：终点位置、半径、方向
	rtn = NMC_CrdArcRadiusEx(crdHandle, segNo++, posArray, (m_r)*mmToPluse, 1, endVel, vel, acc, 0);
	gc_rtn_error(rtn);
	posArray[0] = (-0.5 * m_l) * mmToPluse;
	posArray[1] = (m_r)*mmToPluse;
	rtn = NMC_CrdLineXYZEx(crdHandle, segNo++, 3, posArray, endVel, vel, acc, 0);// 直线插补
	gc_rtn_error(rtn);
	rtn = NMC_CrdBufAxMoveRel(crdHandle, segNo++, 8, &rotPos, 0, 1);	//缓冲区单轴移动(相对位移移动)
	gc_rtn_error(rtn);
	posArray[0] = (-0.5 * m_l + m_r) * mmToPluse;
	posArray[1] = 0;
	// XY平面圆弧插补：终点位置、半径、方向
	rtn = NMC_CrdArcRadiusEx(crdHandle, segNo++, posArray, (m_r)*mmToPluse, 1, endVel, vel, acc, 0);
	gc_rtn_error(rtn);
	posArray[0] = 0;
	posArray[1] = 0;
	rtn = NMC_CrdLineXYZEx(crdHandle, segNo++, 3, posArray, endVel, vel, acc, 0);	// 直线插补
	gc_rtn_error(rtn);
	rtn = NMC_CrdEndMtn(crdHandle);				//结束缓冲区
	gc_rtn_error(rtn);
	rtn = NMC_CrdStartMtn(crdHandle);			//启动缓冲区插补
	gc_rtn_error(rtn);
	return 0; 
}
