public bool UploadAction(int actionId) { if ((actionId < 0) || (actionId >= CONST.AI.MAX_ACTION)) { UpdateInfo(String.Format("Invalid action ID: {0}", actionId), UTIL.InfoType.error); return false; } data.ActionInfo ai = actionTable.action[actionId]; byte[] header = ai.GetData(); // TODO: Error handling // For safety, wait 1s for writing header if (!V2_TryCommand(header, 1000)) { UpdateInfo(String.Format("上传动作档 {0} 的头文件 出错, 动作可能已损坏, 请再次上传.", ai.actionCode), UTIL.InfoType.error); return false; } UInt16 poseCnt = ai.poseCnt; for (UInt16 pId = 0; pId < poseCnt; pId++) { byte[] poseData = ai.GetPoseData(pId); // For safety, wait 1s for writing data if (!V2_TryCommand(poseData, 1000)) { UpdateInfo(String.Format("上传动作档 {0} 出错, 动作可能已损坏, 请再次上传.", ai.actionCode), UTIL.InfoType.error); return false; } } UpdateInfo(String.Format("Action {0} has been uploaded", ai.actionCode)); return true; }
private void ActoinList_InsertPose(InsertMode mode) { int actionId; if ((actionId = GetSelectedActionId()) < 0) { return; } ; data.ActionInfo ai = UBT.actionTable.action[actionId]; if (ai.actionFileExists && !ai.poseLoaded) { MessageBoxResult mbr = MessageBox.Show("现在编辑会以新动作为基础, 先下载原来的动作吗? ", "档案尚未下载", MessageBoxButton.YesNoCancel, MessageBoxImage.Question); if (mbr == MessageBoxResult.Cancel) { return; } if (mbr == MessageBoxResult.Yes) { DownloadAction(); if (!ai.poseLoaded) { return; } } } int poseId; if (mode == InsertMode.END) { poseId = ai.pose.Length; } else { if ((poseId = IsPoseSelected()) < 0) { return; } if (mode == InsertMode.AFTER) { poseId++; } } int newPose = ai.InsertPose((UInt16)poseId); if (newPose < 0) { return; } RefreshAction(actionId); ucActionDetail.SetSelectedIndex(newPose); }
private void LoadActionFile() { int actionId = GetSelectedActionId(); if (actionId < 0) { return; } OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = CONST.ROBOT_ACTION_FILTER; if (openFileDialog.ShowDialog() == true) { String fileName = openFileDialog.FileName; try { long size = new FileInfo(fileName).Length; bool validFileSize = false; if (size >= CONST.AI.HEADER.SIZE) { validFileSize = (((size - CONST.AI.HEADER.SIZE) % CONST.AI.POSE.SIZE) == 0); } if (!validFileSize) { UpdateInfo(String.Format("Invalid file size: {0} has {1} bytes.", fileName, size), UTIL.InfoType.error); return; } FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); byte[] actionData = null; // int can already have 2,147,483,647 , no need to split actionData = br.ReadBytes((int)size); br.Close(); fs.Close(); data.ActionInfo ai = UBT.actionTable.action[actionId]; if (ai.ReadFromArray(actionData)) { UpdateInfo(String.Format("Rebuild action table from {0}", fileName)); } else { UpdateInfo(String.Format("Error building table from {0}", fileName), UTIL.InfoType.error); } } catch (Exception ex) { UpdateInfo(String.Format("Error reading data from {0}: {1}", fileName, ex.Message), UTIL.InfoType.error); } RefreshActionInfo(); } }
private void G2Conv() { int actionId = GetSelectedActionId(); if (actionId < 0) { return; } OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = CONST.ROBOT_CSV_FILTER; if (openFileDialog.ShowDialog() == true) { String fileName = openFileDialog.FileName; char actionCode = UBT.actionTable.action[actionId].actionCode; if (MessageConfirm(String.Format("把動作 {0} 根據 {1} 的設定, 由 Alpha 1 轉成 Alpha 2 的角度, 當前資料將會被覆蓋", actionCode, fileName))) { try { string[] csv = System.IO.File.ReadAllLines(fileName); if (csv.Length < 6) { UpdateInfo(String.Format("Invalid file size: {0} has {1} lines.", fileName, csv.Length), MyUtil.UTIL.InfoType.error); return; } data.ActionInfo ai = UBT.actionTable.action[actionId]; if (ai.G2Conv(csv)) { UpdateInfo(String.Format("Rebuild action table from {0}", fileName)); } else { UpdateInfo(String.Format("Error building table from {0}", fileName), MyUtil.UTIL.InfoType.error); } } catch (Exception ex) { UpdateInfo(String.Format("Error reading data from {0}: {1}", fileName, ex.Message), MyUtil.UTIL.InfoType.error); } RefreshActionInfo(); } } }
private void SaveActionFile() { int actionId = GetSelectedActionId(); if (actionId < 0) { return; } UpdateInfo(); data.ActionInfo ai = UBT.actionTable.action[actionId]; if (ai.actionFileExists && (!ai.poseLoaded)) { MessageBox.Show("动作资料尚未下载到电脑, 请先 [下载动作]", "无法储存", MessageBoxButton.OK, MessageBoxImage.Asterisk); return; } SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = CONST.ROBOT_ACTION_FILTER; if (saveFileDialog.ShowDialog() == true) { String fileName = saveFileDialog.FileName; try { FileStream fs = File.Create(fileName, 2048, FileOptions.None); BinaryWriter bw = new BinaryWriter(fs); bw.Write(ai.GetData()); for (UInt16 poseId = 0; poseId < ai.poseCnt; poseId++) { bw.Write(ai.GetPoseData(poseId)); } bw.Close(); fs.Close(); UpdateInfo(String.Format("Action table saved to {0}", fileName)); } catch (Exception ex) { UpdateInfo(String.Format("Error saving to {0}: {1}", fileName, ex.Message), UTIL.InfoType.error); } } }
private void SaveActionCsvFile() { int actionId = GetSelectedActionId(); if (actionId < 0) { return; } UpdateInfo(); data.ActionInfo ai = UBT.actionTable.action[actionId]; if (ai.actionFileExists && (!ai.poseLoaded)) { MessageBox.Show("动作资料尚未下载到电脑, 请先 [下载动作]", "无法储存", MessageBoxButton.OK, MessageBoxImage.Asterisk); return; } SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = CONST.ROBOT_CSV_FILTER; if (saveFileDialog.ShowDialog() == true) { String fileName = saveFileDialog.FileName; try { TextWriter tw = new StreamWriter(fileName, false); tw.Write(ai.GetCsv()); tw.Write(data.PoseInfo.GetCsvHeader()); for (UInt16 poseId = 0; poseId < ai.poseCnt; poseId++) { tw.Write(ai.GetPoseCsv(poseId)); } tw.Close(); UpdateInfo(String.Format("Action CSV file saved to {0}", fileName)); } catch (Exception ex) { UpdateInfo(String.Format("Error saving to {0}: {1}", fileName, ex.Message), MyUtil.UTIL.InfoType.error); } } }
public bool DownloadAction(byte actionId, bool fullDownload) { data.ActionInfo ai = actionTable.action[actionId]; // A9 9A 03 61 00 66 ED byte[] command = { 0xA9, 0x9A, 0x03, CONST.CMD.GET_ADHEADER, actionId, 0, 0xED }; if (V2_SendCommand(command, true, 1000)) { if (receiveBuffer.Count != CONST.AI.HEADER.SIZE) { return false; } if (ai.ReadHeaderFromArray(receiveBuffer.ToArray())) { if (fullDownload) { if (ai.poseCnt > 0) { ai.SetPoseSize(ai.poseCnt); for (UInt16 pId = 0; pId < ai.poseCnt; pId++) { DownloadActionPose(actionId, pId); } } ai.poseLoaded = true; ai.CheckPoses(); } else { ai.poseLoaded = false; } } } return true; }