[solved] Dashed and dotted lines on different layers

Hello everyone!

I would like to ask, how can I draw a dashed (— — — —) and a dashdot (— - — - — - —) line with dxflib? Also I would like to draw them to different layers: continuous lines to layer ‘0’, dashed line to layer ‘d’, and dashdot line to layer ‘dd’ .
I read the documentation but maybe I’m too tired and missing something, could someone help with this?

Patry0t

You’d typically call writeLinetype between tableLinetypes and tableEnd. Then you can use the linetype name for example for a layer (writeLayer).

thank you for your quick reply.
I tried something like this:

	dw->tableLinetypes(3);
	dxf->writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0));
	dxf->writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0));
	dxf->writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Filled", 0, 0, 0.0));
	/*dxf->writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Dashed", 1, 0, 0.0));
	dxf->writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Dashdot", 2, 0, 0.0));*/
	dw->tableEnd();

	int numberOfLayers = 3;
	dw->tableLayers(numberOfLayers);

	dxf->writeLayer(*dw,
		DL_LayerData("0", 0),
		DL_Attributes(
			std::string(""),			// leave empty
			DL_Codes::black,			// default color
			100,						// default width
			"CONTINUOUS", 1.0));        // default line style

	dxf->writeLayer(*dw,
		DL_LayerData("d", 0),
		DL_Attributes(
			std::string(""),			// leave empty
			DL_Codes::black,			// default color
			100,						// default width
			"CONTINUOUS", 1.0));       // default line style

	dxf->writeLayer(*dw,
		DL_LayerData("dd", 0),
		DL_Attributes(
			std::string(""),			// leave empty
			DL_Codes::black,			// default color
			100,						// default width
			"CONTINUOUS", 1.0));       // default line style

	dw->tableEnd();

Then I would like to draw the line according to my linetypes in LineT (111 filled, 222 dashed, 333 dashdot).

dw->sectionEntities();

	// write all entities in model space:
	for (int i = 0; i < line_endpoints.size(); i++) {
		
		if (lineT[i] == 111) {
			dxf->writeLine(
				*dw,
				DL_LineData(line_endpoints[i][0],   // start point
					line_endpoints[i][1],
					0.0,
					line_endpoints[i][2],   // end point
					line_endpoints[i][3],
					0.0),
				DL_Attributes("0", 256, -1, "BYLAYER", 1.0));
		}
		else if (lineT[i] == 222) {
			dxf->writeLine(
				*dw,
				DL_LineData(line_endpoints[i][0],   // start point
					line_endpoints[i][1],
					0.0,
					line_endpoints[i][2],   // end point
					line_endpoints[i][3],
					0.0),
				DL_Attributes("d", 256, -1, "BYLAYER", 1.0));
		}
		else {
			dxf->writeLine(
				*dw,
				DL_LineData(line_endpoints[i][0],   // start point
					line_endpoints[i][1],
					0.0,
					line_endpoints[i][2],   // end point
					line_endpoints[i][3],
					0.0),
				DL_Attributes("dd", 256, -1, "BYLAYER", 1.0));
		}
	}

	dw->sectionEnd();

But this not seem to work :\

You have two linetypes named “CONTINUOUS”.
All of your layers use linetype “CONTINUOUS”.
Also, you are not actually defining any dashed patterns in the DL_LinetypeData constructors.

yes, I tried something like this:

dw->tableLinetypes(5);
	dxf->writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0));
	dxf->writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0));
	dxf->writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Filled", 0, 0, 0.0));
	dxf->writeLinetype(*dw, DL_LinetypeData("DASHED", "Dashed", 0, 10, 3, 0.111000111));
	dxf->writeLinetype(*dw, DL_LinetypeData("DASHDOT", "Dashdot", 0, 10, 5, 0.111010111));
dw->tableEnd();

But that gives me compile error, because the argument list of LinetypeData is not matched. When I give one less argument it also stops working.

Is it something with the versions? I use AC1015 version just like in the documentation, I dont get it.. I just want to use the version that matches the documentation on your website here, but that gives me compile error.

The last parameter of the DL_LinetypeData constructor is an array of doubles with the desired dash lengths (positive) and gap lengths (negative), e.g.

double dashes[2] = { 2.5, -0.5 };

For a dash of 2.5 Millimeter followed by a gap of 0.5 Millimeter, etc.

something like this? This compiles at least, thanks!

double dashes[2] = { 2.5, -0.5 };
	dw->tableLinetypes(3);
	dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Filled", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("DASHED", "Dashed", 0, 10, 10, dashes));
	dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT", "Dashdot", 0, 10, 10, dashes));
	dw->tableEnd();

This compiles now, which is good, but than I still have only continuous lines when I open it in AutoCAD :\

any idea why?

Did you also pass those linetype names to writeLayer (can you post your adjusted code)?

