# dxf-\>writeHatch, but how ?

**URL:** <http://forum.qcad.org/t/dxf-writehatch-but-how/290>\
**Category:** dxflib 'How Do I' Questions\
**Created:** [June 10, 2009, 9:12am UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290 "2009-06-10T09:12:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonsalty](http://forum.qcad.org/letter_avatar_proxy/v4/letter/b/45deac/32.png) [@bonsalty](http://forum.qcad.org/u/bonsalty)\
**Post date:** [June 10, 2009, 9:12am UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/1 "2009-06-10T09:12:29Z")

</div>

I havent found how to write a hatch into dxf

We have:  
void DL\_Dxf::writeHatch1 ( DL\_WriterA & dw,  
const DL\_HatchData & data,  
const DL\_Attributes & attrib  
)  
where DL\_HatchData (int hNumLoops, bool hSolid, double hScale, double hAngle, const string &hPattern)

First question: what shoud hPattern be and whats this hNumLoops all about?  
Second question:

If I call :  
void DL\_Dxf::writeHatchLoop1 ( DL\_WriterA & dw,  
const DL\_HatchLoopData & data  
)  
should I call the writeHatchEdge functions after this or before the writeHatchLoop?

Third question,why must I call writeHatch2 and how to use it ???

An example would be nice,since its impossible to decode it from the ribbonsofts class reference. Thank you!

---

<div class="post-metadata">

**Author:** ![andrew](http://forum.qcad.org/user_avatar/forum.qcad.org/andrew/32/15013_2.png) [@andrew](http://forum.qcad.org/u/andrew)\
**Post date:** [June 10, 2009, 1:11pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/2 "2009-06-10T13:11:30Z")

</div>

The QCAD code has an example:

[http://www.google.com/codesearch/p?hl=en#QUfLj\_0jKNQ/qcad-2.0.3.3-1.src/qcadlib/src/filters/rs\_filterdxf.cpp&q=::writeHatch](http://www.google.com/codesearch/p?hl=en#QUfLj_0jKNQ/qcad-2.0.3.3-1.src/qcadlib/src/filters/rs_filterdxf.cpp&q=::writeHatch)

---

<div class="post-metadata">

**Author:** ![bonsalty](http://forum.qcad.org/letter_avatar_proxy/v4/letter/b/45deac/32.png) [@bonsalty](http://forum.qcad.org/u/bonsalty)\
**Post date:** [June 10, 2009, 6:27pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/3 "2009-06-10T18:27:44Z")

</div>

Thanks I managed to use it. I inserted a single loop with 3 edges(a triangle 🙂 ) and succeded to make a triangle with hatch.

But pattern name is still a mistery.  
I tried “EARTH”, “ANSI31”,… default pattern names,but nothing happens. However I can see that names in autocad when I click on my hatchobject, but it doesnt change the hatch style?!  
I tried rotate and scale property too,they doesnt work either. Its always a 45 degree hatch.The solid property works fine.

---

<div class="post-metadata">

**Author:** ![wolfhergang](http://forum.qcad.org/letter_avatar_proxy/v4/letter/w/b5e925/32.png) [@wolfhergang](http://forum.qcad.org/u/wolfhergang)\
**Post date:** [November 7, 2013, 10:14pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/4 "2013-11-07T22:14:07Z")

</div>

Im having some troubles in writing a solid arc hatch, i wrote a single hatch, with a single loop with a single edge all of em with the same attributes and it just doesnt work, any advice?

---

<div class="post-metadata">

**Author:** ![wolfhergang](http://forum.qcad.org/letter_avatar_proxy/v4/letter/w/b5e925/32.png) [@wolfhergang](http://forum.qcad.org/u/wolfhergang)\
**Post date:** [November 13, 2013, 11:56pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/5 "2013-11-13T23:56:59Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![andrew](http://forum.qcad.org/user_avatar/forum.qcad.org/andrew/32/15013_2.png) [@andrew](http://forum.qcad.org/u/andrew)\
**Post date:** [November 14, 2013, 1:01pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/6 "2013-11-14T13:01:00Z")

</div>

wolfhergang: Can you post your code or the exact example data?  
I don’t see how a hatch with one loop which contains one arc can form a closed, hatchable contour (unless the arc is in fact a full circle?).

---

<div class="post-metadata">

**Author:** ![wolfhergang](http://forum.qcad.org/letter_avatar_proxy/v4/letter/w/b5e925/32.png) [@wolfhergang](http://forum.qcad.org/u/wolfhergang)\
**Post date:** [November 14, 2013, 11:17pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/7 "2013-11-14T23:17:35Z")

</div>

My code is this:

DxfExporter exportador(“testingWriting.dxf”);  
exportador.addEntity(new DxfCircle(0,0,0,100,DL\_Attributes()));  
DL\_Attributes generalAttrib = DL\_Attributes(“0”,2,16776960, -1, “BYLAYER”);  
exportador.addEntity((new DxfHatch(1,true,1,0,“SOLID”,generalAttrib)));  
exportador.addEntity(new DxfHatchLoop(1,generalAttrib));  
exportador.addEntity(new DxfHatchEdge(0,0,100,0,6.75,true,generalAttrib));

if(exportador.writeToDxfFile()){  
std::cout\<\<“File wrote correctly”\<\<std::endl;  
}else{  
std::cout\<\<“Error at writing”\<\<std::endl;  
}

in my implementation you “add” the entities to a container then you write the container to the file.  
the entity constructors are basically the same as the DL\_Entity ones, but mine store the attributes, thats it.

the arc is supposed to be a full circle from 0 to 6.74 degrees but it doesnt render.

pd: sorry about my english, if something doesnt make sense let me know and ill try to explain myself.

---

<div class="post-metadata">

**Author:** ![wolfhergang](http://forum.qcad.org/letter_avatar_proxy/v4/letter/w/b5e925/32.png) [@wolfhergang](http://forum.qcad.org/u/wolfhergang)\
**Post date:** [November 14, 2013, 11:22pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/8 "2013-11-14T23:22:47Z")

</div>

Maybe im just confused about how im supposed to hatch things…

---

<div class="post-metadata">

**Author:** ![andrew](http://forum.qcad.org/user_avatar/forum.qcad.org/andrew/32/15013_2.png) [@andrew](http://forum.qcad.org/u/andrew)\
**Post date:** [November 15, 2013, 1:13pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/9 "2013-11-15T13:13:02Z")

</div>

wolfhergang: this code does not look familiar to me. dxflib does not provide the classes named “DxfExporter”, “DxfHatch”, “DxfCircle”, etc.

Here’s a minimal but complete example that writes a DXF file with one hatch entity using dxflib 3.3.4:  
#include “dl\_dxf.h”

int main() {  
DL\_Dxf dxf;  
DL\_WriterA\* dw = dxf.out(“hatch.dxf”, DL\_Codes::AC1015);

```
// section header:
dxf.writeHeader(*dw);
dw->sectionEnd();

// section tables:
{ 
    dw->sectionTables();

    // VPORT:
    dxf.writeVPort(*dw);

    // LTYPE:
    dw->tableLinetypes(1);
    dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Continuous", 0, 0, 0.0));
    dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER", "", 0, 0, 0.0));
    dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "", 0, 0, 0.0));
    dw->tableEnd();

    // LAYER:
    dw->tableLayers(1);
    dxf.writeLayer(
        *dw,
        DL_LayerData("0", 0),
        DL_Attributes("", 2, 0, 100, "CONTINUOUS")
    );
    dw->tableEnd();

    // STYLE:
    dw->tableStyle(1);
    DL_StyleData style("Standard", 0, 0.0, 1.0, 0.0, 0, 2.5, "txt", "");
    style.bold = false;
    style.italic = false;
    dxf.writeStyle(*dw, style);
    dw->tableEnd();

    // VIEW:
    dxf.writeView(*dw);

    // UCS:
    dxf.writeUcs(*dw);

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

    // DIMSTYLE:
    dxf.writeDimStyle(*dw, 2.5, 0.625, 0.625, 0.625, 2.5);

    // BLOCK_RECORD:
    dxf.writeBlockRecord(*dw);
    dw->tableEnd();

    dw->sectionEnd();
}

// BLOCK:
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");
dw->sectionEnd();

// ENTITIES:
dw->sectionEntities();

DL_Attributes attributes("0", 2, 0, -1, "BYLAYER");

// start hatch with one loop:
DL_HatchData data(1, false, 100.0, 0.0, "ESCHER", 0.0, 0.0);
dxf.writeHatch1(*dw, data, attributes);

// start loop:
DL_HatchLoopData lData(1);
dxf.writeHatchLoop1(*dw, lData);

// write edge:
DL_HatchEdgeData eData(
    0.0,
    0.0,
    100.0,
    0.0,
    M_PI*2,
    true
);
dxf.writeHatchEdge(*dw, eData);

// end loop:
dxf.writeHatchLoop2(*dw, lData);

// end hatch:
dxf.writeHatch2(*dw, data, attributes);

// end section ENTITIES:
dw->sectionEnd();
dxf.writeObjects(*dw, "MY_OBJECTS");
dxf.writeObjectsEnd(*dw);

dw->dxfEOF();
dw->close();
delete dw;

return 0;

```

}

---

<div class="post-metadata">

**Author:** ![wolfhergang](http://forum.qcad.org/letter_avatar_proxy/v4/letter/w/b5e925/32.png) [@wolfhergang](http://forum.qcad.org/u/wolfhergang)\
**Post date:** [November 15, 2013, 10:51pm UTC](http://forum.qcad.org/t/dxf-writehatch-but-how/290/10 "2013-11-15T22:51:24Z")

</div>

My problem was that i was writing the hatch edge using degrees when im supposed to use radians.

DL\_HatchEdgeData eData(0.0, 0.0, 100.0, 0.0, M\_PI\*2, true);

that single line solved my problem, than you very much Andrew.
