Author Topic: netdxf and dimension styles  (Read 171 times)

0 Members and 1 Guest are viewing this topic.

tonyatglazierscenter

  • Mosquito
  • Posts: 1
netdxf and dimension styles
« on: June 04, 2024, 04:43:15 PM »
Hello,
I'm looking into netdxf (https://github.com/haplokuon/netDxf/wiki) to try to add dimension styles to a dxf file. Can anyone help? I've added the NuGet package for it but am having trouble trying to resolve the following:

I'm trying to set the entity dimensions to a new style that I've created. I've looped thru the dimensions for all entities and am able to change their style but those same entities have some style overrides in the xdata which I haven't been able to change. Is there an update xdata dimensions call I need to perform before saving?

I've tried clearing the style overrides with 'Dim.StyleOverrides.Clear();' and that 'appears' to work until the style is changed within AutoCAD, then the xdata style returns.

The original dxf was saved with the dimensions labeled for the dimension style I'm trying to add but the dimension style was not added to the dimension styles table by the 3rd party software we use.

Also, there are 2 dimension style properties I haven't been able to set properly. Can anyone help?

Fit Tab: 'Annotative' - how to set

Text Style Tab: 'Fill color' - Setting a color does not work for custom colors

Thanks in advance, Tony.

======================
Code - C#: [Select]
  1. private void AddDimensionStylesToDXF(string fileName)
  2.         {
  3.  
  4.             //Load the dxf
  5.             netDxf.DxfDocument doc = netDxf.DxfDocument.Load(fileName);
  6.  
  7.             //Add a dimension style  
  8.             //netDxf.Tables.DimensionStyle style = new netDxf.Tables.DimensionStyle("Architectural-Tony");
  9.             netDxf.Tables.DimensionStyle style = new netDxf.Tables.DimensionStyle("XXXX");
  10.                        
  11.             //Primary Units
  12.             style.DimLengthUnits = netDxf.Units.LinearUnitType.Architectural;
  13.             style.LengthPrecision = 5;
  14.             style.FractionType = netDxf.Units.FractionFormatType.NotStacked;
  15.             style.DecimalSeparator = '.';
  16.             //style.DimRoundoff = 0.5; //Round to 1/2"
  17.             style.DimPrefix = "";
  18.             style.DimSuffix = "";
  19.             style.DimScaleLinear = 1.0; //For 'Apply to layout dimension only, must set dimlfac to inverse (-DimScaleLinear)
  20.  
  21.             style.SuppressLinearLeadingZeros = true;
  22.             style.SuppressLinearTrailingZeros = false;
  23.  
  24.             //Line
  25.             style.DimLineColor = AciColor.Green;
  26.             style.DimLineLinetype = netDxf.Tables.Linetype.Continuous; // line type is hardcoded?
  27.             style.DimLineLineweight = Lineweight.ByLayer;
  28.             style.ExtLineExtend = 2;
  29.             style.ExtLineOffset = 2;
  30.             style.ExtLine1Off = false;
  31.             style.ExtLine2Off = false;
  32.             style.ExtLineFixed = false;
  33.             style.ExtLineFixedLength = 1;
  34.  
  35.             //Symbols and Arrows
  36.             style.DimArrow1 = new netDxf.Blocks.Block("Box"); //Need to grab the arrow types (blocks) from somewhere (styleEntity.SymbolsArrowsStyles.FirstOrDefault()?)
  37.             style.DimArrow2 = new netDxf.Blocks.Block("Box");
  38.             style.ArrowSize = 2;
  39.  
  40.             //Text Style
  41.             style.TextFractionHeightScale = 0.125;
  42.             style.TextHeight = 1.5;
  43.             style.TextStyle = new netDxf.Tables.TextStyle("Basic", "simplex.shx");
  44.             style.TextColor = AciColor.ByLayer;
  45.                        
  46.             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.
  47.  
  48.             style.TextVerticalPlacement = netDxf.Tables.DimensionStyleTextVerticalPlacement.Above;
  49.             style.TextHorizontalPlacement = netDxf.Tables.DimensionStyleTextHorizontalPlacement.Centered;
  50.             style.TextOffset = 2.5; //Displays a rectangular frame around the dimension text when negative values are used.
  51.  
  52.             //Fit
  53.             style.FitOptions = netDxf.Tables.DimensionStyleFitOptions.BestFit;
  54.             //style.DimScaleOverall = 789;
  55.  
  56.             //Alternate Units
  57.             style.AlternateUnits = new netDxf.Tables.DimensionStyleAlternateUnits();
  58.             style.AlternateUnits.Enabled = false;
  59.             style.AlternateUnits.LengthUnits = netDxf.Units.LinearUnitType.Architectural;
  60.             style.AlternateUnits.LengthPrecision = 3;
  61.             style.AlternateUnits.Multiplier = 1;
  62.             //style.AlternateUnits.Roundoff = 1;
  63.             style.AlternateUnits.Prefix = "";
  64.             style.AlternateUnits.Suffix = "";
  65.             style.AlternateUnits.SuppressLinearLeadingZeros = false;
  66.             style.AlternateUnits.SuppressLinearTrailingZeros = false;
  67.  
  68.             doc.DimensionStyles.Add(style);
  69.            
  70.             doc.DrawingVariables.DimStyle = style.Name;
  71.            
  72.             List<Dimension> DimList = doc.Entities.Dimensions.ToList();
  73.             for (int i = 0; i < DimList.Count(); i++)
  74.             {
  75.                 Dimension Dim = DimList[i];
  76.                 netDxf.Tables.DimensionStyle st = style; // Dim.Style;
  77.                 Dim.Style = st;
  78.                 //Dim.Block = null;
  79.                 doc.BuildDimensionBlocks = true;
  80.                 Dim.StyleOverrides.Clear();
  81.  
  82.                 Dim.Update();
  83.             }
  84.                                  
  85.             // Save the dxf
  86.             bool result = doc.Save(fileName);
  87.  
  88.             if (result)
  89.                 MessageBox.Show("DXF successfully written.");
  90.            
  91.         }
  92.  

edit-kerry: code formatting tags added
« Last Edit: June 04, 2024, 05:23:43 PM by kdub_nz »