View Issue Details

IDProjectCategoryView StatusLast Update
0002043DrawingPatchpublic2015-12-15 13:18
Reportertriplus Assigned Toyorik  
Status closedResolutionfixed 
Platform14.04OSUbuntuOS VersionLinux
Fixed in Version0.16 
Summary0002043: DXF export of Drawing pages (template unopenable)
DescriptionI will probably make supplementary set of DXF drawing templates:

I familiarized myself with the procedure:

There was one issue i decided to investigate as i didn't like the limitation:

"This last modifications unfortunately turns the template unopenable, I still didn't find how to solve that."

Adding $blocks and $entities information in the DXF drawing template should be done as comments.



- 999 group code indicates the next line (value) is comment.
- 0 group code is the group code that already existed before ENDSEC and before we added the comment (no new 0 group code is added before ENDSEC)!

This change will make DXF drawing template openable! As for FreeCAD ( supporting this change this lines needs to change:

result = template.replace("$blocks",blocks[:-1])
result = result.replace("$entities",entities[:-1])

Supporting comments:
result = template.replace("999\n$blocks",blocks[:-1])
result = result.replace("999\n$entities",entities[:-1])

But actually newline has to be taken into consideration therefore the long version:
result = template.replace("999\n$blocks",blocks[:-1])
result = result.replace("999\n$entities",entities[:-1])
result = template.replace("999\r$blocks",blocks[:-1])
result = result.replace("999\r$entities",entities[:-1])
result = template.replace("999\r\n$blocks",blocks[:-1])
result = result.replace("999\r\n$entities",entities[:-1])

Would do. For example current A4_Landscape.dxf and A3_Landscape.dxf use Windows convention for newline '\r\n' and when i tested it it didn’t work as i only added the changes for \n newline the first time.

The last changes needed are adding 999 group code to existing two DXF drawing templates just before the following lines:

TagsNo tags attached.


child of 0002065 closedyorik FreeCAD DXF Drawing Templates 



2015-04-09 00:51

developer   ~0005980

I assumed this would work:

result = template.replace("999\n$blocks",blocks[:-1])
result = result.replace("999\n$entities",entities[:-1])
result = template.replace("999\r$blocks",blocks[:-1])
result = result.replace("999\r$entities",entities[:-1])
result = template.replace("999\r\n$blocks",blocks[:-1])
result = result.replace("999\r\n$entities",entities[:-1])

But after testing it more i noticed only last (two) lines were picked up at export and only DXF drawing template with \r\n newline will work. If i switch the order an put \n lines at the end only DXF drawing template with \n newline will work...

I could investigate this more but i am quite sure for whoever merges this changes will know what to do to make DXF drawing templates with all 3 possible newline character(s) to work.


2015-04-09 03:29

administrator   ~0005981

Very good idea triplus... We just need to sort out that character thing, but in any case if your 6-lines solution works, we can already commit it...


2015-04-09 13:06

developer   ~0005982


I was sure it would work but after additional tests in the end it looks like only last two lines are picked up.

I will investigate more.


2015-04-09 15:31

developer   ~0005983

The most elegant solution would be to use universal newline support:


Alternative would be to code a solution that would work in Python 2 and Python 3 and would take care of converting newline to default newline as Python sees it \n in situations like this. I would say that probably makes little sense as universal newline support is enabled by default in Python on majority of systems. If FreeCAD would be used on a platform where universal newline support was explicitly disabled in Python the worst case scenario from exporting DXF drawing template point of view would be only the DXF template itself would be the output of the DXF export operation without the drawing views on it. To overcome this user should convert the newline to \n manually in the DXF drawing template itself. This will work on such systems if the original DXF drawing template had \r\n or \r newline instead of \n.

Side note: Mac users will potentially use/make DXF drawing templates with \r newline. This will work with my proposed solution just fine but from more general FreeCAD point of view i noticed importing such a template in FreeCAD will fail and importing in LibreCAD will partially fail. The reason i mention this is maybe in the future there will be a policy to use universal newline support in more places in FreeCAD.

Anyway what i would do first i would enable universal newline support when opening DXF drawing template:

f = pythonopen(template,"U")

And add support for DXF comments like this:

Supporting comments:
result = template.replace("999\n$blocks",blocks[:-1])
result = result.replace("999\n$entities",entities[:-1])

I discovered LibreCAD is a bit "fragile" ATM when it comes to comments and it won't do comments everywhere in the DXF file. If you plan to add additional comment groups somewhere else in DXF drawing template in the future test it if it plays along fine with LibreCAD.

After you will add 999 group code to A4_Landscape.dxf drawing template open it in LibreCAD and you will notice additional point in bottom left corner when pressing Auto Zoom that you will probably want to remove.



2015-04-09 15:57

developer   ~0005984

While you are at editing the templates additional note.

Probably it would make sense for you to convert \r\n to \n newline in current A4_Landscape.dxf and A3_Landscape.dxf drawing templates. That would make mentioned DXF drawing templates to always work as expected including on rare occasions when FreeCAD is run on systems where Python doesn't have universal newline support enabled.


2015-04-10 15:09

developer   ~0005991

Last edited: 2015-04-10 15:11

What i can do is i can make the changes to default two set of DXF templates and upload them together with ISO7200 and PLAIN set of DXF drawing templates i intend to do.

I can add \n newline and remove that dot i mentioned in A4_Landscape.dxf. I discovered there are some artefacts in two default set of DXF drawing templates like exported font mixed with real font...

I will add some layers in ISO7200 and PLAIN set of DXF drawing templates with predefined values for font and line thickness and i was thinking i could do the same with current two drawing templates for consistency between default set of DXF drawing templates. I would take all default values from existing SVG counterparts just like i will do with ISO7200 and PLAIN set of DXF drawing templates.

If you agree you just take care of adding comment support in and 999 group code to existing two DXF templates and close this issue.


2015-04-10 15:17

administrator   ~0005992

Can you make the changes to and upload it here (or a patch generated with "git diff > somefile.patch")? It would save me some time... Thanks!


2015-04-10 16:00

developer   ~0005995

Last edited: 2015-04-10 16:00


I downloaded, A3_Landscape.dxf and A4_Landscape.dxf files. I made this changes:

- Universal newline support and 999 group code detection.
- Added 999 group code to DXF templates.

I uploaded the changes as


2015-04-10 16:01

developer (42,479 bytes)


2015-04-15 23:32

developer   ~0006029


"- Save as DXF r12 in the same folder and with the same name as the corresponding SVG template"

Is there any specific reason DXF r12 "MUST" be used? I want to provide standard width for drawing template lines and titleblock text by default but it looks like saving in DXF r12 format in LibreCAD this information is lost. DXF r14 works better but for what i would like to achieve it starts working as expected when choosing and saving in DXF 2000 file format in LibreCAD.

I did some quick tests and basically it comes down to additional group codes are added to the file. I didn’t investigate but probably DXF r12 doesn't have that set of group codes and that is why information is lost when saving in DXF r12 format. I imported files saved with:

- DXF r12
- DXF r14
- DXF 2000

In FreeCAD and Inkscape and the result is the same therefore i am thinking it shouldn't make much difference if i use DXF 2000 instead of DXF r12 for drawing templates?

DXF importers should not complain as they probably read only supported set of group codes and leave the rest? User can still save exported drawing as DXF r12 file if needed for whatever reason. Line and text width would be lost by doing that but if DXF r12 format is needed i guess that is expected behaviour.



2015-04-16 14:32

developer   ~0006031

I decided i will use DXF 2000 format as it has the needed group codes to preserve information for line and text width. DXF r14 could probably suffice but in less elegant way.

DXF importers capable of reading for example only DXF r12 format will simply left out this group codes and import geometry without line and text width information. Users can still re-save exported DXF drawings in DXF r12 format if they need that. ATM i do believe it makes more sense to provide to the FreeCAD users standard line and text width by default! It's too much hassle applying that again and again after each DXF export.

- LibereCAD font situation is currently a bit lacking and ATM i can't choose default font that would provide wide range of characters for majority of languages.
- Dimensions have some settings available ATM but it is not possible to be 100% standard compliant by default with current capabilities.

Anyway i wanted to share some insights and in a couple of days the DXF drawing templates and basic tests will be completed and i will upload the templates to be merged!



2015-04-16 16:34

administrator   ~0006032

The R12 format is not mandatory, but FreeCAD exports its stuff as R12, so it's probably safer to have everything under the same version. But if you make a R2000 (or more) template and it works when exporting (at least, let's say, both in qcad/librecad and autocad), I think there is no problem!


