OutputStreamWriter out=null;
try {
out = new OutputStreamWriter(new FileOutputStream(destinationFile),"UTF-8");
MachineConfiguration mc = MachineConfiguration.getSingleton();
DrawingTool tool = mc.GetCurrentTool();
out.write(mc.GetConfigLine()+";\n");
out.write(mc.GetBobbinLine()+";\n");
out.write("G00 G90;\n");
tool.WriteChangeTo(out);
tool.WriteOff(out);
parser.parse(srcFile, DXFParser.DEFAULT_ENCODING);
DXFDocument doc = parser.getDocument();
Bounds b = doc.getBounds();
double width = b.getMaximumX() - b.getMinimumX();
double height = b.getMaximumY() - b.getMinimumY();
double cx = ( b.getMaximumX() + b.getMinimumX() ) / 2.0f;
double cy = ( b.getMaximumY() + b.getMinimumY() ) / 2.0f;
double sy = mc.GetPaperHeight()*10/height;
double sx = mc.GetPaperWidth()*10/width;
double scale = (sx<sy? sx:sy ) * mc.paper_margin;
sx = scale * (MachineConfiguration.getSingleton().reverseForGlass? -1 : 1);
sy = scale;
// count all entities in all layers
Iterator<DXFLayer> layer_iter = (Iterator<DXFLayer>)doc.getDXFLayerIterator();
int entity_total=0;
int entity_count=0;
while(layer_iter.hasNext()) {
DXFLayer layer = (DXFLayer)layer_iter.next();
Log("<font color='yellow'>Found layer "+layer.getName()+"</font>\n");
Iterator<String> entity_iter = (Iterator<String>)layer.getDXFEntityTypeIterator();
while(entity_iter.hasNext()) {
String entity_type = (String)entity_iter.next();
List<DXFEntity> entity_list = (List<DXFEntity>)layer.getDXFEntities(entity_type);
Log("<font color='yellow'>+ Found "+entity_list.size()+" of type "+entity_type+"</font>\n");
entity_total+=entity_list.size();
}
}
// set the progress meter
pm.setMinimum(0);
pm.setMaximum(entity_total);
// convert each entity
layer_iter = doc.getDXFLayerIterator();
while(layer_iter.hasNext()) {
DXFLayer layer = (DXFLayer)layer_iter.next();
Iterator<String> entity_type_iter = (Iterator<String>)layer.getDXFEntityTypeIterator();
while(entity_type_iter.hasNext()) {
String entity_type = (String)entity_type_iter.next();
List<DXFEntity> entity_list = layer.getDXFEntities(entity_type);
if(entity_type.equals(DXFConstants.ENTITY_TYPE_LINE)) {
for(int i=0;i<entity_list.size();++i) {
pm.setProgress(entity_count++);
DXFLine entity = (DXFLine)entity_list.get(i);
Point start = entity.getStartPoint();
Point end = entity.getEndPoint();
double x=(start.getX()-cx)*sx;
double y=(start.getY()-cy)*sy;
double x2=(end.getX()-cx)*sx;
double y2=(end.getY()-cy)*sy;
// is it worth drawing this line?
double dx = x2-x;
double dy = y2-y;
if(dx*dx+dy*dy < tool.GetDiameter()/2.0) {
continue;
}
dx = dxf_x2 - x;
dy = dxf_y2 - y;
if(dx*dx+dy*dy > tool.GetDiameter()/2.0) {
if(tool.DrawIsOn()) {
tool.WriteOff(out);
}
tool.WriteMoveTo(out, (float)x,(float)y);
}
if(tool.DrawIsOff()) {
tool.WriteOn(out);
}
tool.WriteMoveTo(out, (float)x2,(float)y2);
dxf_x2=x2;
dxf_y2=y2;
}
} else if(entity_type.equals(DXFConstants.ENTITY_TYPE_SPLINE)) {
for(int i=0;i<entity_list.size();++i) {
pm.setProgress(entity_count++);
DXFSpline entity = (DXFSpline)entity_list.get(i);
DXFPolyline polyLine = DXFSplineConverter.toDXFPolyline(entity,30);
boolean first=true;
for(int j=0;j<polyLine.getVertexCount();++j) {
DXFVertex v = polyLine.getVertex(j);
double x = (v.getX()-cx)*sx;
double y = (v.getY()-cy)*sy;
double dx = dxf_x2 - x;
double dy = dxf_y2 - y;
if(first==true) {
first=false;
if(dx*dx+dy*dy > tool.GetDiameter()/2.0) {
// line does not start at last tool location, lift and move.
if(tool.DrawIsOn()) {
tool.WriteOff(out);
}
tool.WriteMoveTo(out, (float)x,(float)y);
}
// else line starts right here, do nothing.
} else {
// not the first point, draw.
if(tool.DrawIsOff()) tool.WriteOn(out);
if(j<polyLine.getVertexCount()-1 && dx*dx+dy*dy<tool.GetDiameter()/2.0) continue; // less than 1mm movement? Skip it.
tool.WriteMoveTo(out, (float)x,(float)y);
}
dxf_x2=x;
dxf_y2=y;
}
}
} else if(entity_type.equals(DXFConstants.ENTITY_TYPE_POLYLINE)) {
for(int i=0;i<entity_list.size();++i) {
pm.setProgress(entity_count++);
DXFPolyline entity = (DXFPolyline)entity_list.get(i);
boolean first=true;
for(int j=0;j<entity.getVertexCount();++j) {
DXFVertex v = entity.getVertex(j);
double x = (v.getX()-cx)*sx;
double y = (v.getY()-cy)*sy;
double dx = dxf_x2 - x;
double dy = dxf_y2 - y;
if(first==true) {
first=false;
if(dx*dx+dy*dy > tool.GetDiameter()/2.0) {
// line does not start at last tool location, lift and move.
if(tool.DrawIsOn()) {
tool.WriteOff(out);
}
tool.WriteMoveTo(out, (float)x,(float)y);
}
// else line starts right here, do nothing.
} else {
// not the first point, draw.
if(tool.DrawIsOff()) tool.WriteOn(out);
if(j<entity.getVertexCount()-1 && dx*dx+dy*dy<tool.GetDiameter()/2.0) continue; // less than 1mm movement? Skip it.
tool.WriteMoveTo(out, (float)x,(float)y);
}
dxf_x2=x;
dxf_y2=y;
}
}
}
}
}
// entities finished. Close up file.
tool.WriteOff(out);
tool.WriteMoveTo(out, 0, 0);
ok=true;
} catch(IOException e) {
e.printStackTrace();
} catch (org.kabeja.parser.ParseException e) {