DWG file from scratch shows empty

Hi Aspose team,

I’m trying to create a DWG file from scratch using Aspose.CAD for .NET (version 24.10.0), without using any existing template.

I followed this official example, but when I open the generated file in AutoCAD, it shows up completely empty — no geometry at all. I also tried adding entities to a *Model_Space block manually, and experimented with CadLayerTable, CadPolyline, etc., but no luck.

Using DxfImage works perfectly with the exact same content.

I also tried saving the DWG with new DwgOptions(), but it throws a NullReferenceException.

So my questions are:

  1. Is DWG generation from scratch currently expected to work reliably?
  2. If yes, could you share a minimal example that produces a working DWG file with visible geometry?

Thanks in advance!
Ab

@Abne

Could you please provide the specific code you are using to create the DWG file from scratch, including how you are adding entities to the Model_Space block?

Thanks for your reply.

I’ve already shared the code I’m using — it’s the same as in your official example https://gist.githubusercontent.com/aspose-com-gists/9a239eab0b9dda0e1c54be533ea399bb/raw/bd2ea155d3316a3a6a4df64b5ed36a7f1c43de0c/generation-of-dwg.cs. I used that exact example as a starting point, and tried adding entities like CadLine, CadCircle, CadSolid, etc., to the *Model_Space block using:

DwgImage dwgImage = new DwgImage();
//LINE, CIRCLE, ARC, TEXT, POINT, SOLID, 3DFACE and POLYLINE

//add line
CadLine line = new CadLine(new Cad3DPoint(0, 0, 0), new Cad3DPoint(100, 100, 100));
dwgImage.AddEntity(line);

//add circle
CadCircle circle = new CadCircle(new Cad3DPoint(50, 0), 10);
dwgImage.AddEntity(circle);

//add arc
CadArc arc = new CadArc(new Cad3DPoint(100, 0), 10, 45, 135);
dwgImage.AddEntity(arc);

CadText text = new CadText();
text.FirstAlignment = new Cad3DPoint(0, -50, 0);
text.TextHeight = 10;
text.DefaultValue = "text value";
dwgImage.AddEntity(text);

CadPoint point = new CadPoint();
point.CenterPoint = new Cad3DPoint(-10, -10, -10);
dwgImage.AddEntity(point);

CadSolid solid = new CadSolid();
solid.FirstCorner = new Cad3DPoint(200, 200, 0);
solid.SecondCorner = new Cad3DPoint(210, 200, 0);
solid.ThirdCorner = new Cad3DPoint(210, 220, 0);
solid.FourthCorner = new Cad3DPoint(200, 220, 0);
dwgImage.AddEntity(solid);

Cad3DFace cad3DFace = new Cad3DFace();
cad3DFace.FirstCorner = new Cad3DPoint(-200, 200, 0);
cad3DFace.SecondCorner = new Cad3DPoint(-210, 200, 0);
cad3DFace.ThirdCorner = new Cad3DPoint(-210, 220, 0);
cad3DFace.FourthCorner = new Cad3DPoint(-200, 220, 0);
dwgImage.AddEntity(cad3DFace);


CadPolyline polyline = new CadPolyline();
polyline.VerticesFollowFlag = 1;

var vertex1 = new Cad2DVertex() { LocationPoint = new Cad3DPoint(20, 0) };
var vertex2 = new Cad2DVertex() { LocationPoint = new Cad3DPoint(40, -40) };
var vertex3 = new Cad2DVertex() { LocationPoint = new Cad3DPoint(60, -40) };

dwgImage.AddEntity(polyline);
dwgImage.AddEntity(vertex1);
dwgImage.AddEntity(vertex2);
dwgImage.AddEntity(vertex3);
CadSeqend seqend = new CadSeqend();
dwgImage.AddEntity(seqend);

