Exemplo n.º 1
0
        public void NotAllowDuplicatesTest()
        {
            BlockRecord record = new BlockRecord();

            Line l1 = new Line();

            record.Entities.Add(l1);
            Assert.Throws <ArgumentException>(() => record.Entities.Add(l1));
        }
Exemplo n.º 2
0
        static IEnumerable <Insert> getInsertEntities(string file, string blockname)
        {
            CadDocument doc = DwgReader.Read(file);

            // Get the model space where all the drawing entities are
            BlockRecord modelSpace = doc.BlockRecords["*Model_Space"];

            // Get the insert instance that is using the block that you are looking for
            return(modelSpace.Entities.OfType <Insert>().Where(e => e.Block.Name == blockname));
        }
Exemplo n.º 3
0
        internal MPQStream(MPQ mpq, BlockRecord blockRecord)
        {
            this.mpq         = mpq;
            this.blockRecord = blockRecord;
            this.blockSize   = 0x200 << mpq.Header.BlockSize;

            if (blockRecord.IsCompressed && !blockRecord.SingleUnit)
            {
                LoadBlockOffsets();
            }
        }
Exemplo n.º 4
0
        public void BlockRecordTest()
        {
            string      name   = "my_block";
            BlockRecord record = new BlockRecord(name);

            Assert.Equal(name, record.Name);

            Assert.NotNull(record.BlockEntity);
            Assert.Equal(record.Name, record.BlockEntity.Name);

            Assert.NotNull(record.BlockEnd);
        }
Exemplo n.º 5
0
        public MPQStream OpenFile(string filename)
        {
            if (!GetHashRecord(filename, out HashRecord hash))
            {
                throw new FileNotFoundException("File not found: " + filename);
            }

            BlockRecord block = blockTable[(int)hash.FileBlockIndex];

            block.FileName       = filename.ToLower();
            block.EncryptionSeed = CalculateEncryptionSeed(block);
            return(new MPQStream(this, block));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <c>Block</c> class.
        /// </summary>
        /// <param name="name">Block name.</param>
        public Block(string name)
            : base(DxfObjectCode.Block)
        {
            if (string.IsNullOrEmpty(name))
                throw (new ArgumentNullException("name"));

            this.name = name;
            this.basePoint = Vector3d.Zero;
            this.layer = Layer.Default;
            this.attributes = new Dictionary<string, AttributeDefinition>();
            this.entities = new List<IEntityObject>();
            this.record=new BlockRecord(name);
            this.end = new BlockEnd(this.layer);
        }
Exemplo n.º 7
0
        private uint CalculateEncryptionSeed(BlockRecord record)
        {
            if (record.FileName == null)
            {
                return(0);
            }

            uint seed = HashString(System.IO.Path.GetFileName(record.FileName), MPQ_HASH_FILE_KEY);

            if (record.KeyAdjusted)
            {
                seed = (seed + record.BlockOffset) ^ record.FileSize;
            }
            return(seed);
        }
Exemplo n.º 8
0
        private static UInt32 ComputeFileKey(string filePath, BlockRecord blockRecord, UInt32 archiveOffset)
        {
            var fileName = filePath.Split('\\').Last();

            // Hash the name to get the base key
            var fileKey = HashString(fileName, MPQ_HASH_FILE_KEY);

            // Offset-adjust the key if necessary
            if (blockRecord.KeyAdjusted)
            {
                fileKey = (fileKey + blockRecord.BlockOffset) ^ blockRecord.FileSize;
            }

            return(fileKey);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the <c>Block</c> class.
        /// </summary>
        /// <param name="name">Block name.</param>
        public Block(string name) : base(DxfObjectCode.Block)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw (new ArgumentNullException("name"));
            }

            this.name       = name;
            this.basePoint  = Vector3f.Zero;
            this.layer      = Layer.Default;
            this.attributes = new Dictionary <string, AttributeDefinition>();
            this.entities   = new List <IEntityObject>();
            this.record     = new BlockRecord(name);
            this.end        = new BlockEnd(this.layer);
        }
Exemplo n.º 10
0
        public void AddEntityTest()
        {
            BlockRecord record = new BlockRecord();

            Line l1 = new Line();
            Line l2 = new Line();
            Line l3 = new Line();
            Line l4 = new Line();

            record.Entities.Add(l1);
            record.Entities.Add(l2);
            record.Entities.Add(l3);
            record.Entities.Add(l4);

            foreach (Entity e in record.Entities)
            {
                Assert.Equal(record, e.Owner);
            }
        }
