private void WriteValue(int code, object value) { var type = DxfCodePair.ExpectedType(code); if (type == typeof(string)) { WriteString((string)value); } else if (type == typeof(double)) { WriteDouble((double)value); } else if (type == typeof(short)) { WriteShort((short)value); } else if (type == typeof(int)) { WriteInt((int)value); } else if (type == typeof(long)) { WriteLong((long)value); } else if (type == typeof(bool)) { if (DxfCodePair.IsPotentialShortAsBool(code) && value.GetType() == typeof(short)) { WriteShort((short)value); } else { WriteBool((bool)value); } } else if (type == typeof(byte[])) { WriteBinary((byte[])value); } else { throw new InvalidOperationException("No writer available"); } }
public bool TryGetStyleFromXDataDifference(DxfXDataApplicationItemCollection xdataItemCollection, out DxfDimStyle style) { // style data is encoded as // 1001 DSTYLE // 1002 { // ... style overrides // 1002 } style = default(DxfDimStyle); if (xdataItemCollection == null) { return(false); } for (int i = 0; i < xdataItemCollection.Count - 1; i++) { if (xdataItemCollection[i] is DxfXDataString xdataString && xdataString.Value == XDataStyleName && xdataItemCollection[i + 1] is DxfXDataItemList itemList) { if (itemList.Items.Count % 2 != 0) { // must be an even number return(false); } var codePairs = new List <DxfCodePair>(); for (int j = 0; j < itemList.Items.Count; j += 2) { if (!(itemList.Items[j] is DxfXDataInteger codeItem)) { // must alternate between int/<data> return(false); } DxfCodePair pair; var valueItem = itemList.Items[j + 1]; var code = codeItem.Value; switch (DxfCodePair.ExpectedType(code).Name) { case nameof(Boolean): pair = new DxfCodePair(code, true); break; case nameof(Double) when valueItem is DxfXDataDistance dist: pair = new DxfCodePair(code, dist.Value); break; case nameof(Double) when valueItem is DxfXDataReal real: pair = new DxfCodePair(code, real.Value); break; case nameof(Double) when valueItem is DxfXDataScaleFactor scale: pair = new DxfCodePair(code, scale.Value); break; case nameof(Int16) when valueItem is DxfXDataInteger i32: pair = new DxfCodePair(code, i32.Value); break; case nameof(Int32) when valueItem is DxfXDataLong i32: pair = new DxfCodePair(code, i32.Value); break; case nameof(Int64) when valueItem is DxfXDataLong i32: pair = new DxfCodePair(code, i32.Value); break; case nameof(String) when valueItem is DxfXDataString str: pair = new DxfCodePair(code, str.Value); break; default: // unexpected code pair type return(false); } codePairs.Add(pair); } if (codePairs.Count == 0) { // no difference to apply return(false); } // if we made it this far, there is a difference that should be applied style = Clone(); foreach (var pair in codePairs) { style.ApplyCodePair(pair); } return(true); } } return(false); }
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 bool TryGetStyleFromXDataDifference(DxfXData xdata, out DxfDimStyle style) { style = default(DxfDimStyle); var dimStyleList = xdata?.Items.OfType <DxfXDataNamedList>().FirstOrDefault(l => l.Name == XDataStyleName); if (dimStyleList == null) { // no dim style override return(false); } if (dimStyleList.Items.Count % 2 != 0) { // must be an even number return(false); } var codePairs = new List <DxfCodePair>(); for (int i = 0; i < dimStyleList.Items.Count; i += 2) { if (!(dimStyleList.Items[i] is DxfXDataInteger codeItem)) { // must alternate between int/<data> return(false); } DxfCodePair pair; var valueItem = dimStyleList.Items[i + 1]; var code = codeItem.Value; switch (DxfCodePair.ExpectedType(code).Name) { case nameof(Boolean): pair = new DxfCodePair(code, true); break; case nameof(Double) when valueItem is DxfXDataDistance dist: pair = new DxfCodePair(code, dist.Value); break; case nameof(Double) when valueItem is DxfXDataReal real: pair = new DxfCodePair(code, real.Value); break; case nameof(Double) when valueItem is DxfXDataScaleFactor scale: pair = new DxfCodePair(code, scale.Value); break; case nameof(Int16) when valueItem is DxfXDataInteger i32: pair = new DxfCodePair(code, i32.Value); break; case nameof(Int32) when valueItem is DxfXDataLong i32: pair = new DxfCodePair(code, i32.Value); break; case nameof(Int64) when valueItem is DxfXDataLong i32: pair = new DxfCodePair(code, i32.Value); break; case nameof(String) when valueItem is DxfXDataString str: pair = new DxfCodePair(code, str.Value); break; default: // unexpected code pair type return(false); } codePairs.Add(pair); } if (codePairs.Count == 0) { // no difference to apply return(false); } // if we made it this far, there is a differnce that should be applied style = Clone(); foreach (var pair in codePairs) { style.ApplyCodePair(pair); } return(true); }
private DxfCodePair GetCodePair() { if (_lineEnumerator.MoveNext()) { var codeLine = _lineEnumerator.Current; var codeLineNumber = _lineNumber; int code; 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, _lineEnumerator.Current, short.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v)); } else if (expectedType == typeof(double)) { pair = GetCodePair <double>(code, _lineEnumerator.Current, double.TryParse, NumberStyles.Float, (c, v) => new DxfCodePair(c, v)); } else if (expectedType == typeof(int)) { pair = GetCodePair <int>(code, _lineEnumerator.Current, int.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v)); } else if (expectedType == typeof(long)) { pair = GetCodePair <long>(code, _lineEnumerator.Current, long.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v)); } else if (expectedType == typeof(string)) { pair = new DxfCodePair(code, DxfReader.TransformControlCharacters(_lineEnumerator.Current.Trim())); } else if (expectedType == typeof(bool)) { short result; if (short.TryParse(_lineEnumerator.Current, NumberStyles.Integer, CultureInfo.InvariantCulture, out result)) { pair = DxfCodePair.IsPotentialShortAsBool(code) ? new DxfCodePair(code, result) : new DxfCodePair(code, result != 0); } else { throw new DxfReadException("Unsupported value for code", _lineNumber); } } else { throw new DxfReadException("Unsupported code " + code, codeLineNumber); } if (pair != null) { pair.Offset = codeLineNumber; } return(pair); } else { throw new DxfReadException("Expected value", _lineNumber); } } else { throw new DxfReadException("Unexpected code value", _lineNumber); } } else { return(null); } }
internal DxfCodePair GetCodePair() { var codeOffset = _totalBytesRead + _miniBufferStart; int code; if (!TryReadCode(out code)) { return(null); } var expectedType = DxfCodePair.ExpectedType(code); DxfCodePair pair; if (expectedType == typeof(short)) { short s; if (!TryReadInt16(out s)) { return(null); } pair = new DxfCodePair(code, s); } else if (expectedType == typeof(double)) { double d; if (!TryReadDouble(out d)) { return(null); } pair = new DxfCodePair(code, d); } else if (expectedType == typeof(int)) { int i; if (!TryReadInt32(out i)) { return(null); } pair = new DxfCodePair(code, i); } else if (expectedType == typeof(long)) { long l; if (!TryReadInt64(out l)) { return(null); } pair = new DxfCodePair(code, l); } else if (expectedType == typeof(string)) { var sb = new StringBuilder(); byte b; for (; TryReadByte(out b) && b != 0;) { sb.Append((char)b); } pair = new DxfCodePair(code, sb.ToString()); } else if (expectedType == typeof(bool)) { if (_isPostR13File) { // after R13 bools are encoded as a single byte if (!TryReadByte(out var value)) { return(null); } pair = new DxfCodePair(code, value != 0); } else { if (!TryReadInt16(out var value)) { return(null); } pair = DxfCodePair.IsPotentialShortAsBool(code) ? new DxfCodePair(code, value) : new DxfCodePair(code, value != 0); } } else if (expectedType == typeof(byte[])) { if (!TryReadByte(out var length)) { return(null); } if (!TryReadBytes(length, out var data)) { return(null); } pair = new DxfCodePair(code, data); } else { throw new DxfReadException("Unsupported code " + code, codeOffset); } pair.Offset = codeOffset; return(pair); }
private 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, _lineEnumerator.Current, short.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v), minValue: short.MinValue, maxValue: short.MaxValue); } else if (expectedType == typeof(double)) { pair = GetCodePair <double>(code, _lineEnumerator.Current, double.TryParse, NumberStyles.Float, (c, v) => new DxfCodePair(c, v)); } else if (expectedType == typeof(int)) { pair = GetCodePair <int>(code, _lineEnumerator.Current, int.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v), minValue: int.MinValue, maxValue: int.MaxValue); } else if (expectedType == typeof(long)) { pair = GetCodePair <long>(code, _lineEnumerator.Current, long.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v), minValue: long.MinValue, maxValue: long.MaxValue); } else if (expectedType == typeof(string)) { pair = new DxfCodePair(code, DxfReader.TransformControlCharacters(_lineEnumerator.Current.Trim())); } else if (expectedType == typeof(bool)) { // this should really parse as a short, but it could be anything double result; if (double.TryParse(_lineEnumerator.Current, 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 '{_lineEnumerator.Current}' for code '{code}'", _lineNumber); } } else { throw new DxfReadException("Unsupported code " + code, codeLineNumber); } if (pair != null) { pair.Offset = codeLineNumber; } return(pair); } else { throw new DxfReadException("Expected value", _lineNumber); } } else { throw new DxfReadException("Unexpected code value", _lineNumber); } } else { return(null); } }
private DxfCodePair GetCodePair() { var codeOffset = _totalBytesRead + _miniBufferStart; int code; if (!TryReadCode(out code)) { return(null); } var expectedType = DxfCodePair.ExpectedType(code); DxfCodePair pair; if (expectedType == typeof(short)) { short s; if (!TryReadInt16(out s)) { return(null); } pair = new DxfCodePair(code, s); } else if (expectedType == typeof(double)) { double d; if (!TryReadDouble(out d)) { return(null); } pair = new DxfCodePair(code, d); } else if (expectedType == typeof(int)) { int i; if (!TryReadInt32(out i)) { return(null); } pair = new DxfCodePair(code, i); } else if (expectedType == typeof(long)) { long l; if (!TryReadInt64(out l)) { return(null); } pair = new DxfCodePair(code, l); } else if (expectedType == typeof(string)) { var sb = new StringBuilder(); byte b; for (; TryReadByte(out b) && b != 0;) { sb.Append((char)b); } pair = new DxfCodePair(code, DxfReader.TransformControlCharacters(sb.ToString())); } else if (expectedType == typeof(bool)) { var value = _reader.ReadInt16(); pair = DxfCodePair.IsPotentialShortAsBool(code) ? new DxfCodePair(code, value) : new DxfCodePair(code, value != 0); } else { throw new DxfReadException("Unsupported code " + code, codeOffset); } pair.Offset = codeOffset; return(pair); }