Hi everyone,
In order to help dj-nemo on his topic , I have a problem with rendering. The following code will explain how it works:
[CommandMethod("RenderFromFile")] public static void RenderFromFile() { string dwgPath = @"C:\Temp\Render.dwg"; string imagePath = @"C:\Temp\Render.png"; Document doc = Application.DocumentManager.Open(dwgPath); RenderToFile(doc, imagePath); } [CommandMethod("RenderCurrentView")] public static void RenderCurrentView() { string imagePath = @"C:\Temp\Render.png"; Document doc = Application.DocumentManager.MdiActiveDocument; RenderToFile(doc, imagePath); } private static void RenderToFile(Document doc, string imagePath) { try { Database workDb = HostApplicationServices.WorkingDatabase; HostApplicationServices.WorkingDatabase = doc.Database; // Get the identification number of the current viewport int viewportNumber = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT")); // Get AutoCAD's GS view for this viewport GraphicsSystem.View gsv = doc.GraphicsManager.GetGsView(viewportNumber, true); doc.GraphicsManager.SetViewFromViewport(gsv, viewportNumber); // Render the view to an image Bitmap bitmap = gsv.RenderToImage(); // Save the bitmap to an image file bitmap.Save(imagePath); HostApplicationServices.WorkingDatabase = workDb; } catch (Autodesk.AutoCAD.Runtime.Exception ex) { var ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage(ex.Message + "\n" + ex.StackTrace); } }
The command RenderFromFile will render the 3D drawing file at C:\Temp\Render.dwg (see this file from attachment), and then write the output image to Render.png at the same folder. The rendered image is on the top view and has visual materials.
The command RenderCurrentView will render the current open 3D drawing, then write the output image to C:\Temp\Render.png. The rendered image is on 3D view and does not have visual materials.
Both two commands call the same shared method RenderToFile(), but their output images are different. I don't know why they are different?
Method RenderToFile is a simplified version from Kean's code. I try to make the code short and simple to easily solve the problem.
Thanks,
-Khoa
Hi Khoa,
To try and reproduce the behavior, I took the code for the "ReadToFile" from Kean's blog post and used the rest of the code from yours. I then tried it with AutoCAD 2011 with the drawing that you shared and I could not make out the difference as you had mentioned. I have attached a video recording of what I tried. Am I missing some step ?
Here is the code snippet :
[CommandMethod("RenderFromFile", CommandFlags.Session)] public static void RenderFromFile() { string dwgPath = @"C:\Temp\Render.dwg"; string imagePath = @"C:\Temp\Render.png"; Document doc = Application.DocumentManager.Open(dwgPath); RenderToFile(doc, imagePath); } [CommandMethod("RenderCurrentView", CommandFlags.Session)] public static void RenderCurrentView() { string imagePath = @"C:\Temp\Render.png"; RenderToFile(null, imagePath); } static public void RenderToFile(Document doc, string filename) { if(doc == null) doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; int vpn = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT")); View gsv = doc.GraphicsManager.GetGsView(vpn, true); using (View view = gsv.Clone(true, true)) { Device dev = doc.GraphicsManager.CreateAutoCADOffScreenDevice(); using (dev) { dev.OnSize(doc.GraphicsManager.DisplaySize); dev.DeviceRenderType = RendererType.FullRender; dev.Add(view); using (Bitmap bitmap = view.RenderToImage()) { bitmap.Save(filename); ed.WriteMessage("\nRendered image saved to: " + filename); } } } }
The video recording was too large to upload here.
So I have put it in Dropbox. Here is the link :
https://www.dropbox.com/s/f8uche9w3qw416a/Demo.zip