用java读DXF文件(ReadWorldmap.java)源码
不知道有没有用?
//ReadWorldmap.java
//读取世界图对象
import java.io.*;
import java.util.*;
import java.net.*;
public class ReadWorldmap
{
private URL url;
private String FileName;
private InputStream inStream;
private InputStreamReader inStreamReader;
private BufferedReader bReader;
private String temp,temp1,temp2,temp3;
private int line;
public Worldmap worldmap=new Worldmap();
//构造函数,context是索引文件所在的URL,spec是世界图名
public ReadWorldmap(URL context,String SetName,String spec)
{
line=0;
this.url=context;
FileName=spec+".NDX";
try
{
//inStream=(new URL(url,"//"+SetName+"//"+spec+"//"+FileName)).openStream();
inStream=(new URL(url,SetName+"/"+spec+"/"+FileName)).openStream();
inStreamReader=new InputStreamReader(inStream);
bReader=new BufferedReader(inStreamReader);
// System.out.println("
Begin read file "+FileName);
}
catch(IOException e)
{
// System.out.println("Connection was not found."+"Can not read file "+FileName);
}
}
//****************************************************************************
private void error_report()
{
//System.out.println(" :< error appear at line "+line+"in file "+FileName);
}
//世界图初始化函数,返回一个世界图对象,在这个世界图对象中,仅包含世界图的基本属性,
//如坐标、放大倍数、层的信息等,而不包含具体的实体信息。
public Worldmap initialise()
{
boolean mainFlag=true;
double wmy1=0,wmy2=100; //临时变量
while(mainFlag)
{
try
{
line++;
temp=new String(bReader.readLine());
}
catch(IOException e) { error_report();}
temp=temp.trim();
StringTokenizer st=new StringTokenizer(temp,"<>,");
temp1=st.nextToken();
if(temp1.equals("NAME"))
{
temp2=st.nextToken();
temp2=temp2.trim();
worldmap._worldmap_name =temp2;
continue;
}
if(temp1.equals("DEC_COUNT"))
{
temp2=st.nextToken();
temp2=temp2.trim();
//worldmap.dec_count=Integer.valueOf(temp2).intValue();
continue;
}
if(temp1.equals("BEGIN_WC"))
{
temp2=st.nextToken();
temp2=temp2.trim();
temp3=st.nextToken();
temp3=temp3.trim();
worldmap._begin_world_coordinate_x=Double.valueOf(temp2).doubleValue();
// worldmap._begin_world_coordinate_y=Double.valueOf(temp3).doubleValue();
wmy2=Double.valueOf(temp3).doubleValue();
continue;
}
if(temp1.equals("END_WC"))
{
temp2=st.nextToken();
temp2=temp2.trim();
temp3=st.nextToken();
temp3=temp3.trim();
worldmap._end_world_coordinate_x=Double.valueOf(temp2).doubleValue();
// worldmap._end_world_coordinate_y=Double.valueOf(temp3).doubleValue();
wmy1=Double.valueOf(temp3).doubleValue();
continue;
}
if(temp1.equals("DECLUTTER"))
{
boolean dec_Flag=true; //标志变量
Declutter tempDeclutter=new Declutter();
double decy1=0,decy2=100; //临时变量
while(dec_Flag)
{
try
{
temp=new String(bReader.readLine());
}
catch(IOException e) { error_report(); }
temp=temp.trim();
StringTokenizer st1=new StringTokenizer(temp,"<>,");
temp1=st1.nextToken();
temp1=temp1.trim();
if(temp1.equals("NO"))
{
temp2=st1.nextToken();
temp2=temp2.trim();
// tempDeclutter.index=temp2;
continue;
}
if( temp1.equals("NAME"))
{
temp2=st1.nextToken();
temp2=temp2.trim();
tempDeclutter._declutter_name=temp2;
continue;
}
if(temp1.equals("SEG_COUNT"))
{
temp2=st1.nextToken();
temp2=temp2.trim();
// tempDeclutter.segment_number=Integer.valueOf(temp2).intValue();
continue;
}
if(temp1.equals("BEGIN_WC"))
{
temp2=st1.nextToken();
temp2=temp2.trim();
temp3=st1.nextToken();
temp3=temp3.trim();
tempDeclutter._begin_world_coordinate_x=Double.valueOf(temp2).doubleValue();
// tempDeclutter._begin_world_coordinate_y=Double.valueOf(temp3).doubleValue();
decy2=Double.valueOf(temp3).doubleValue();
continue;
}
if(temp1.equals("END_WC"))
{
temp2=st1.nextToken();
temp2=temp2.trim();
temp3=st1.nextToken();
temp3=temp3.trim();
tempDeclutter._end_world_coordinate_x=Double.valueOf(temp2).doubleValue();
// tempDeclutter._end_world_coordinate_y=Double.valueOf(temp3).doubleValue();
decy1=Double.valueOf(temp3).doubleValue();
continue;
}
if(temp1.equals("MAX_ZOOM"))
{
temp2=st1.nextToken();
temp2=temp2.trim();
tempDeclutter._display_zoomfactor_max=Double.valueOf(temp2).doubleValue();
continue;
}
if(temp1.equals("MIN_ZOOM"))
{
temp2=st1.nextToken();
temp2=temp2.trim();
tempDeclutter._display_zoomfactor_min=Double.valueOf(temp2).doubleValue()*1.1;
continue;
}
if(temp1.equals("SEGMENT"))
{
boolean seg_Flag=true;
Segment tempSegment=new Segment();
double segy1=0,segy2=100; //临时变量
while(seg_Flag)
{
try
{
temp=new String(bReader.readLine());
}
catch(IOException e) { error_report();}
temp=temp.trim();
StringTokenizer s_st=new StringTokenizer(temp,"<>,");
temp1=s_st.nextToken();
temp1=temp1.trim();
if(temp1.equals("NO"))
{
temp2=s_st.nextToken();
temp2=temp2.trim();
// tempSegment.index=Integer.valueOf(temp2).intValue();
continue;
}
if( temp1.equals("NAME"))
{
temp2=s_st.nextToken();
temp2=temp2.trim();
tempSegment._segment_name=temp2;
continue;
}
if(temp1.equals("BEGIN_WC"))
{
temp2=s_st.nextToken();
temp2=temp2.trim();
temp3=s_st.nextToken();
temp3=temp3.trim();
tempSegment._begin_world_coordinate_x=Double.valueOf(temp2).doubleValue();
// tempSegment._begin_world_coordinate_y=Double.valueOf(temp3).doubleValue();
segy2=Double.valueOf(temp3).doubleValue();
continue;
}
if(temp1.equals("END_WC"))
{
temp2=s_st.nextToken();
temp2=temp2.trim();
temp3=s_st.nextToken();
temp3=temp3.trim();
tempSegment._end_world_coordinate_x=Double.valueOf(temp2).doubleValue();
// tempSegment._end_world_coordinate_y=Double.valueOf(temp3).doubleValue();
segy1=Double.valueOf(temp3).doubleValue();
continue;
}
if(temp1.equals("MAX_ZOOM"))
{
temp2=s_st.nextToken();
temp2=temp2.trim();
tempSegment._display_zoomfactor_max=Double.valueOf(temp2).doubleValue();
continue;
}
if(temp1.equals("MIN_ZOOM"))
{
temp2=s_st.nextToken();
temp2=temp2.trim();
tempSegment._display_zoomfactor_min=Double.valueOf(temp2).doubleValue()*1.1;
continue;
}
if(temp1.equals("/SEGMENT"))
{
seg_Flag=false;
tempSegment._begin_world_coordinate_y=segy1;
tempSegment._end_world_coordinate_y=segy2;
tempDeclutter.add(tempSegment);
}
}
continue;
}
if(temp1.equals("/DECLUTTER"))
{
dec_Flag=false;
tempDeclutter._begin_world_coordinate_y =decy1;
tempDeclutter._end_world_coordinate_y =decy2;
worldmap.add(tempDeclutter);
continue;
}
}
continue;
}
if(temp1.equals("/WORLDMAP"))
{
mainFlag=false;
worldmap._begin_world_coordinate_y =wmy1;
worldmap._end_world_coordinate_y =wmy2;
continue;
}
}
this.stop();
return worldmap;
}
//读层函数
/* public void fillDeclutter(int vector_index) throws NotFoundException
{
ReadSegment rp;
Declutter dec;
Segment seg;
int dec_index=vector_index;
String filename=new String();
dec=worldmap.getDeclutterByIndex(dec_index);
for(int i=0;i {
seg=dec.getSegmentByIndex(i);
filename=worldmap._worldmap_name +"_"+dec._declutter_name +"_"+seg._segment_name +".dxf";
rp=new ReadSegment(url,seg,filename);
rp.execute();
}
} */
//私有函数。关闭所有打开的流,在initialise()函数结束时调用。
private void stop()
{
try
{
inStream.close();
inStreamReader.close();
bReader.close();
//System.out.println("
End read file "+FileName);
}
catch(IOException e) {}
}
}