//Color example
CadLine lineColor = new CadLine(new Cad3DPoint(0, 0, 0), new Cad3DPoint(-100, 0, 100));
dwgImage.AddEntity(lineColor);
lineColor.ColorId = 15; //ACI color id

//polyline style example
CadPolyline polylineStyle = new CadPolyline();
polylineStyle.VerticesFollowFlag = 1;
polyline.StartWidth = 3;
polyline.EndWidth = 3;

var vertex1Style = new Cad2DVertex() { LocationPoint = new Cad3DPoint(-20, 0) };
var vertex2Style = new Cad2DVertex() { LocationPoint = new Cad3DPoint(-40, -40) };
var vertex3Style = new Cad2DVertex() { LocationPoint = new Cad3DPoint(-60, -40) };

dwgImage.AddEntity(polylineStyle);
dwgImage.AddEntity(vertex1Style);
dwgImage.AddEntity(vertex2Style);
dwgImage.AddEntity(vertex3Style);

CadSeqend seqendStyle = new CadSeqend();
dwgImage.AddEntity(seqendStyle);

dwgImage.Save("fileResult.dwg");

@Abne,

Hi.
It seems there are issues with polylines in this example, but all other entities present in the file (they are near origin of the drawing). We will investigate this, could you please try the modified example below.

DwgImage dwgImage = new DwgImage();
//LINE, CIRCLE, ARC, TEXT, POINT, SOLID, 3DFACE and POLYLINE

//add line
CadLine line = new CadLine(new Cad3DPoint(0, 0, 0), new Cad3DPoint(100, 100, 100));
dwgImage.AddEntity(line);

//add circle
CadCircle circle = new CadCircle(new Cad3DPoint(50, 0), 10);
dwgImage.AddEntity(circle);

//add arc
CadArc arc = new CadArc(new Cad3DPoint(100, 0), 10, 45, 135);
dwgImage.AddEntity(arc);

CadText text = new CadText();
text.FirstAlignment = new Cad3DPoint(0, -50, 0);
text.TextHeight = 10;
text.DefaultValue = "text value";
dwgImage.AddEntity(text);

CadPoint point = new CadPoint();
point.CenterPoint = new Cad3DPoint(-10, -10, -10);
dwgImage.AddEntity(point);

CadSolid solid = new CadSolid();
solid.FirstCorner = new Cad3DPoint(200, 200, 0);
solid.SecondCorner = new Cad3DPoint(210, 200, 0);
solid.ThirdCorner = new Cad3DPoint(210, 220, 0);
solid.FourthCorner = new Cad3DPoint(200, 220, 0);
dwgImage.AddEntity(solid);

Cad3DFace cad3DFace = new Cad3DFace();
cad3DFace.FirstCorner = new Cad3DPoint(-200, 200, 0);
cad3DFace.SecondCorner = new Cad3DPoint(-210, 200, 0);
cad3DFace.ThirdCorner = new Cad3DPoint(-210, 220, 0);
cad3DFace.FourthCorner = new Cad3DPoint(-200, 220, 0);
dwgImage.AddEntity(cad3DFace);


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
};

dwgImage.AddEntity(cadPolyline);

//Color example
CadLine lineColor = new CadLine(new Cad3DPoint(0, 0, 0), new Cad3DPoint(-100, 100, 0));
dwgImage.AddEntity(lineColor);
lineColor.ColorId = 15; //ACI color id

dwgImage.Save("fileResult.dwg");

Hi,

Thank you very much — this updated example works great!

I can now see the drawn entities clearly in the DWG file, and that’s exactly what I needed. Really appreciate your quick and effective response.

I’m currently working on a project where we need to generate complete CAD files — including layers, blocks, and entities — entirely programmatically, without using AutoCAD. I hope Aspose.CAD will be able to support our needs for that.

Thanks again for the support!

Best regards,
Abne

@Abne,
glad to hear it worked :slight_smile:
But please take into account that some entities or objects are very complex to create, and we also support writing of DWG 2018 format only.