使用C#三维绘图控件快速搭建DXF查看程序
本例使用AnyCAD .Net三维图形控件快速实现一个DXF文件的读取、显示、导出JPG、PNG、PDF的应用。
代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AnyCAD.Platform; namespace DxfViewer { public partial class MainForm : Form { private AnyCAD.Presentation.RenderWindow3d renderView = null; public MainForm() { InitializeComponent(); this.renderView = new AnyCAD.Presentation.RenderWindow3d(); this.renderView.Location = new System.Drawing.Point(0, 27); this.renderView.Size = this.Size; this.renderView.TabIndex = 1; this.Controls.Add(this.renderView); } private void MainForm_Load(object sender, EventArgs e) { this.renderView.View3d.ShowCoordinateAxis(true); this.renderView.View3d.ShowWorkingGrid(false); renderView.ExecuteCommand("ShadeWithEdgeMode"); renderView.ExecuteCommand("TopView");
//设置下背景色,像AutoCAD那样 ColorValue clr = new ColorValue(33f / 255f, 40f / 255f, 48f / 255f, 1); renderView.SetBackgroundColor(clr, clr, clr); this.renderView.RequestDraw(); } private void MainForm_Resize(object sender, EventArgs e) { if (renderView != null) renderView.Size = this.Size; } private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "DXF (*.dxf)|*.dxf"; if (DialogResult.OK == dlg.ShowDialog()) { AnyCAD.Exchange.DxfReader reader = new AnyCAD.Exchange.DxfReader(); if (reader.Read(dlg.FileName, new AnyCAD.Exchange.ShowShapeReaderContext(renderView.SceneManager))) renderView.RequestDraw(); } renderView.View3d.FitAll(); } private void pDFToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "PDF (*.pdf)|*.pdf"; if (DialogResult.OK == dlg.ShowDialog()) { renderView.View3d.Print(dlg.FileName); } } private void imageToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Image File (*.jpg;*.png)|*.jpg;*.png"; if (DialogResult.OK == dlg.ShowDialog()) { renderView.View3d.GetRenderWindow().CaptureImage(dlg.FileName); } } private void dToolStripMenuItem_Click(object sender, EventArgs e) { renderView.ExecuteCommand("IsoView"); renderView.View3d.FitAll(); } } }
效果: