python读取cad图纸_DWG⽂件信息的读取在不使⽤任务插件的情况下读取DWG⽂件的缩略图,以便在没有安装AutoCAD的计算机上浏览。
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)))
python怎么读取桌面上的文件
{
throw new FileNotFoundException("⽂件没有被到");
}
FileStream DwgF; //⽂件流
int PosSentinel; //⽂件描述块的位置
BinaryReader br; //读取⼆进制⽂件
int TypePreview; //缩略图格式
int PosBMP;      //缩略图位置
int LenBMP;      //缩略图⼤⼩
short biBitCount; //缩略图⽐特深度
BITMAPFILEHEADER biH; //BMP⽂件头,DWG⽂件中不包含位图⽂件头,要⾃⾏加上去byte[] BMPInfo;      //包含在DWG⽂件中的BMP⽂件体
MemoryStream BMPF = new MemoryStream(); //保存位图的内存⽂件流
BinaryWriter bmpr = new BinaryWriter(BMPF); //写⼆进制⽂件类
Image myImg = null;
try
{
DwgF = new FileStream(FileName, FileMode.Open, FileAccess.Read);  //⽂件流
br = new BinaryReader(DwgF);
DwgF.Seek(13, SeekOrigin.Begin); //从第⼗三字节开始读取
PosSentinel = br.ReadInt32(); //第13到17字节指⽰缩略图描述块的位置
DwgF.Seek(PosSentinel + 30, SeekOrigin.Begin); //将指针移到缩略图描述块的第31字节TypePreview = br.ReadByte(); //第31字节为缩略图格式信息,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); //移动指针到位图块
biBitCount = br.ReadInt16(); //读取⽐特深度
DwgF.Seek(PosBMP, SeekOrigin.Begin); //从位图块开始处读取全部位图内容备⽤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; //保留字节
biH.bfReserved2 = 0; //保留字节
biH.bfOffBits = 14 + 40 + 1024; //图像数据偏移
//以下开始写⼊位图⽂件头
bmpr.Write(biH.bfType); //⽂件类型
bmpr.Write(biH.bfSize); //⽂件⼤⼩
bmpr.Write(biH.bfReserved1); //0
bmpr.Write(biH.bfReserved2); //0
bmpr.Write(biH.bfOffBits); //图像数据偏移
bmpr.Write(BMPInfo); //写⼊位图
BMPF.Seek(0, SeekOrigin.Begin); //指针移到⽂件开始处
myImg = Image.FromStream(BMPF); //创建位图⽂件对象
bmpr.Close();
BMPF.Close();
}
return myImg;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
} } } }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。