你好,我这边有一个需求,就是对于现有的DXF文件进行二次编辑再输出DXF文件,但是一直不成功,新的文件打不开,代码如下
try (DxfImage image = (DxfImage) DxfImage.load(“C:/Users/dsl/Desktop/Drawing56.dxf”)) {
List entities1 = image.getEntities();
List newEntities = new ArrayList<>(entities1);
for (CadEntityBase entity : entities1) {
System.out.println(entity.getTypeName());
if (entity.getTypeName() == CadEntityTypeName.INSERT) {
CadInsertObject entity1 = (CadInsertObject) entity;
Cad3DPoint insertionPoint = entity1.getInsertionPoint();
System.out.println(entity1.getName()+“,”+insertionPoint.getX()+“,”+insertionPoint.getY()+“,”+insertionPoint.getZ());
if (“SHELVES”.equalsIgnoreCase(entity1.getName())) {
CadMText text = new CadMText();
text.setText(“huojia”);
text.setInsertionPoint(entity1.getInsertionPoint());
newEntities.add(text);
}
}
}
image.setEntities(newEntities);
image.save(“C:/Users/dsl/Desktop/Drawing1206.dxf”);
/-------------------------------------------------------------------------------------------------------------/
} catch (Exception ex) {
ex.printStackTrace();
}
Drawing56.zip (2.0 KB)
你好,是的。
但是我直接生成DXF也是打不开的,代码如下:
try (DxfImage image = (DxfImage) DxfImage.load(“C:/Users/dsl/Desktop/Drawing56.dxf”)) {
image.save(“C:/Users/dsl/Desktop/Drawing1206.dxf”);
} catch (Exception ex) {
ex.printStackTrace();
}
我用的是24.4的体验版,不知道是不是跟这个体验版有关系,如果有进一步的消息请及时反馈
@luotg,
以下是在块内添加 MTEXT 的正确示例:
CadImage cadImage = (CadImage) Image.load(inputFile);
List<CadEntityBase> entities1 = cadImage.getEntities();
for (CadEntityBase entity : entities1)
{
if (entity.getTypeName() == CadEntityTypeName.INSERT)
{
CadInsertObject entity1 = (CadInsertObject)entity;
Cad3DPoint insertionPoint = entity1.getInsertionPoint();
if ("SHELVES".equalsIgnoreCase(entity1.getName()))
{
CadBlockEntity block = cadImage.getBlockEntities().get_Item(entity1.getName());
List<CadEntityBase> newEntities = block.getEntities();
CadMText text = new CadMText();
text.setText("huojia");
text.setInsertionPoint(insertionPoint);
String newObjectID = "FFFFFF";
if (cadImage.getHeader().getHeaderProperties().containsKey(CadHeaderAttribute.HANDSEED))
{
CadStringParameter handseed = (CadStringParameter)(cadImage.getHeader().getHeaderProperties().get(CadHeaderAttribute.HANDSEED).get(0));
newObjectID = handseed.getValue();
int nextAvailableID = Integer.parseInt(newObjectID, 16) + 1;
((CadStringParameter)(cadImage.getHeader().getHeaderProperties().get(CadHeaderAttribute.HANDSEED).get(0))).setValue(Integer.toHexString(nextAvailableID));
}
text.setInitialTextHeight(4);
text.setLayerName("0");
text.setObjectHandle(newObjectID);
newEntities.add(text);
block.setEntities(newEntities);
}
}
}
cadImage.save(dxfName);
不幸的是,我们可以确认这不起作用,我们已经创建了 CADJAVA-11811 来解决这个问题。
这仅适用于导出为线条(在最新的 24.6.1 版本上测试):
DxfOptions dxfOptions = new DxfOptions();
dxfOptions.setOutputMode(CadOutputMode.Render);
cadImage.save(dxfName, dxfOptions);
还有一个问题:
String newObjectID = “FFFFFF”;
if (cadImage.getHeader().getHeaderProperties().containsKey(CadHeaderAttribute.HANDSEED))
{
CadStringParameter handseed = (CadStringParameter)(cadImage.getHeader().getHeaderProperties().get(CadHeaderAttribute.HANDSEED).get(0));
newObjectID = handseed.getValue();
int nextAvailableID = Integer.parseInt(newObjectID, 16) + 1;
((CadStringParameter)(cadImage.getHeader().getHeaderProperties().get(CadHeaderAttribute.HANDSEED).get(0))).setValue(Integer.toHexString(nextAvailableID));
}
这段代码是什么意思呢