如何使用jdxf库在Java中设置DXF图层颜色,让实体继承该颜色?
解决jdxf库中图层颜色设置与实体"ByLayer"继承问题
问题现状
使用jdxf库生成DXF文件时,无法实现**图层设置指定颜色,实体颜色继承图层(ByLayer)**的需求:当前代码仅能直接给实体设置颜色,但图层默认颜色为黑白,生成的DXF中实体是单独的颜色,未关联图层颜色。
原有代码
DXFHandler类
public class DXFHandler { DXFGraphics graphics; DXFDocument dXFDocument; int mirrorY, mirrorX; public DXFHandler() { dXFDocument = new DXFDocument(); dXFDocument.setUnits(4);//set units to mm dXFDocument.setPrecisionDigits(1);//set precision digits dXFDocument.setViewportCenter(0, 0); graphics = dXFDocument.getGraphics(); mirrorX=1; mirrorY=-1; } public void addLayer(String layerName, Color c) { dXFDocument.setLayer(layerName); graphics.setColor(c);//This will only achieve that the drawing entities get this color, but the layer's color would not be touched. } public void drawLine(double[]x, double[]y){ if (x!=null&&y!=null&x.length==y.length) { for (int i = 1; i < x.length; i++) { graphics.drawLine(mirrorX*x[i-1], mirrorY*y[i-1],mirrorX*x[i],mirrorY*y[i]); } } } public void drawPoint(double[]x, double[]y){ if (x!=null&&y!=null&x.length==y.length) { for (int i = 0; i < x.length; i++) { graphics.drawPoint(mirrorX*x[i], mirrorY*y[i]); } } } public void drawRect(double x1, double y1, double x2, double y2){ graphics.drawRect(mirrorX*x1, mirrorY*y1, mirrorX*x2, mirrorY*y2); } public void saveToDXF(String outputPath) throws IOException { String stringOutput = dXFDocument.toDXFString(); FileWriter fileWriter = new FileWriter(outputPath); fileWriter.write(stringOutput); fileWriter.flush(); fileWriter.close(); } }
测试代码
public static void main(String[] args) { String outputFile="d:\\test.dxf"; int points=500;//Number of drawn points . String layer1Name="Layer_1"; double[]xCoordinatesOfPoints1=new double[points];//creating first test-pointset_Xcoordinates double[]yCoordinatesOfPoints1=new double[points];//creating first test-pointset_Ycoordinates for (int i = 0; i < xCoordinatesOfPoints1.length; i++) { xCoordinatesOfPoints1[i]=i; yCoordinatesOfPoints1[i]=i; } String layer2Name="Layer_2"; double[]xCoordinatesOfPoints2=new double[points];//creating second test-pointset_Xcoordinates double[]yCoordinatesOfPoints2=new double[points];//creating second test-pointset_Ycoordinates for (int i = 0; i < xCoordinatesOfPoints1.length; i++) { xCoordinatesOfPoints2[i]=-1*i; yCoordinatesOfPoints2[i]=i; } DXFHandler dXFHandler=new DXFHandler(); //Creating dxf handler object dXFHandler.addLayer(layer1Name, Color.red);//Creating layer1 dXFHandler.drawPoint(xCoordinatesOfPoints1, yCoordinatesOfPoints1);//drawing points of layer1 dXFHandler.addLayer(layer2Name, Color.GREEN);//Creating layer2 dXFHandler.drawPoint(xCoordinatesOfPoints2, yCoordinatesOfPoints2);//drawing points of layer2 try { dXFHandler.saveToDXF(outputFile);//Exporting dxf file } catch (IOException ex) { System.out.println("FAILED"); } }
解决方案
修改addLayer方法,直接操作DXFLayer对象设置图层颜色,同时将绘图上下文的颜色设为0(代表ByLayer),让实体继承图层颜色:
修改后的DXFHandler关键代码
public void addLayer(String layerName, Color c) { // 获取或创建目标图层 DXFLayer layer = dXFDocument.getLayer(layerName); // 将Java Color转换为DXF颜色索引 int dxfColor = DXFColor.getClosestDXFColorIndex(c); // 设置图层颜色 layer.setColor(dxfColor); // 切换到该图层 dXFDocument.setLayer(layerName); // 设置图形上下文颜色为ByLayer(DXF颜色代码0) graphics.setColor(0); }
完整修改后的DXFHandler类
public class DXFHandler { DXFGraphics graphics; DXFDocument dXFDocument; int mirrorY, mirrorX; public DXFHandler() { dXFDocument = new DXFDocument(); dXFDocument.setUnits(4);//set units to mm dXFDocument.setPrecisionDigits(1);//set precision digits dXFDocument.setViewportCenter(0, 0); graphics = dXFDocument.getGraphics(); mirrorX=1; mirrorY=-1; } public void addLayer(String layerName, Color c) { DXFLayer layer = dXFDocument.getLayer(layerName); int dxfColor = DXFColor.getClosestDXFColorIndex(c); layer.setColor(dxfColor); dXFDocument.setLayer(layerName); graphics.setColor(0); // 设置为ByLayer模式 } public void drawLine(double[]x, double[]y){ if (x!=null&&y!=null&x.length==y.length) { for (int i = 1; i < x.length; i++) { graphics.drawLine(mirrorX*x[i-1], mirrorY*y[i-1],mirrorX*x[i],mirrorY*y[i]); } } } public void drawPoint(double[]x, double[]y){ if (x!=null&&y!=null&x.length==y.length) { for (int i = 0; i < x.length; i++) { graphics.drawPoint(mirrorX*x[i], mirrorY*y[i]); } } } public void drawRect(double x1, double y1, double x2, double y2){ graphics.drawRect(mirrorX*x1, mirrorY*y1, mirrorX*x2, mirrorY*y2); } public void saveToDXF(String outputPath) throws IOException { String stringOutput = dXFDocument.toDXFString(); FileWriter fileWriter = new FileWriter(outputPath); fileWriter.write(stringOutput); fileWriter.flush(); fileWriter.close(); } }
原理说明
dXFDocument.getLayer(layerName):如果图层不存在会自动创建,返回对应的DXFLayer对象DXFColor.getClosestDXFColorIndex(c):将Java的Color对象转换为DXF标准的颜色索引值layer.setColor(dxfColor):直接设置图层本身的颜色graphics.setColor(0):DXF中颜色代码0代表ByLayer,后续绘制的所有实体都会自动继承当前图层的颜色
运行修改后的代码,生成的DXF文件中:
- 图层
Layer_1颜色为红色,Layer_2颜色为绿色 - 所有实体的颜色模式为"ByLayer",会跟随图层颜色变化
内容的提问来源于stack exchange,提问作者mnystrktr
相关产品推荐
相关产品推荐

