Different rendered images for opened and un-opened DWG files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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