二、模型快照
April 18, 2007
Taking a snapshot of the AutoCAD model (take 2)
In this previous post, we looked at some code to do a programmatic snapshot of AutoCAD's modelspace, saving the results to an image file.
From the discussion that followed, I realised that the code had an undesired (and unnecessary) side-effect of creating a new 3D GS View and leaving the modelspace with that view active. GS Views in AutoCAD 2007 have grey backgrounds by default, and so this change can be quite disturbing for users. The only reason we created the GS View in the first place (if one didn't already exist), was to use it to query the view position/target/up vector/field width and height and apply it to our new view. Thankfully it seems this can also be determined directly from the viewport.
So rather than calling GetGSView() and using the returned view to get that information, we now simply call SetViewFromViewport() specifying the viewport number held in CVPORT, and the graphics system manager for that document handles the rest.
Here's the updated C# code, which appears to achieve the same goals without the side-effect. Check line 124 for the new code, a few extraneous lines around it having been removed:-
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.GraphicsInterface;
- using Autodesk.AutoCAD.GraphicsSystem;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Interop;
- using System.Drawing;
- namespace OffscreenImageCreation
- {
- public class Commands
- {
- [CommandMethod("OSS")]
- static public void OffscreenSnapshot()
- {
- CreateSphere();
- SnapshotToFile(
- "c:\\sphere-Wireframe2D.png",
- VisualStyleType.Wireframe2D
- );
- SnapshotToFile(
- "c:\\sphere-Hidden.png",
- VisualStyleType.Hidden
- );
- SnapshotToFile(
- "c:\\sphere-Basic.png",
- VisualStyleType.Basic
- );
- SnapshotToFile(
- "c:\\sphere-ColorChange.png",
- VisualStyleType.ColorChange
- );
- SnapshotToFile(
- "c:\\sphere-Conceptual.png",
- VisualStyleType.Conceptual
- );
- SnapshotToFile(
- "c:\\sphere-Flat.png",
- VisualStyleType.Flat
- );
- SnapshotToFile(
- "c:\\sphere-Gouraud.png",
- VisualStyleType.Gouraud
- );
- SnapshotToFile(
- "c:\\sphere-Realistic.png",
- VisualStyleType.Realistic
- );
- }
- static public void CreateSphere()
- {
- Document doc =
- Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Transaction tr =
- doc.TransactionManager.StartTransaction();
- using (tr)
- {
- BlockTable bt =
- (BlockTable)tr.GetObject(
- db.BlockTableId,
- OpenMode.ForRead
- );
- BlockTableRecord btr =
- (BlockTableRecord)tr.GetObject(
- bt[BlockTableRecord.ModelSpace],
- OpenMode.ForWrite
- );
- Solid3d sol = new Solid3d();
- sol.CreateSphere(10.0);
- const string matname =
- "Sitework.Paving - Surfacing.Riverstone.Mortared";
- DBDictionary matdict =
- (DBDictionary)tr.GetObject(
- db.MaterialDictionaryId,
- OpenMode.ForRead
- );
- if (matdict.Contains(matname))
- {
- sol.Material = matname;
- }
- else
- {
- ed.WriteMessage(
- "\nMaterial (" + matname + ") not found" +
- " - sphere will be rendered without it.",
- matname
- );
- }
- btr.AppendEntity(sol);
- tr.AddNewlyCreatedDBObject(sol, true);
- tr.Commit();
- }
- AcadApplication acadApp =
- (AcadApplication)Application.AcadApplication;
- acadApp.ZoomExtents();
- }
- static public void SnapshotToFile(
- string filename,
- VisualStyleType vst
- )
- {
- Document doc =
- Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- Manager gsm = doc.GraphicsManager;
- // Get some AutoCAD system variables
- int vpn =
- System.Convert.ToInt32(
- Application.GetSystemVariable("CVPORT")
- );
- using (View view = new View())
- {
- gsm.SetViewFromViewport(view, vpn);
|