internal Block(string name, bool checkName, IList <EntityObject> entities, IList <AttributeDefinition> attributes) : base(name, DxfObjectCode.Block, checkName) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } this.reserved = string.Equals(name, DefaultModelSpaceName, StringComparison.OrdinalIgnoreCase); this.readOnly = this.reserved || name.StartsWith(DefaultPaperSpaceName, StringComparison.OrdinalIgnoreCase); this.description = string.Empty; this.origin = Vector3.Zero; this.layer = Layer.Default; this.xrefFile = string.Empty; this.entities = new EntityCollection(); this.entities.BeforeAddItem += this.Entities_BeforeAddItem; this.entities.AddItem += this.Entities_AddItem; this.entities.BeforeRemoveItem += this.Entities_BeforeRemoveItem; this.entities.RemoveItem += this.Entities_RemoveItem; this.attributes = new AttributeDefinitionDictionary(); this.attributes.BeforeAddItem += this.AttributeDefinitions_BeforeAddItem; this.attributes.AddItem += this.AttributeDefinitions_ItemAdd; this.attributes.BeforeRemoveItem += this.AttributeDefinitions_BeforeRemoveItem; this.attributes.RemoveItem += this.AttributeDefinitions_RemoveItem; this.owner = new BlockRecord(name); this.flags = BlockTypeFlags.None; this.end = new EndBlock { Owner = this.owner }; if (entities != null) { this.entities.AddRange(entities); } if (attributes != null) { this.attributes.AddRange(attributes); } }
internal Block(string name, IEnumerable <EntityObject> entities, IEnumerable <AttributeDefinition> attributes, bool checkName) : base(name, DxfObjectCode.Block, checkName) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } IsReserved = string.Equals(name, DefaultModelSpaceName, StringComparison.OrdinalIgnoreCase); forInternalUse = name.StartsWith("*"); description = string.Empty; origin = Vector3.Zero; layer = Layer.Default; xrefFile = string.Empty; Owner = new BlockRecord(name); flags = BlockTypeFlags.None; end = new EndBlock(this); this.entities = new EntityCollection(); this.entities.BeforeAddItem += Entities_BeforeAddItem; this.entities.AddItem += Entities_AddItem; this.entities.BeforeRemoveItem += Entities_BeforeRemoveItem; this.entities.RemoveItem += Entities_RemoveItem; if (entities != null) { this.entities.AddRange(entities); } this.attributes = new AttributeDefinitionDictionary(); this.attributes.BeforeAddItem += AttributeDefinitions_BeforeAddItem; this.attributes.AddItem += AttributeDefinitions_ItemAdd; this.attributes.BeforeRemoveItem += AttributeDefinitions_BeforeRemoveItem; this.attributes.RemoveItem += AttributeDefinitions_RemoveItem; if (attributes != null) { this.attributes.AddRange(attributes); } }
internal Block(string name, bool checkName, ICollection<EntityObject> entities, ICollection<AttributeDefinition> attributes) : base(name, DxfObjectCode.Block, checkName) { if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); this.reserved = name.Equals(DefaultModelSpaceName, StringComparison.OrdinalIgnoreCase); this.readOnly = this.reserved || name.StartsWith(DefaultPaperSpaceName, StringComparison.OrdinalIgnoreCase); this.description = string.Empty; this.position = Vector3.Zero; this.layer = Layer.Default; this.entities = new EntityCollection(); this.entities.BeforeAddItem += this.Entities_BeforeAddItem; this.entities.AddItem += this.Entities_AddItem; this.entities.BeforeRemoveItem += this.Entities_BeforeRemoveItem; this.entities.RemoveItem += this.Entities_RemoveItem; this.attributes = new AttributeDefinitionDictionary(); this.attributes.BeforeAddItem += this.AttributeDefinitions_BeforeAddItem; this.attributes.AddItem += this.AttributeDefinitions_ItemAdd; this.attributes.BeforeRemoveItem += this.AttributeDefinitions_BeforeRemoveItem; this.attributes.RemoveItem += this.AttributeDefinitions_RemoveItem; this.owner = new BlockRecord(name); this.flags = BlockTypeFlags.None; this.end = new EndBlock { Owner = this.owner }; if (entities != null) this.entities.AddRange(entities); if (attributes != null) this.attributes.AddRange(attributes); }
private Block ReadBlock() { Debug.Assert(this.chunk.ReadString() == DxfObjectCode.BeginBlock); BlockRecord blockRecord; Layer layer = Layer.Default; string name = string.Empty; string handle = string.Empty; BlockTypeFlags type = BlockTypeFlags.None; Vector3 basePoint = Vector3.Zero; List<EntityObject> entities = new List<EntityObject>(); List<AttributeDefinition> attDefs = new List<AttributeDefinition>(); this.chunk.Next(); while (this.chunk.Code != 0) { switch (this.chunk.Code) { case 5: handle = this.chunk.ReadString(); this.chunk.Next(); break; case 8: string layerName = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); layer = this.GetLayer(layerName); this.chunk.Next(); break; case 2: name = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); this.chunk.Next(); break; case 70: type = (BlockTypeFlags) this.chunk.ReadShort(); this.chunk.Next(); break; case 10: basePoint.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 20: basePoint.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 30: basePoint.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 3: //I don't know the reason of these duplicity since code 2 also contains the block name //The program EASE exports code 3 with an empty string (use it or don't use it but do NOT mix information) //name = dxfPairInfo.Value; this.chunk.Next(); break; default: this.chunk.Next(); break; } } // read block entities while (this.chunk.ReadString() != DxfObjectCode.EndBlock) { EntityObject entity = this.ReadEntity(true); if (entity != null) { AttributeDefinition attDef = entity as AttributeDefinition; if (attDef != null) attDefs.Add(attDef); else entities.Add(entity); } } // read the end block object until a new element is found this.chunk.Next(); string endBlockHandle = string.Empty; while (this.chunk.Code != 0) { switch (this.chunk.Code) { case 5: endBlockHandle = this.chunk.ReadString(); this.chunk.Next(); break; case 8: // the EndBlock layer and the Block layer are the same this.chunk.Next(); break; default: this.chunk.Next(); break; } } if (!this.blockRecords.TryGetValue(name, out blockRecord)) throw new DxfTableException(DxfObjectCode.BlockRecordTable, "The block record " + name + " is not defined"); EndBlock end = new EndBlock { Handle = endBlockHandle, Owner = blockRecord, }; Block block = new Block(name, false, null, null) { Handle = handle, Owner = blockRecord, Position = basePoint, Layer = layer, Flags = type, End = end }; if (name.StartsWith(Block.DefaultPaperSpaceName, StringComparison.OrdinalIgnoreCase)) { // the dxf is not consistent with the way they handle entities that belong to different paper spaces. // While the entities of *Paper_Space block are stored in the ENTITIES section as the *Model_Space, // the list of entities in *Paper_Space# are stored in the block definition itself. // As all this entities do not need an insert entity to have a visual representation, // they will be stored in the global entities lists together with the rest of the entities of *Model_Space and *Paper_Space foreach (EntityObject entity in entities) this.entityList.Add(entity, blockRecord.Handle); // this kind of blocks do not store attribute definitions } else { // add attribute definitions foreach (AttributeDefinition attDef in attDefs) { // AutoCAD allows duplicate tags in attribute definitions, but this library does not having duplicate tags is not recommended in any way, // since there will be no way to know which is the definition associated to the insert attribute if(!block.AttributeDefinitions.ContainsTag(attDef.Tag)) block.AttributeDefinitions.Add(attDef); } // block entities for post processing (MLines and Images references other objects (MLineStyle and ImageDef) that will be defined later this.blockEntities.Add(block, entities); } return block; }