private void Set3DArray <T>(T[, ,] array, C3DParameterType type) where T : IConvertible { Int32 size = this.GetParameterDataSize(type); this.SetDimension((Byte)array.GetLength(0), (Byte)array.GetLength(1), (Byte)array.GetLength(2)); this._parameterType = type; this._parameterData = new Byte[array.Length * size]; Int32 index = 0; for (Int32 x = 0; x < array.GetLength(0); x++) { for (Int32 y = 0; y < array.GetLength(1); y++) { for (Int32 z = 0; z < array.GetLength(2); z++) { if (size == 1) { this._parameterData[index++] = array[x, y, z].ToByte(null); } else { Array.Copy(C3DBitConverter.GetBytes <T>(array[x, y, z]), 0, this._parameterData, index++ *size, size); } } } } }
/// <summary> /// 初始化新的C3D参数项 /// </summary> /// <param name="processorType">原处理器类型</param> /// <param name="id">参数项ID</param> /// <param name="name">参数项名称数据块</param> /// <param name="isLocked">是否锁定</param> /// <param name="lastData">最后一部分数据块</param> internal C3DParameter(C3DProcessorType processorType, SByte id, Byte[] name, Boolean isLocked, Byte[] lastData) : base(id, name, isLocked) { if (lastData == null) { this._parameterType = C3DParameterType.Invalid; this._dimensions = null; this._parameterData = null; return; } this._parameterType = (C3DParameterType)lastData[0]; Int32 dataLength = 1, dimension = lastData[1]; if (dimension > 0) { this._dimensions = new Byte[dimension]; for (Byte i = 0; i < dimension; i++) { this._dimensions[i] = lastData[2 + i]; dataLength *= this._dimensions[i]; } dataLength *= GetParameterDataSize(this._parameterType); } else { this._dimensions = null; dataLength = GetParameterDataSize(this._parameterType); } this._parameterData = new Byte[dataLength]; Array.Copy(lastData, 2 + dimension, this._parameterData, 0, this._parameterData.Length); //修正不同处理器文档格式问题 if (this._parameterType == C3DParameterType.Int16 || this._parameterType == C3DParameterType.Single) { this.UpdateData(this._parameterType, processorType, dimension); } this.SetDescription(lastData, 2 + dimension + this._parameterData.Length); }
private void Set1DArray <T>(T[] array, C3DParameterType type) where T : IConvertible { Int32 size = this.GetParameterDataSize(type); this.SetDimension((Byte)array.Length); this._parameterType = type; this._parameterData = new Byte[array.Length * size]; for (Int32 i = 0; i < array.Length; i++) { if (size == 1) { this._parameterData[i] = array[i].ToByte(null); } else { Array.Copy(C3DBitConverter.GetBytes <T>(array[i]), 0, this._parameterData, i * size, size); } } }
private void Set1DStringArray(String[] array) { Byte count = (Byte)array.Length, maxLen = 0; for (Int32 i = 0; i < array.Length; i++) { maxLen = Math.Max((Byte)array[i].Length, maxLen); } this.SetDimension(maxLen, count); this._parameterType = C3DParameterType.Char; this._parameterData = new Byte[count * maxLen]; for (Int32 i = 0; i < this._parameterData.Length; i++) { this._parameterData[i] = 0x20;//全部初始化为空格 } for (Int32 i = 0; i < count; i++) { this.WrtieStringToBytes(array[i], 0, array[i].Length, this._parameterData, i * maxLen); } }
/// <summary> /// 更新参数内数据 /// </summary> /// <param name="parameterType">参数类型</param> /// <param name="processorType">处理器类型</param> /// <param name="dimensionCount">维数</param> private void UpdateData(C3DParameterType parameterType, C3DProcessorType processorType, Int32 dimensionCount) { #region Basic Type if (dimensionCount == 0) { if (parameterType == C3DParameterType.Int16) { this.InternalSetData <Int16>(C3DBitConverter.ToInt16(processorType, this._parameterData)); } else if (parameterType == C3DParameterType.Single) { this.InternalSetData <Single>(C3DBitConverter.ToSingle(processorType, this._parameterData)); } return; } #endregion #region 1D-Array if (dimensionCount == 1) { if (parameterType == C3DParameterType.Int16) { Byte[][] datas = this.Get1DByteArray(sizeof(Int16)); Int16[] newdata = new Int16[datas.Length]; for (Int32 i = 0; i < datas.Length; i++) { newdata[i] = C3DBitConverter.ToInt16(processorType, datas[i]); } this.InternalSetData <Int16[]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[][] datas = this.Get1DByteArray(sizeof(Single)); Single[] newdata = new Single[datas.Length]; for (Int32 i = 0; i < datas.Length; i++) { newdata[i] = C3DBitConverter.ToSingle(processorType, datas[i]); } this.InternalSetData <Single[]>(newdata); } return; } #endregion #region 2D-Array if (dimensionCount == 2) { if (parameterType == C3DParameterType.Int16) { Byte[, ][] datas = this.Get2DByteArray(sizeof(Int16)); Int16[,] newdata = new Int16[datas.GetLength(0), datas.GetLength(1)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { newdata[x, y] = C3DBitConverter.ToInt16(processorType, datas[x, y]); } } this.InternalSetData <Int16[, ]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[, ][] datas = this.Get2DByteArray(sizeof(Single)); Single[,] newdata = new Single[datas.GetLength(0), datas.GetLength(1)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { newdata[x, y] = C3DBitConverter.ToSingle(processorType, datas[x, y]); } } this.InternalSetData <Single[, ]>(newdata); } return; } #endregion #region 3D-Array if (dimensionCount == 3) { if (parameterType == C3DParameterType.Int16) { Byte[, , ][] datas = this.Get3DByteArray(sizeof(Int16)); Int16[, ,] newdata = new Int16[datas.GetLength(0), datas.GetLength(1), datas.GetLength(2)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { for (Int32 z = 0; z < datas.GetLength(2); z++) { newdata[x, y, z] = C3DBitConverter.ToInt16(processorType, datas[x, y, z]); } } } this.InternalSetData <Int16[, , ]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[, , ][] datas = this.Get3DByteArray(sizeof(Single)); Single[, ,] newdata = new Single[datas.GetLength(0), datas.GetLength(1), datas.GetLength(2)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { for (Int32 z = 0; z < datas.GetLength(2); z++) { newdata[x, y, z] = C3DBitConverter.ToSingle(processorType, datas[x, y, z]); } } } this.InternalSetData <Single[, , ]>(newdata); } return; } #endregion }
/// <summary> /// 设置参数数据 /// </summary> /// <typeparam name="T">参数类型</typeparam> /// <param name="data">参数数据</param> /// <exception cref="C3DException">未知数据类型</exception> internal void InternalSetData <T>(T data) { #region Basic Type if (typeof(T) == typeof(Char)) { this.SetDimension(); this._parameterType = C3DParameterType.Char; this._parameterData = new Byte[1] { Convert.ToByte(data) }; return; } if (typeof(T) == typeof(Byte)) { this.SetDimension(); this._parameterType = C3DParameterType.Byte; this._parameterData = new Byte[1] { Convert.ToByte(data) }; return; } if (typeof(T) == typeof(Int16)) { this.SetDimension(); this._parameterType = C3DParameterType.Int16; this._parameterData = C3DBitConverter.GetBytes((Int16)(Object)data); return; } if (typeof(T) == typeof(UInt16)) { this.SetDimension(); this._parameterType = C3DParameterType.Int16; this._parameterData = C3DBitConverter.GetBytes((Int16)(UInt16)(Object)data); return; } if (typeof(T) == typeof(Single)) { this.SetDimension(); this._parameterType = C3DParameterType.Single; this._parameterData = C3DBitConverter.GetBytes((Single)(Object)data); return; } if (typeof(T) == typeof(String)) { String s = data as String; this.SetDimension((Byte)s.Length); this._parameterType = C3DParameterType.Char; this._parameterData = this.WrtieStringToBytes(s); return; } #endregion #region 1D-Array if (typeof(T) == typeof(Char[])) { this.Set1DArray <Char>(data as Char[], C3DParameterType.Char); return; } if (typeof(T) == typeof(Byte[])) { this.Set1DArray <Byte>(data as Byte[], C3DParameterType.Byte); return; } if (typeof(T) == typeof(Int16[])) { this.Set1DArray <Int16>(data as Int16[], C3DParameterType.Int16); return; } if (typeof(T) == typeof(UInt16[])) { this.Set1DArray <UInt16>(data as UInt16[], C3DParameterType.Int16); return; } if (typeof(T) == typeof(Single[])) { this.Set1DArray <Single>(data as Single[], C3DParameterType.Single); return; } if (typeof(T) == typeof(String[])) { this.Set1DStringArray(data as String[]); return; } #endregion #region 2D-Array if (typeof(T) == typeof(Char[, ])) { this.Set2DArray <Char>(data as Char[, ], C3DParameterType.Char); return; } if (typeof(T) == typeof(Byte[, ])) { this.Set2DArray <Byte>(data as Byte[, ], C3DParameterType.Byte); return; } if (typeof(T) == typeof(Int16[, ])) { this.Set2DArray <Int16>(data as Int16[, ], C3DParameterType.Int16); return; } if (typeof(T) == typeof(UInt16[, ])) { this.Set2DArray <UInt16>(data as UInt16[, ], C3DParameterType.Int16); return; } if (typeof(T) == typeof(Single[, ])) { this.Set2DArray <Single>(data as Single[, ], C3DParameterType.Single); return; } #endregion #region 3D-Array if (typeof(T) == typeof(Char[, , ])) { this.Set3DArray <Char>(data as Char[, , ], C3DParameterType.Char); return; } if (typeof(T) == typeof(Byte[, , ])) { this.Set3DArray <Byte>(data as Byte[, , ], C3DParameterType.Byte); return; } if (typeof(T) == typeof(Int16[, , ])) { this.Set3DArray <Int16>(data as Int16[, , ], C3DParameterType.Int16); return; } if (typeof(T) == typeof(UInt16[, , ])) { this.Set3DArray <UInt16>(data as UInt16[, , ], C3DParameterType.Int16); return; } if (typeof(T) == typeof(Single[, , ])) { this.Set3DArray <Single>(data as Single[, , ], C3DParameterType.Single); return; } #endregion throw new C3DException("Parameter type is unknown."); }
/// <summary> /// 获取参数数据大小 /// </summary> /// <param name="type">参数数据类型</param> /// <returns>参数数据大小</returns> private Int32 GetParameterDataSize(C3DParameterType type) { return(Math.Abs((SByte)type)); }