/*Draw Image*/ public static void DrawImage(netDxf.Entities.Image xImg, Canvas mainCanvas) { string local = AppDomain.CurrentDomain.BaseDirectory; System.Windows.Controls.Image wImg = new System.Windows.Controls.Image(); System.Windows.Media.Imaging.BitmapImage bi3 = new System.Windows.Media.Imaging.BitmapImage(); bi3.BeginInit(); /*Uri img_uri = new Uri(xImg.Definition.FileName.Replace(@".\", local));*/ //Uri img_uri; try { //Uri.TryCreate(xImg.Definition.FileName.Replace(@".\", local),System.UriKind.Absolute,out img_uri); Uri img_uri = new Uri(xImg.Definition.Name.Replace(@".\", local)); //new Uri(xImg.Definition.FileName.Replace(@".\", local)); if (img_uri.IsFile == true) { bi3.UriSource = img_uri; /*bi3.UriSource = new Uri(@"C:\Users\Michel\Documents\SharpDevelop Projects\NetDXFViewer\NetDXFViewer\bin\Debug\image.jpg");*/ bi3.EndInit(); wImg.Stretch = Stretch.Fill; wImg.Source = bi3; wImg.Height = xImg.Height; wImg.Width = xImg.Width; Canvas.SetLeft(wImg, xImg.Position.X); Canvas.SetTop(wImg, mainCanvas.Height - xImg.Position.Y - wImg.Height); mainCanvas.Children.Add(wImg); } } catch { Debug.WriteLine("Image introuvable:" + xImg.Definition.Name.ToString()); } //if (Uri.TryCreate(xImg.Definition.FileName.Replace(@".\", local),System.UriKind.Absolute,out img_uri)) //if (Uri.TryCreate(xImg.Definition.FileName,System.UriKind.Relative,out img_uri)) /*if (img_uri.IsFile==true) * { * bi3.UriSource = img_uri; * //bi3.UriSource = new Uri(@"C:\Users\Michel\Documents\SharpDevelop Projects\NetDXFViewer\NetDXFViewer\bin\Debug\image.jpg"); * bi3.EndInit(); * wImg.Stretch = Stretch.Fill; * wImg.Source = bi3; * wImg.Height = xImg.Height; * wImg.Width = xImg.Width; * Canvas.SetLeft(wImg, xImg.Position.X); * Canvas.SetTop(wImg, mainCanvas.Height - xImg.Position.Y - wImg.Height); * mainCanvas.Children.Add(wImg); * }*/ }
private static void ImageClippingBoundary() { ImageDefinition imageDef = new ImageDefinition(@".\img\image02.jpg", "MyImage"); imageDef.ResolutionUnits = ImageResolutionUnits.Centimeters; double width = imageDef.Width / imageDef.HorizontalResolution; double height = imageDef.Height / imageDef.VerticalResolution; Image image = new Image(imageDef, new Vector2(0, 0), width, height); image.Rotation = 30; // the coordinates of the clipping boundary are relative to the image with its actual dimensions and not to the width and height of its definition. // this clipping boundary will only show the middle center of the image. double x = width / 4; double y = height / 4; ClippingBoundary clip = new ClippingBoundary(x, y, 2 * x, 2 * y); image.ClippingBoundary = clip; DxfDocument doc = new DxfDocument(); doc.AddEntity(image); doc.Save("image.dxf"); DxfDocument test = DxfDocument.Load("image.dxf"); test.Save("test.dxf"); }
private static void WriteImage() { ImageDefinition imageDefinition = new ImageDefinition("img\\image01.jpg"); Image image = new Image(imageDefinition, 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 ImageDefinition imageDef2 = new ImageDefinition("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 ClippingBoundary clip = new ClippingBoundary(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("test.dxf"); //dxf.RemoveEntity(image2); //dxf.Save("image2.dxf"); //dxf.RemoveEntity(image3); //dxf.RemoveEntity(image); //dxf.Save("image3.dxf"); }
private static void ImageUsesAndRemove() { ImageDefinition imageDef1 = new ImageDefinition("img\\image01.jpg"); Image image1 = new Image(imageDef1, Vector3.Zero, 10, 10); ImageDefinition imageDef2 = new ImageDefinition("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"); }
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 ImageDefinition imgDefinition = new ImageDefinition("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). imgDefinition.ResolutionUnits = ImageResolutionUnits.Inches; // this image will be 10x10 mm in size Image img = new Image(imgDefinition, Vector3.Zero, 10, 10); dxf.AddEntity(img); dxf.Save("Document Units.dxf"); DxfDocument dxfLoad = DxfDocument.Load("Document Units.dxf"); }
private Image ReadImage() { Vector3 position = Vector3.Zero; Vector3 u = Vector3.Zero; Vector3 v = Vector3.Zero; double width = 0.0, height = 0.0; string imageDefHandle = null; ImageDisplayFlags displayOptions = ImageDisplayFlags.ShowImage | ImageDisplayFlags.ShowImageWhenNotAlignedWithScreen | ImageDisplayFlags.UseClippingBoundary; bool clipping = false; short brightness = 50; short contrast = 50; short fade = 0; double x = 0.0; double y = 0.0; List<Vector2> vertexes = new List<Vector2>(); ClippingBoundaryType boundaryType = ClippingBoundaryType.Rectangular; List<XData> xData = new List<XData>(); this.chunk.Next(); while (this.chunk.Code != 0) { switch (this.chunk.Code) { case 10: position.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 20: position.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 30: position.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 11: u.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 21: u.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 31: u.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 12: v.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 22: v.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 32: v.Z = 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 = (ClippingBoundaryType) this.chunk.ReadShort(); this.chunk.Next(); break; case 91: // we cannot rely in this information it might or might not appear this.chunk.Next(); break; case 14: x = this.chunk.ReadDouble(); this.chunk.Next(); break; case 24: y = this.chunk.ReadDouble(); this.chunk.Next(); vertexes.Add(new Vector2(x, y)); 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 normal = Vector3.CrossProduct(u, v); Vector3 uOCS = MathHelper.Transform(u, normal, CoordinateSystem.World, CoordinateSystem.Object); double rotation = Vector2.Angle(new Vector2(uOCS.X, uOCS.Y))*MathHelper.RadToDeg; double uLength = u.Modulus(); double vLength = v.Modulus(); for (int i = 0; i < vertexes.Count; i++) { double vx = vertexes[i].X * uLength; double vy = vertexes[i].Y * vLength; vertexes[i] = new Vector2(vx, vy); } ClippingBoundary clippingBoundary = boundaryType == ClippingBoundaryType.Rectangular ? new ClippingBoundary(vertexes[0], vertexes[1]) : new ClippingBoundary(vertexes); Image image = new Image { Width = width*uLength, Height = height*vLength, Position = position, Normal = normal, Rotation = rotation, DisplayOptions = displayOptions, Clipping = clipping, Brightness = brightness, Contrast = contrast, Fade = fade, ClippingBoundary = clippingBoundary }; image.XData.AddRange(xData); if (string.IsNullOrEmpty(imageDefHandle) || imageDefHandle == "0") return null; this.imgToImgDefHandles.Add(image, imageDefHandle); return image; }