码迷,mamicode.com
首页 > Windows程序 > 详细

使用NetApi渲染Cad模型

时间:2016-01-02 20:27:44      阅读:471      评论:0      收藏:0      [点我收藏+]

标签:


原帖:http://through-the-interface.typepad.com/through_the_interface/graphics_system/
一、使用NetApi渲染Cad模型
April 05, 2007
Rendering AutoCAD models offscreen using .NET
This question came up in an internal discussion, and I thought I‘d share the code provided by our Engineering team with you (with a few minor additions from my side, of course).


这个问题出现在一个内部的讨论,我想与你们分享我们的工程团队提供的代码(从我身边只有少许增加,当然)。
The idea is to render a 3D scene off-screen (i.e. save to file the rendered image not visible in the editor). In the below code, we do zoom to the extents of the 3D model that gets created, just to show it‘s there, but the rendering activity itself is not performed by the 3D view that is used to generate the graphics for AutoCAD‘s editor window.


这个想法是为了渲染3 d场景离屏(即保存到文件渲染后的图像编辑器中不可见的)。在以下代码中,我们放大的区段所创建的3 d模型,为了显示它的存在,但呈现活动本身并不是由3 d视图,用于为AutoCAD生成图形的编辑器窗口。
A 3D model does need to be loaded in the editor for this to work, so I decided to add a simple sphere to the modelspace and set its material to one that renders nicely (Sitework.Paving - Surfacing.Riverstone.Mortared). The code doesn‘t import the material programmatically, so you‘ll want to make sure it‘s there in the drawing to get the full effect of the rendering (by dragging the material into the drawing view from the materials tool palette, for instance), or to change the code to point to one that exists in the active drawing.




需要加载一个3 d模型编辑器中为这个工作,所以我决定添加一个简单的球体modelspace和设置它的材料很好地呈现(Sitework。铺平- Surfacing.Riverstone.Mortared)。不进口材料的代码编程,所以你要确保它是在画的完整效果渲染(通过拖动材料从材料到绘图视图工具面板,例如),或改变代码指存在于活动图。


1
using Autodesk.AutoCAD.ApplicationServices; 2 using Autodesk.AutoCAD.DatabaseServices; 3 using Autodesk.AutoCAD.EditorInput; 4 using Autodesk.AutoCAD.Runtime; 5 using Autodesk.AutoCAD.GraphicsSystem; 6 //这个位置需要注意一下,在2013以后这个Interop好像没有啦 7 using System.Drawing; 8 using Autodesk.AutoCAD.Interop; 9 10 namespace OffscreenRendering 11 { 12 public class Commands 13 { 14 [CommandMethod("OSR")] 15 static public void OffscreenRender() 16 { 17 CreateSphere(); 18 //会在这样的一个位置上生成一个材质图 19 RenderToFile("c:\\sphere.png"); 20 } 21 22 static public void CreateSphere() 23 { 24 Document doc = 25 Application.DocumentManager.MdiActiveDocument; 26 Database db = doc.Database; 27 Editor ed = doc.Editor; 28 29 Transaction tr = 30 doc.TransactionManager.StartTransaction(); 31 using (tr) 32 { 33 BlockTable bt = 34 (BlockTable)tr.GetObject( 35 db.BlockTableId, 36 OpenMode.ForRead 37 ); 38 BlockTableRecord btr = 39 (BlockTableRecord)tr.GetObject( 40 bt[BlockTableRecord.ModelSpace], 41 OpenMode.ForWrite 42 ); 43 Solid3d sol = new Solid3d(); 44 sol.CreateSphere(100.0); 45 46 //这个地方要新建一个名字叫Sitework.Paving - Surfacing.Riverstone.Mortared这个的新定义的材质 47 //这个需要处理那个贴图的比例,这个很重要,要不你就出不来kean的结果 48 const string matname = 49 "Sitework.Paving - Surfacing.Riverstone.Mortared"; 50 DBDictionary matdict = 51 (DBDictionary)tr.GetObject( 52 db.MaterialDictionaryId, 53 OpenMode.ForRead 54 ); 55 if (matdict.Contains(matname)) 56 { 57 sol.Material = matname; 58 } 59 else 60 { 61 ed.WriteMessage( 62 "\nMaterial (" + matname + ") not found" + 63 " - sphere will be rendered without it.", 64 matname 65 ); 66 } 67 btr.AppendEntity(sol); 68 69 tr.AddNewlyCreatedDBObject(sol, true); 70 tr.Commit(); 71 } 72 AcadApplication acadApp = 73 (AcadApplication)Application.AcadApplication; 74 acadApp.ZoomExtents(); 75 } 76 77 static public void RenderToFile(string filename) 78 { 79 Document doc = 80 Application.DocumentManager.MdiActiveDocument; 81 Editor ed = doc.Editor; 82 int vpn = 83 System.Convert.ToInt32( 84 Application.GetSystemVariable("CVPORT") 85 ); 86 View gsv = 87 doc.GraphicsManager.GetGsView(vpn, true); 88 using (View view = gsv.Clone(true, true)) 89 { 90 Device dev = 91 doc.GraphicsManager.CreateAutoCADOffScreenDevice(); 92 using (dev) 93 { 94 dev.OnSize(doc.GraphicsManager.DisplaySize); 95 dev.DeviceRenderType = RendererType.FullRender; 96 dev.Add(view); 97 using (Bitmap bitmap = view.RenderToImage()) 98 { 99 bitmap.Save(filename); 100 ed.WriteMessage( 101 "\nRendered image saved to: " + 102 filename 103 ); 104 } 105 } 106 } 107 } 108 } 109 }

And here‘s the resized contents of the file created at c:\sphere.png by the OSR command:

 

 

使用NetApi渲染Cad模型

标签:

原文地址:http://www.cnblogs.com/sinper/p/5095125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!