double dashes[2] = { 2.5, -0.5 };
	double dashdot[2] = { 5.0, -0.5 };
	dw->tableLinetypes(5);
	dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Filled", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("DASHED", "Dashed", 0, 10, 5, dashes));
	dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT", "Dashdot", 0, 10, 5, dashdot));
	dw->tableEnd();

	int numberOfLayers = 3;
	dw->tableLayers(numberOfLayers);

	dxf.writeLayer(*dw,
		DL_LayerData("0", 0),
		DL_Attributes(
			std::string(""),			// leave empty
			DL_Codes::black,			// default color
			100,						// default width
			"CONTINUOUS", 1.0));        // default line style

	dxf.writeLayer(*dw,
		DL_LayerData("d", 0),
		DL_Attributes(
			std::string(""),			// leave empty
			DL_Codes::red,			// default color
			100,						// default width
			"DASHED", 1.0));       // default line style

	dxf.writeLayer(*dw,
		DL_LayerData("dd", 0),
		DL_Attributes(
			std::string(""),			// leave empty
			DL_Codes::blue,			// default color
			100,						// default width
			"DASHDOT", 1.0));       // default line style

	dw->tableEnd();

	dw->tableStyle(1);
		dxf.writeStyle(*dw, DL_StyleData("standard", 0, 2.5, 1.0, 0.0, 0, 2.5, "txt", ""));
	dw->tableEnd();

	dxf.writeView(*dw);
	dxf.writeUcs(*dw);

	dw->tableAppid(1);
		dxf.writeAppid(*dw, "ACAD");
	dw->tableEnd();

	dxf.writeDimStyle(*dw, 1, 1, 1, 1, 1);

	//FOR
	//minden egyenesre egy blokk
	dxf.writeBlockRecord(*dw);
	dxf.writeBlockRecord(*dw, "myblock1");
	dw->tableEnd();

	dw->sectionEnd();

	dw->sectionBlocks();
	dxf.writeBlock(*dw, DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0));
	dxf.writeEndBlock(*dw, "*Model_Space");
	dxf.writeBlock(*dw, DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0));
	dxf.writeEndBlock(*dw, "*Paper_Space");
	dxf.writeBlock(*dw, DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0));
	dxf.writeEndBlock(*dw, "*Paper_Space0");

	
	dxf.writeBlock(*dw, DL_BlockData("myblock1", 0, 0.0, 0.0, 0.0));
	dxf.writeEndBlock(*dw, "myblock1");
	dw->sectionEnd();

	dw->sectionEntities();

	// write all entities in model space:
	for (int i = 0; i < line_endpoints.size(); i++) {
		
		if (lineT[i] == 111) {
			dxf.writeLine(
				*dw,
				DL_LineData(line_endpoints[i][0],   // start point
					line_endpoints[i][1],
					0.0,
					line_endpoints[i][2],   // end point
					line_endpoints[i][3],
					0.0),
				DL_Attributes("0", 256, -1, "BYLAYER", 1.0));
		}
		else if (lineT[i] == 222) {
			dxf.writeLine(
				*dw,
				DL_LineData(line_endpoints[i][0],   // start point
					line_endpoints[i][1],
					0.0,
					line_endpoints[i][2],   // end point
					line_endpoints[i][3],
					0.0),
				DL_Attributes("d", 256, -1, "BYLAYER", 1.0));
		}
		else {
			dxf.writeLine(
				*dw,
				DL_LineData(line_endpoints[i][0],   // start point
					line_endpoints[i][1],
					0.0,
					line_endpoints[i][2],   // end point
					line_endpoints[i][3],
					0.0),
				DL_Attributes("dd", 256, -1, "BYLAYER", 1.0));
		}
	}

	dw->sectionEnd();

So I would like to have 3 layers here, ‘0’, ‘d’ and ‘dd’. 0 layer contains continuous lines, d layer contains dashed lines and dd layer the dashdot lines.
But for some reason this prints out all the lines in continuous.

Could you please attach your DXF file written with the above code?

yes, sure but its now updated with circles and arcs too. But I changed nothing on the lines, so its still the problem with dashes and dashdot.
myDxf.dxf (11.5 KB)

If you look at the linetype definitions in the DXF file, you can see that there is a problem with the dash pattern definition:

0
LTYPE
5
31
100
AcDbSymbolTableRecord
100
AcDbLinetypeTableRecord
2
DASHED
70
0
3
Dashed
72
65
73
10
40
6.0
49
2.5 ← dash: OK
74
0
49
-0.5 ← gap: OK
74
0
49
-92559631349317830736831783200707727132248687965119994463780864.0 ← NOT OK.
74
0
49
-92559631349317830736831783200707727132248687965119994463780864.0 ← NOT OK.

Reason: the number of dashes passed to the DL_LinetypeData constructor is wrong and so is the pattern length:

Should probably be:
DL_LinetypeData(“DASHED”, “Dashed”, 0, 2, 3.0, dashes)
DL_LinetypeData(“DASHDOT”, “Dashdot”, 0, 2, 5.5, dashdot)

Thank You, finally I understand it. You helped me a lot!

I could solve it like this:

double dashes[2] = { 2.5, -0.5 };
	double dashdot[4] = { 5.0, -0.25, 0.25, -0.25};
	dw->tableLinetypes(5);
	dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Filled", 0, 0, 0.0));
	dxf.writeLinetype(*dw, DL_LinetypeData("DASHED", "Dashed", 0, 2, 3.0, dashes));
	dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT", "Dashdot", 0, 4, 5.75, dashdot));
	dw->tableEnd();