internal override void AfterRead()
        {
            var hex = string.Join(string.Empty, _bitmapPreviewData.ToArray());

            _bitmapPreviewData.Clear(); // don't keep this around
            BitmapData = DxfCommonConverters.HexBytes(hex);
        }
Exemple #2
0
        internal IEnumerable <DxfCodePair> GetValuePairs(DxfAcadVersion version, bool outputHandles)
        {
            var list = new List <DxfCodePair>();

            if (version >= DxfAcadVersion.R14)
            {
                list.Add(new DxfCodePair(0, ClassText));
                list.Add(new DxfCodePair(1, ClassDxfRecordName));
                list.Add(new DxfCodePair(2, CppClassName));
                list.Add(new DxfCodePair(3, ApplicationName));
                list.Add(new DxfCodePair(90, ProxyCapabilities.Value));
                if (version >= DxfAcadVersion.R2004)
                {
                    list.Add(new DxfCodePair(91, InstanceCount));
                }
            }
            else
            {
                // version <= DxfAcadVersion.R13
                list.Add(new DxfCodePair(0, ClassDxfRecordName));
                list.Add(new DxfCodePair(1, CppClassName));
                list.Add(new DxfCodePair(2, ApplicationName));
                list.Add(new DxfCodePair(90, ClassVersionNumber));
            }

            list.Add(new DxfCodePair(280, DxfCommonConverters.BoolShort(!WasClassLoadedWithFile)));
            list.Add(new DxfCodePair(281, DxfCommonConverters.BoolShort(IsEntity)));

            return(list);
        }
        internal override void BeforeWrite()
        {
            _bitmapPreviewData.Clear();
            var str = DxfCommonConverters.HexBytes(BitmapData);

            _bitmapPreviewData.AddRange(DxfCommonConverters.SplitIntoLines(str));
        }
Exemple #4
0
        internal void AddValuePairs(List <DxfCodePair> pairs)
        {
            pairs.Add(new DxfCodePair(49, DashDotSpaceLength));
            pairs.Add(new DxfCodePair(74, (short)ComplexFlags));
            if (ComplexFlags != 0)
            {
                var value = IsEmbeddedElementAString ? 0 : ShapeNumber;
                pairs.Add(new DxfCodePair(75, (short)value));
                if (StylePointer.Handle.Value != 0)
                {
                    pairs.Add(new DxfCodePair(340, DxfCommonConverters.HandleString(StylePointer.Handle)));
                }
            }

            pairs.AddRange(ScaleValues.Select(s => new DxfCodePair(46, s)));
            if (IsEmbeddedElementAShape || IsEmbeddedElementAString)
            {
                pairs.Add(new DxfCodePair(50, RotationAngle));
            }

            foreach (var offset in Offsets)
            {
                pairs.Add(new DxfCodePair(44, offset.X));
                pairs.Add(new DxfCodePair(45, offset.Y));
            }

            if (IsEmbeddedElementAString)
            {
                pairs.Add(new DxfCodePair(9, TextString));
            }
        }
Exemple #5
0
        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);
            }
        }
Exemple #7
0
 private void WriteBinary(byte[] value)
 {
     if (textWriter != null)
     {
         WriteLine(DxfCommonConverters.HexBytes(value));
     }
     else if (binWriter != null)
     {
         binWriter.Write((byte)value.Length);
         binWriter.Write(value);
     }
 }
Exemple #8
0
        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
            }
        }
        internal void TrySetPair(DxfCodePair pair)
        {
            switch (pair.Code)
            {
            case 2:
                Name = pair.StringValue;
                break;

            case 5:
                Handle = DxfCommonConverters.UIntHandle(pair.StringValue);
                break;

            case 330:
                OwnerHandle = DxfCommonConverters.UIntHandle(pair.StringValue);
                break;
            }
        }
Exemple #10
0
 private static void SetOwnerPair(IDxfCodePairOrGroup codePairOrGroup, IDxfItemInternal owner)
 {
     if (codePairOrGroup.IsCodePair)
     {
         var pair = (DxfCodePair)codePairOrGroup;
         if (pair.Code == 330)
         {
             pair.Value = DxfCommonConverters.UIntHandle(owner.Handle);
         }
     }
     else
     {
         foreach (var item in ((DxfCodePairGroup)codePairOrGroup).Items)
         {
             SetOwnerPair(item, owner);
         }
     }
 }
        internal void AddCommonValuePairs(List <DxfCodePair> pairs, DxfAcadVersion version, bool outputHandles)
        {
            pairs.Add(new DxfCodePair(0, DxfTable.TableTypeToName(TableType)));
            if (outputHandles)
            {
                int code = TableType == DxfTableType.DimStyle ? 105 : 5;
                pairs.Add(new DxfCodePair(code, DxfCommonConverters.UIntHandle(Handle)));
            }

            foreach (var group in ExtensionDataGroups)
            {
                group.AddValuePairs(pairs, version, outputHandles);
            }

            if (version >= DxfAcadVersion.R2000)
            {
                pairs.Add(new DxfCodePair(330, DxfCommonConverters.UIntHandle(OwnerHandle)));
            }

            pairs.Add(new DxfCodePair(100, "AcDbSymbolTableRecord"));
        }
