用户登录
用户注册

分享至

c#中CAD文件读取介绍

  • 作者: ADAMSIR
  • 来源: 51数据库
  • 2022-09-20
导读 在本篇文章里小编给大家整理的是一篇关于c#中CAD文件读取实例内容,有兴趣的朋友们可以学习参考下。
本篇实例内容是关于C#读取CAD文件的,直接看代码
//在不使用任务插件的情况下读取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)))
                {
                    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);="" }="" }="" }="" }="" }="">
实例内容扩展:
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文件读入到一个临时的内存数据库中
db.ReadDwgFile(fullFileName, System.IO.FileShare.ReadWrite, true, null);
//现在进入数据库并获得数据库的块表引用
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
//从块表的模型空间特性中获得块表记录,块表记录对象包含DWG文件数据库实体
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;//这一句极其关键
AttributeReference aRef = (AttributeReference)trans.GetObject(aId, OpenMode.ForRead, false, true);
aRef.TextString;//此语句即获得属性单行文本,请自行在此语句前添加 属性单行文本 赋于的变量
}
}
}
}
trans.Commit(); //提交事务处理
btr.Dispose();
bt.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show(“\n出错啦: ” + ex.Message);
}
finally
{
db.Dispose();
}

到此这篇关于c#中CAD文件读取实例的文章就介绍到这了

软件
前端设计
程序设计
Java相关