It’s easy enough to convert a DWG to a SVG in .NET C# using the Nuget Package. However there is no resources, examples, no research on how to set the layers that you get from the DWG file to the generated SVG. In my example I’m able to obtain the DWG layers but I’m not able to apply them to my SVG file when it’s generated.
string fileName = e.File.Name.Split(‘.’).First().ToString();
string extention = e.File.Name.Split(‘.’).Last().ToString();
using (MemoryStream ms = new MemoryStream())
{
await e.File.OpenReadStream().CopyToAsync(ms);
ms.Position = 0;
using (Image image = Image.Load(ms))
{
//get dwg layers
CadImage cadImage = (CadImage)image;
var allLayers = cadImage.Layers.GetLayersNames();
// create an instance of CadRasterizationOptions and set page height & width
var rasterizationOptions = new CadRasterizationOptions()
{
PageWidth = !string.IsNullOrEmpty(width) ? float.Parse(this.width) : 1200,
PageHeight = !string.IsNullOrEmpty(this.height) ? float.Parse(this.height) : 1600,
DrawType = !this.blackAndWhite ? Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseObjectColor : Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseDrawColor, //keeps the original color else if not used coverts to black and white
BackgroundColor = this.backgroundColor == "black" ? Color.Black : Color.White,
NoScaling = true,
AutomaticLayoutsScaling = true,
ExportAllLayoutContent = true,
Layers = allLayers.Select(layer => layer.Replace(" ", "")).ToArray()
};
var svgOptions = new SvgOptions();
svgOptions.VectorRasterizationOptions = rasterizationOptions;
//save image
string output = $"C:\\Users\\vou.xiong\\OneDrive - Wagstaff, Inc\\Desktop\\Aspose Converted\\{extention}-converted-to-svg.svg";
image.Save(output,svgOptions);
}
}
@vouxiong
To maintain the layer information from a DWG file when converting it to SVG using Aspose.CAD in C#, you can follow a method that involves iterating through the layers and applying them to the SVG output. Here’s a general approach based on the context provided:
- Load the DWG File: Use the
Image.Load
method to load your DWG file.
- Retrieve Layer Names: Access the layers from the
CadImage
object.
- Set Up Rasterization Options: Create an instance of
CadRasterizationOptions
and set the layers you want to include in the SVG.
- Iterate Through Layers: For each layer, create a new SVG output and set the layer name as an ID for the
<g>
tag in the SVG.
Here’s a code example that demonstrates this process:
using Aspose.CAD;
using Aspose.CAD.ImageOptions;
using System;
using System.IO;
using System.Linq;
public class DWGToSVGConverter
{
public static void ConvertDWGToSVG(string inputFilePath, string outputDirectory)
{
using (Image image = Image.Load(inputFilePath))
{
CadImage cadImage = (CadImage)image;
var allLayers = cadImage.Layers.GetLayersNames();
foreach (var layer in allLayers)
{
string layerName = layer.Replace(" ", "");
var rasterizationOptions = new CadRasterizationOptions()
{
Layers = new string[] { layer },
// Set other options as needed
};
var svgOptions = new SvgOptions
{
VectorRasterizationOptions = rasterizationOptions,
TextAsShapes = true // Optional: depending on your needs
};
string outputFilePath = Path.Combine(outputDirectory, $"{layerName}.svg");
image.Save(outputFilePath, svgOptions);
}
}
}
}
Key Points:
- Layer Handling: The code iterates through each layer and applies it to the SVG output, ensuring that each layer is represented correctly in the resulting SVG files.
- Output File Naming: Each SVG file is named after the corresponding layer, which helps in organizing the output.
This approach allows you to maintain the layer structure from the DWG file in the generated SVG files. If you encounter issues with rendering specific elements, consider checking the settings in CadRasterizationOptions
to ensure they align with your requirements.
For further reference, you might want to look at the example provided in the Aspose.CAD GitHub repository, which demonstrates basic SVG export functionality here.
Sources:
[1]: How to render cad block in converted svg
[2]: ExportToSVG.cs
I’m looking for something like this
<g id="layer_Freeways" class="layer_Freeways>
<p>Notice how the <g id=“layer_Freeways”</p>
@vouxiong,
Hello.
We don’t have the option to save layers in SVG unfortunately. We have created CADNET-10063 to implement this feature. Could you please take a look at this sample file, whether it is formed as you need?
layer.zip (335 Bytes)