• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

batch exporting script SVG to DXF scaling issue

Community Beginner ,
Oct 02, 2024 Oct 02, 2024

Copy link to clipboard

Copied

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.");
}

TOPICS
Import and export , Scripting

Views

125

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Oct 02, 2024 Oct 02, 2024

Copy link to clipboard

Copied

LATEST

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!");

})();

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines