Esempio n. 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:
                Dimension dim = (Dimension)entity;
                dim.Style = this.dimStyles.Add(dim.Style, assignHandle);
                this.dimStyles.References[dim.Style.Name].Add(dim);
                // create the block that represent the dimension drawing
                Block dimBlock = dim.Block;
                if (dimBlock == null)
                {
                    dimBlock = dim.BuildBlock("*D" + ++this.DimensionBlocksGenerated);
                }
                dim.Block = this.blocks.Add(dimBlock);
                this.blocks.References[dimBlock.Name].Add(dim);
                if (!isBlockEntity)
                {
                    this.dimensions.Add(dim);
                }
                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 insert = (Insert)entity;
                insert.Block = this.blocks.Add(insert.Block, assignHandle);
                this.blocks.References[insert.Block.Name].Add(insert);
                foreach (Attribute attribute in insert.Attributes.Values)
                {
                    attribute.Layer = this.layers.Add(attribute.Layer, assignHandle);
                    this.layers.References[attribute.Layer.Name].Add(attribute);

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

                    attribute.Style = this.textStyles.Add(attribute.Style, assignHandle);
                    this.textStyles.References[attribute.Style.Name].Add(attribute);
                }
                if (!isBlockEntity)
                {
                    this.inserts.Add(insert);
                }
                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, assignHandle);
                this.textStyles.References[text.Style.Name].Add(text);
                if (!isBlockEntity)
                {
                    this.texts.Add(text);
                }
                break;

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

            case EntityType.Image:
                Image image = (Image)entity;
                image.Definition = this.imageDefs.Add(image.Definition, assignHandle);
                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 mline = (MLine)entity;
                mline.Style = this.mlineStyles.Add(mline.Style, assignHandle);
                this.mlineStyles.References[mline.Style.Name].Add(mline);
                if (!isBlockEntity)
                {
                    this.mLines.Add(mline);
                }
                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;
                this.AddEntity(viewport.ClippingBoundary, isBlockEntity, assignHandle);
                if (!isBlockEntity)
                {
                    this.viewports.Add(viewport);
                }
                break;

            case EntityType.AttributeDefinition:
                AttributeDefinition attDef = (AttributeDefinition)entity;
                attDef.Style = this.textStyles.Add(attDef.Style, assignHandle);
                this.textStyles.References[attDef.Style.Name].Add(attDef);
                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.AppIds)
            {
                entity.XData[appReg].ApplicationRegistry = this.appRegistries.Add(entity.XData[appReg].ApplicationRegistry, assignHandle);
                this.appRegistries.References[appReg].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, assignHandle);
            this.layers.References[entity.Layer.Name].Add(entity);

            entity.LineType = this.lineTypes.Add(entity.LineType, assignHandle);
            this.lineTypes.References[entity.LineType.Name].Add(entity);
            return(true);
        }