2015-04-16 17:08

developer   ~0006033

Great. Yes once the work is finished it will be easy to test compatibility with other CAD software. The way i understand it based on my simple tests (LibreCAD) R2000 format for example adds additional group codes with additional information not available in R12 format. BUT other group codes that determines for example a line are the same between both formats. Therefore a line written in R2000 format will have additional group code for line width and other group codes will stay the same as in R12. Anyway this is the theory but once the work is finished it will be tested in real world.

Just a side note:

There was one observation about how line weight differs after export:

"There are still some glitches here and there.
The lineweights are decided by the Arch section planes view, it performs a cut between the model and the section plane, then retains the sections and shows them on top of the rest with a thicker lineweight (you can configure that in the arch prefs), something must not have worked correctly there."

I would assume everything worked as expected but thicker line weight information was lost as appropriate group code for R12 format doesn't exist. Don't take this as fact as i don't have much experience with DXF format but based on my investigation and LibreCAD behaviour i would make that conclusion. DXF exporter would need to support group code for line weight and probably that indicates R12 format won't do.


2015-04-16 18:52

administrator   ~0006034

I think we can work around that without much problems, for ex. place these lines in another layer. But after all, if all the templates are R2000, we could also "upgrade" the output of the Drawing module. Just, in that case, we also need to build a default empty DXF header for the cases when there is no template.

