.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Different rendered images for opened and un-opened DWG files

2 REPLIES 2
Reply
Message 1 of 3
khoa.ho
528 Views, 2 Replies

Different rendered images for opened and un-opened DWG files

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

2 REPLIES 2
Message 2 of 3
Balaji_Ram
in reply to: khoa.ho

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);
			}
		}
	}
}

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 3 of 3
Balaji_Ram
in reply to: khoa.ho

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



Balaji
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost