Beispiel #1
0
        internal bool AddEntity(EntityObject entity, bool isBlockEntity, bool assignHandle)
        {
            // no null entities allowed
            if (entity == null)
            {
                return(false);
            }

            if (!string.IsNullOrEmpty(entity.Handle))
            {
                // check if the entity handle has been assigned
                if (this.AddedObjects.ContainsKey(entity.Handle))
                {
                    // if the handle is equal the entity might come from another document, check if it is exactly the same object
                    DxfObject existing = this.AddedObjects[entity.Handle];
                    // if the entity is already in the document return false, do not add it again
                    if (existing.Equals(entity))
                    {
                        return(false);
                    }
                }
            }

            if (assignHandle)
            {
                this.NumHandles = entity.AsignHandle(this.NumHandles);
            }

            // the entities that are part of a block do not belong to any of the entities lists but to the block definition.
            switch (entity.Type)
            {
            case EntityType.Arc:
                if (!isBlockEntity)
                {
                    this.arcs.Add((Arc)entity);
                }
                break;

            case EntityType.Circle:
                if (!isBlockEntity)
                {
                    this.circles.Add((Circle)entity);
                }
                break;

            case EntityType.Dimension:
                if (!isBlockEntity)
                {
                    this.dimensions.Add((Dimension)entity);
                }
                ((Dimension)entity).Style = this.dimStyles.Add(((Dimension)entity).Style);
                this.dimStyles.References[((Dimension)entity).Style.Name].Add(entity);

                // create the block that represent the dimension drawing
                Block dimBlock = ((Dimension)entity).Block;
                if (dimBlock == null)
                {
                    dimBlock = ((Dimension)entity).BuildBlock("*D" + ++this.DimensionBlocksGenerated);
                    if (this.blocks.Contains(dimBlock.Name))
                    {
                        throw new ArgumentException("The list already contains the block: " + dimBlock.Name + ". The block names that start with *D are reserverd for dimensions");
                    }
                    dimBlock.Flags = BlockTypeFlags.AnonymousBlock;
                }
                this.blocks.Add(dimBlock);
                this.blocks.References[dimBlock.Name].Add(entity);
                break;

            case EntityType.Ellipse:
                if (!isBlockEntity)
                {
                    this.ellipses.Add((Ellipse)entity);
                }
                break;

            case EntityType.Face3D:
                if (!isBlockEntity)
                {
                    this.faces3d.Add((Face3d)entity);
                }
                break;

            case EntityType.Spline:
                if (!isBlockEntity)
                {
                    this.splines.Add((Spline)entity);
                }
                break;

            case EntityType.Hatch:
                Hatch hatch = (Hatch)entity;
                hatch.AddPatternXData();
                if (!isBlockEntity)
                {
                    this.hatches.Add(hatch);
                }
                break;

            case EntityType.Insert:
                ((Insert)entity).Block = this.blocks.Add(((Insert)entity).Block);
                this.blocks.References[((Insert)entity).Block.Name].Add(entity);
                foreach (Attribute attribute in ((Insert)entity).Attributes.Values)
                {
                    attribute.Layer = this.layers.Add(attribute.Layer);
                    this.layers.References[attribute.Layer.Name].Add(attribute);

                    attribute.LineType = this.lineTypes.Add(attribute.LineType);
                    this.lineTypes.References[attribute.LineType.Name].Add(attribute);

                    attribute.Style = this.textStyles.Add(attribute.Style);
                    this.textStyles.References[attribute.Style.Name].Add(attribute);
                }
                if (!isBlockEntity)
                {
                    this.inserts.Add((Insert)entity);
                }
                break;

            case EntityType.LightWeightPolyline:
                if (!isBlockEntity)
                {
                    this.lwPolylines.Add((LwPolyline)entity);
                }
                break;

            case EntityType.Line:
                if (!isBlockEntity)
                {
                    this.lines.Add((Line)entity);
                }
                break;

            case EntityType.Point:
                if (!isBlockEntity)
                {
                    this.points.Add((Point)entity);
                }
                break;

            case EntityType.PolyfaceMesh:
                if (!isBlockEntity)
                {
                    this.polyfaceMeshes.Add((PolyfaceMesh)entity);
                }
                break;

            case EntityType.Polyline:
                if (!isBlockEntity)
                {
                    this.polylines.Add((Polyline)entity);
                }
                break;

            case EntityType.Solid:
                if (!isBlockEntity)
                {
                    this.solids.Add((Solid)entity);
                }
                break;

            case EntityType.Mesh:
                if (!isBlockEntity)
                {
                    this.meshes.Add((Mesh)entity);
                }
                break;

            case EntityType.Text:
                Text text = (Text)entity;
                text.Style = this.textStyles.Add(text.Style);
                this.textStyles.References[text.Style.Name].Add(entity);
                if (!isBlockEntity)
                {
                    this.texts.Add(text);
                }
                break;

            case EntityType.MText:
                MText mText = (MText)entity;
                mText.Style = this.textStyles.Add(mText.Style);
                this.textStyles.References[mText.Style.Name].Add(entity);
                if (!isBlockEntity)
                {
                    this.mTexts.Add(mText);
                }
                break;

            case EntityType.Image:
                Image image = (Image)entity;
                image.Definition = this.imageDefs.Add(image.Definition);
                this.imageDefs.References[image.Definition.Name].Add(image);
                if (!image.Definition.Reactors.ContainsKey(image.Handle))
                {
                    ImageDefReactor reactor = new ImageDefReactor(image.Handle);
                    this.NumHandles = reactor.AsignHandle(this.NumHandles);
                    image.Definition.Reactors.Add(image.Handle, reactor);
                }
                if (!isBlockEntity)
                {
                    this.images.Add(image);
                }
                break;

            case EntityType.MLine:
                ((MLine)entity).Style = this.mlineStyles.Add(((MLine)entity).Style);
                this.mlineStyles.References[((MLine)entity).Style.Name].Add(entity);
                if (!isBlockEntity)
                {
                    this.mLines.Add((MLine)entity);
                }
                break;

            case EntityType.Ray:
                if (!isBlockEntity)
                {
                    this.rays.Add((Ray)entity);
                }
                break;

            case EntityType.XLine:
                if (!isBlockEntity)
                {
                    this.xlines.Add((XLine)entity);
                }
                break;

            case EntityType.Viewport:
                Viewport viewport = (Viewport)entity;
                if (!isBlockEntity)
                {
                    this.viewports.Add(viewport);
                }
                this.AddEntity(viewport.ClippingBoundary, isBlockEntity, assignHandle);
                break;

            case EntityType.AttributeDefinition:
                AttributeDefinition attDef = (AttributeDefinition)entity;
                attDef.Style = this.textStyles.Add(attDef.Style);
                this.textStyles.References[attDef.Style.Name].Add(entity);
                if (!isBlockEntity)
                {
                    this.attributeDefinitions.Add(attDef);
                }
                break;

            case EntityType.Attribute:
                throw new ArgumentException("The entity " + entity.Type + " is only allowed as part of an insert entity.", "entity");

            default:
                throw new ArgumentException("The entity " + entity.Type + " is not implemented or unknown.");
            }

            foreach (string appReg in entity.XData.Keys)
            {
                entity.XData[appReg].ApplicationRegistry = this.appRegistries.Add(entity.XData[appReg].ApplicationRegistry);
                this.appRegistries.References[appReg].Add(entity);
            }

            if (!isBlockEntity && this.layouts != null)
            {
                entity.Owner = this.layouts[this.activeLayout].AssociatedBlock;
                this.layouts.References[this.activeLayout].Add(entity);
            }

            this.AddedObjects.Add(entity.Handle, entity);

            if (this.layers != null)
            {
                entity.Layer = this.layers.Add(entity.Layer);
                this.layers.References[entity.Layer.Name].Add(entity);
            }

            if (this.lineTypes != null)
            {
                entity.LineType = this.lineTypes.Add(entity.LineType);
                this.lineTypes.References[entity.LineType.Name].Add(entity);
            }

            /*if (!isBlockEntity)
             * {
             *  entity.Owner = this.layouts[this.activeLayout].AssociatedBlock;
             *  this.layouts.References[this.activeLayout].Add(entity);
             * }
             *
             * this.AddedObjects.Add(entity.Handle, entity);
             *
             * entity.Layer = this.layers.Add(entity.Layer);
             * this.layers.References[entity.Layer.Name].Add(entity);
             *
             * entity.LineType = this.lineTypes.Add(entity.LineType);
             * this.lineTypes.References[entity.LineType.Name].Add(entity);*/
            return(true);
        }