关于CAD转PDF一个错位问题

1.png (3.0 KB)

1.png (3.0 KB)

图1为原始cad图纸,图2为转换pdf的效果:不知道为什么有两个标记错位了,但是也存在正确位置的标记,不知道问题出在哪里。下面zip为原始图纸

special.zip (188.4 KB)

我的代码:

License license = new License();
license.setLicense("Aspose.CAD.Java.lic");
// 加载CAD文件并设置指定的编码
LoadOptions loadOptions = new LoadOptions();
loadOptions.setSpecifiedEncoding(CodePages.SimpChinese);
//读取原始CAD
CadImage cadImage = (CadImage)CadImage.load(inputFilePath,loadOptions);
//重新计算仅包含可见部分的图像大小
cadImage.updateSize();
for (CadEntityBase entity : cadImage.getEntities()) {
      if (entity instanceof CadTolerance) {
            CadTolerance tolerance = (CadTolerance) entity;
            String currentText = tolerance.getToleranceString();
            // replacing concentricity symbol
            tolerance.setToleranceString(currentText.replace("{\\Fgdt;r}","\u2609"));
        }
}
for (int i = 0, size = cadImage.getLayers().size(); i < size; i++)
{
    CadLayerTable layer = (CadLayerTable) cadImage.getLayers().get_Item(i);
    layer.setLineWeight((short) 0);
}
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.setLayouts(new String[] { "Model" });
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
cadRasterizationOptions.setNoScaling(false);

PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
cadImage.save(outputPdfFilePath, pdfOptions);

@wangwei001

Cause

The issue of misalignment in your CAD to PDF conversion could stem from several factors, including incorrect scaling settings, unit type discrepancies, or specific properties of the CAD entities being processed. The presence of both correctly positioned and misaligned markers suggests that the problem may be related to how certain entities are rendered.

Solution

To troubleshoot and resolve the misalignment issue, consider the following steps:

  1. Check Unit Type: Ensure that the unit type of your CAD drawing is correctly identified and that the scaling is appropriately set based on whether the units are metric or imperial.

  2. Adjust Rasterization Options: Modify the rasterization options to ensure that the layout scaling is correctly applied. You may want to explicitly set the page width and height based on the unit type.

  3. Review Entity Properties: Inspect the properties of the entities that are misaligned. Sometimes, specific attributes or settings in the CAD file can affect how they are rendered in the PDF.

  4. Update Line Weights: Ensure that line weights are set appropriately for all layers, as this can sometimes affect the visual output.

Code

Here’s a modified version of your code that includes checks for unit type and adjusts the rasterization options accordingly:

License license = new License();
license.setLicense("Aspose.CAD.Java.lic");

LoadOptions loadOptions = new LoadOptions();
loadOptions.setSpecifiedEncoding(CodePages.SimpChinese);
CadImage cadImage = (CadImage) CadImage.load(inputFilePath, loadOptions);
cadImage.updateSize();

for (CadEntityBase entity : cadImage.getEntities()) {
    if (entity instanceof CadTolerance) {
        CadTolerance tolerance = (CadTolerance) entity;
        String currentText = tolerance.getToleranceString();
        tolerance.setToleranceString(currentText.replace("{\\Fgdt;r}", "\u2609"));
    }
}

for (int i = 0; i < cadImage.getLayers().size(); i++) {
    CadLayerTable layer = (CadLayerTable) cadImage.getLayers().get_Item(i);
    layer.setLineWeight((short) 0);
}

CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.setLayouts(new String[] { "Model" });
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
cadRasterizationOptions.setNoScaling(false);

// Set page dimensions based on unit type
if (cadImage.getUnitType() == UnitType.Millimeter) {
    cadRasterizationOptions.setPageWidth(210);
    cadRasterizationOptions.setPageHeight(297);
} else {
    cadRasterizationOptions.setPageWidth(8.27f); // A4 width in inches
    cadRasterizationOptions.setPageHeight(11.69f); // A4 height in inches
}

PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
cadImage.save(outputPdfFilePath, pdfOptions);

Next Steps

  1. Test the modified code to see if it resolves the misalignment issue.
  2. If the problem persists, examine the original CAD file for any peculiarities in the entities that might affect rendering.
  3. Reach out to the Aspose community or support for further assistance if needed.

Please implement these steps and let me know the outcome!

尝试了添加

// Set page dimensions based on unit type
if (cadImage.getUnitType() == UnitType.Millimeter) {
    cadRasterizationOptions.setPageWidth(210);
    cadRasterizationOptions.setPageHeight(297);
} else {
    cadRasterizationOptions.setPageWidth(8.27f); // A4 width in inches
    cadRasterizationOptions.setPageHeight(11.69f); // A4 height in inches
}

效果还是不对,并且转换的结果比以前更糟糕

@wangwei001,
我们可以确认该问题,似乎公差已被移动,我们已经创建了 CADJAVA-12001 来解决该问题。