private void AddDimensionStylesToDXF(string fileName)
{
//Load the dxf
netDxf.DxfDocument doc = netDxf.DxfDocument.Load(fileName);
//Add a dimension style
//netDxf.Tables.DimensionStyle style = new netDxf.Tables.DimensionStyle("Architectural-Tony");
netDxf
.Tables.DimensionStyle style
= new netDxf
.Tables.DimensionStyle("XXXX");
//Primary Units
style.DimLengthUnits = netDxf.Units.LinearUnitType.Architectural;
style.LengthPrecision = 5;
style.FractionType = netDxf.Units.FractionFormatType.NotStacked;
style.DecimalSeparator = '.';
//style.DimRoundoff = 0.5; //Round to 1/2"
style.DimPrefix = "";
style.DimSuffix = "";
style.DimScaleLinear = 1.0; //For 'Apply to layout dimension only, must set dimlfac to inverse (-DimScaleLinear)
style.SuppressLinearLeadingZeros = true;
style.SuppressLinearTrailingZeros = false;
//Line
style.DimLineColor = AciColor.Green;
style.DimLineLinetype = netDxf.Tables.Linetype.Continuous; // line type is hardcoded?
style.DimLineLineweight = Lineweight.ByLayer;
style.ExtLineExtend = 2;
style.ExtLineOffset = 2;
style.ExtLine1Off = false;
style.ExtLine2Off = false;
style.ExtLineFixed = false;
style.ExtLineFixedLength = 1;
//Symbols and Arrows
style
.DimArrow1 = new netDxf
.Blocks.Block("Box"); //Need to grab the arrow types (blocks) from somewhere (styleEntity.SymbolsArrowsStyles.FirstOrDefault()?) style
.DimArrow2 = new netDxf
.Blocks.Block("Box"); style.ArrowSize = 2;
//Text Style
style.TextFractionHeightScale = 0.125;
style.TextHeight = 1.5;
style
.TextStyle = new netDxf
.Tables.TextStyle("Basic",
"simplex.shx"); style.TextColor = AciColor.ByLayer;
style.TextFillColor = null; //Set to null to specify no color. Custom colors do not work, AutoCAD doesn't read this value (noted from ezdxf documentation). Can set to none, background color. or block.
style.TextVerticalPlacement = netDxf.Tables.DimensionStyleTextVerticalPlacement.Above;
style.TextHorizontalPlacement = netDxf.Tables.DimensionStyleTextHorizontalPlacement.Centered;
style.TextOffset = 2.5; //Displays a rectangular frame around the dimension text when negative values are used.
//Fit
style.FitOptions = netDxf.Tables.DimensionStyleFitOptions.BestFit;
//style.DimScaleOverall = 789;
//Alternate Units
style
.AlternateUnits = new netDxf
.Tables.DimensionStyleAlternateUnits(); style.AlternateUnits.Enabled = false;
style.AlternateUnits.LengthUnits = netDxf.Units.LinearUnitType.Architectural;
style.AlternateUnits.LengthPrecision = 3;
style.AlternateUnits.Multiplier = 1;
//style.AlternateUnits.Roundoff = 1;
style.AlternateUnits.Prefix = "";
style.AlternateUnits.Suffix = "";
style.AlternateUnits.SuppressLinearLeadingZeros = false;
style.AlternateUnits.SuppressLinearTrailingZeros = false;
doc.DimensionStyles.Add(style);
doc.DrawingVariables.DimStyle = style.Name;
List<Dimension> DimList = doc.Entities.Dimensions.ToList();
for (int i = 0; i < DimList.Count(); i++)
{
Dimension Dim = DimList[i];
netDxf.Tables.DimensionStyle st = style; // Dim.Style;
Dim.Style = st;
//Dim.Block = null;
doc.BuildDimensionBlocks = true;
Dim.StyleOverrides.Clear();
Dim.Update();
}
// Save the dxf
bool result = doc.Save(fileName);
if (result)
MessageBox.Show("DXF successfully written.");
}