Java+AutoCAD-Ycad文件读取

Ycad是一款开源的DXF文件解析的工具包,地址为:http://sourceforge.net/projects/ycad。网站上提供的说明为:Ycad is a library of CAD functions in Java. Currently onlyDXF is supported for reading, viewing and writing. The DXF drawing may berendered to a Graphics object for printing or imaging。

Ycad提供的例子,是一个基于applet展现的方式。虽然提供了源代码,但是缺少说明性文档,并且类之间的关系也没有拓扑图,读起来比较费劲。

本文的解析程序,去掉了applet,以供全程序调用。

1、设定要解析的文件

在com.ysystems.ycad.app.ycadv.Ycadv中,调用main函数启动。Init函数启动线程,读取文件内容。加入了start函数,用于判断解析文件的状态。

public static voidmain(String argv[]){

      String[]filename = newString[1];

      filename[0] = "src=E:/cad/ycad-1.0.2/bin/dat/xingbao4.dxf";

    // Instantiate the Applet(processes argv)

   Ycadv vapplet = new Ycadv(filename);

 

    vapplet.init();

 

    vapplet.start();

}

 

public void init()

    {

       System.out.println("++Ycadv:init|BEG");

 

        // Create drawing

        D = new Yxxf();

 

        // Create I/OHandler

       System.out.println("++Ycadv:runningAsApplet="+runningAsApplet);

 

       D.iohandler = newYutilIOHandler();

 

        // Create emptymain drawing ioname

        D.ioname = newYutilIOHandlerName("main");

 

 

        // Create GetHandler

        gethandler =  new YdxfGetHandler();

 

        // Set props_user -sets I/O name values

       processProperties();

 

        status = 1;//运行状态 0未开始,1运行中,2正常结束,3异常

        gethandler.commandGetStart(D);

       

       System.out.println("++Ycadv:init|END");

}

 

public

    void start()

    {

      while(true){

          if(D.getDrawingComplete()==true){

              status = 2;//运行状态 0未开始,1运行中,2正常结束,3异常

             System.out.println("over............");

              break;

         }

      }

}

2、读入到数据流

将整个文件读入InputStream中。

Public InputStreamopenInputStream(com.ysystems.lib.yutil.YutilIOHandlerNamearg_ioname)

    {

       System.out.println("++YutilIOHandler:==================================================");

       System.out.println("++YutilIOHandler:openInputStream|arg_ioname=[" + arg_ioname + "]");

 

       InputStreamretis = arg_ioname.is =openInputStreamAction(arg_ioname.name,

                                                                 arg_ioname.baseurl,

                                                                 arg_ioname.src, arg_ioname.srcfile,arg_ioname.srcurl);

 

       System.out.println("++YutilIOHandler:==================================================");

        return retis;

    }

3、内容解析

将InputStream,逐段读入,并进行解析。这里要明白各个名称的含义才能进行。其中大部分程序是员原来的程序,去掉了appet的处理。

内容的解析又分别解析了header、tables、blocks、entities部分。

Header部分和blocks部分是整体的,内容比较少。

Tables部分则分为VPORT、LTYPE、LAYER和STYLE。

Entities是我们解析的重点,内容也比较多,也是我们将来要用到的重点内容。

static public void get(YdxfGetBuffer getbfr)

    {

       Yxxf D = getbfr.getDrawing();

 

        getbfr.get();

        while (true)

       {

            if (getbfr.codEquals(0))

           {

                int kwval = getbfr.keywordValue();

 

                if (kwval ==YdxfKeyword.KW_I_EOF)

                   break;

 

                if (kwval ==YdxfKeyword.KW_I_SECTION)

               {

                   getbfr.get();

 

                   kwval = getbfr.keywordValue();

 

                   if (kwval == YdxfKeyword.KW_I_HEADER)

                   {

                       YdxfGetSecHeader.get(getbfr);//读取header信息

                       continue;

                   }

 

                   if (kwval == YdxfKeyword.KW_I_TABLES)

                   {

                       YdxfGetSecTables.get(getbfr);//读取tables信息

 

                       continue;

                   }

 

                   if (kwval == YdxfKeyword.KW_I_BLOCKS)

                   {

 

                       YdxfGetSecBlocks.get(getbfr);//读取blocks信息

                       //getbfr.getDrawing().secBlocks.listBlocks();//输出blocks

 

                       continue;

                   }

 

                   if (kwval == YdxfKeyword.KW_I_ENTITIES)

                   {

                       D.setDrawingReady(true);

 

                       if (getbfr.get_type ==YdxfGetBuffer.GET_TYPE_FONT) // quitif load font drawing

                            break;

                                            

                       YdxfGetSecEntities.get(getbfr);//读取entities信息

 

                       continue;

                   }

               }

           }

            getbfr.get();

       }

 

       

        D.calcResult();

       

        D.setDrawingReady(true);

        D.setDrawingComplete(true);

 

        getbfr.close();

    }

 

Entities解析出来的内容很多,包括直线图元、多行文本图元、圆弧图元等。其中直线图元,打印出来的内容如下:

begpnt=[2113750.8379795915-2810631.1969831306 0.0]//开始节点的坐标xyz

endpnt=[2086641.0393874221-2810631.1969831306 0.0] //结束节点的坐标xyz

thickness=0.0//厚度

xtruDir=[0.00.0 1.0] //拉伸的坐标xyz

4、主要函数说明

(1)com.ysystems.ycad.app.ycadv.Ycadv

main函数启动

init函数初始化

gethandler.commandGetStart(D);开始加载数据

(2)com.ysystems.ycad.lib.ydxf.YdxfGetHandler

从文件读入InputStream

main_D.iohandler.openInputStream(main_D.ioname);

(3)com.ysystems.ycad.lib.ydxf.YdxfGetT

InputStream的处理

Run函数中处理

(4)com.ysystems.ycad.lib.ydxf.YdxfGet

static public void get(YdxfGetBuffer getbfr)函数

解析文件数据流信息

(5)com.ysystems.ycad.lib.yxxf.Yxxf

被观察者,解析CAD文件的信息

(6)com.ysystems.ycad.lib.yxxf.YxxfDrwViewHandler

观察者,监听画板的变动事件,如单击、放大、缩小等。

调用YxxfDrwTilemode1View,启动YxxfDrwTilemode1DrawT的线程,画图形

(7)com.ysystems.ycad.lib.ydxf.YdxfGetSecHeader解析header部分

YdxfGetSecTables解析table部分

    YdxfGetSecBlocks、YdxfGetSecEntities解析blocks、entities部分

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值