haplokuon / netDxf

.net dxf Reader-Writer
MIT License
995 stars 405 forks source link

Saving DxfDocument including a certain dimension causes error. #285

Open kyosukeSakurai opened 3 years ago

kyosukeSakurai commented 3 years ago

Saving DxfDocument including a certain dimension causes error.

There is a Dimension that causes an error when adding and saving to DxfDocument.(dimension.dxf) The error reproduction code is as follows.

 static void Main(string[] args)
    {
        DxfDocument dxffile = new DxfDocument();
        string debugfile = "C:\\tmp\\dimension.dxf";
        DxfDocument a = DxfDocument.Load(debugfile);
        var dimensions = a.Dimensions.ToList();
        List<EntityObject> tmp = new List<EntityObject>();
        foreach (netDxf.Entities.Dimension dim in dimensions)
        {
            netDxf.Entities.Dimension c = (netDxf.Entities.Dimension)dim.Clone();
            c.Block = (Block)dim.Block.Clone();
            tmp.Add(c as EntityObject);
        }
        dxffile.AddEntity(tmp);

        a.Save(@"C:\tmp\AfterDimension.dxf");
    }

note: To try the sample code, create a folder named 'tmp' directly under the C drive in advance to store the sample dxf file 'dimension.dxf'. dimension.zip

There is an error on line 3532 of DxfWriter.cs.

    case DimensionStyleOverrideType.FitOptions:
        xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short) 289));
        xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short) styleOverride.Value));

I tentatively changed the code as follows, the error will not occur.(I don't think this is the right change...)

    case DimensionStyleOverrideType.FitOptions:
        xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short) 289));
        //xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short) styleOverride.Value));
        xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short)0));

Would you please confirm if it's a code bug?

haplokuon commented 3 years ago

This issue was fixed a few months ago with the 2.4.2 release, also the piece of code you have shown is also outdated.