haplokuon / netDxf

.net dxf Reader-Writer
MIT License
993 stars 404 forks source link

FontFamilyName getting overwritten #471

Open ChristianSchroth opened 1 year ago

ChristianSchroth commented 1 year ago

When I want to add a Text to my DxfDocument, the FontFamilyName gets overwritten. Code to reproduce the bug:

DxfDocument doc = new DxfDocument();
TextStyle textStyle = TextStyle.Default;
textStyle.FontFamilyName = "Arial";
Text text = new Text() { Value = "Some Text", Position = new Vector3(0), Style = textStyle };
doc.Entities.Add(text);

doc.Save(@"C:\Temp\Test.dxf");

I can open the exported DXF-File with my CAD program an the text is at right position, but the Font is now "simplex" The only workaround that I found was to add the following line after adding the entity:

text.Style.FontFamilyName = "Arial";
haplokuon commented 1 year ago

Read this. It is the same kind of problem, your Default text style is being overwritten by the one already present in your DxfDocument.

DavidWishengrad commented 1 year ago

It's just like adding a block to a drawing that already has that block definition.

Running into these errors with code can be a little tricky. If your code breaks after your drawing has a text style or block incorrectly defined then you will always get that wrong definition inserting a new style or block with the same name, until you remove all references to it and then purge it out. You don't get the warning prompt 'block already exists, do you wish to redefine, when doing it directly from code.

ChristianSchroth commented 1 year ago

Thank you for the quick reply. What I don´t understand is, that its a new Document add I add a Entity. So there is nothing inside this document. So I thought it would apply the style of the new Element. The example with the layers is understandable. If you add the same layer again it doesn´t get overwritten. But in my case I don´t overwrite anything, I just add a new EntityObject. Maybe I don´t understand DXF right, but I thought that the individual options are higher ranked than the layer options.

Did I get it right, that its the normal behavior of the DXF-Definition?

DavidWishengrad commented 1 year ago

There is a defualt dimstyle already present in a new doc, as is clearly stated in the link that will solve your problem. Read the link and try using the code which was supplied to you in it.

haplokuon commented 1 year ago

A Text entity holds a reference to a TextStyle, so when the text is added to the document so does the style, same thing happens with its layer. All classes derived form TableObject behave in the same way, the destination document always takes precedence. If for whatever reason you want to change this behavior it is your responsibility to check if a table already exists with the same name and act in consequence.

ChristianSchroth commented 1 year ago

Thanks for your replies. I will keep that in mind.