2023年12月11日发(作者:)

java打开dwg_用java和c#实现dwg图片预览

[Java]

public class ViewDWG

{

//C# TO JAVA CONVERTER WARNING: Java does not allow user-defined

value types. The behavior of this class will differ from the

original:

//ORIGINAL LINE: struct BITMAPFILEHEADER

private final static class BITMAPFILEHEADER

{

public short bfType;

public int bfSize;

public short bfReserved1;

public short bfReserved2;

public int bfOffBits;

public BITMAPFILEHEADER clone()

{

BITMAPFILEHEADER varCopy = new BITMAPFILEHEADER();

= ;

= ;

rved1 = rved1;

rved2 = rved2;

its = its;

return varCopy;

}

}

public final Image GetDwgImage(String FileName)

{

if (!((new (FileName)).isFile()))

{

throw new FileNotFoundException("文件没有被找到");

}

FileStream DwgF; //文件流

int PosSentinel; //文件描述块的位置BinaryReader br; //读取二进制文件

int TypePreview; //缩略图格式

int PosBMP; //缩略图位置

int LenBMP; //缩略图大小

short biBitCount; //缩略图比特深度

BITMAPFILEHEADER biH = new BITMAPFILEHEADER();

//BMP文件头,DWG文件中不包含位图文件头,要自行加上去

//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no

direct equivalent in Java:

//ORIGINAL LINE: byte[] BMPInfo;

byte[] BMPInfo; //包含在DWG文件中的BMP文件体

MemoryStream BMPF = new MemoryStream(); //保存位图的内存文件流

BinaryWriter bmpr = new BinaryWriter(BMPF); //写二进制文件类

Image myImg = null;

try

{

DwgF = new FileStream(FileName, , );

//文件流

br = new BinaryReader(DwgF);

(13, ); //从第十三字节开始读取

PosSentinel = t32(); //第13到17字节指示缩略图描述块的位置

(PosSentinel + 30, );

//将指针移到缩略图描述块的第31字节

TypePreview = te(); //第31字节为缩略图格式信息,2 为BMP格式,3为WMF格式

if (TypePreview == 1)

{

}

else if (TypePreview == 2 || TypePreview == 3)

{

PosBMP = t32(); //DWG文件保存的位图所在位置

LenBMP = t32(); //位图的大小

(PosBMP + 14, ); //移动指针到位图块

biBitCount = t16(); //读取比特深度

(PosBMP, ); //从位图块开始处读取全部位图内容备用BMPInfo = tes(LenBMP); //不包含文件头的位图信息

();

();

= 19778; //建立位图文件头

if (biBitCount < 9)

{

= 54 + 4 * (int)((2, biBitCount)) +

LenBMP;

}

else

{

= 54 + LenBMP;

}

rved1 = 0; //保留字节

rved2 = 0; //保留字节

its = 14 + 40 + 1024; //图像数据偏移

//以下开始写入位图文件头

(); //文件类型

(); //文件大小

(rved1); //0

(rved2); //0

(its); //图像数据偏移

(BMPInfo); //写入位图

(0, ); //指针移到文件开始处

myImg = ream(BMPF); //创建位图文件对象

();

();

}

return myImg;

}

catch (RuntimeException ex)

{

throw new RuntimeException(sage());

}}

}

[c#]

最近刚刚接触CAD二次开发,记录下开发的过程。查阅很多资料。

C#读取DWG文件

struct BITMAPFILEHEADER

{

public short bfType;

public int bfSize;

public short bfReserved1;

public short bfReserved2;

public int bfOffBits;

}

public static GetDwgImage(string

FileName)

{

if (!((FileName)))

{

throw new FileNotFoundException("文件没有被找到");

}

FileStream DwgF=null; //文件流

int PosSentinel; //文件描述块的位置

BinaryReader br=null; //读取二进制文件

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); //写二进制文件类

myImg = null;

try

{DwgF = new FileStream(FileName, , );

//文件流

br = new BinaryReader(DwgF);

(13, ); //从第十三字节开始读取

PosSentinel = t32(); //第13到17字节指示缩略图描述块的位置

(PosSentinel + 30, );

//将指针移到缩略图描述块的第31字节

TypePreview = te(); //第31字节为缩略图格式信息,2 为BMP格式,3为WMF格式

if (TypePreview == 1)

{

}

else if (TypePreview == 2 || TypePreview == 3)

{

PosBMP = t32(); //DWG文件保存的位图所在位置

LenBMP = t32(); //位图的大小

(PosBMP + 14, ); //移动指针到位图块

biBitCount = t16(); //读取比特深度

(PosBMP, ); //从位图块开始处读取全部位图内容备用

BMPInfo = tes(LenBMP); //不包含文件头的位图信息

();

();

= 19778; //建立位图文件头

if (biBitCount < 9)

{

= 54 + 4 * (int)((2, biBitCount)) +

LenBMP;

}

else

{

= 54 + LenBMP;

}

rved1 = 0; //保留字节

rved2 = 0; //保留字节

its = 14 + 40 + 1024; //图像数据偏移//以下开始写入位图文件头

(); //文件类型

(); //文件大小

(rved1); //0

(rved2); //0

(its); //图像数据偏移

(BMPInfo); //写入位图

(0, ); //指针移到文件开始处

myImg = ream(BMPF); //创建位图文件对象

();

();

}

return myImg;

}

catch (EndOfStreamException)

{

throw new EndOfStreamException("文件不是标准的DWG格式文件,无法预览!");

}

catch (IOException ex)

{

if (e == "试图将文件指针移到文件开头之前。/r/n")

{

throw new IOException("文件不是标准的DWG格式文件,无法预览!");

}

else if (e == "文件“" + FileName +

"”正由另一进程使用,因此该进程无法访问该文件。")

{

//复制文件,继续预览

(FileName, pPath + @"/",

true);

Image image = GetDwgImage(pPath +

@"/");

(pPath + @"/");

return image;}

else

{

throw new Exception(e);

}

}

catch (Exception ex)

{

throw new Exception(e);

}

finally

{

if (DwgF != null)

{

();

}

if (br != null)

{

();

}

();

();

}

}

读取出来的背景色为白色,效果比较差,很多颜色显示不出来,当时认为显示DWG文件出错误了,问了些高手,(呵呵,别人告诉自己本身

取出的就是白色背景,需要自己改变背景色,在此鄙视一下自己)所以继续用C#操作返回的IMAGE对象,改变背景色

///

///显示DWG文件

///

///

name="Pwidth">要显示的宽度

///

name="PHeight">要显示的高度

///public static ShowDWG(int Pwidth,intPHeight,string FilePath){ image = GetDwgImage(FilePath);Bitmap bitmap = new Bitmap(image);int Height = ;int Width = ;Bitmap newbitmap = new Bitmap(Width, Height);Bitmap oldbitmap = (Bitmap)bitmap;Color pixel;for (int x = 1; x < Width; x++){for (int y = 1; y < Height; y++){pixel = el(x, y);int r = pixel.R, g = pixel.G, b = pixel.B;if ( == "ffffffff" || == "ff000000"){r = 255 - pixel.R;g = 255 - pixel.G;b = 255 - pixel.B;}el(x, y, gb(r, g, b));}}Bitmap bt = new Bitmap(newbitmap, Pwidth, PHeight);return newbitmap;}