CoatingSegmentPart.cs 5.27 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace FLY.Thick.Base.Common
{
    public class CoatingSegmentPart : INotifyPropertyChanged
    {
        private double total;
        //涂长+留白单位mm
        public double Total
        {
            get { return total; }
            set
            {
                if (total != value)
                {
                    total = value;
                    NotifyPropertyChanged("Total");
                }
            }
        }
        private double distance;
        public double Distance//留白单位mm
        {
            get { return distance; }
            set
            {
                if (distance != value)
                {
                    distance = value;
                    NotifyPropertyChanged("Distance");
                }
            }
        }
        public CoatingSegmentPart Clone() 
        {
            CoatingSegmentPart p = new CoatingSegmentPart();
            p.Total = Total;
            p.Distance = Distance;
            return p;
        }
        public byte[] ToByte()
        {
            List<byte> buf = new List<byte>();
            buf.AddRange(BitConverter.GetBytes(Total));
            buf.AddRange(BitConverter.GetBytes(Distance));

            return buf.ToArray();
        }
        /// <summary>
        /// 16个字节转换
        /// </summary>
        /// <param name="value"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value, int offset)
        {
            if (value.Length - offset < 16)
                return false;

            Total = BitConverter.ToDouble(value, offset + 0);
            Distance = BitConverter.ToDouble(value, offset + 8);
            return true;
        }


        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class CoatingSegmentProfile : INotifyPropertyChanged
    {

        string productname;
        /// <summary>
        /// 产品名称
        /// </summary>
        public string ProductName
        {
            get { return productname; }
            set
            {
                if (productname != value)
                {
                    productname = value;
                    NotifyPropertyChanged("ProductName");
                }
            }
        }


        ObservableCollection<CoatingSegmentPart> coatingpartlist = new ObservableCollection<CoatingSegmentPart>();
        /// <summary>
        /// 涂布段长 &amp; 留白
        /// </summary>
        public ObservableCollection<CoatingSegmentPart> CoatingPartList
        {
            get { return coatingpartlist; }
        }



        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            byte[] name_bytes = Misc.Converter.StringToBytes(ProductName);
            if (name_bytes == null)
            {
                buf.AddRange(BitConverter.GetBytes(0));
            }
            else{
                buf.AddRange(BitConverter.GetBytes((int)name_bytes.Length));
                buf.AddRange(name_bytes);
            }
            
            buf.AddRange(BitConverter.GetBytes(CoatingPartList.Count()));
            for (int i = 0; i < CoatingPartList.Count(); i++)
                buf.AddRange(CoatingPartList[i].ToByte());
            
            return buf.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="offset"></param>
        /// <param name="cnt"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value, int offset, out int cnt)
        {
            cnt = (4 + 4);
            if (value.Length - offset < cnt)
                return false;
            int idx = offset;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len;
            if (value.Length < cnt)
                return false;

            byte[] bytes = new byte[len];
            Array.Copy(value, idx, bytes, 0, len);
            idx += len;
            string name = Misc.Converter.BytesToString(bytes);

            int len2 = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len2 * 16;
            if (value.Length < cnt)
                return false;

            CoatingPartList.Clear();

            ProductName = name;
            for (int i = 0; i < len2; i++)
            {
                CoatingSegmentPart coatingPart = new CoatingSegmentPart();
                coatingPart.TryParse(value, idx);
                idx += 16;
                CoatingPartList.Add(coatingPart);
            }
            return true;
        }




        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}