How to convert dxf to dwg?

Hello,

Is it possible to convert dxf file to dwg file using aspose lib?

@msdos41,
Hi.
Here is the basic example:

using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(fileName))
{
      cadImage.Save("result.dwg", new DwgOptions());
}

@oleksii.gorokhovatskyi
Hi,

Here is my current code:

using (var cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(dxfPath))
{
	var dwgPath = Path.Combine(saveFolder, $"{Path.GetFileNameWithoutExtension(filePath)}_{DateTime.Now:yyyyMMddHHmmssffff}.dwg");
	var options = new Aspose.CAD.ImageOptions.DwgOptions();
	var format = options.TargetFormat;
	cadImage.Save(dwgPath, options);
}

Here is 2 problems:

  1. I found the targetformat default value is AutoCAD2018, which is readonly. Could it be changed?
  2. right now I have successfully converted dxf to dwg using this code. In my pc, Autocad 2019 has been installed. When I use “Insert” command in AutoCAD to insert this dwg file, an error occurred " File C:\Users\xj2ssf\Desktop\SvgToDwg_test\0301411.9_Fixing_1_202402230939484966.dwg was created by an incompatible version of AutoCAD" and it worked well when just “Open” this dwg file.
    However, when I “insert” the same dwg file in another computer, it works well. The difference between two computers is the product version of AutoCad2019.
    You could find the attachment which show the normal version and abnormal version as long as the test file.
    test.zip (257.6 KB)

@oleksii.gorokhovatskyi
Hi,

I have another quesion. I am using aspose.cad to load dxf file. Is it possible to parse the elements in the dxf file and remove some of them? Like the graphic in the attachment above, I want to delete the rectangle boundaries in the dxf file.

@msdos41,
Hi.

  1. We have support to write 2018 DWG only with DwgOptions at the moment. It is possible to preserve also DWG 2013 format if the original image has it with cadImage.Save(“newFile.dwg”) without export options.
  2. I’m afraid this looks like not the issue with file but probably with AutoCAD itself or its particular version/installation. I can see that INSERT works fine with 2019.1.2 for me, we will try to do some research and get additional information on this if possible.

Removing entities from DXF is possible, the main question seems to be - how to identify the required entities. Here is the basic example how to remove border for the file above (the border is polyline and is identified by quantity of vertices in it):

using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(fileName))
{
   List<CadEntityBase> entitiesToRemove = new List<CadEntityBase>();
 
   foreach (CadEntityBase ceb in cadImage.Entities)
   {
	   System.Console.WriteLine(ceb.TypeName);

	   if (ceb.TypeName == CadEntityTypeName.POLYLINE)
	   {
		   CadPolyline polyline = (CadPolyline)ceb;
		   
		   if (polyline.ChildObjects.Count == 6)
		   {
			   entitiesToRemove.Add(ceb);
		   }
	   }
   }

   List<CadEntityBase> entities = new List<CadEntityBase>(cadImage.Entities);

   foreach (CadEntityBase ceb in entitiesToRemove)
   {
	   cadImage.TryRemoveEntity(ceb);
	   entities.Remove(ceb);
   }

   cadImage.Entities = entities;

   cadImage.UpdateSize();

   cadImage.Save(outPath);
}

@oleksii.gorokhovatskyi
Hi, I tried directly save as dwg without DwgOptions. But it occurred an error. Check out the attachment.

using (var cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(dxfPath))
{
	var dwgPath = Path.Combine(saveFolder, $"{Path.GetFileNameWithoutExtension(filePath)}_{DateTime.Now:yyyyMMddHHmmssffff}.dwg");

	cadImage.Save(dwgPath);
}

error.PNG (8.3 KB)

@msdos41,
Hello.
Could you please attach this file?

@oleksii.gorokhovatskyi
Please check the dxf

SvgToDwg_test.7z (1.5 KB)

@msdos41,
Hi.
The common idea of using cadImage.Save() without any options is the preserving of the initial format, but it works a bit differently for DXF/DWG now. For this case the initial DXF has R12 format and writing it to DWG of the same format is not possible, so message with error occurs. The recommended way to do DXF to DWG is using DWG options, e.g., cadImage.Save(outPath, new DwgOptions()).

@oleksii.gorokhovatskyi

Hi, I am using Aspose.Imaging lib to convert svg to dxf, then use that dxf convert to dwg.
You meant the dxf format is R12. So which format could be converted to dwg without options? How to do it?

My svg to dxf codes:

using (var image = Aspose.Imaging.Image.Load(filePath))
{
	// Create an instance of DxfOptions
	var exportOptions = new Aspose.Imaging.ImageOptions.DxfOptions() { TextAsLines = false, ConvertTextBeziers = true };
	Aspose.Imaging.ImageOptions.VectorRasterizationOptions rasterizationOptions = new Aspose.Imaging.ImageOptions.SvgRasterizationOptions();
	rasterizationOptions.PageWidth = image.Width;
	rasterizationOptions.PageHeight = image.Height;
	rasterizationOptions.FullFrame = false;
	exportOptions.VectorRasterizationOptions = rasterizationOptions;

	// Save svg to dxf
	var dxfPath = Path.Combine(saveFolder, $"{Path.GetFileNameWithoutExtension(filePath)}_{DateTime.Now:yyyyMMddHHmmssffff}.dxf");
	image.Save(dxfPath, exportOptions, Aspose.Imaging.Rectangle.Empty);
}

@msdos41,
Hi.
During the conversion between different formats the expected and correct way is to use options. So, now you can convert DXF R12 and other DXF formats to DWG 2018 with new DwgOptions(). Conversion from DXF to DWG without options causing errors at the moment, we have created CADNET-9670 to cover them, but this is not preferrable way to do this conversion. Unfortuntely we don’t have yet the support to convert DXF R12 to more recent DXF (e.g., 2018) that could be helpful here with the goal to use saving to DWG without options. Aspose.Imaging has support for DXF R12 only too.