Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <c>Image</c> class.
 /// </summary>
 /// <param name="imageDefinition">Image definition.</param>
 /// <param name="position">Image <see cref="Vector3">position</see> in world coordinates.</param>
 public Image(ImageDef imageDefinition, Vector3 position)
     : base(EntityType.Image, DxfObjectCode.Image)
 {
     this.imageDef         = imageDefinition;
     this.position         = position;
     this.width            = imageDefinition.Width * imageDefinition.OnePixelSize.X;
     this.height           = imageDefinition.Height * imageDefinition.OnePixelSize.Y;
     this.rotation         = 0;
     this.clipping         = false;
     this.brightness       = 50.0f;
     this.contrast         = 50.0f;
     this.fade             = 0.0f;
     this.displayOptions   = ImageDisplayFlags.ShowImage | ImageDisplayFlags.ShowImageWhenNotAlignedWithScreen | ImageDisplayFlags.UseClippingBoundary;
     this.clippingBoundary = new ImageClippingBoundary(-0.5, -0.5, imageDefinition.Width, imageDefinition.Height);
 }
Ejemplo n.º 2
0
        private Image ReadImage()
        {
            double posX = 0.0, posY = 0.0, posZ = 0.0;
            double uX = 0.0, uY = 0.0, uZ = 0.0;
            double vX = 0.0, vY = 0.0, vZ = 0.0;
            double width = 0, height = 0;
            string imageDefHandle = null;
            ImageDisplayFlags displayOptions = ImageDisplayFlags.ShowImage | ImageDisplayFlags.ShowImageWhenNotAlignedWithScreen | ImageDisplayFlags.UseClippingBoundary;
            bool clipping = false;
            short brightness = 50;
            short contrast = 50;
            short fade = 0;
            ImageClippingBoundaryType boundaryType = ImageClippingBoundaryType.Rectangular;
            ImageClippingBoundary clippingBoundary = null;

            List<XData> xData = new List<XData>();
            this.chunk.Next();
            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 10:
                        posX = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 20:
                        posY = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 30:
                        posZ = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 11:
                        uX = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 21:
                        uY = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 31:
                        uZ = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 12:
                        vX = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 22:
                        vY = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 32:
                        vZ = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 13:
                        width = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 23:
                        height = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 340:
                        imageDefHandle = this.chunk.ReadString();
                        this.chunk.Next();
                        break;
                    case 70:
                        displayOptions = (ImageDisplayFlags) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 280:
                        clipping = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 281:
                        brightness = this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 282:
                        contrast = this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 283:
                        fade = this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 71:
                        boundaryType = (ImageClippingBoundaryType) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 91:
                        int numVertexes = this.chunk.ReadInt();
                        List<Vector2> vertexes = new List<Vector2>();
                        this.chunk.Next();
                        for (int i = 0; i < numVertexes; i++)
                        {
                            double x = 0.0;
                            double y = 0.0;
                            if (this.chunk.Code == 14) x = this.chunk.ReadDouble();
                            this.chunk.Next();
                            if (this.chunk.Code == 24) y = this.chunk.ReadDouble();
                            this.chunk.Next();
                            vertexes.Add(new Vector2(x, y));
                        }
                        if (boundaryType == ImageClippingBoundaryType.Rectangular)
                            clippingBoundary = new ImageClippingBoundary(vertexes[0], vertexes[1]);
                        else if (boundaryType == ImageClippingBoundaryType.Polygonal)
                            clippingBoundary = new ImageClippingBoundary(vertexes);
                        else
                            clippingBoundary = null;
                        break;
                    case 1001:
                        string appId = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        XData data = this.ReadXDataRecord(appId);
                        xData.Add(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;
                }
            }

            Vector3 u = new Vector3(uX, uY, uZ);
            Vector3 v = new Vector3(vX, vY, vZ);
            Vector3 normal = Vector3.CrossProduct(u, v);
            Vector3 uOCS = MathHelper.Transform(u, normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object);
            double rotation = Vector2.Angle(new Vector2(uOCS.X, uOCS.Y))*MathHelper.RadToDeg;
            double uLength = u.Modulus();
            double vLength = v.Modulus();

            Image image = new Image
            {
                Width = width*uLength,
                Height = height*vLength,
                Position = new Vector3(posX, posY, posZ),
                Normal = normal,
                Rotation = rotation,
                DisplayOptions = displayOptions,
                Clipping = clipping,
                Brightness = brightness,
                Contrast = contrast,
                Fade = fade,
                ClippingBoundary = clippingBoundary
            };

            image.XData.AddRange(xData);

            this.imgToImgDefHandles.Add(image, imageDefHandle);

            return image;
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <c>Image</c> class.
 /// </summary>
 /// <param name="imageDefinition">Image definition.</param>
 /// <param name="position">Image <see cref="Vector3">position</see> in world coordinates.</param>
 /// <param name="width">Image width in world coordinates.</param>
 /// <param name="height">Image height in world coordinates.</param>
 public Image(ImageDef imageDefinition, Vector3 position, double width, double height)
     : base(EntityType.Image, DxfObjectCode.Image)
 {
     this.imageDef = imageDefinition;
     this.position = position;
     this.width = width;
     this.height = height;
     this.rotation = 0;
     this.clipping = false;
     this.brightness = 50;
     this.contrast = 50;
     this.fade = 0;
     this.displayOptions = ImageDisplayFlags.ShowImage | ImageDisplayFlags.ShowImageWhenNotAlignedWithScreen | ImageDisplayFlags.UseClippingBoundary;
     this.clippingBoundary = new ImageClippingBoundary(-0.5, -0.5, imageDefinition.Width, imageDefinition.Height);
 }
Ejemplo n.º 4
0
        private static void WriteImage()
        {
            ImageDef imageDef = new ImageDef("img\\image01.jpg");
            Image image = new Image(imageDef, Vector3.Zero, 10, 10);

            XData xdata1 = new XData(new ApplicationRegistry("netDxf"));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "xData image position"));
            xdata1.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionX, image.Position.X));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionY, image.Position.Y));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionZ, image.Position.Z));
            xdata1.XDataRecord.Add(XDataRecord.CloseControlString);
            image.XData.Add(xdata1);

            //image.Normal = new Vector3(1, 1, 1);
            //image.Rotation = 30;

            // you can pass a name that must be unique for the image definiton, by default it will use the file name without the extension
            ImageDef imageDef2 = new ImageDef("img\\image02.jpg", "MyImage");
            Image image2 = new Image(imageDef2, new Vector3(0, 150, 0), 10, 10);
            Image image3 = new Image(imageDef2, new Vector3(150, 150, 0), 10, 10);

            // clipping boundary definition in local coordinates
            ImageClippingBoundary clip = new ImageClippingBoundary(100, 100, 500, 300);
            image.ClippingBoundary = clip;
            // set to null to restore the default clipping boundary (full image)
            image.ClippingBoundary = null;

            // images can be part of a block definition
            Block block = new Block("ImageBlock");
            block.Entities.Add(image2);
            block.Entities.Add(image3);
            Insert insert = new Insert(block, new Vector3(0, 100, 0));

            DxfDocument dxf = new DxfDocument();

            dxf.AddEntity(image);
            //dxf.AddEntity(image2);
            //dxf.AddEntity(image3);
            dxf.AddEntity(insert);

            dxf.Save("image.dxf");
            dxf = DxfDocument.Load("image.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010;
            dxf.Save("image.dxf");

            //dxf.RemoveEntity(image2);
            //dxf.Save("image2.dxf");
            //dxf.RemoveEntity(image3);
            //dxf.RemoveEntity(image);
            //dxf.Save("image3.dxf");

        }