Teigha4.0加载显示Dwg文件
网上显示Dwg的例子不是很好找,最好还是去官网下载例子。
这里的demo大抵参考官网例子完成的。
初始化驱动
在窗体加载事件里面初始化显示驱动,代码怎么看都很奇怪,但人家就是这种写法
需要注意的是加载模块的名称,官网例子是WinOpenGL.txv、3.0版本的是WinGDI.gs、这里加载WinBitmap.txv模块,这几个到底啥含义没有去深究。
PaintGraphics = Graphics.FromHwnd(panel.Handle);GsModule gsModule = (GsModule)SystemObjects.DynamicLinker.LoadModule("WinBitmap.txv", false, true);Device = gsModule.CreateDevice();Device.Properties.AtPut("WindowHWND", new RxVariant(panel.Handle));Device.Properties.AtPut("WindowHDC", new RxVariant(PaintGraphics.GetHdc()));Device.Properties.AtPut("DoubleBufferEnabled", new RxVariant(true));Device.Properties.AtPut("EnableSoftwareHLR", new RxVariant(true));panel.Resize += Panel_Resize;
加载数据显示
首先打开数据源->创建数据上下文->初始化布局->设置数据显示范围
OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.RestoreDirectory = true;openFileDialog.Filter = "DWG文件(*.dwg)|**.dwg";if (openFileDialog.ShowDialog() == DialogResult.OK){DwgDatabase = new Database(false, false);DwgDatabase.ReadDwgFile(openFileDialog.FileName, FileOpenMode.OpenForReadAndReadShare, false, "");ContextForDbDatabase contextForDbDatabase = new ContextForDbDatabase(DwgDatabase);contextForDbDatabase.UseGsModel = true;LayoutHelper = LayoutHelperDevice.SetupActiveLayoutViews(Device, contextForDbDatabase);LayoutHelper.UserGiContext = contextForDbDatabase;LayoutHelper.SetLogicalPalette(Device.DarkPalette);Rectangle rectangle = panel.Bounds;rectangle.Offset(-panel.Location.X, -panel.Location.Y);LayoutHelper.OnSize(rectangle);panel.Invalidate();}
释放资源
在窗体关闭时需要释放相关的对象实例
if (DwgDatabase != null){DwgDatabase.Dispose();Device.Dispose();LayoutHelper.Dispose();PaintGraphics.Dispose();}
全部代码
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 Teigha.DatabaseServices;
using Teigha.GraphicsInterface;
using Teigha.GraphicsSystem;
using Teigha.Runtime;namespace WindowsFormsApp1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}public Database DwgDatabase { get; set; }public Device Device { get; set; }public LayoutHelperDevice LayoutHelper { get; set; }public Graphics PaintGraphics { get; set; }private void Form1_Load(object sender, EventArgs e){CreateDevice();}private void panel_Paint(object sender, PaintEventArgs e){if (LayoutHelper == null){return;}LayoutHelper.Update();}private void Panel_Resize(object sender, EventArgs e){if (LayoutHelper == null){return;}Rectangle rectangle = panel.Bounds;rectangle.Offset(-panel.Location.X, -panel.Location.Y);LayoutHelper.OnSize(rectangle);}private void 打开ToolStripMenuItem_Click(object sender, EventArgs e){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.RestoreDirectory = true;openFileDialog.Filter = "DWG文件(*.dwg)|**.dwg";if (openFileDialog.ShowDialog() == DialogResult.OK){DwgDatabase = new Database(false, false);DwgDatabase.ReadDwgFile(openFileDialog.FileName, FileOpenMode.OpenForReadAndReadShare, false, "");CreateLayout();}}private void CreateDevice(){PaintGraphics = Graphics.FromHwnd(panel.Handle);GsModule gsModule = (GsModule)SystemObjects.DynamicLinker.LoadModule("WinBitmap.txv", false, true);Device = gsModule.CreateDevice();Device.Properties.AtPut("WindowHWND", new RxVariant(panel.Handle));Device.Properties.AtPut("WindowHDC", new RxVariant(PaintGraphics.GetHdc()));Device.Properties.AtPut("DoubleBufferEnabled", new RxVariant(true));Device.Properties.AtPut("EnableSoftwareHLR", new RxVariant(true));panel.Resize += Panel_Resize;}private void CreateLayout(){ContextForDbDatabase contextForDbDatabase = new ContextForDbDatabase(DwgDatabase);contextForDbDatabase.UseGsModel = true;LayoutHelper = LayoutHelperDevice.SetupActiveLayoutViews(Device, contextForDbDatabase);LayoutHelper.UserGiContext = contextForDbDatabase;LayoutHelper.SetLogicalPalette(Device.DarkPalette);Rectangle rectangle = panel.Bounds;rectangle.Offset(-panel.Location.X, -panel.Location.Y);LayoutHelper.OnSize(rectangle);panel.Invalidate();}private void Form1_FormClosing(object sender, FormClosingEventArgs e){if (DwgDatabase != null){DwgDatabase.Dispose();Device.Dispose();LayoutHelper.Dispose();PaintGraphics.Dispose();}}}
}