Exemplo n.º 11
0
        private void readEntries <T>(DwgTableTemplate <T> tableTemplate)
            where T : TableEntry
        {
            //Read all the entries until the end of the table
            while (this._reader.LastValueAsString != DxfFileToken.EndTable)
            {
                this.readCommonObjectData(out string name, out ulong handle, out ulong?ownerHandle, out ulong?xdictHandle, out List <ulong> reactors);

                Debug.Assert(this._reader.LastValueAsString == DxfSubclassMarker.TableRecord);
                Debug.Assert(this._reader.LastValueAsString == DxfSubclassMarker.TableRecord);

                this._reader.ReadNext();

                CadTemplate template = null;

                //Get the entry
                switch (name)
                {
                case DxfFileToken.TableAppId:
                    AppId appid = new AppId();
                    template = new CadTableEntryTemplate <AppId>(appid);
                    this.readMapped <AppId>(appid, template);
                    break;

                case DxfFileToken.TableBlockRecord:
                    BlockRecord record = new BlockRecord();
                    template = new CadBlockRecordTemplate(record);
                    this.readMapped <BlockRecord>(record, template);
                    break;

                case DxfFileToken.TableDimstyle:
                    DimensionStyle dimStyle = new DimensionStyle();
                    template = new DwgDimensionStyleTemplate(dimStyle);
                    this.readMapped <DimensionStyle>(dimStyle, template);
                    break;

                case DxfFileToken.TableLayer:
                    Layer layer = new Layer();
                    template = new CadLayerTemplate(layer);
                    this.readMapped <Layer>(layer, template);
                    break;

                case DxfFileToken.TableLinetype:
                    LineType ltype = new LineType();
                    template = new CadLineTypeTemplate(ltype);
                    this.readMapped <LineType>(ltype, template);
                    break;

                case DxfFileToken.TableStyle:
                    TextStyle style = new TextStyle();
                    template = new CadTableEntryTemplate <TextStyle>(style);
                    this.readMapped <TextStyle>(style, template);
                    break;

                case DxfFileToken.TableUcs:
                    UCS ucs = new UCS();
                    template = new CadUcsTemplate(ucs);
                    this.readMapped <UCS>(ucs, template);
                    break;

                case DxfFileToken.TableView:
                    View view = new View();
                    template = new CadViewTemplate(view);
                    this.readMapped <View>(view, template);
                    break;

                case DxfFileToken.TableVport:
                    VPort vport = new VPort();
                    template = new CadVPortTemplate(vport);
                    this.readMapped <VPort>(vport, template);
                    break;

                default:
                    Debug.Fail($"Unhandeled table {name}.");
                    break;
                }

                //Setup the common fields
                template.CadObject.Handle = handle;
                template.OwnerHandle      = ownerHandle;
                template.XDictHandle      = xdictHandle;
                template.ReactorsHandles  = reactors;

                tableTemplate.EntryHandles.Add(template.CadObject.Handle);

                //Add the object and the template to the builder
                this._builder.AddTemplate(template);
            }
        }
Exemplo n.º 12
0
        private void AddBlockRecordUnitsXData(BlockRecord record)
        {
            // for dxf versions prior to AutoCad2007 the block record units is stored in an extended data block
            XData xdataEntry;
            if (record.XData.ContainsAppId(ApplicationRegistry.DefaultName))
            {
                xdataEntry = record.XData[ApplicationRegistry.DefaultName];
                xdataEntry.XDataRecord.Clear();
            }
            else
            {
                xdataEntry = new XData(new ApplicationRegistry(ApplicationRegistry.DefaultName));
                record.XData.Add(xdataEntry);
            }

            xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.String, "DesignCenter Data"));
            xdataEntry.XDataRecord.Add(XDataRecord.OpenControlString);
            xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short)1));
            xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short)record.Units));
            xdataEntry.XDataRecord.Add(XDataRecord.CloseControlString);
        }
Exemplo n.º 13
0
        private BlockRecord ReadBlockRecord()
        {
            Debug.Assert(this.chunk.ReadString() == SubclassMarker.BlockRecord);

            string name = null;
            DrawingUnits units = DrawingUnits.Unitless;
            bool allowExploding = true;
            bool scaleUniformly = false;
            List<XData> xData = new List<XData>();
            XData designCenterData = null;
            string pointerToLayout = null;

            this.chunk.Next();

            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 2:
                        name = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        this.chunk.Next();
                        break;
                    case 70:
                        units = (DrawingUnits) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 280:
                        allowExploding = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 281:
                        scaleUniformly = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 340:
                        pointerToLayout = this.chunk.ReadString();
                        this.chunk.Next();
                        break;
                    case 1001:
                        string appId = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        XData data = this.ReadXDataRecord(appId);
                        xData.Add(data);
                        if (data.ApplicationRegistry.Name.Equals(ApplicationRegistry.DefaultName, StringComparison.OrdinalIgnoreCase))
                            designCenterData = data;
                        break;
                    default:
                        if (this.chunk.Code >= 1000 && this.chunk.Code <= 1071)
                            throw new DxfInvalidCodeValueEntityException(this.chunk.Code, this.chunk.ReadString(),
                                "The extended data of an entity must start with the application registry code.");
                        this.chunk.Next();
                        break;
                }
            }

            if (string.IsNullOrEmpty(name)) return null;

            // we need to check for generated blocks by dimensions, even if the dimension was deleted the block might persist in the drawing.
            this.CheckDimBlockName(name);

            // here is where dxf versions prior to AutoCad2007 stores the block units
            if (designCenterData != null)
            {
                foreach (XDataRecord data in designCenterData.XDataRecord)
                {
                    // the second 1070 code is the one that stores the block units,
                    // it will override the first 1070 that stores the Autodesk Design Center version number
                    if (data.Code == XDataCode.Int16)
                        units = (DrawingUnits)(short)data.Value;
                }
            }

            BlockRecord record = new BlockRecord(name)
            {
                Units = units,
                AllowExploding = allowExploding,
                ScaleUniformly = scaleUniformly
            };

            record.XData.AddRange(xData);

            if (!string.IsNullOrEmpty(pointerToLayout) && pointerToLayout != "0")
                this.blockRecordPointerToLayout.Add(pointerToLayout, record);

            return record;
        }
