Unable to add layers and assign entities in DWG using DwgImage (Aspose.CAD)

Dear Aspose Support,

I’m trying to create a DWG file from scratch using DwgImage in C# (.NET), and I would like to add layers and assign entities to specific layers. Unfortunately, this doesn’t seem to work as expected.

Below is a simplified example of what I’m doing:


DwgImage dwgImage = new DwgImage();
var linesLayer = new CadLayerTable { Name = "LinesLayer" };
dwgImage.Layers.Add(linesLayer);

var pointsLayer = new CadLayerTable { Name = "PointsLayer" };
dwgImage.Layers.Add(pointsLayer);
//add line
CadLine line = new CadLine(new Cad3DPoint(0, 0, 0), new Cad3DPoint(100, 100, 100));
line.LayerName = "LinesLayer";
dwgImage.AddEntity(line);
CadPoint point = new CadPoint();
point.CenterPoint = new Cad3DPoint(-10, -10, -10);
point.LayerName = "PointsLayer";
dwgImage.AddEntity(point);

var cadPoints = new List<Cad2DPoint>();
var point1 = new Cad2DPoint(-5, -5);
var point2 = new Cad2DPoint(-5, 10);
var point3 = new Cad2DPoint(20, 10);
var point4 = new Cad2DPoint(20, -5);

cadPoints.Add(point1);
cadPoints.Add(point2);
cadPoints.Add(point3);
cadPoints.Add(point4);

var cadPolyline = new CadLwPolyline
{
    Coordinates = cadPoints,
    Flag = CadLwPolylineFlag.Closed,
    ConstantWidth = 0.1,
    PointCount = cadPoints.Count,
    LayerName = "LinesLayer"
};

dwgImage.AddEntity(cadPolyline);
CadLine lineColor = new CadLine(new Cad3DPoint(0, 0, 0), new Cad3DPoint(-100, 100, 0));
lineColor.LayerName = "LinesLayer";
dwgImage.AddEntity(lineColor);
lineColor.ColorId = 15; //ACI color id
dwgImage.Save("fileResult.dwg");

:exclamation: Problem:

  • The file is saved, but the added layers are not visible in AutoCAD or other DWG viewers.
  • When I set LayerName = "...", the entities do not appear and no custom layers show up.
  • If I remove LayerName, the entities are displayed, but always on the default "0" layer.

:test_tube: Context:

  • I’m using the latest version of Aspose.CAD via NuGet.
  • My free trial license has expired – could that affect visibility of layers or entities?
  • I couldn’t find clear documentation or working examples on how to properly add layers to a new DwgImage.

:pray: My questions:

  1. Is it officially supported to add new layers to a DwgImage?
  2. Do you have a simple working example of:
  • a DWG file with one custom layer (e.g., "MyLayer")
  • one entity (e.g., a line) assigned to that layer
  1. Where can I find up-to-date documentation for working with CadLayer and DwgImage?
  2. Does an expired evaluation license hide or suppress custom layers/entities in any way?

Thank you in advance for your time and support — I’m working on a project to generate DWG files from XML and would like to use Aspose.CAD for this.

Best regards,
Abne

@Abne,
Hello,
thank you for all useful information provided.

We don’t have ready example to add layer for DWG. Here are the references for documentation and API. Expired license should not affect this case. Let us inspect adding layers into DWG on our side and come back with example and more details.

@Abne,
Hello,
please find if the attached example is useful. Now you need to initialize a lot of fields required in DWG, but we are going to simplify the API for more convenient usage in future versions.
Add layer.zip (1.2 KB)

Hi,

Thank you again for the example. I noticed you’re using this line in the code:

    var placeholder = dwgImage.Objects.OfType<CadACDBPlaceHolder>().FirstOrDefault();

However, I can’t find the class CadACDBPlaceHolder anywhere in the current version of the Aspose.CAD .NET API.
Even with all relevant using statements added, the class does not seem to exist or be accessible.

Could you please clarify:

  • Where can I find or import the CadACDBPlaceHolder type?

  • Is this part of an internal model, a renamed type, or maybe a type that has been removed in newer versions?

Thank you in advance — I’m close to getting the full DWG generation working with layers and this would really help.

Best regards,
Abdel

@Abne,
We are sorry for the confusion, we will make this class available in the future versions.
Please find the updated example.
AddLayers.zip (1.1 KB)

1 Like

Hi @oleksii.gorokhovatskyi ,

Thank you very much for the updated example — I tested it, and it works great now.
Also, I appreciate the clarification regarding the class availability in future versions. That makes sense.

Thanks again for your quick and helpful support!

Best regards,
Abdel

@Abne,
thank you for kind words, we are happy to help :slight_smile: