Exemplo n.º 1
0
        public static DwgObjectCache Parse(BitReader reader, DwgVersionId version)
        {
            var objectCache  = new DwgObjectCache(version);
            var lastHandle   = 0;
            var lastLocation = 0;

            reader.StartCrcCheck();
            var sectionSize = reader.ReadShortBigEndian();

            while (sectionSize != 2)
            {
                var sectionStart = reader.Offset;
                var sectionEnd   = sectionStart + sectionSize - 2;
                while (reader.Offset < sectionEnd)
                {
                    // read data
                    var handleOffset   = reader.Read_MC(allowNegation: false);
                    var locationOffset = reader.Read_MC();
                    var handle         = lastHandle + handleOffset;
                    var location       = lastLocation + locationOffset;
                    objectCache._handleToOffset.Add(handle, location);
                    lastHandle   = handle;
                    lastLocation = location;
                }

                reader.ValidateCrc(initialValue: DwgHeaderVariables.InitialCrcValue, readCrcAsMsb: true);
                reader.StartCrcCheck();
                sectionSize = reader.ReadShortBigEndian();
            }

            reader.ValidateCrc(initialValue: DwgHeaderVariables.InitialCrcValue, readCrcAsMsb: true);
            return(objectCache);
        }
Exemplo n.º 2
0
 internal DwgFileHeader(DwgVersionId version, int maintenenceVersion, int imagePointer, short codePage)
 {
     Version            = version;
     MaintenenceVersion = maintenenceVersion;
     ImagePointer       = imagePointer;
     CodePage           = codePage;
 }
Exemplo n.º 3
0
        public static string VersionString(this DwgVersionId id)
        {
            switch (id)
            {
            case DwgVersionId.R13:
                return(R13String);

            case DwgVersionId.R14:
                return(R14String);

            default:
                throw new NotSupportedException($"Unrecognized version {id}");
            }
        }
Exemplo n.º 4
0
        public static DwgFileVersion ToFileVersion(this DwgVersionId version)
        {
            switch (version)
            {
            case DwgVersionId.R13:
                return(DwgFileVersion.R13);

            case DwgVersionId.R14:
                return(DwgFileVersion.R14);

            default:
                return(DwgFileVersion.R14);
            }
        }
Exemplo n.º 5
0
        internal static IList <DwgClassDefinition> Parse(BitReader reader, DwgVersionId version)
        {
            reader.ValidateSentinel(StartSentinel);
            reader.StartCrcCheck();
            var sectionSize     = reader.Read_RL();
            var dataStartOffset = reader.Offset;
            var dataEndOffset   = dataStartOffset + sectionSize;
            var classes         = new List <DwgClassDefinition>();

            while (reader.Offset < dataEndOffset - 1) // may be in the middle of a byte
            {
                classes.Add(DwgClassDefinition.Parse(reader));
            }

            reader.ValidateCrc(initialValue: DwgHeaderVariables.InitialCrcValue);
            reader.ValidateSentinel(EndSentinel);
            return(classes);
        }
Exemplo n.º 6
0
        internal static DwgHeaderVariables Parse(BitReader reader, DwgVersionId version)
        {
            var header = new DwgHeaderVariables();

            reader.ValidateSentinel(StartSentinel);
            reader.StartCrcCheck();
            var size        = reader.Read_RL();
            var startOffset = reader.Offset;

            header.ReadVariables(reader, version);
            reader.AlignToByte();
            var unreadByteCount = Math.Max(startOffset + size - reader.Offset, 0);
            var unreadBytes     = reader.ReadBytes(unreadByteCount);

            reader.ValidateCrc(initialValue: InitialCrcValue);
            reader.ValidateSentinel(EndSentinel);
            return(header);
        }
Exemplo n.º 7
0
        internal void Write(BitWriter writer, DwgVersionId version)
        {
            writer.AlignByte();
            using (var ms = new MemoryStream())
            {
                // write the variables to memory
                var variableWriter = new BitWriter(ms);
                WriteVariables(variableWriter, version);
                var variableBytes = variableWriter.AsBytes();

                // now write it all out
                writer.WriteBytes(StartSentinel);
                writer.StartCrcCalculation(initialValue: InitialCrcValue);
                writer.Write_RL(variableBytes.Length);
                writer.WriteBytes(variableBytes);
                writer.WriteCrc();
                writer.WriteBytes(EndSentinel);
            }
        }
Exemplo n.º 8
0
 internal override void ParseSpecific(BitReader reader, DwgVersionId version)
 {
     Name            = reader.Read_T();
     Description     = reader.Read_T();
     Flags           = reader.Read_BS();
     FillColor       = DwgColor.FromRawValue(reader.Read_BS());
     StartAngle      = reader.Read_BD();
     EndAngle        = reader.Read_BD();
     _lineStyleCount = reader.Read_RC();
     for (int i = 0; i < _lineStyleCount; i++)
     {
         _lineStyleOffsets.Add(reader.Read_BD());
         _lineStyleColors.Add(reader.Read_BS());
         _lineStyleLineTypeIndicies.Add(reader.Read_BS());
     }
     _parentHandle = reader.Read_H();
     for (int i = 0; i < _reactorCount; i++)
     {
         _reactorHandles.Add(reader.Read_H());
     }
     _xDictionaryObjectHandle = reader.Read_H();
 }
Exemplo n.º 9
0
 internal override void ParseSpecific(BitReader reader, DwgVersionId version)
 {
     Name               = reader.Read_T();
     _64flag            = reader.Read_B();
     _xrefIndex         = reader.Read_BS();
     _isDependentOnXRef = reader.Read_B();
     Description        = reader.Read_T();
     PatternLength      = reader.Read_BD();
     _alignment         = (char)reader.Read_RC();
     _numDashes         = reader.Read_RC();
     for (int i = 0; i < _numDashes; i++)
     {
         var dashInfo = DwgLineTypeDashInfo.Parse(reader);
         DashInfos.Add(dashInfo);
     }
     LineTypeControlHandle = reader.Read_H();
     for (int i = 0; i < _reactorCount; i++)
     {
         _reactorHandles.Add(reader.Read_H());
     }
     _xDictionaryObjectHandle = reader.Read_H();
     _nullHandle = reader.Read_H();
 }
Exemplo n.º 10
0
 private DwgObjectCache(DwgVersionId version)
 {
     _version = version;
 }
Exemplo n.º 11
0
        internal static DwgObject Parse(BitReader reader, DwgObjectCache objectCache, DwgVersionId version)
        {
            reader.StartCrcCheck();
            var size     = reader.Read_MS();
            var crcStart = reader.Offset + size;
            var typeCode = reader.Read_BS();

            if (!Enum.IsDefined(typeof(DwgObjectType), typeCode))
            {
                // unsupported
                return(null);
            }

            var type = (DwgObjectType)typeCode;
            var obj  = CreateObject(type);

            obj.ReadCommonDataStart(reader);
            obj.ParseSpecific(reader, version);
            obj.ReadCommonDataEnd(reader);

            // ensure there's no extra data
            reader.AlignToByte();
            reader.SkipBytes(Math.Max(0, crcStart - reader.Offset));

            reader.ValidateCrc(initialValue: DwgHeaderVariables.InitialCrcValue);
            obj.ValidateCommonValues();
            obj.OnAfterObjectRead(reader, objectCache);
            return(obj);
        }
Exemplo n.º 12
0
        internal void Write(BitWriter writer, DwgObjectMap objectMap, HashSet <int> writtenHandles, int pointerOffset, DwgVersionId version)
        {
            if (!writtenHandles.Add(Handle.HandleOrOffset))
            {
                // already been written
                return;
            }

            PrepareCommonValues();
            OnBeforeObjectWrite();
            SetCommonValues();
            objectMap.SetOffset(Handle.HandleOrOffset, writer.Position);

            // write object to memory so the size can be computed
            using (var ms = new MemoryStream())
            {
                var tempWriter = new BitWriter(ms);
                tempWriter.Write_BS((short)Type);
                WriteCommonDataStart(tempWriter, objectMap, pointerOffset);
                WriteSpecific(tempWriter, objectMap, pointerOffset, version);
                WriteCommonDataEnd(tempWriter, objectMap, pointerOffset);
                var tempBytes = tempWriter.AsBytes();

                // now output everything
                writer.StartCrcCalculation(initialValue: DwgHeaderVariables.InitialCrcValue);
                writer.Write_MS(tempBytes.Length);
                writer.WriteBytes(tempBytes);
                writer.WriteCrc();
            }

            foreach (var child in ChildItems)
            {
                child.Write(writer, objectMap, writtenHandles, pointerOffset, version);
            }
        }
Exemplo n.º 13
0
 internal abstract void WriteSpecific(BitWriter writer, DwgObjectMap objectMap, int pointerOffset, DwgVersionId version);
Exemplo n.º 14
0
 internal abstract void ParseSpecific(BitReader reader, DwgVersionId version);
Exemplo n.º 15
0
 internal override void WriteSpecific(BitWriter writer, DwgObjectMap objectMap, int pointerOffset, DwgVersionId version)
 {
     writer.Write_T(Name);
     writer.Write_T(Description);
     writer.Write_BS(Flags);
     writer.Write_BS(FillColor.RawValue);
     writer.Write_BD(StartAngle);
     writer.Write_BD(EndAngle);
     writer.Write_RC(_lineStyleCount);
     for (int i = 0; i < _lineStyleCount; i++)
     {
         writer.Write_BD(_lineStyleOffsets[i]);
         writer.Write_BS(_lineStyleColors[i]);
         writer.Write_BS(_lineStyleLineTypeIndicies[i]);
     }
     writer.Write_H(_parentHandle);
     for (int i = 0; i < _reactorCount; i++)
     {
         writer.Write_H(_reactorHandles[i]);
     }
     writer.Write_H(_xDictionaryObjectHandle);
 }
Exemplo n.º 16
0
 internal override void WriteSpecific(BitWriter writer, DwgObjectMap objectMap, int pointerOffset, DwgVersionId version)
 {
     writer.Write_T(Name);
     writer.Write_B(_64flag);
     writer.Write_BS(_xrefIndex);
     writer.Write_B(_isDependentOnXRef);
     writer.Write_T(Description);
     writer.Write_BD(PatternLength);
     writer.Write_RC((byte)_alignment);
     writer.Write_RC((byte)DashInfos.Count);
     foreach (var dashInfo in DashInfos)
     {
         dashInfo.Write(writer);
     }
     writer.Write_H(LineTypeControlHandle);
     foreach (var reactorHandle in _reactorHandles)
     {
         writer.Write_H(reactorHandle);
     }
     writer.Write_H(_xDictionaryObjectHandle);
     writer.Write_H(_nullHandle);
 }