Java如何导出不同版本的DXF

现在导出的是默认2013版的,最终需要的是2007版的,如何在导出DXF时转换成
2007版本的类型,不需要再手动由高转低,导出后直接可以用2007版打开。

2个回答

ASCII 格式的DXF文件可以用java利用组码实现,每个部分可进行封装。组码的书写需要注意空格,每行组码结束不能有空格,不然无法解析

1.header实现,getBoundX getBoundY是定义当前画布的左上角XY坐标,getBoundWidth(),getBoundHeight()是画布长宽
private void writeHeader ()
throws IOException
{
writeBufferLine ( " 0" ) ;
writeBufferLine ( "SECTION" ) ;
writeBufferLine ( " 2" ) ;
writeBufferLine ( "HEADER" ) ;

    writeBufferLine ( "  9" ) ;
    writeBufferLine ( "$EXTMIN" ) ;
    writeBufferLine ( " 10" ) ;
    writeBufferLine ( new Double ( getBoundX () ).toString () ) ;
    writeBufferLine ( "20" ) ;
    writeBufferLine ( new Double ( getBoundY () ).toString () ) ;
    writeBufferLine ( "30" ) ;
    writeBufferLine ( "0.0" ) ;
    writeBufferLine ( "  9" ) ;
    writeBufferLine ( "$EXTMAX" ) ;
    writeBufferLine ( " 10" ) ;
    writeBufferLine ( new Double ( convertXToPaper ( getBoundX () + getBoundWidth () ) ).
                      toString () ) ;
    writeBufferLine ( "20" ) ;
    writeBufferLine ( new Double ( convertYToPaper ( getBoundY () + getBoundHeight () ) ).
                      toString () ) ;

    writeBufferLine ( "  9" ) ;
    writeBufferLine ( "$LIMMIN" ) ;
    writeBufferLine ( " 10" ) ;
    writeBufferLine ( new Double ( convertXToPaper ( getBoundX () ) ).toString () ) ;
    writeBufferLine ( "  20" ) ;
    writeBufferLine ( new Double ( convertYToPaper ( getBoundY () ) ).toString () ) ;
    writeBufferLine ( "  9" ) ;
    writeBufferLine ( "$LIMMAX" ) ;
    writeBufferLine ( " 10" ) ;
    writeBufferLine ( new Double ( convertXToPaper ( getBoundX () + getBoundWidth () ) ).
                      toString () ) ;
    writeBufferLine ( "  20" ) ;
    writeBufferLine ( new Double ( convertYToPaper ( getBoundY () + getBoundHeight () ) ).
                      toString () ) ;

    writeBufferLine ( "  9" ) ;
    writeBufferLine ( "$PEXTMIN" ) ;
    writeBufferLine ( " 10" ) ;
    writeBufferLine ( new Double ( convertXToPaper ( getBoundX () ) ).toString () ) ;
    writeBufferLine ( "20" ) ;
    writeBufferLine ( new Double ( convertYToPaper ( getBoundY () ) ).toString () ) ;
    writeBufferLine ( "30" ) ;
    writeBufferLine ( "0.0" ) ;
    writeBufferLine ( "  9" ) ;
    writeBufferLine ( "$PEXTMAX" ) ;
    writeBufferLine ( " 10" ) ;
    writeBufferLine ( new Double ( convertXToPaper ( getBoundX () + getBoundWidth () ) ).
                      toString () ) ;
    writeBufferLine ( "20" ) ;
    writeBufferLine ( new Double ( convertYToPaper( getBoundY () + getBoundHeight () ) ).
                      toString () ) ;
    writeBufferLine ( "30" ) ;
    writeBufferLine ( "0.0" ) ;

    writeBufferLine ( "  0" ) ;
    writeBufferLine ( "ENDSEC" ) ;
}
    2.定义图层和类型
     private void writeTables ()
    throws IOException
{
    writeBufferLine ( "  0" ) ;
    writeBufferLine ( "SECTION" ) ;
    writeBufferLine ( "  2" ) ;
    writeBufferLine ( "TABLES" ) ;

    writeTableLAYER () ;
    writeTableLTYPE () ;

    writeBufferLine ( "  0" ) ;
    writeBufferLine ( "ENDSEC" ) ;
}
    3.具体的资源

 资源一般是点,线,多条线,面,面资源也是无数条线组成,所以就点、线结构

 可对写点,写线,和点线的标注进行封装,写入资源的方式很重要,1种是直接写入ENTITIES中,

 这种写法当用mapinfo转成tab格式时,每个图层可用表展现,但是表里不会带入点、线的属性。

