We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Error in size and location of elements when converting DWG files to SVG files

Hi, I’m trying to convert DWG files to SVG files.
I use ASPOSE.CAD to read DWG file and I use .NET Xml to make SVG file.
I have some problem while converting, some SVG files i convert looks weird.
weirdSvg.png (1.1 MB)
All elements are centrally aligned and maximized(above image).
Most files are well converted, but only a few are strangely converted.

Difference of two things is “transform property of g tag”.
featureOfStrange.png (85.4 KB)
normalOne.png (96.1 KB)
Normal SVG file’s value of transform property are all same,
but strange files are not.
I think that value changed to be centralized and maximized.

Here is a part of my code.

XmlDocument origin = new XmlDocument();
var svgTag = origin.CreateElement("svg", "http://www.w3.org/2000/svg");
svgTag.SetAttribute("width", $"{image.Width}");
svgTag.SetAttribute("height", $"{image.Height}");
origin.AppendChild(svgTag);
XmlNamespaceManager manager = new XmlNamespaceManager(origin.NameTable);
manager.AddNamespace("svg", "http://www.w3.org/2000/svg");
manager.AddNamespace("g", "http://www.w3.org/2000/svg");

foreach (var layer in layers)
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        string layerName = layer.Replace(" ", "");
        SvgOptions options = new SvgOptions();
        rasterizationOptions.Layers = new string[] { layer.Trim() };
        options.TextAsShapes = false;
        options.VectorRasterizationOptions = rasterizationOptions;
        image.Save(memoryStream, options);
        memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
        using (var reader = new StreamReader(memoryStream))
        {
            var contents = reader.ReadToEnd();
            XmlDocument svg = new XmlDocument();
            svg.LoadXml(contents);
            var root = svg.SelectSingleNode("descendant::svg:g", manager);
            if (root == null)
            {
                continue;
            }
            ((XmlElement)root).SetAttribute("id", layerName);
            XmlNode importNode = svgTag.OwnerDocument.ImportNode(root, true);
            svgTag.AppendChild(importNode);
        }
    }
}
origin.Save(Path.Combine(distPath, $"{dwg.FileName}-merged.svg"));

I don’t know well in CAD and SVG, so i have no idea whether the CAD drawing is a problem, SVG library is a problem, or this library is a problem.

So I need your help.

@Cad_MAster,
Hi, you can convert DWG to SVG using Aspose.CAD:

using (CadImage cadImage = (CadImage)Image.Load(fileName))
{
   CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
   rasterizationOptions.Layouts = new[] { "Model" };

   using (FileStream fileStream = new FileStream("result.svg", FileMode.Create, FileAccess.Write))
   {
      SvgOptions options = new SvgOptions();
      options.TextAsShapes = false;
      options.VectorRasterizationOptions = rasterizationOptions;
      cadImage.Save(fileStream, options);
   }
}