But let's test your templates first, then we iron the details out...


2015-04-16 21:04

developer   ~0006035

Last edited: 2015-04-16 21:10

Yes it will make more sense to go into details a bit latter if there will be issues. At least in theory i don't see issues as R12 is a subset of group codes compared to R2000.

For example i didn't change the header of R12 format file and i did put R2000 group codes for line width in that R12 file and LibreCAD didn't complain a bit. If it would only support R12 format it would probably just not use group code for line width and would only use recognized group code set from R12 format. The same as when importing DXF file in FreeCAD. Therefore i am thinking there should be no issues exporting/attaching a set of R12 group codes (produced and added by FreeCAD DXF exporter) in R2000 file (DXF template).

I shared my understanding of some issues as i have a filling you will be confronted with this challenges in the future if trying to extend FreeCAD DXF capabilities.

As for layers at least in regards to LibreCAD this only starts working using R14 format. That is why i said earlier i will rather use R2000 format as it has group codes for both default layer line width and individual entities like text or line width.


2015-04-17 18:48

developer   ~0006039

Last edited: 2015-04-17 18:54

If you will ever want to add DXF lineweight support to exported DXF drawings, like in mentioned Arch section planes view DXF export example above, here is the most basic explanation what needs to be done:

DXF exporter should add additional lineweight information to the line entity by adding group code 370. Example: lineweight to be exported is 0.5mm. The line entity in exported DXF file should have this additional group code + value pair added:


Note that LibreCAD only supports standard lineweights (max. 2.11mm):

"Well, the DXF reference defines the lineweight as a value from 0 to 211 in 1/100th mm. That means the the maximum lineweight is 2.11mm or 0.083 Inch."


2015-04-19 22:05

developer   ~0006054

I completed the DXF templates but as i have little time ATM and i need to test them before the upload and it will probably take a couple of days to do that (test them). Anyway i will include all the templates including current default 2 ones where i cleaned up some small artefacts.


Would you by any chance know, why current default two templates always gets 2 additional empty blocks at export time (Blocks named *U0 and *U1)? I didn’t notice that in first tests with reworked DXF templates and before i investigate i was hoping you have an explanation.


Universal newline support it looks like the most elegant solution but they hidden this fact:

"'U' mode is deprecated and will raise an exception in future versions
    of Python. It has no effect in Python 3. Use newline to control
    universal newlines mode."

It will work just fine until FreeCAD migrates to Python 3 and after that who knows? We can't use Python 3 solution ATM can we?

Should we just use universal newline support for now or search for something else?


I noticed something regarding:

# find & replace editable texts

There is a good chance user will use for example SCALE tag in SVG template and SCALE predefined name in DXF template. Replacement will therefore work just fine but additional lines will be (partially) replaced. For example saving in R2000 format in LibreCAD:


Should we pay attention to this or just take care default set of templates don't have name/tag clashes for now?



2015-04-20 14:57

developer   ~0006060


I am not getting empty blocks named *U0 and *U1 in reworked DXF drawing templates and therefore i will not investigate this any further for now.


Python 2:

open("filename", 'U')

Python 3:

open("filename", 'r')


We can use 'U' for now (Python 2) to enable universal newline support. As is has no effect in Python 3 and in Python 3 defaults (text mode) will be used instead. Python 3 defaults are mode='r' newline=None. Universal newlines mode is enabled in Python 3 by default (text mode):

"On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller."

Therefore i think we should just use mode='U' for now and it will continue to work in Python 3. If there is separate FreeCAD Python 3 branch default text mode 'r' can be set instead to open the DXF drawing template with universal newline support and not to raise an exception.


I will use simple fix instead of code changes and that is prefix FC- when in doubt (example FC-SCALE) for now.

I have some spare time today and will try to conclude testing and upload DXF files to be reviewed and merged.


Related Changesets

FreeCAD: master bf324049

2015-04-23 00:01:55


Details Diff
added new set of Drawing templates - fixes 0002065 0002043 0002005 Affected Issues
0002005, 0002043, 0002065
mod - src/Mod/Draft/ Diff File
add - src/Mod/Drawing/Templates/A0_Landscape_ISO7200.dxf Diff File
mod - src/Mod/Drawing/Templates/A0_Landscape_ISO7200.svg Diff File
add - src/Mod/Drawing/Templates/A0_Landscape_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A0_Landscape_plain.svg Diff File
add - src/Mod/Drawing/Templates/A0_Portrait_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A0_Portrait_plain.svg Diff File
add - src/Mod/Drawing/Templates/A1_Landscape_ISO7200.dxf Diff File
mod - src/Mod/Drawing/Templates/A1_Landscape_ISO7200.svg Diff File
add - src/Mod/Drawing/Templates/A1_Landscape_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A1_Landscape_plain.svg Diff File
add - src/Mod/Drawing/Templates/A1_Portrait_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A1_Portrait_plain.svg Diff File
add - src/Mod/Drawing/Templates/A2_Landscape_ISO7200.dxf Diff File
mod - src/Mod/Drawing/Templates/A2_Landscape_ISO7200.svg Diff File
add - src/Mod/Drawing/Templates/A2_Landscape_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A2_Landscape_plain.svg Diff File
add - src/Mod/Drawing/Templates/A2_Portrait_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A2_Portrait_plain.svg Diff File
mod - src/Mod/Drawing/Templates/A3_Landscape.dxf Diff File
mod - src/Mod/Drawing/Templates/A3_Landscape.svg Diff File
add - src/Mod/Drawing/Templates/A3_Landscape_ISO7200.dxf Diff File
mod - src/Mod/Drawing/Templates/A3_Landscape_ISO7200.svg Diff File
add - src/Mod/Drawing/Templates/A3_Landscape_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A3_Landscape_plain.svg Diff File
add - src/Mod/Drawing/Templates/A3_Portrait_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A3_Portrait_plain.svg Diff File
mod - src/Mod/Drawing/Templates/A4_Landscape.dxf Diff File
mod - src/Mod/Drawing/Templates/A4_Landscape.svg Diff File
add - src/Mod/Drawing/Templates/A4_Landscape_ISO7200.dxf Diff File
mod - src/Mod/Drawing/Templates/A4_Landscape_ISO7200.svg Diff File
add - src/Mod/Drawing/Templates/A4_Landscape_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A4_Landscape_plain.svg Diff File
add - src/Mod/Drawing/Templates/A4_Portrait_ISO7200.dxf Diff File
mod - src/Mod/Drawing/Templates/A4_Portrait_ISO7200.svg Diff File
add - src/Mod/Drawing/Templates/A4_Portrait_plain.dxf Diff File
mod - src/Mod/Drawing/Templates/A4_Portrait_plain.svg Diff File

Issue History

Date Modified Username Field Change
2015-04-08 21:52 triplus New Issue
2015-04-09 00:51 triplus Note Added: 0005980
2015-04-09 03:29 yorik Note Added: 0005981
2015-04-09 03:29 yorik Assigned To => yorik
2015-04-09 03:29 yorik Status new => assigned
2015-04-09 13:06 triplus Note Added: 0005982
2015-04-09 15:31 triplus Note Added: 0005983
2015-04-09 15:57 triplus Note Added: 0005984
2015-04-10 15:09 triplus Note Added: 0005991
2015-04-10 15:11 triplus Note Edited: 0005991
2015-04-10 15:17 yorik Note Added: 0005992
2015-04-10 16:00 triplus Note Added: 0005995
2015-04-10 16:00 triplus Note Edited: 0005995
2015-04-10 16:01 triplus File Added:
2015-04-15 23:32 triplus Note Added: 0006029
2015-04-16 14:32 triplus Note Added: 0006031
2015-04-16 16:34 yorik Note Added: 0006032
2015-04-16 17:08 triplus Note Added: 0006033
2015-04-16 18:52 yorik Note Added: 0006034
2015-04-16 21:04 triplus Note Added: 0006035
2015-04-16 21:10 triplus Note Edited: 0006035
2015-04-17 18:48 triplus Note Added: 0006039
2015-04-17 18:54 triplus Note Edited: 0006039
2015-04-19 22:05 triplus Note Added: 0006054
2015-04-20 14:57 triplus Note Added: 0006060
2015-04-21 20:01 yorik Relationship added child of 0002065
2015-04-22 22:11 yorik Changeset attached => FreeCAD Master master bf324049
2015-04-22 22:11 yorik Status assigned => closed
2015-04-22 22:11 yorik Resolution open => fixed
2015-12-15 13:18 yorik Fixed in Version => 0.16