Exemple #12
0
 private static bool BoolShort(short?s)
 {
     return(DxfCommonConverters.BoolShort(s ?? 0));
 }
 protected static int EnsurePositiveOrDefault(int value, int defaultValue)
 {
     return(DxfCommonConverters.EnsurePositiveOrDefault(value, defaultValue));
 }
 protected static double EnsurePositiveOrDefault(double value, double defaultValue)
 {
     return(DxfCommonConverters.EnsurePositiveOrDefault(value, defaultValue));
 }
 protected static string UIntHandle(uint u)
 {
     return(DxfCommonConverters.UIntHandle(u));
 }
Exemple #16
0
 private static Guid GuidString(string s)
 {
     return(DxfCommonConverters.GuidString(s));
 }
Exemple #17
0
        internal static DxfClass FromBuffer(DxfCodePairBufferReader buffer, DxfAcadVersion version)
        {
            var cls = new DxfClass();

            // version R13 has varying values for the leading 0 code pair
            var pair = buffer.Peek();

            Debug.Assert(pair.Code == 0);
            if (version <= DxfAcadVersion.R13)
            {
                cls.ClassDxfRecordName = pair.StringValue;
            }
            else
            {
                // swallow (0, CLASS)
                Debug.Assert(pair.StringValue == ClassText);
            }

            buffer.Advance();
            while (buffer.ItemsRemain)
            {
                pair = buffer.Peek();
                if (pair.Code == 0)
                {
                    break;
                }

                buffer.Advance();
                switch (pair.Code)
                {
                case 1:
                    if (version <= DxfAcadVersion.R13)
                    {
                        cls.CppClassName = pair.StringValue;
                    }
                    else
                    {
                        cls.ClassDxfRecordName = pair.StringValue;
                    }
                    break;

                case 2:
                    if (version <= DxfAcadVersion.R13)
                    {
                        cls.ApplicationName = pair.StringValue;
                    }
                    else
                    {
                        cls.CppClassName = pair.StringValue;
                    }
                    break;

                case 3:
                    if (version >= DxfAcadVersion.R14)
                    {
                        cls.ApplicationName = pair.StringValue;
                    }
                    break;

                case 90:
                    if (version <= DxfAcadVersion.R13)
                    {
                        cls.ClassVersionNumber = pair.IntegerValue;
                    }
                    else
                    {
                        cls.ProxyCapabilities = new DxfProxyCapabilities(pair.IntegerValue);
                    }
                    break;

                case 91:
                    cls.InstanceCount = pair.IntegerValue;
                    break;

                case 280:
                    cls.WasClassLoadedWithFile = !DxfCommonConverters.BoolShort(pair.ShortValue);
                    break;

                case 281:
                    cls.IsEntity = DxfCommonConverters.BoolShort(pair.ShortValue);
                    break;
                }
            }

            return(cls);
        }
Exemple #18
0
 protected static Func <int, int> EnsurePositiveOrDefault(int defaultValue)
 {
     return(DxfCommonConverters.EnsurePositiveOrDefault(defaultValue));
 }
Exemple #19
0
 protected static Func <double, double> EnsurePositiveOrDefault(double defaultValue)
 {
     return(DxfCommonConverters.EnsurePositiveOrDefault(defaultValue));
 }
Exemple #20
0
 private static string UIntHandle(uint u)
 {
     return(DxfCommonConverters.UIntHandle(u));
 }
Exemple #21
0
 private static uint UIntHandle(string s)
 {
     return(DxfCommonConverters.UIntHandle(s));
 }
Exemple #22
0
 private static short StringShort(string s)
 {
     return(DxfCommonConverters.StringShort(s));
 }
Exemple #23
0
 private static short BoolShort(bool?b)
 {
     return(DxfCommonConverters.BoolShort(b ?? false));
 }
 protected static short BoolShort(bool b)
 {
     return(DxfCommonConverters.BoolShort(b));
 }
Exemple #25
0
 private static string DefaultIfNullOrEmpty(string value, string defaultValue)
 {
     return(DxfCommonConverters.DefaultIfNullOrEmpty(value, defaultValue));
 }
 protected static uint UIntHandle(string s)
 {
     return(DxfCommonConverters.UIntHandle(s));
 }
Exemple #27
0
        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);
            }
        }
Exemple #28
0
 private static string GuidString(Guid g)
 {
     return(DxfCommonConverters.GuidString(g));
 }
Exemple #29
0
        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);
            }
        }
 protected static bool BoolShort(short s)
 {
     return(DxfCommonConverters.BoolShort(s));
 }