Example #1
0
        /// <summary>
        /// Adds a block to the list.
        /// </summary>
        /// <param name="block"><see cref="Block">Block</see> to add to the list.</param>
        /// <param name="assignHandle">Specifies if a handle needs to be generated for the block parameter.</param>
        /// <returns>
        /// If a block already exists with the same name as the instance that is being added the method returns the existing block,
        /// if not it will return the new block.
        /// </returns>
        internal override Block Add(Block block, bool assignHandle)
        {
            if (list.Count >= MaxCapacity)
            {
                throw new OverflowException(string.Format("Table overflow. The maximum number of elements the table {0} can have is {1}", CodeName, MaxCapacity));
            }

            if (block == null)
            {
                throw new ArgumentNullException(nameof(block));
            }

            Block add;

            if (list.TryGetValue(block.Name, out add))
            {
                return(add);
            }

            if (assignHandle || string.IsNullOrEmpty(block.Handle))
            {
                Owner.NumHandles = block.AsignHandle(Owner.NumHandles);
            }

            list.Add(block.Name, block);
            references.Add(block.Name, new List <DxfObject>());

            block.Layer = Owner.Layers.Add(block.Layer);
            Owner.Layers.References[block.Layer.Name].Add(block);

            //for new block definitions configure its entities
            foreach (EntityObject entity in block.Entities)
            {
                Owner.AddEntityToDocument(entity, block, assignHandle);
            }

            //for new block definitions configure its attributes
            foreach (AttributeDefinition attDef in block.AttributeDefinitions.Values)
            {
                Owner.AddAttributeDefinitionToDocument(attDef, assignHandle);
            }

            block.Record.Owner = this;

            block.NameChanged                += Item_NameChanged;
            block.LayerChanged               += Block_LayerChanged;
            block.EntityAdded                += Block_EntityAdded;
            block.EntityRemoved              += Block_EntityRemoved;
            block.AttributeDefinitionAdded   += Block_AttributeDefinitionAdded;
            block.AttributeDefinitionRemoved += Block_AttributeDefinitionRemoved;

            Owner.AddedObjects.Add(block.Handle, block);
            Owner.AddedObjects.Add(block.Owner.Handle, block.Owner);

            return(block);
        }
Example #2
0
        private void Block_EntityAdded(TableObject sender, BlockEntityChangeEventArgs e)
        {
            if (e.Item.Owner != null)
            {
                // the block and its entities must belong to the same document
                if (!ReferenceEquals(e.Item.Owner.Record.Owner.Owner, Owner))
                {
                    throw new ArgumentException("The block and the entity must belong to the same document. Clone it instead.");
                }

                // the entity cannot belong to another block
                if (e.Item.Owner.Record.Layout == null)
                {
                    throw new ArgumentException("The entity cannot belong to another block. Clone it instead.");
                }

                // we will exchange the owner of the entity
                Owner.RemoveEntity(e.Item);
            }
            Owner.AddEntityToDocument(e.Item, (Block)sender, string.IsNullOrEmpty(e.Item.Handle));
        }