cart: empty [ Login ]

Highlight particular object and save it to bitmap

1 
LueftungsElite
12/27/2018 5:35 PM
wanna open up a dwg drawing select couple of lines change their color to red for example, then save the vieport to a bitmap with those lines i selected being red and the rest of the drawing black
Wout
12/28/2018 1:37 PM
Hi, The best way to do this is probably to:
  1. Create GDIGraphics3D and enable updating of drawables: GDIGraphics3D.EnableDrawablesUpdate().
  2. Set the GraphicsConfig.FixedForegroundColor and set GDIGraphics3D.GraphicsConfig.
  3. Create drawables.
  4. Change the GraphicsConfig.FixedForegroundColor of GDIGraphics3D.GraphicsConfig.
  5. For those entities that you want to highlight, recreate the drawables: UpdateDrawables(DxfEntity entity).
  6. Set GraphicsConfig.FixedForegroundColor to null again.
  7. Render onto a bitmap.
Here's a demo app:
C# Code:
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using WW.Cad.Base; using WW.Cad.Drawing; using WW.Cad.Drawing.GDI; using WW.Cad.IO; using WW.Cad.Model; using WW.Cad.Model.Entities; using WW.Drawing; using WW.Math; namespace ConsoleApplication { public static class EntityHighlightDemo { public static void DrawingHightlightDemo() { DxfModel model = new DxfModel(); model.Entities.Add(new DxfCircle(EntityColors.Blue, Point3D.Zero, 4d)); model.Entities.Add(new DxfLine(EntityColors.Orange, new Point3D(-2, -2, 0), new Point3D(2, 2, 0))); model.Entities.Add(new DxfLine(EntityColors.Red, new Point3D(-2, 3, 0), new Point3D(2, -3, 0))); GDIGraphics3D graphics = new GDIGraphics3D(); graphics.EnableDrawablesUpdate(); GraphicsConfig graphicsConfig = (GraphicsConfig)GraphicsConfig.AcadLikeWithBlackBackground.Clone(); graphics.Config = graphicsConfig; // Create drawing with specific foreground color. graphicsConfig.FixedForegroundColor = ArgbColors.Yellow; graphics.CreateDrawables(model); // Highlight specific entities. graphicsConfig.FixedForegroundColor = ArgbColors.Cyan; graphics.UpdateDrawables(model.Entities[0]); graphicsConfig.FixedForegroundColor = null; Bounds3D bounds = new Bounds3D(); graphics.BoundingBox(bounds); Matrix4D transform = Matrix4D.Identity; Size maxSize = new Size(800, 800); const int Margin = 2; int maxWidth = maxSize.Width - 2 * Margin; int maxHeight = maxSize.Height - 2 * Margin; // Scale and transform such that its fits max width/height // and the top left corner will be at (0, 0, 0). Matrix4D to2DTransform = DxfUtil.GetScaleTransform( bounds.Corner1, bounds.Corner2, new Point3D(bounds.Corner1.X, bounds.Corner2.Y, 0d), new Point3D(0d, maxHeight, 0d), new Point3D(maxWidth, 0d, 0d), new Point3D(Margin, Margin, 0d) ) * transform; bounds.Reset(); graphics.BoundingBox(bounds, to2DTransform); Image bitmap; SmoothingMode smoothingMode = SmoothingMode.Default; if (bounds.Initialized) { Vector3D delta = bounds.Delta; // Use System.Math.Min to prevent rounding errors to go over the maxSize limit. int width = System.Math.Min(maxSize.Width, (int)System.Math.Ceiling(delta.X) + 2 * Margin); int height = System.Math.Min(maxSize.Height, (int)System.Math.Ceiling(delta.Y) + 2 * Margin); bitmap = ImageExporter.CreateBitmap(graphics, smoothingMode, to2DTransform, graphicsConfig.BackColor, width, height); } else { bitmap = ImageExporter.CreateEmptyBitmap(graphicsConfig.BackColor, maxSize.Width, maxSize.Height); } using (Stream stream = File.Create(@"D:\support\bitmap.jpg")) { ImageExporter.EncodeImageToJpeg(bitmap, stream); } } } }
Note that this approach has its limitations, for example when you try to highlight an entity inside a block, when this block is inserted multiple times, the entity will be highlighted multiple times. There are ways to also do that, but that will get quite a bit more complex. - Wout
Wout
12/28/2018 1:44 PM
Can you change the topic title to something more useful to other users in case they search for something similar? Thanks, - Wout
LueftungsElite
12/28/2018 3:12 PM
I will try it out as soon as I finish other modules of my software and let you know if i have additional question, Thanks Alot, P.S i hope the topic's fine now
LueftungsElite
4/2/2019 2:16 PM
Can you tell me how to create bitmap from the viewport instead of the drawing itself, cause i want to create a bitmap from a zoomed in viewport. Thank You
LueftungsElite
4/3/2019 12:55 PM
if (bounds.Initialized) { Vector3D delta = bounds.Delta; // Use System.Math.Min to prevent rounding errors to go over the maxSize limit. int width = System.Math.Min(maxSize.Width, (int)System.Math.Ceiling(delta.X) + 2 * Margin); int height = System.Math.Min(maxSize.Height, (int)System.Math.Ceiling(delta.Y) + 2 * Margin); bitmap = ImageExporter.CreateBitmap(graphics, smoothingMode, to2DTransform, graphicsConfig.BackColor, width, height); bitmap2 = ImageExporter.CreateViewportConfigurationBitmap(viewControl.Model,viewControl.Name, graphicsConfig, smoothingMode, width, height); } else { bitmap = ImageExporter.CreateEmptyBitmap(graphicsConfig.BackColor, maxSize.Width, maxSize.Height); bitmap2 = ImageExporter.CreateEmptyBitmap(graphicsConfig.BackColor, maxSize.Width, maxSize.Height); } // bitmap2 is returning blank bitmap
LueftungsElite
4/3/2019 3:46 PM
btw my License file is not working, throwing some exception, even though i followed instructions from other posts, + ($exception).InnerException {"babel m1 1355604: System.InvalidOperationException: babel m8 WW.License, WW.License, Version=4.0.0.0, Culture=neutral, PublicKeyToken=87d16b8f7b531b65\r\n at .( )\r\n at .( , BinaryWriter , DynamicILInfo )\r\n at .( , BinaryWriter , DynamicILInfo )\r\n at .( )\r\n at .( ,  )\r\n at .(Int32 )"} System.Exception {System.InvalidOperationException} Thanks,
1