c#中CAD文件讀取實(shí)例
本篇實(shí)例內(nèi)容是關(guān)于C#讀取CAD文件的,直接看代碼
//在不使用任務(wù)插件的情況下讀取DWG文件的縮略圖,以便在沒有安裝AutoCAD的計(jì)算機(jī)上瀏覽。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 瀏覽dwg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ViewDWG viewDwg = new ViewDWG();
pictureBox1.Image = viewDwg.GetDwgImage("c:\\1.dwg");
}
class ViewDWG
{
struct BITMAPFILEHEADER
{
public short bfType;
public int bfSize;
public short bfReserved1;
public short bfReserved2;
public int bfOffBits;
}
public Image GetDwgImage(string FileName)
{
if (!(File.Exists(FileName)))
{
throw new FileNotFoundException("文件沒有被找到");
}
FileStream DwgF; //文件流
int PosSentinel; //文件描述塊的位置
BinaryReader br; //讀取二進(jìn)制文件
int TypePreview; //縮略圖格式
int PosBMP; //縮略圖位置
int LenBMP; //縮略圖大小
short biBitCount; //縮略圖比特深度
BITMAPFILEHEADER biH; //BMP文件頭,DWG文件中不包含位圖文件頭,要自行加上去
byte[] BMPInfo; //包含在DWG文件中的BMP文件體
MemoryStream BMPF = new MemoryStream(); //保存位圖的內(nèi)存文件流
BinaryWriter bmpr = new BinaryWriter(BMPF); //寫二進(jìn)制文件類
Image myImg = null;
try
{
DwgF = new FileStream(FileName, FileMode.Open, FileAccess.Read); //文件流
br = new BinaryReader(DwgF);
DwgF.Seek(13, SeekOrigin.Begin); //從第十三字節(jié)開始讀取
PosSentinel = br.ReadInt32(); //第13到17字節(jié)指示縮略圖描述塊的位置
DwgF.Seek(PosSentinel + 30, SeekOrigin.Begin); //將指針移到縮略圖描述塊的第31字節(jié)
TypePreview = br.ReadByte(); //第31字節(jié)為縮略圖格式信息,2 為BMP格式,3為WMF格式
if (TypePreview == 1)
{
}
else if (TypePreview == 2 || TypePreview == 3)
{
PosBMP = br.ReadInt32(); //DWG文件保存的位圖所在位置
LenBMP = br.ReadInt32(); //位圖的大小
DwgF.Seek(PosBMP + 14, SeekOrigin.Begin); //移動(dòng)指針到位圖塊
biBitCount = br.ReadInt16(); //讀取比特深度
DwgF.Seek(PosBMP, SeekOrigin.Begin); //從位圖塊開始處讀取全部位圖內(nèi)容備用
BMPInfo = br.ReadBytes(LenBMP); //不包含文件頭的位圖信息
br.Close();
DwgF.Close();
biH.bfType = 19778; //建立位圖文件頭
if (biBitCount < 9)
{
biH.bfSize = 54 + 4 * (int)(Math.Pow(2, biBitCount)) + LenBMP;
}
else
{
biH.bfSize = 54 + LenBMP;
}
biH.bfReserved1 = 0; //保留字節(jié)
biH.bfReserved2 = 0; //保留字節(jié)
biH.bfOffBits = 14 + 40 + 1024; //圖像數(shù)據(jù)偏移
//以下開始寫入位圖文件頭
bmpr.Write(biH.bfType); //文件類型
bmpr.Write(biH.bfSize); //文件大小
bmpr.Write(biH.bfReserved1); //0
bmpr.Write(biH.bfReserved2); //0
bmpr.Write(biH.bfOffBits); //圖像數(shù)據(jù)偏移
bmpr.Write(BMPInfo); //寫入位圖
BMPF.Seek(0, SeekOrigin.Begin); //指針移到文件開始處
myImg = Image.FromStream(BMPF); //創(chuàng)建位圖文件對(duì)象
bmpr.Close();
BMPF.Close();
}
return myImg;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
}
實(shí)例內(nèi)容擴(kuò)展:
C#中讀取cad文件中的屬性
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
Database db = new Database(false, true);
try
{
//把DWG文件讀入到一個(gè)臨時(shí)的內(nèi)存數(shù)據(jù)庫中
db.ReadDwgFile(fullFileName, System.IO.FileShare.ReadWrite, true, null);
//現(xiàn)在進(jìn)入數(shù)據(jù)庫并獲得數(shù)據(jù)庫的塊表引用
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
//從塊表的模型空間特性中獲得塊表記錄,塊表記錄對(duì)象包含DWG文件數(shù)據(jù)庫實(shí)體
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false, true);
foreach (ObjectId btrId in btr)
{
DBObject entBlock = (DBObject)trans.GetObject(btrId, OpenMode.ForRead, false, true);
if (entBlock.GetRXClass().DxfName.ToUpper() == “INSERT”)
{
BlockReference bRef = (BlockReference)entBlock;
if (bRef.AttributeCollection.Count != 0)
{
System.Collections.IEnumerator bRefEnum = bRef.AttributeCollection.GetEnumerator();
while (bRefEnum.MoveNext())
{
ObjectId aId = (ObjectId)bRefEnum.Current;//這一句極其關(guān)鍵
AttributeReference aRef = (AttributeReference)trans.GetObject(aId, OpenMode.ForRead, false, true);
aRef.TextString;//此語句即獲得屬性單行文本,請(qǐng)自行在此語句前添加 屬性單行文本 賦于的變量
}
}
}
}
trans.Commit(); //提交事務(wù)處理
btr.Dispose();
bt.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show(“\n出錯(cuò)啦: ” + ex.Message);
}
finally
{
db.Dispose();
}
到此這篇關(guān)于c#中CAD文件讀取實(shí)例的文章就介紹到這了,更多相關(guān)c# CAD文件讀取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實(shí)例詳解
這篇文章主要介紹了C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實(shí)例以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-08-08
C#實(shí)現(xiàn)String字符串轉(zhuǎn)化為SQL語句中的In后接的參數(shù)詳解
在本篇文章中小編給大家分享的是一篇關(guān)于C#實(shí)現(xiàn)String字符串轉(zhuǎn)化為SQL語句中的In后接的實(shí)例內(nèi)容和代碼,需要的朋友們參考下。2020-01-01
基于WPF實(shí)現(xiàn)3D畫廊動(dòng)畫效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)簡單的3D畫廊動(dòng)畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
C#使用SQL DataReader訪問數(shù)據(jù)的優(yōu)點(diǎn)和實(shí)例
今天小編就為大家分享一篇關(guān)于C#使用SQL DataReader訪問數(shù)據(jù)的優(yōu)點(diǎn)和實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10

