.NET

.NET

Reply
Mentor
khoa.ho
Posts: 209
Registered: ‎09-15-2011
Message 1 of 3 (263 Views)

Different rendered images for opened and un-opened DWG files

263 Views, 2 Replies
09-11-2012 05:55 PM

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

ADN Support Specialist
Balaji_Ram
Posts: 693
Registered: ‎03-21-2011
Message 2 of 3 (206 Views)

Re: Different rendered images for opened and un-opened DWG files

09-20-2012 08:48 AM 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

ADN Support Specialist
Balaji_Ram
Posts: 693
Registered: ‎03-21-2011
Message 3 of 3 (204 Views)

Re: Different rendered images for opened and un-opened DWG files

09-20-2012 09:29 AM 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

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.