// AutoCAD 2009 SP3 x86 Enu
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForms = System.Windows.Forms;
//Autodesk namespaces ***************
using acad = Autodesk.AutoCAD.ApplicationServices.Application;
using AcApp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcRtm = Autodesk.AutoCAD.Runtime;
using AcPub = Autodesk.AutoCAD.Publishing;
using AcPlt = Autodesk.AutoCAD.PlottingServices;
using AcGem = Autodesk.AutoCAD.Geometry;
using AcCol = Autodesk.AutoCAD.Colors;
using AcCMod = Autodesk.AutoCAD.ComponentModel;
using AcGInt = Autodesk.AutoCAD.GraphicsInterface;
using AcGSys = Autodesk.AutoCAD.GraphicsSystem;
using AcLayMng = Autodesk.AutoCAD.LayerManager;
using AcWin = Autodesk.AutoCAD.Windows;
using AcICen = Autodesk.AutoCAD.AcInfoCenterConn;
//************************************
namespace AcadGUI {
public class TestClass {
/// <summary>
/// The "GetScreenAndSnapshot" shows how to call 'RenderOffScreen'
/// and 'GetSnapshot'.
/// NOTE: You must supply at least one 3D Object for it to work.
/// </summary>
[AcRtm.CommandMethod("GetScreenAndSnapshot")]
public void GetScreenAndSnapshot() {
AcApp.Document doc = acad.DocumentManager.MdiActiveDocument;
// get the viewport number from the cvport variable
Int16 nViewportNumber = (Int16)acad.GetSystemVariable("CVPORT");
// construct a gsview from the cvport window number
AcGSys.View oGsView = doc.GraphicsManager.GetGsView(nViewportNumber, true);
using (AcGSys.View view = oGsView.Clone(true, true)) {
// create the offscreen device to render to
using (AcGSys.Device dev = doc.GraphicsManager.CreateAutoCADOffScreenDevice()) {
// resize the same as the cvport window size
dev.OnSize(doc.GraphicsManager.DisplaySize);
// set to full render
dev.DeviceRenderType = AcGSys.RendererType.FullRender;
// add the view to the device
dev.Add(view);
// now create the bitmaps:
// Get snapshot:
// ExternalException: A generic error occurred in GDI+.
using (System.Drawing.Bitmap bitmap = view.GetSnapshot(
new System.Drawing.Rectangle(0,
0,
(Int32
)view
.FieldWidth,
(Int32
)view
.FieldHeight))) { bitmap.Save(@"c:\temp\snapshot.bmp");
}
// Get screen:
// ExternalException: A generic error occurred in GDI+.
using (System.Drawing.Bitmap bitmap = view.RenderToImage()) {
bitmap.Save(@"c:\temp\test.bmp");
}
}
}
// all clean up is handled automatically this way
}
/// <summary>
/// The next "GetScreen2" shows how to use GetSnapShot, notice the code is
/// implemented slightly differently (mirrors more the ObjectARX way of doing it).
/// </summary>
[AcRtm.CommandMethod("GetScreen2")]
public void GetScreen2() {
using (AcDb
.Database newDb
= new AcDb
.Database(true,
true)) { using (AcDb.Transaction tr = newDb.TransactionManager.StartTransaction()) {
using (AcDb
.BlockTableRecord TestBlock
= new AcDb
.BlockTableRecord()) { using (AcDb.BlockTable PBlockTable = tr.GetObject(newDb.BlockTableId,
AcDb.OpenMode.ForWrite) as AcDb.BlockTable) {
using (AcDb.BlockTableRecord modspace = tr.GetObject(PBlockTable[
AcDb.BlockTableRecord.ModelSpace],
AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord) {
TestBlock.Name = "Testblock";
AcDb
.DBText testtext
= new AcDb
.DBText(); testtext
.Position = new AcGem
.Point3d(0,
0,
0); testtext.TextString = "ABC-abc";
testtext.Height = 50;
modspace.AppendEntity(testtext);
tr.AddNewlyCreatedDBObject(testtext, true);
tr.Commit();
}
}
}
}
// get the active document
AcApp.Document document = acad.DocumentManager.MdiActiveDocument;
// get the current viewport number
Int16 cvPort = (Int16)acad.GetSystemVariable("CVPORT");
// next get the graphics system manager so we can access the off screen device functionality
using (AcGSys.Manager gsManager = document.GraphicsManager) {
// create an offscreen device
using (AcGSys.Device offDevice = gsManager.CreateAutoCADOffScreenDevice()) {
// now set the size of the device
offDevice.OnSize(document.GraphicsManager.DisplaySize);
// now create a new view
using (AcGSys
.View view
= new AcGSys
.View()) { // set the mode of the view to just a normal wire frame
view.Mode = Autodesk.AutoCAD.GraphicsSystem.RenderMode.Wireframe;
offDevice.DeviceRenderType = AcGSys.RendererType.Default;
// add the empty view to the device
offDevice.Add(view);
offDevice.Update();
// now lets create a model to put into the view
using (AcGSys.Model model = gsManager.CreateAutoCADModel()) {
try {
using (AcDb.Transaction trans = newDb.TransactionManager.StartTransaction()) {
// just add the whole modelspace to the model view
AcDb.BlockTableRecord btr = trans.GetObject(newDb.CurrentSpaceId,
AcDb.OpenMode.ForRead) as AcDb.BlockTableRecord;
view.Add(btr, model);
}
}
catch {
}
// just zoom out a bit
AcGem
.Point3d extmin
= new AcGem
.Point3d(-100,
-100,
0); AcGem
.Point3d extmax
= new AcGem
.Point3d(100,
100,
0); // now zoom the view to the extents of the drawing
view.ZoomExtents(extmin, extmax);
System.Drawing.Rectangle rect = view.Viewport;
// no render it
// ExternalException: A generic error occurred in GDI+.
System.Drawing.Bitmap bitmap = view.GetSnapshot(rect);
// finally save
bitmap.Save(@"c:\temp\test2.bmp");
// we must clean up a little otherwise AutoCAD may become unstable
view.EraseAll();
offDevice.Erase(view);
}
}
}
}
}
}
}
}