a:写线
private void writePolylineAD ( Point2D[] _pts , int _color ,
String _lineStyle , String _layerName )
throws IOException
{
if ( _pts.length > 2 )
{
for ( int i = 0 ; i <= _pts.length - 1 ; i++ )
{
if ( i < _pts.length - 1 )
{
Point2D[] pts = new Point2D.Double[2 ] ;
pts[ 0 ] = _pts[ i ] ;
pts[ 1 ] = _pts[ i + 1 ] ;
writePolylineAD ( pts , _color , _lineStyle , _layerName ) ;
}
}
return ;
}
writeBufferLine ( " 0" ) ;
writeBufferLine ( "POLYLINE" ) ;
writeBufferLine ( " 8" ) ;
writeBufferLine ( _layerName ) ;
writeBufferLine ( " 62" ) ;
writeBufferLine ( new Integer ( _color ).toString () ) ;
writeBufferLine ( " 6" ) ;
writeBufferLine ( _lineStyle ) ;
writeBufferLine ( " 66" ) ;
writeBufferLine ( " 1" ) ;
writeBufferLine ( " 10" ) ;
writeBufferLine ( "0.0" ) ;
writeBufferLine ( " 20" ) ;
writeBufferLine ( "0.0" ) ;
writeBufferLine ( " 30" ) ;
writeBufferLine ( "0.0" ) ;

    for ( int j = 0 ; j < _pts.length ; j++ )
    {
        writeBufferLine ( "  0" ) ;
        writeBufferLine ( "VERTEX" ) ;
        writeBufferLine ( "  8" ) ;
        writeBufferLine ( _layerName ) ;
        writeBufferLine ( " 10" ) ;
        writeBufferLine ( new Double ( _pts[ j ].getX () ).toString () ) ;
        writeBufferLine ( " 20" ) ;
        writeBufferLine ( new Double ( _pts[ j ].getY () ).toString () ) ;
        writeBufferLine ( " 30" ) ;
        writeBufferLine ( "0.0" ) ;
    }

    writeBufferLine ( "  0" ) ;
    writeBufferLine ( "SEQEND" ) ;
    writeBufferLine ( "  8" ) ;
    writeBufferLine ( _layerName ) ;
}
    b:写标注
      private void writeText ( double _x , double _y , double _fontSize ,
                         int _color , String _text , int Align ,
                         String _layerName , double _angle,double xOffset,double yOffset )
    throws IOException
{
    if(_text.contains("<br>"))
    {
        ArrayList<String> textA=splitBR(_text);
        for(int j=0;j<textA.size();j++)
        {
            double tY;
            tY=_y-j*_fontSize;
            writeText(_x,tY,_fontSize,_color,textA.get(j).toString(),Align,_layerName,_angle,xOffset,yOffset);
        }
    }else
    {
        Rectangle2D rect = new Rectangle2D.Double ( getBoundX () , getBoundY () ,
                getBoundWidth () , getBoundHeight () ) ;
            int out = rect.outcode ( _x , _y ) ;
            if ( out != 0 )
            {
                 return ;
            }

            if ( _text == null || _text.equals ( "" ) )
                return ;
            writeBufferLine ( "  0" ) ;
            writeBufferLine ( "TEXT" ) ;
            writeBufferLine ( "  8" ) ;

// if ( m_bLabelMerge )
// writeBufferLine ( "label" ) ;
// else
writeBufferLine ( _layerName ) ;
writeBufferLine ( " 10" ) ;
writeBufferLine ( new Double ( convertX ( _x ) +xOffset).toString () ) ;
writeBufferLine ( " 20" ) ;
writeBufferLine ( new Double ( convertY ( _y )+yOffset ).toString () ) ;
writeBufferLine ( " 30" ) ;
writeBufferLine ( "0.0" ) ;
writeBufferLine ( " 11" ) ;
writeBufferLine ( new Double ( convertX ( _x ) +xOffset).toString () ) ;
writeBufferLine ( " 21" ) ;
writeBufferLine ( new Double ( convertY ( _y )+yOffset ).toString () ) ;
writeBufferLine ( " 31" ) ;
writeBufferLine ( "0.0" ) ;
writeBufferLine ( " 40" ) ;
writeBufferLine ( new Double ( _fontSize ).toString () ) ;
writeBufferLine ( " 41" ) ;
writeBufferLine ( "0.78" ) ;
writeBufferLine ( " 50" ) ;
writeBufferLine ( new Double ( _angle ).toString () ) ;
if ( false )
{
//文字颜色,暂时不实现
writeBufferLine ( " 62" ) ;
writeBufferLine ( new Integer ( _color ).toString () ) ;
}
writeBufferLine ( " 1" ) ;
//去掉换行符,该版本不支持
_text = _text.replace ( "\r" , "" ) ;
_text = _text.replace ( "\n" , "" ) ;

            writeBufferLine ( _text ) ;
            writeBufferLine ( " 72" ) ;
            writeBufferLine ( new Integer ( Align ).toString () ) ;
            writeBufferLine ( " 73" ) ;
            writeBufferLine ( "3" ) ;
    }

}
      第二种是把点线以BLOCKS包装,属于带属性的导出。

  点属性导出
         private void writeGisPointSingleAtt(IMapVertex v,String layerName,int blockNum,int objNum)
 throws IOException
 {
    writeBufferLine ( "  0" ) ;
    writeBufferLine ( "BLOCK" ) ;
    writeBufferLine ( "  8" ) ;
    writeBufferLine ( layerName ) ;            
    writeBufferLine ( "  2" ) ;
    //组码 block 名称
    writeBufferLine ( "BLKP"+String.valueOf(blockNum)+"p"+String.valueOf(objNum) ) ;
    writeBufferLine ( " 70" ) ;
    writeBufferLine ( "  2" ) ;
    writeBufferLine ( "  10" ) ;
    writeBufferLine ( "0.0" ) ;
    writeBufferLine ( "  20" ) ;
    writeBufferLine ( "0.0" ) ;
    writeBufferLine ( "  30" ) ;
    writeBufferLine ( "0.0" ) ;
    writeBufferLine ( "  0" ) ;
    writeBufferLine ( "POINT" ) ;
    writeBufferLine ( "  8" ) ;
    writeBufferLine ( layerName ) ;
    writeBufferLine ( "  62" ) ;
    writeBufferLine ( "  1" ) ;
    writeBufferLine ( "  10" ) ;
    writeBufferLine ( new Double(v.getX()).toString() ) ;
    writeBufferLine ( "  20" ) ;
    writeBufferLine ( new Double(v.getY()).toString() ) ;
    writeBufferLine ( "  30" ) ;
    writeBufferLine ( "0.0" ) ;
    ArrayList<String> attr=new ArrayList<String>();
    attr.add("Name");
    attr.add("Longitude");
    attr.add("Latitude");
    attr.add("Planid");
    attr.add("IsVirtual");
    attr.add("Isabpoint");
    attr.add("Iskeyres");
    attr.add("Cutpoints");
    writeAttrInVertex(attr,layerName);

    writeBufferLine ( "  0" ) ;
    writeBufferLine ( "ENDBLK" ) ;
 }
    //每个点带属性导出至DXF,便于mapinfo的转换
