SoFunction
Updated on 2025-03-06

CAD file reading example in c#

This example content is about reading CAD files in C#. Just look at the code

//Read the thumbnail of the DWG file without using the task plugin to browse on computers without AutoCAD installed.

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;


namespace Browsedwg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            ViewDWG viewDwg = new ViewDWG();
             = ("c:\\");
        }
        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 (!((FileName)))
                {
                    throw new FileNotFoundException("The file was not found");
                }
                FileStream DwgF; //File Stream                int PosSentinel; //The location of the file description block                BinaryReader br; //Read binary file                int TypePreview; //Thumbnail format                int PosBMP;       //Thumbnail location                int LenBMP;       //Thumbnail size                short biBitCount; //Thumbnail bit depth                BITMAPFILEHEADER biH; //BMP file header, DWG file does not contain bitmap file header, you must add it yourself                byte[] BMPInfo;       //BMP file body contained in DWG file                MemoryStream BMPF = new MemoryStream(); //Save the bitmap's memory file stream                BinaryWriter bmpr = new BinaryWriter(BMPF); //Write binary file class                Image myImg = null;
                try
                {
                    DwgF = new FileStream(FileName, , );   //File Stream                    br = new BinaryReader(DwgF);
                    (13, ); //Read from the thirteenth byte                    PosSentinel = br.ReadInt32(); //Bytes 13 to 17 indicate the position of the thumbnail description block                    (PosSentinel + 30, ); //Move the pointer to byte 31 of the thumbnail description block                    TypePreview = (); //Byte 31 is thumbnail format information, 2 is BMP format, and 3 is WMF format                    if (TypePreview == 1)
                    {
                    }
                    else if (TypePreview == 2 || TypePreview == 3)
                    {
                        PosBMP = br.ReadInt32(); //The location of the bitmap saved by the DWG file is located                        LenBMP = br.ReadInt32(); //Bitmap size                        (PosBMP + 14, ); //Move the pointer into the bitmap block                        biBitCount = br.ReadInt16(); //Read bit depth                        (PosBMP, ); //Read all bitmap content from the start of the bitmap block for later use                        BMPInfo = (LenBMP); //Do not include file header bitmap information                        ();
                        ();
                         = 19778; //Create a bitmap file header                        if (biBitCount < 9)
                        {
                             = 54 + 4 * (int)((2, biBitCount)) + LenBMP;
                        }
                        else
                        {
                             = 54 + LenBMP;
                        }
                        biH.bfReserved1 = 0; //Reserve bytes                        biH.bfReserved2 = 0; //Reserve bytes                         = 14 + 40 + 1024; //Image data offset                        //The following starts writing to the bitmap file header                        (); //File Type                        (); //File size                        (biH.bfReserved1); //0
                        (biH.bfReserved2); //0
                        (); //Image data offset                        (BMPInfo); //Write to bitmap                        (0, ); //Move the pointer to the beginning of the file                        myImg = (BMPF); //Create a bitmap file object                        ();
                        ();
                    }
                    return myImg;
                }
                catch (Exception ex)
                {
                    throw new Exception();
                }
            }
        }


    }
}

Instance content extension:

Read properties in cad file in C#

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

Database db = new Database(false, true);
try
{
//Read the DWG file into a temporary memory database(fullFileName, , true, null);
//Now enter the database and get the database's block table referenceBlockTable bt = (BlockTable)(, , false, true);
// Obtain block table records from the model space characteristics of the block table. The block table record object contains the DWG file database entityBlockTableRecord btr = (BlockTableRecord)(bt[], , false, true);
foreach (ObjectId btrId in btr)
{
DBObject entBlock = (DBObject)(btrId, , false, true);
if (().() == “INSERT”)
{
BlockReference bRef = (BlockReference)entBlock;
if ( != 0)
{
 bRefEnum = ();
while (())
{
ObjectId aId = (ObjectId);//This sentence is extremely criticalAttributeReference aRef = (AttributeReference)(aId, , false, true);
;//This statement obtains the attribute single-line text. Please add the attribute single-line text variable to be assigned to before this statement.}
}
}
}
(); //Submit transaction processing();
();
}
catch ( ex)
{
(“\nSomething went wrong: ” + );
}
finally
{
();
}

This is the end of this article about CAD file reading examples in C#. For more related C# CAD file reading content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!