Exemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="image"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="db"></param>
        /// <param name="r"></param>
        public void Draw(object doc, Test2d.XImage image, double dx, double dy, ImmutableArray<Test2d.ShapeProperty> db, Test2d.Record r)
        {
            var _doc = doc as DxfDocument;

            var bytes = _state.ImageCache.GetImage(image.Path);
            if (bytes != null)
            {
                var rect = Test2d.Rect2.Create(image.TopLeft, image.BottomRight, dx, dy);

                if (_enableImageCache
                    && _biCache.ContainsKey(image.Path))
                {
                    var dxfImageDefinition = _biCache[image.Path];
                    var dxfImage = new Image(
                        dxfImageDefinition,
                        new Vector3(ToDxfX(rect.X), ToDxfY(rect.Y +  rect.Height), 0),
                        rect.Width,
                        rect.Height);
                    _doc.AddEntity(dxfImage);
                }
                else
                {
                    if (_state.ImageCache == null || string.IsNullOrEmpty(image.Path))
                        return;

                    var path = System.IO.Path.Combine(_outputPath, System.IO.Path.GetFileName(image.Path));
                    System.IO.File.WriteAllBytes(path, bytes);
                    var dxfImageDefinition = new ImageDef(path);

                    if (_enableImageCache)
                        _biCache[image.Path] = dxfImageDefinition;

                    var dxfImage = new Image(
                        dxfImageDefinition,
                        new Vector3(ToDxfX(rect.X), ToDxfY(rect.Y +  rect.Height), 0),
                        rect.Width,
                        rect.Height);
                    _doc.AddEntity(dxfImage);
                }
            }
        }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
0
        private void WriteImageDef(ImageDef imageDef, string ownerHandle)
        {
            this.chunk.Write(0, imageDef.CodeName);
            this.chunk.Write(5, imageDef.Handle);

            this.chunk.Write(102, "{ACAD_REACTORS");
            this.chunk.Write(330, ownerHandle);
            foreach (ImageDefReactor reactor in imageDef.Reactors.Values)
            {
                this.chunk.Write(330, reactor.Handle);
            }
            this.chunk.Write(102, "}");

            this.chunk.Write(330, ownerHandle);

            this.chunk.Write(100, SubclassMarker.RasterImageDef);
            this.chunk.Write(1, imageDef.FileName);

            this.chunk.Write(10, (double) imageDef.Width);
            this.chunk.Write(20, (double) imageDef.Height);

            // The documentation says that this is the size of one pixel in AutoCAD units, but it seems that this is always the size of one pixel in millimeters
            // this value is used to calculate the image resolution in PPI or PPC, and the default image size.
            double factor = UnitHelper.ConversionFactor((ImageUnits) imageDef.ResolutionUnits, DrawingUnits.Millimeters);
            this.chunk.Write(11, factor/imageDef.HorizontalResolution);
            this.chunk.Write(21, factor/imageDef.VerticalResolution);

            this.chunk.Write(280, (short) 1);
            this.chunk.Write(281, (short) imageDef.ResolutionUnits);
        }
Exemplo n.º 4
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="size">Image <see cref="Vector2">size</see> in world coordinates.</param>
 public Image(ImageDef imageDefinition, Vector3 position, Vector2 size)
     :this(imageDefinition, position, size.X, size.Y)
 {
 }
Exemplo n.º 5
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="Vector2">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, Vector2 position, double width, double height)
     : this(imageDefinition, new Vector3(position.X, position.Y, 0.0), width, height)
 {
 }
Exemplo n.º 6
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="Vector2">position</see> in world coordinates.</param>
 /// <param name="size">Image <see cref="Vector2">size</see> in world coordinates.</param>
 public Image(ImageDef imageDefinition, Vector2 position, Vector2 size)
     : this(imageDefinition, new Vector3(position.X, position.Y, 0.0), size.X, size.Y)
 {
 }
Exemplo n.º 7
0
        private ImageDef ReadImageDefinition()
        {
            string handle = null;
            string ownerHandle = null;
            string fileName = null;
            string name = null;
            double width = 0, height = 0;
            double wPixel = 0.0;
            double hPixel = 0.0;
            ImageResolutionUnits units = ImageResolutionUnits.NoUnits;

            this.chunk.Next();
            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 5:
                        handle = this.chunk.ReadString();
                        this.chunk.Next();
                        break;
                    case 1:
                        fileName = this.chunk.ReadString();
                        this.chunk.Next();
                        break;
                    case 10:
                        width = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 20:
                        height = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 11:
                        wPixel = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 21:
                        hPixel = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 281:
                        units = (ImageResolutionUnits) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 330:
                        ownerHandle = this.chunk.ReadString();
                        this.chunk.Next();
                        break;
                    default:
                        this.chunk.Next();
                        break;
                }
            }

            if (ownerHandle != null)
            {
                DictionaryObject imageDefDict = this.dictionaries[ownerHandle];
                if (handle == null)
                    throw new NullReferenceException("Null handle in ImageDef dictionary.");
                name = imageDefDict.Entries[handle];
            }

            // The documentation says that this is the size of one pixel in AutoCAD units, but it seems that this is always the size of one pixel in millimeters
            // this value is used to calculate the image resolution in PPI or PPC, and the default image size.
            // The documentation in this regard and its relation with the final image size in drawing units is a complete nonsense
            double factor = UnitHelper.ConversionFactor((ImageUnits) units, DrawingUnits.Millimeters);
            ImageDef imageDef = new ImageDef(fileName, name, (int) width, factor/wPixel, (int) height, factor/hPixel, units)
            {
                Handle = handle
            };

            this.imgDefHandles.Add(imageDef.Handle, imageDef);
            return imageDef;
        }