Exemplo n.º 14
0
        private BlockRecord ReadBlockRecord()
        {
            Debug.Assert(this.chunk.ReadString() == SubclassMarker.BlockRecord);

            string name = null;
            DrawingUnits units = DrawingUnits.Unitless;
            bool allowExploding = true;
            bool scaleUniformly = false;
            List<XData> xData = new List<XData>();
            string pointerToLayout = null;

            this.chunk.Next();

            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 2:
                        name = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        this.chunk.Next();
                        break;
                    case 70:
                        units = (DrawingUnits) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 280:
                        allowExploding = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 281:
                        scaleUniformly = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 340:
                        pointerToLayout = this.chunk.ReadHex();
                        this.chunk.Next();
                        break;
                    case 1001:
                        string appId = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        XData data = this.ReadXDataRecord(this.GetApplicationRegistry(appId));
                        xData.Add(data);
                        break;
                    default:
                        if (this.chunk.Code >= 1000 && this.chunk.Code <= 1071)
                            throw new Exception("The extended data of an entity must start with the application registry code.");

                        this.chunk.Next();
                        break;
                }
            }

            if (string.IsNullOrEmpty(name))
                return null;

            // we need to check for generated blocks by dimensions, even if the dimension was deleted the block might persist in the drawing.
            this.CheckDimBlockName(name);

            BlockRecord record = new BlockRecord(name)
            {
                Units = units,
                AllowExploding = allowExploding,
                ScaleUniformly = scaleUniformly
            };

            record.XData.AddRange(xData);

            // here is where dxf versions prior to AutoCad2007 stores the block units
            // read the layer transparency from the extended data
            XData designCenterData;
            if (record.XData.TryGetValue(ApplicationRegistry.DefaultName, out designCenterData))
            {
                IEnumerator<XDataRecord> records = designCenterData.XDataRecord.GetEnumerator();
                while (records.MoveNext())
                {
                    XDataRecord data = records.Current;

                    // the record units are stored under the string "DesignCenter Data"
                    if (data.Code == XDataCode.String && string.Equals((string) data.Value, "DesignCenter Data", StringComparison.OrdinalIgnoreCase))
                    {
                        if (records.MoveNext())
                            data = records.Current;
                        else
                            break; // premature end

                        // all style overrides are enclosed between XDataCode.ControlString "{" and "}"
                        if (data.Code != XDataCode.ControlString)
                            break; // premature end

                        if (records.MoveNext())
                            data = records.Current;
                        else
                            break; // premature end

                        while (data.Code != XDataCode.ControlString)
                        {
                            if (records.MoveNext())
                                data = records.Current;
                            else
                                break; // premature end

                            // the second 1070 code is the one that stores the block units,
                            // it will override the first 1070 that stores the Autodesk Design Center version number
                            if (data.Code == XDataCode.Int16)
                                record.Units = (DrawingUnits) (short) data.Value;
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(pointerToLayout) && pointerToLayout != "0")
                this.blockRecordPointerToLayout.Add(pointerToLayout, record);

            return record;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Writes a new block record to the table section.
        /// </summary>
        /// <param name="blockRecord">Block.</param>
        public void WriteBlockRecord(BlockRecord blockRecord)
        {
            if (this.activeTable != StringCode.BlockRecordTable)
            {
                throw new InvalidDxfTableException(this.activeTable, this.file);
            }
            this.WriteCodePair(0, blockRecord.CodeName);
            this.WriteCodePair(5, blockRecord.Handle);
            this.WriteCodePair(100, SubclassMarker.TableRecord);

            this.WriteCodePair(100, SubclassMarker.BlockRecord);

            this.WriteCodePair(2, blockRecord);
        }
Exemplo n.º 16
0
 public BlockEnd(BlockRecord record) : base()
 {
     this.Owner = record;
 }
Exemplo n.º 17
0
        /// <summary>
        /// Writes a new block record to the table section.
        /// </summary>
        /// <param name="blockRecord">Block.</param>
        private void WriteBlockRecord(BlockRecord blockRecord)
        {
            Debug.Assert(this.activeTable == DxfObjectCode.BlockRecordTable);

            this.chunk.Write(0, blockRecord.CodeName);
            this.chunk.Write(5, blockRecord.Handle);
            this.chunk.Write(330, blockRecord.Owner.Handle);

            this.chunk.Write(100, SubclassMarker.TableRecord);

            this.chunk.Write(100, SubclassMarker.BlockRecord);

            this.chunk.Write(2, this.EncodeNonAsciiCharacters(blockRecord.Name));

            // Hard-pointer ID/handle to associated LAYOUT object
            this.chunk.Write(340, blockRecord.Layout == null ? "0" : blockRecord.Layout.Handle);

            // internal blocks do not need more information
            if (blockRecord.IsForInternalUseOnly) return;

            // The next three values will only work for dxf version AutoCad2007 and upwards
            this.chunk.Write(70, (short) blockRecord.Units);
            this.chunk.Write(280, blockRecord.AllowExploding ? (short) 1 : (short) 0);
            this.chunk.Write(281, blockRecord.ScaleUniformly ? (short) 1 : (short) 0);

            this.AddBlockRecordUnitsXData(blockRecord);
            this.WriteXData(blockRecord.XData);
        }
Exemplo n.º 18
0
        private BlockRecord[] ReadChunkBKDT(Stream stream, Chunk chunk, MphdRecord mphdRecord)
        {
            BlockRecord[] blockRecords = new BlockRecord[mphdRecord.NumBlockStruct];
            bool lsb = mphdRecord.LSB;
            for(int index = 0; index < mphdRecord.NumBlockStruct; index++)
            {
                stream.Position = chunk.FilePosition + mphdRecord.BlockStructSize * index;

                BlockRecord blockRecord = new BlockRecord();
                blockRecord.BackgroundOffset = ReadSignedLong(stream, lsb);
                blockRecord.ForegroundOffset = ReadSignedLong(stream, lsb);
                blockRecord.BackgroundOffset2 = ReadSignedLong(stream, lsb);
                blockRecord.ForegroundOffset2 = ReadSignedLong(stream, lsb);
                blockRecord.User1 = ReadUnsignedLong(stream, lsb);
                blockRecord.User2 = ReadUnsignedLong(stream, lsb);
                blockRecord.User3 = ReadUnsignedShort(stream, lsb);
                blockRecord.User4 = ReadUnsignedShort(stream, lsb);
                blockRecord.User5 = ReadUnsignedByte(stream);
                blockRecord.User6 = ReadUnsignedByte(stream);
                blockRecord.User7 = ReadUnsignedByte(stream);
                blockRecord.Flags = ReadUnsignedByte(stream);
                blockRecords[index] = blockRecord;
            }

            return blockRecords;
        }
Exemplo n.º 19
0
        private TableEntry readEntry(DxfEntryTemplate template)
        {
            TableEntry table = null;

            //Get the entry
            switch (template.TableName)
            {
            case DxfFileToken.TableAppId:
                table = new AppId(template);
                break;

            case DxfFileToken.TableBlockRecord:
                table = new BlockRecord(template);
                break;

            case DxfFileToken.TableDimstyle:
                table = new DimensionStyle(template);
                break;

            case DxfFileToken.TableLayer:
                table = new Layer(template);
                break;

            case DxfFileToken.TableLinetype:
                table = new LineType(template);
                break;

            case DxfFileToken.TableStyle:
                table = new Style(template);
                break;

            case DxfFileToken.TableUcs:
                table = new UCS(template);
                break;

            case DxfFileToken.TableView:
                table = new View(template);
                break;

            case DxfFileToken.TableVport:
                table = new VPort(template);
                break;

            default:
                Debug.Fail($"Unhandeled table {template.Name}.");
                break;
            }

            //Jump the SubclassMarker
            m_reader.ReadNext();

            Dictionary <DxfCode, object> map = table?.GetCadObjectMap() ?? new Dictionary <DxfCode, object>();

            while (m_reader.LastDxfCode != DxfCode.Start)
            {
                //Check if the dxf code is registered
                if (map.ContainsKey(m_reader.LastDxfCode))
                {
                    //Set the value
                    map[m_reader.LastDxfCode] = m_reader.LastValue;
                }

                m_reader.ReadNext();
            }

            //Build the table based on the map
            table?.Build(map);

            return(table);
        }