//20121221
private void writeGisPointWithAtt(ArrayList<IMapVertex> res,ArrayList<String> layerNameRes) 
throws IOException
{
    for(int i=0;i<res.size();i++)
    {
         YVertexReduce v=(YVertexReduce)res.get(i).getID();
          writeBufferLine ( "  0" ) ;
          writeBufferLine ( "BLOCK" ) ;
          writeBufferLine ( "  8" ) ;
          writeBufferLine ( layerNameRes.get(i) ) ;            
          writeBufferLine ( "  2" ) ;
          //组码 block 名称
          writeBufferLine ( "BLK"+String.valueOf(i) ) ;
          writeBufferLine ( " 70" ) ;
          writeBufferLine ( "  2" ) ;
          writeBufferLine ( "  10" ) ;
          writeBufferLine ( "0.0" ) ;
          writeBufferLine ( "  20" ) ;
          writeBufferLine ( "0.0" ) ;
          writeBufferLine ( "  30" ) ;
          writeBufferLine ( "0.0" ) ;
          writeBufferLine ( "  0" ) ;
          writeBufferLine ( "POINT" ) ;
          writeBufferLine ( "  8" ) ;
          writeBufferLine ( layerNameRes.get(i) ) ;
          writeBufferLine ( "  62" ) ;
          writeBufferLine ( "  1" ) ;
          writeBufferLine ( "  10" ) ;
          writeBufferLine ( new Double(v.geoX).toString() ) ;
          writeBufferLine ( "  20" ) ;
          writeBufferLine ( new Double(v.geoY).toString() ) ;
          writeBufferLine ( "  30" ) ;
          writeBufferLine ( "0.0" ) ;
          ArrayList<String> attr=new ArrayList<String>();
          attr.add("Name");
          attr.add("Longitude");
          attr.add("Latitude");
          attr.add("Planid");
          attr.add("IsVirtual");
          attr.add("Isabpoint");
          attr.add("Iskeyres");
          attr.add("Cutpoints");
          writeAttrInVertex(attr,layerNameRes.get(i));

          writeBufferLine ( "  0" ) ;
          writeBufferLine ( "ENDBLK" ) ;
    }
}
     4.结尾
    private void writeTail ()
    throws IOException
{
    writeBufferLine ( "  0" ) ;
    writeBufferLine ( "EOF" ) ;
}

@qq_42835472,你的这个也可以,不过我是直接用的ftl,已经解决了,也谢谢你的回答。- _-

SDFDHY
SDFDHY 回复qq_42835472: 求分享源码
8 个月之前 回复
qq_42835472
qq_42835472 嗯嗯,美食,多探讨
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!