/// <summary> /// バイナリ形式からMotionDataSetを読み込みます。 /// </summary> /// <param title="reader">読み込み元</param> public void ReadBinary(BinaryReader reader) { string mdsb = reader.ReadString(); if (mdsb != "MDSB") { throw new InvalidDataException("Invalid file format"); } this.ReportProgress(0, "Load Data Set: Header"); string headerString = reader.ReadString(); using (MemoryStream headerStream = new MemoryStream(Encoding.UTF8.GetBytes(headerString))) using (XmlReader headerReader = XmlReader.Create(headerStream)) { Header = MotionDataHeader.Deserialize(headerReader); } string version = reader.ReadString(); if (version != "BinaryVersion:1") { throw new InvalidDataException("binary version mismatch"); } int length = reader.ReadInt32(); for (int i = 0; i < length; i++) { this.ReportProgress(100 * i / length, string.Format("Load Data Set: {0} / {1}", i, length)); decimal time = reader.ReadDecimal(); MotionDataFrame frame = new MotionDataFrame(); frame.ReadBinary(reader); AddFrame(time, frame); } }
public MotionDataObjectInfo(MotionDataHeader parent) { if (parent == null) { throw new ArgumentNullException("parent", "'parent' cannot be null"); } _parent = parent; _objectType = MotionDataObjectType.Undefined; }
public static MotionDataHeader Deserialize(XmlReader reader) { reader.MoveToContent(); reader.ReadStartElement(typeof(MotionDataHeader).Name); MotionDataHeader ret = new MotionDataHeader(); List <MotionDataObjectInfo> infos = new List <MotionDataObjectInfo>(); for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.None; reader.MoveToContent()) { if (reader.IsEmptyElement) { reader.Skip(); continue; } switch (reader.Name) { case "MotionFieldState": XmlSerializer ser = new XmlSerializer(typeof(MotionFieldState)); ret.FieldState = (MotionFieldState)ser.Deserialize(reader); break; case "Infos": reader.ReadStartElement("Infos"); for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.None; reader.MoveToContent()) { if (reader.IsEmptyElement) { reader.Skip(); continue; } switch (reader.Name) { case "MotionDataObjectInfo": infos.Add(MotionDataObjectInfo.Deserialize(reader, ret)); break; default: reader.Skip(); break; } } reader.ReadEndElement(); break; default: reader.Skip(); break; } } reader.ReadEndElement(); ret._infos = infos.ToArray(); return(ret); }
/// <summary> /// データをXML形式から読み込みます。 /// </summary> /// <param title="reader"></param> public void ReadXml(XmlReader reader) { reader.MoveToContent(); if (reader.IsEmptyElement) { reader.Skip(); return; } reader.ReadStartElement("MotionDataSet"); int frameLength = 0; int count = 0; for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.None; reader.MoveToContent()) { switch (reader.Name) { case "MotionDataHeader": lock (this) { Header = MotionDataHeader.Deserialize(reader); } break; case "EnumerateFrame()": if (reader.IsEmptyElement) { reader.Skip(); continue; } string lengthStr; if ((lengthStr = reader.GetAttribute("Length")) != null) { int length; if (int.TryParse(lengthStr, out length)) { frameLength = length; } } reader.ReadStartElement("EnumerateFrame()"); while (reader.NodeType != XmlNodeType.None) { if (reader.NodeType == XmlNodeType.EndElement) { break; } string timeStr; switch (reader.Name) { case "Frame": if (reader.IsEmptyElement || (timeStr = reader.GetAttribute("Time")) == null) { reader.Skip(); continue; } reader.ReadStartElement("Frame"); decimal time; if (decimal.TryParse(timeStr, out time) && reader.IsStartElement("MotionDataFrame")) { MotionDataFrame frame = MotionDataFrame.Deserialize(reader); AddFrame(time, frame); if (frameLength != 0) { this.ReportProgress(100 * count / frameLength, string.Format("Load Data Set: {0} / {1}", count, frameLength)); } else { this.ReportProgress(100 - (10000 / (count + 100)), string.Format("Load Data Set: {0}", count)); } count++; } while (reader.NodeType != XmlNodeType.None && reader.NodeType != XmlNodeType.EndElement) { reader.Skip(); } reader.ReadEndElement(); break; default: reader.Skip(); break; } } while (reader.NodeType != XmlNodeType.None && reader.NodeType != XmlNodeType.EndElement) { reader.Skip(); } reader.ReadEndElement(); break; default: reader.Skip(); break; } } reader.ReadEndElement(); }
/// <summary> /// 既定のコンストラクタ /// </summary> public MotionDataSet() { Header = new MotionDataHeader(); _frames = new SortedList <decimal, MotionDataFrame>(); }
public MotionDataObjectInfo(MotionDataHeader parent, MotionDataObjectType type, int index) : this(parent) { this.ObjectType = type; this.ObjectIndex = index; }
public static MotionDataObjectInfo Deserialize(XmlReader reader, MotionDataHeader parent) { if (parent == null) { throw new ArgumentNullException("parent", "'parent' cannot be null"); } reader.MoveToContent(); if (reader.IsEmptyElement) { return(null); } else { string name = "", groupName = ""; Color color = Color.White; MotionDataObjectType type = MotionDataObjectType.Undefined; int index = -1; bool visible = true; bool isVirtual = false; reader.ReadStartElement(typeof(MotionDataObjectInfo).Name); for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.None; reader.MoveToContent()) { if (reader.IsEmptyElement) { reader.Skip(); continue; } switch (reader.Name) { case "Name": name = reader.ReadElementContentAsString(); break; case "GroupName": groupName = reader.ReadElementContentAsString(); break; case "ObjectType": type = (MotionDataObjectType)Enum.Parse(typeof(MotionDataObjectType), reader.ReadElementContentAsString()); break; case "ObjectIndex": index = reader.ReadElementContentAsInt(); break; case "Show": case "Visible": visible = reader.ReadElementContentAsBoolean(); break; case "Virtual": isVirtual = reader.ReadElementContentAsBoolean(); break; case "Color": var cstr = reader.ReadElementContentAsString(); int cint; if (int.TryParse(cstr, out cint)) { color = Color.FromArgb(cint); } else { color = ColorTranslator.FromHtml(cstr); } break; default: reader.Skip(); break; } } reader.ReadEndElement(); if (type == MotionDataObjectType.Undefined || index == -1) { return(null); } var ret = new MotionDataObjectInfo(parent, type, index); ret.Name = name; ret.GroupName = groupName; ret.Color = color; ret.Visible = visible; ret.Virtual = isVirtual; return(ret); } }