Exemplo n.º 8
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");

        }
Exemplo n.º 9
0
        private static void ImageUsesAndRemove()
        {
            ImageDef imageDef1 = new ImageDef("img\\image01.jpg");
            Image image1 = new Image(imageDef1, Vector3.Zero, 10, 10);

            ImageDef imageDef2 = new ImageDef("img\\image02.jpg");
            Image image2 = new Image(imageDef2, new Vector3(0, 220, 0), 10, 10);
            Image image3 = new Image(imageDef2, image2.Position + new Vector3(280, 0, 0), 10, 10);

            Block block =new Block("MyImageBlock");
            block.Entities.Add(image1);

            Insert insert = new Insert(block);

            DxfDocument dxf = new DxfDocument();
            dxf.AddEntity(insert);
            dxf.AddEntity(image2);
            dxf.AddEntity(image3);
            dxf.Save("test netDxf.dxf");

           
            dxf.RemoveEntity(insert);
            dxf.Blocks.Remove(insert.Block.Name);
            // imageDef1 has no references in the document
            List<DxfObject> uses = dxf.ImageDefinitions.GetReferences(imageDef1.Name);
            dxf.Save("test netDxf with unreferenced imageDef.dxf");
            dxf = DxfDocument.Load("test netDxf with unreferenced imageDef.dxf");

            // once we have removed the insert and then the block that contained image1 we don't have more references to imageDef1
            dxf.ImageDefinitions.Remove(imageDef1.Name);
            dxf.Save("test netDxf with deleted imageDef.dxf");

        }
Exemplo n.º 10
0
        private static void DocumentUnits()
        {
            DxfDocument dxf = new DxfDocument();

            // setting the LUnit variable to engineering or architectural will also set the InsUnits variable to Inches,
            // this need to be this way since AutoCad will show those units in feet and inches and will always consider the drawing base units as inches.
            // You can change again the InsUnits it at your own risk.
            // its main purpose is at the user interface level
            //dxf.DrawingVariables.LUnits = LinearUnitType.Engineering;

            // this is the recommended document unit type
            dxf.DrawingVariables.LUnits = LinearUnitType.Decimal;

            // this is the real important unit,
            // it is used when inserting blocks or images into the drawing as this and the block units will give the scale of the resulting Insert
            dxf.DrawingVariables.InsUnits = DrawingUnits.Millimeters;

            // the angle unit type is purely cosmetic as it has no influence on how the angles are stored in the dxf 
            // its purpose is only at the user interface level
            dxf.DrawingVariables.AUnits = AngleUnitType.Radians;

            // even though we have set the drawing angles in radians the dxf always stores angle data in degrees,
            // this arc goes from 45 to 270 degrees and not radians or whatever the AUnits header variable says.
            Arc arc = new Arc(Vector2.Zero, 5, 45, 270);
            // Remember, at the moment, once the entity has been added to the document is not safe to modify it, changes in some of their properties might generate problems
            dxf.AddEntity(arc);

            // the units of this line will correspond to the ones set in InsUnits
            Line lineM = new Line(new Vector2(-5, -5), new Vector2(5, 5));
            dxf.AddEntity(lineM);

            // All entities added to a block are expressed in the coordinates defined by the block
            // You can set a default unit so all new blocks will use them, the default value is Unitless
            // You might want to use the same units as the drawing, this is just a convenient way to make sure all blocks share the same units 
            BlockRecord.DefaultUnits = dxf.DrawingVariables.InsUnits;

            // In this case the line will be 10 cm long
            Line lineCm = new Line(new Vector2(-5, 0), new Vector2(5, 0));
            Block blockCm = new Block("CmBlock");
            // You can override the default units changing the block.Record.Units value
            blockCm.Record.Units = DrawingUnits.Centimeters;
            blockCm.Entities.Add(lineCm);
            Insert insCm = new Insert(blockCm);

            // In this case the line will be 10 dm long
            Line lineDm = new Line(new Vector2(0, 5), new Vector2(0, -5));
            Block blockDm = new Block("DmBlock");
            blockDm.Record.Units = DrawingUnits.Decimeters;
            // AllowExploding and ScaleUniformy properties will only be recognized by dxf version AutoCad2007 and upwards
            blockDm.Record.AllowExploding = false;
            blockDm.Record.ScaleUniformly = true;
            blockDm.Entities.Add(lineDm);
            blockDm.Entities.Add(insCm);
            Insert insDm = new Insert(blockDm);

            dxf.AddEntity(insDm);

            // the image units are stored in the raster variables units, it is recommended to use the same units as the document to avoid confusions
            dxf.RasterVariables.Units = ImageUnits.Millimeters;
            // Sometimes AutoCad does not like image file relative paths, in any case reloading the references will fix the problem
            ImageDef imgDef = new ImageDef("image.jpg");
            // the resolution units is only used to calculate the image resolution that will return pixels per inch or per centimeter (the use of NoUnits is not recommended).
            imgDef.ResolutionUnits = ImageResolutionUnits.Inches;
            // this image will be 10x10 mm in size
            Image img = new Image(imgDef, Vector3.Zero, 10, 10);
            dxf.AddEntity(img);

            dxf.Save("Document Units.dxf");

            DxfDocument dxfLoad = DxfDocument.Load("Document Units.dxf");

        }