Skip to main content
Participant
October 2, 2024
Question

batch exporting script SVG to DXF scaling issue

  • October 2, 2024
  • 1 reply
  • 251 views

Does anyone have any idea why when I run the following java script my resulting DXF file is the wrong scale? If I do the process manually I get the correct result. Thanks very much

 

 

// Define the folder containing the SVG files
var folderPath = Folder.selectDialog("Select the folder with SVG files");

// Check if the folder is valid
if (folderPath != null) {
// Get all SVG files in the folder
var svgFiles = folderPath.getFiles("*.svg");

// Loop through each SVG file
for (var i = 0; i < svgFiles.length; i++) {
var svgFile = svgFiles[i];

// Open the SVG file
var activeDoc = app.open(svgFile);

// Select all objects in the active document
activeDoc.selectObjectsOnActiveArtboard();

// Set the name for the DXF file (same as the SVG file, but with .dxf extension)
var dxfFileName = svgFile.name.replace(".svg", ".dxf");
var dxfFilePath = folderPath.fsName + "/" + dxfFileName;

// Set the export options for DXF
var dxfOptions = new ExportOptionsAutoCAD();
dxfOptions.exportFileFormat = AutoCADExportFileFormat.DXF; // Use DXF format

// Set artwork scale to ensure 1 point = 1 unit
dxfOptions.artworkScale = 1; // 1 point = 1 unit

// Preserve appearance
dxfOptions.preserveAppearance = true;

// Only export selected objects
dxfOptions.selectedOnly = true; // Export only selected art

// Export the selected objects as DXF to the same location
var dxfFile = new File(dxfFilePath);
activeDoc.exportFile(dxfFile, ExportType.AUTOCAD, dxfOptions);

// Close the document without saving changes
activeDoc.close(SaveOptions.DONOTSAVECHANGES);

// Alert the user for each file processed
$.writeln("Processed: " + svgFile.name);
}

// Alert when all SVGs have been exported
alert("All SVG files have been successfully exported as DXF to the same folder!");
} else {
alert("No folder selected. Script canceled.");
}

This topic has been closed for replies.

1 reply

m1b
Community Expert
Community Expert
October 3, 2024

Hi @ezrah66252122, the code—chatGPT I guess—had some settings that don't seem to exist in the documentation so that might have been a problem. I've fixed it up as far as I can, but I don't have any software that can test the scale, except by opening the exported dxf back into Illustrator, which works perfectly. Give it a try.

- Mark

 

 

(function () {

    // Define the folder containing the SVG files
    var folderPath = Folder.selectDialog("Select the folder with SVG files");

    if (!folderPath)
        return;

    // Get all SVG files in the folder
    var svgFiles = folderPath.getFiles("*.svg");

    // Loop through each SVG file
    for (var i = 0; i < svgFiles.length; i++) {

        var svgFile = svgFiles[i];

        // Open the SVG file
        var activeDoc = app.open(svgFile);
        // Select all objects in the active document
        activeDoc.selectObjectsOnActiveArtboard();

        // Set the name for the DXF file (same as the SVG file, but with .dxf extension)
        var dxfFileName = svgFile.name.replace(".svg", ".dxf");
        var dxfFilePath = folderPath.fsName + "/" + dxfFileName;

        // Set the export options for DXF
        var dxfOptions = new ExportOptionsAutoCAD();
        dxfOptions.exportFileFormat = AutoCADExportFileFormat.DXF; // Use DXF format
        dxfOptions.unit = AutoCADUnit.Points;
        dxfOptions.unitScaleRatio = 1;
        dxfOptions.exportSelectedArtOnly = true;
        dxfOptions.version = AutoCADCompatibility.AutoCADRelease24;
 
        // Export the selected objects as DXF to the same location
        var dxfFile = new File(dxfFilePath);
        activeDoc.exportFile(dxfFile, ExportType.AUTOCAD, dxfOptions);

        // Close the document without saving changes
        activeDoc.close(SaveOptions.DONOTSAVECHANGES);

        // Alert the user for each file processed
        $.writeln("Processed: " + svgFile.name);
    }

    // Alert when all SVGs have been exported
    alert("All SVG files have been successfully exported as DXF to the same folder!");

})();