internal override void BeforeWrite() { _bitmapPreviewData.Clear(); var str = DxfCommonConverters.HexBytes(BitmapData); _bitmapPreviewData.AddRange(DxfCommonConverters.SplitIntoLines(str)); }
internal override void AfterRead() { var hex = string.Join(string.Empty, _bitmapPreviewData.ToArray()); _bitmapPreviewData.Clear(); // don't keep this around BitmapData = DxfCommonConverters.HexBytes(hex); }
public static IEnumerable <string> StringsFromByteArray(byte[] bytes) { // write lines in 128-byte chunks (expands to 256 hex bytes) var hex = DxfCommonConverters.HexBytes(bytes); var lines = DxfCommonConverters.SplitIntoLines(hex, 256); return(lines); }
internal override void BeforeWrite() { _bitmapPreviewData.Clear(); var str = DxfCommonConverters.HexBytes(BitmapData); foreach (var line in DxfCommonConverters.SplitIntoLines(str)) { _bitmapPreviewData.Add(line); } }
internal static DxfXDataItem FromBuffer(DxfCodePairBufferReader buffer) { var pair = buffer.Peek(); buffer.Advance(); switch ((DxfXDataType)pair.Code) { case DxfXDataType.String: return(new DxfXDataString(pair.StringValue)); case DxfXDataType.ControlString: return(DxfXDataItemList.ListFromBuffer(buffer)); case DxfXDataType.Layer: return(new DxfXDataLayerName(pair.StringValue)); case DxfXDataType.BinaryData: return(new DxfXDataBinaryData(DxfCommonConverters.HexBytes(pair.StringValue))); case DxfXDataType.Handle: return(new DxfXDataHandle(DxfCommonConverters.UIntHandle(pair.StringValue))); case DxfXDataType.RealTriple: return(new DxfXData3Reals(ReadPoint(pair, buffer, pair.Code))); case DxfXDataType.WorldSpacePosition: return(new DxfXDataWorldSpacePosition(ReadPoint(pair, buffer, pair.Code))); case DxfXDataType.WorldSpaceDisplacement: return(new DxfXDataWorldSpaceDisplacement(ReadPoint(pair, buffer, pair.Code))); case DxfXDataType.WorldDirection: return(new DxfXDataWorldDirection(ReadPoint(pair, buffer, pair.Code))); case DxfXDataType.Real: return(new DxfXDataReal(pair.DoubleValue)); case DxfXDataType.Distance: return(new DxfXDataDistance(pair.DoubleValue)); case DxfXDataType.ScaleFactor: return(new DxfXDataScaleFactor(pair.DoubleValue)); case DxfXDataType.Integer: return(new DxfXDataInteger(pair.ShortValue)); case DxfXDataType.Long: return(new DxfXDataLong(pair.IntegerValue)); default: return(null); // unexpected XDATA code pair } }
private void WriteBinary(byte[] value) { if (textWriter != null) { WriteLine(DxfCommonConverters.HexBytes(value)); } else if (binWriter != null) { binWriter.Write((byte)value.Length); binWriter.Write(value); } }
internal void AddValuePairs(List <DxfCodePair> pairs, DxfAcadVersion version, bool outputHandles) { var code = (int)Type; switch (this) { case DxfXDataString s: pairs.Add(new DxfCodePair(code, s.Value)); break; case DxfXDataItemList l: AddListValuePairs(l.Items, pairs, version, outputHandles); break; case DxfXDataNamedList n: pairs.Add(new DxfCodePair(code, n.Name)); AddListValuePairs(n.Items, pairs, version, outputHandles); break; case DxfXDataLayerName l: pairs.Add(new DxfCodePair(code, l.Value)); break; case DxfXDataBinaryData b: pairs.Add(new DxfCodePair(code, DxfCommonConverters.HexBytes(b.Value))); break; case DxfXDataHandle h: pairs.Add(new DxfCodePair(code, DxfCommonConverters.UIntHandle(h.Value))); break; case DxfXData3Reals r: pairs.Add(new DxfCodePair(code, r.Value.X)); pairs.Add(new DxfCodePair(code + 10, r.Value.Y)); pairs.Add(new DxfCodePair(code + 20, r.Value.Z)); break; case DxfXDataWorldSpacePosition w: pairs.Add(new DxfCodePair(code, w.Value.X)); pairs.Add(new DxfCodePair(code + 10, w.Value.Y)); pairs.Add(new DxfCodePair(code + 20, w.Value.Z)); break; case DxfXDataWorldSpaceDisplacement w: pairs.Add(new DxfCodePair(code, w.Value.X)); pairs.Add(new DxfCodePair(code + 10, w.Value.Y)); pairs.Add(new DxfCodePair(code + 20, w.Value.Z)); break; case DxfXDataWorldDirection w: pairs.Add(new DxfCodePair(code, w.Value.X)); pairs.Add(new DxfCodePair(code + 10, w.Value.Y)); pairs.Add(new DxfCodePair(code + 20, w.Value.Z)); break; case DxfXDataReal r: pairs.Add(new DxfCodePair(code, r.Value)); break; case DxfXDataDistance d: pairs.Add(new DxfCodePair(code, d.Value)); break; case DxfXDataScaleFactor s: pairs.Add(new DxfCodePair(code, s.Value)); break; case DxfXDataInteger i: pairs.Add(new DxfCodePair(code, i.Value)); break; case DxfXDataLong l: pairs.Add(new DxfCodePair(code, l.Value)); break; default: throw new InvalidOperationException("Unexpected XDATA item " + Type); } }
public DxfCodePair GetCodePair() { if (_lineEnumerator.MoveNext()) { var codeLine = _lineEnumerator.Current; var codeLineNumber = _lineNumber; int code; if (string.IsNullOrEmpty(codeLine)) { // a blank line when expecting a code means we can't parse any further return(null); } else if (int.TryParse(codeLine, NumberStyles.Integer, CultureInfo.InvariantCulture, out code)) { if (_lineEnumerator.MoveNext()) { DxfCodePair pair = null; var valueLine = _lineEnumerator.Current; var expectedType = DxfCodePair.ExpectedType(code); if (expectedType == typeof(short)) { pair = GetCodePair <short>(code, valueLine, TryParseShortValue, (c, v) => new DxfCodePair(c, v), minValue: short.MinValue, maxValue: short.MaxValue); } else if (expectedType == typeof(double)) { pair = GetCodePair <double>(code, valueLine, TryParseDoubleValue, (c, v) => new DxfCodePair(c, v)); } else if (expectedType == typeof(int)) { pair = GetCodePair <int>(code, valueLine, TryParseIntValue, (c, v) => new DxfCodePair(c, v), minValue: int.MinValue, maxValue: int.MaxValue); } else if (expectedType == typeof(long)) { pair = GetCodePair <long>(code, valueLine, TryParseLongValue, (c, v) => new DxfCodePair(c, v), minValue: long.MinValue, maxValue: long.MaxValue); } else if (expectedType == typeof(string)) { var value = valueLine.Trim(); if (_encoding == null) { // read as ASCII, transform UTF codes value = DxfReader.TransformUnicodeCharacters(value); } pair = new DxfCodePair(code, DxfReader.TransformControlCharacters(value)); } else if (expectedType == typeof(bool)) { // this should really parse as a short, but it could be anything double result; if (double.TryParse(valueLine, NumberStyles.Float, CultureInfo.InvariantCulture, out result)) { if (result < short.MinValue) { result = short.MinValue; } if (result > short.MaxValue) { result = short.MaxValue; } pair = DxfCodePair.IsPotentialShortAsBool(code) ? new DxfCodePair(code, (short)result) : new DxfCodePair(code, result != 0.0); } else { throw new DxfReadException($"Unsupported value '{valueLine}' for code '{code}'", _lineNumber); } } else if (expectedType == typeof(byte[])) { var data = DxfCommonConverters.HexBytes(valueLine); pair = new DxfCodePair(code, data); } else { throw new DxfReadException("Unsupported code " + code, codeLineNumber); } if (pair != null) { pair.Offset = codeLineNumber; } return(pair); } else { throw new DxfReadException("Expected value", _lineNumber); } } else { if (codeLineNumber == 1) { // parsing the very first code pair failed throw new DxfReadException($"Not a valid DXF file header: `{codeLine}`.", _lineNumber); } else { throw new DxfReadException("Unexpected code value", _lineNumber); } } } else { return(null); } }
public static byte[] ByteArrayFromStrings(IEnumerable <string> lines) { return(DxfCommonConverters.HexBytes(string.Join(string.Empty, lines.ToArray()))); }
internal void AddValuePairs(List <DxfCodePair> pairs, DxfAcadVersion version, bool outputHandles) { var code = (int)Type; switch (Type) { case DxfXDataType.String: pairs.Add(new DxfCodePair(code, ((DxfXDataString)this).Value)); break; case DxfXDataType.ControlString: pairs.Add(new DxfCodePair(code, "{")); foreach (var subItem in ((DxfXDataControlGroup)this).Items) { subItem.AddValuePairs(pairs, version, outputHandles); } pairs.Add(new DxfCodePair(code, "}")); break; case DxfXDataType.Layer: pairs.Add(new DxfCodePair(code, ((DxfXDataLayerName)this).Value)); break; case DxfXDataType.BinaryData: pairs.Add(new DxfCodePair(code, DxfCommonConverters.HexBytes(((DxfXDataBinaryData)this).Value))); break; case DxfXDataType.Handle: pairs.Add(new DxfCodePair(code, DxfCommonConverters.UIntHandle(((DxfXDataHandle)this).Value))); break; case DxfXDataType.RealTriple: { var point = ((DxfXData3Reals)this).Value; pairs.Add(new DxfCodePair(code, point.X)); pairs.Add(new DxfCodePair(code + 10, point.Y)); pairs.Add(new DxfCodePair(code + 20, point.Z)); break; } case DxfXDataType.WorldSpacePosition: { var point = ((DxfXDataWorldSpacePosition)this).Value; pairs.Add(new DxfCodePair(code, point.X)); pairs.Add(new DxfCodePair(code + 10, point.Y)); pairs.Add(new DxfCodePair(code + 20, point.Z)); break; } case DxfXDataType.WorldSpaceDisplacement: { var point = ((DxfXDataWorldSpaceDisplacement)this).Value; pairs.Add(new DxfCodePair(code, point.X)); pairs.Add(new DxfCodePair(code + 10, point.Y)); pairs.Add(new DxfCodePair(code + 20, point.Z)); break; } case DxfXDataType.WorldDirection: { var point = ((DxfXDataWorldDirection)this).Value; pairs.Add(new DxfCodePair(code, point.X)); pairs.Add(new DxfCodePair(code + 10, point.Y)); pairs.Add(new DxfCodePair(code + 20, point.Z)); break; } case DxfXDataType.Real: pairs.Add(new DxfCodePair(code, ((DxfXDataReal)this).Value)); break; case DxfXDataType.Distance: pairs.Add(new DxfCodePair(code, ((DxfXDataDistance)this).Value)); break; case DxfXDataType.ScaleFactor: pairs.Add(new DxfCodePair(code, ((DxfXDataScaleFactor)this).Value)); break; case DxfXDataType.Integer: pairs.Add(new DxfCodePair(code, ((DxfXDataInteger)this).Value)); break; case DxfXDataType.Long: pairs.Add(new DxfCodePair(code, ((DxfXDataLong)this).Value)); break; default: throw new InvalidOperationException("Unexpected XDATA item " + Type); } }