import static lib.MtnLib.*;
import static lib.MtnLib_com.*;
import static lib.MtnLib_ext.*;
import static lib.MtnLib_ome.*;
import static lib.MtnLib_err.*;
import com.sun.jna.*;
import com.sun.jna.ptr.*;

public class Main {
    static {
        String libPath = System.getProperty("user.dir") + "\\lib";
        System.setProperty("java.library.path", libPath);
        System.load(libPath + "\\msvcr100.dll");
        System.load(libPath + "\\msvcp100.dll");
        System.load(libPath + "\\nmc_lib20.dll");
    }
    private static void checkError(short rtn, String operation) {
        if (rtn != 0) {
            System.err.printf("错误: %s (代码: %d)\n", operation, rtn);
//            System.exit(1);
        }
    }
    public static void main(String[] args) {

        short rtn;// 指令返回值
        short axisNum = 0;// 轴号，从0开始
        HAND devHandle = new HAND();// 设备句柄
        PHAND pDevHandle = new PHAND();
        HAND axisHandle = new HAND();// 轴句柄
        PHAND pAxisHandle = new PHAND();
        HAND crdHandle = new HAND();
        PHAND pcrdHandle = new PHAND();

        short[] devCount = new short[1];
        TDevInfo[] devInfoList = new TDevInfo[10];
        rtn = NMC_DevSearch(TSearchMode.Ethernet, devCount, devInfoList);
        checkError(rtn,"搜索设备出错");
        rtn = NMC_DevOpen(devInfoList[0].address, pDevHandle);				//打开控制器
        checkError(rtn,"打开控制器出错");
        devHandle = pDevHandle.getHandle();
        rtn = NMC_MtOpen(devHandle, axisNum, pAxisHandle); // 打开轴
        checkError(rtn,"打开轴出错");
        axisHandle = pAxisHandle.getHandle();
        rtn = NMC_MtSetSvOn(axisHandle);//打开轴使能(根据实际情况使用)
        checkError(rtn,"轴使能出错");
        rtn = NMC_CrdOpen(devHandle, pcrdHandle);			//打开坐标系
        checkError(rtn,"打开坐标系出错");
        crdHandle = pcrdHandle.getHandle();

        double pulse2Rad = 2 * Math.PI / 288000;			//A轴旋转一圈脉冲为
        NativeLong deltaX = new NativeLong(-3687);						//0转到PI/3后重新校正到一点是的X偏移量
        NativeLong deltaY = new NativeLong(-9494);						//0转到PI/3后重新校正到一点是的Y偏移量
        NativeLongByReference toolX = new NativeLongByReference(new NativeLong(0L));
        NativeLongByReference toolY = new NativeLongByReference(new NativeLong(0L));
        rtn = NMC_CrdSetXYZAToolCalc(crdHandle, Math.PI / 3, deltaX, deltaY, toolX, toolY);//计算工具偏移
        checkError(rtn,"计算工具偏移出错");
        rtn = NMC_CrdSetXYZAPara(crdHandle, pulse2Rad, toolX.getValue(), toolY.getValue());//设置工具参数
        checkError(rtn,"设置工具参数出错");
        short[] pMapAxisArray = { 0, 1, 2, 3 };
        rtn = NMC_CrdSetTransXYZA(crdHandle, pMapAxisArray);//设置XYZA机型
        checkError(rtn,"设置XYZA机型出错");
    }
}
