Skip to content

Commit

Permalink
initial support for HATCH entity
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed May 9, 2020
1 parent 378d5d2 commit c658e7c
Show file tree
Hide file tree
Showing 13 changed files with 1,380 additions and 5 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ save the file as either R12 or the newest version possible (e.g., R2013 or R2010
## Status

Support for DXF files is complete from versions R10 through R2014 _EXCEPT_ for the following entities:
- HATCH
- MESH
- MLEADER
- SURFACE
Expand Down
15 changes: 14 additions & 1 deletion src/IxMilia.Dxf.Generator/GeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,12 @@ public string WriteConverter(XElement property)
}
}

public IEnumerable<string> WriteCustomCode(XElement spec)
{
var code = AttributeOrDefault(spec, "Code");
return new[] { code };
}

public IEnumerable<string> WriteProperty(XElement spec, XElement entity)
{
var property = GetPropertiesAndPointers(entity).Single(p => Name(p) == spec.Attribute("Property").Value);
Expand Down Expand Up @@ -894,6 +900,8 @@ public IEnumerable<string> WriteSpecificValue(XElement spec)
}
}

private int _foreachLevel = 0;

public IEnumerable<string> WriteValue(XElement spec, XElement entity)
{
switch (spec.Name.LocalName)
Expand All @@ -914,9 +922,12 @@ public IEnumerable<string> WriteValue(XElement spec, XElement entity)
lines.Add("{");
}

lines.Add($"{indent}foreach (var item in {property})");
var itemSuffix = _foreachLevel == 0 ? "" : _foreachLevel.ToString();
lines.Add($"{indent}foreach (var item{itemSuffix} in {property})");
lines.Add($"{indent}{{");
_foreachLevel++;
lines.AddRange(spec.Elements().SelectMany(e => WriteValue(e, entity)).Select(l => $"{indent} {l}"));
_foreachLevel--;
lines.Add($"{indent}}}");
if (condition != null)
{
Expand All @@ -927,6 +938,8 @@ public IEnumerable<string> WriteValue(XElement spec, XElement entity)
return lines;
case "WriteExtensionData":
return new[] { "AddExtensionValuePairs(pairs, version, outputHandles);" };
case "WriteCustomCode":
return WriteCustomCode(spec);
default:
throw new NotSupportedException();
}
Expand Down
75 changes: 74 additions & 1 deletion src/IxMilia.Dxf.Generator/Specs/EntitiesSpec.xml
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,80 @@
HATCH
-->
<!-- TODO MinVersion="R14" -->
<Entity Name="DxfHatch" EntityType="Hatch" SubclassMarker="AcDbHatch" TypeString="HATCH" GenerateReaderFunction="false" MinVersion="R14">
<Property Name="ElevationPoint" Code="10" Type="DxfPoint" DefaultValue="DxfPoint.Origin" CodeOverrides="10,20,30" />
<Property Name="ExtrusionDirection" Code="210" Type="DxfVector" DefaultValue="DxfVector.ZAxis" CodeOverrides="210,220,230" />
<Property Name="PatternName" Code="2" Type="string" DefaultValue="null" />
<Property Name="FillMode" Code="70" Type="DxfHatchPatternFillMode" DefaultValue="DxfHatchPatternFillMode.PatternFill" ReadConverter="(DxfHatchPatternFillMode){0}" WriteConverter="(short){0}" />
<Property Name="FillColor" Code="63" Type="DxfColor" DefaultValue="DxfColor.ByLayer" ReadConverter="DxfColor.FromRawValue({0})" WriteConverter="DxfColor.GetRawValue({0})" MinVersion="R2004" />
<Property Name="IsAssociative" Code="71" Type="bool" DefaultValue="false" ReadConverter="BoolShort({0})" WriteConverter="BoolShort({0})" />
<Property Name="_boundaryPathCount" Code="91" Type="int" DefaultValue="0" />
<Property Name="BoundaryPaths" Code="92" Type="BoundaryPathBase" DefaultValue="" AllowMultiples="true" />
<Property Name="HatchStyle" Code="75" Type="DxfHatchStyle" DefaultValue="DxfHatchStyle.OddParity" ReadConverter="(DxfHatchStyle){0}" WriteConverter="(short){0}" />
<Property Name="PatternType" Code="76" Type="DxfHatchPatternType" DefaultValue="DxfHatchPatternType.UserDefined" ReadConverter="(DxfHatchPatternType){0}" WriteConverter="(short){0}" />
<Property Name="PatternAngle" Code="52" Type="double" DefaultValue="0.0" WriteCondition="FillMode == DxfHatchPatternFillMode.PatternFill" />
<Property Name="PatternScale" Code="41" Type="double" DefaultValue="1.0" WriteCondition="FillMode == DxfHatchPatternFillMode.PatternFill" />
<Property Name="IsPatternDoubled" Code="77" Type="bool" DefaultValue="false" ReadConverter="BoolShort({0})" WriteConverter="BoolShort({0})" WriteCondition="FillMode == DxfHatchPatternFillMode.PatternFill" />
<Property Name="_patternDefinitionLineCount" Code="78" Type="int" DefaultValue="0" WriteConverter="(short){0}" />
<Property Name="PatternDefinitionLines" Code="53" Type="PatternDefinitionLine" DefaultValue="" AllowMultiples="true" />
<Property Name="PixelSize" Code="47" Type="double" DefaultValue="1.0" />
<Property Name="_seedPointCount" Code="98" Type="int" Accessibility="private" DefaultValue="0" />
<Property Name="SeedPoints" Code="10" Type="DxfPoint" DefaultValue="DxfPoint.Origin" AllowMultiples="true" CodeOverrides="10,20" />
<Property Name="IsGradient" Code="450" Type="bool" DefaultValue="false" ReadConverter="BoolLong({0})" WriteConverter="BoolLong({0})" MinVersion="R2004" />
<Property Name="_zero" Code="451" Type="long" DefaultValue="0" Accessibility="private" MinVersion="R2004" Comment="Reserved for future use." />
<Property Name="GradientColorMode" Code="452" Type="DxfGradientColorMode" DefaultValue="DxfGradientColorMode.TwoColorGradient" ReadConverter="(DxfGradientColorMode){0}" WriteConverter="(long){0}" MinVersion="R2004" Comment="Only used by dialog code." />
<Property Name="NumberOfColors" Code="453" Type="long" DefaultValue="0" MinVersion="R2004" Comment="Unknown usage." />
<Property Name="GradientRotationAngle" Code="460" Type="double" DefaultValue="0.0" MinVersion="R2004" Comment="Rotation angle for gradients in radians." />
<Property Name="GradientDefinitionShift" Code="461" Type="double" DefaultValue="0.0" MinVersion="R2004" Comment="Unknown usage." />
<Property Name="ColorTint" Code="462" Type="double" DefaultValue="0.0" MinVersion="R2004" Comment="Range is [0.0, 1.0]." />
<Property Name="_reserved" Code="463" Type="double" DefaultValue="0.0" Accessibility="private" MinVersion="R2004" Comment="Reserved for future use." />
<Property Name="StringValue" Code="470" Type="string" DefaultValue="LINEAR" MinVersion="R2004" Comment="Unknown usage." />
<WriteOrder>
<WriteSpecificValue Code="100" Value="&quot;AcDbHatch&quot;" />
<WriteProperty Property="ElevationPoint" />
<WriteProperty Property="ExtrusionDirection" />
<WriteProperty Property="PatternName" />
<WriteProperty Property="FillMode" />
<WriteProperty Property="FillColor" />
<WriteProperty Property="IsAssociative" />
<WriteSpecificValue Code="91" Value="BoundaryPaths.Count" />
<Foreach Property="BoundaryPaths">
<WriteCustomCode Code="item.AddValuePairs(pairs, version, outputHandles);" />
</Foreach>
<WriteProperty Property="HatchStyle" />
<WriteProperty Property="PatternType" />
<WriteProperty Property="PatternAngle" />
<WriteProperty Property="PatternScale" />
<WriteProperty Property="IsPatternDoubled" />
<WriteSpecificValue Code="78" Value="(short)PatternDefinitionLines.Count" />
<Foreach Property="PatternDefinitionLines">
<WriteSpecificValue Code="53" Value="item.Angle" />
<WriteSpecificValue Code="43" Value="item.BasePoint.X" />
<WriteSpecificValue Code="44" Value="item.BasePoint.Y" />
<WriteSpecificValue Code="45" Value="item.Offset.X" />
<WriteSpecificValue Code="46" Value="item.Offset.Y" />
<WriteSpecificValue Code="79" Value="(short)item.DashLengths.Count" />
<Foreach Property="item.DashLengths">
<WriteSpecificValue Code="49" Value="item1" />
</Foreach>
</Foreach>
<WriteProperty Property="PixelSize" />
<WriteSpecificValue Code="98" Value="SeedPoints.Count" />
<Foreach Property="SeedPoints">
<WriteSpecificValue Code="10" Value="item.X" />
<WriteSpecificValue Code="20" Value="item.Y" />
</Foreach>
<WriteProperty Property="IsGradient" />
<WriteProperty Property="_zero" />
<WriteProperty Property="GradientColorMode" />
<WriteProperty Property="NumberOfColors" />
<WriteProperty Property="GradientRotationAngle" />
<WriteProperty Property="GradientDefinitionShift" />
<WriteProperty Property="ColorTint" />
<WriteProperty Property="_reserved" />
<WriteProperty Property="StringValue" />
</WriteOrder>
</Entity>
<!--
HELIX
Expand Down
10 changes: 10 additions & 0 deletions src/IxMilia.Dxf.Generator/Specs/EntitiesSpec.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,21 @@
</xs:complexType>
</xs:element>

<xs:element name="WriteCustomCode">
<xs:complexType>
<xs:attribute name="Code" type="xs:string" use="required" />
</xs:complexType>
</xs:element>

<xs:element name="Foreach">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="WriteProperty" />
<xs:element ref="WriteSpecificValue" />
<xs:element ref="Foreach" />
<xs:element ref="WriteExtensionData" />
<xs:element ref="WriteCustomCode" />
</xs:choice>
</xs:sequence>
<xs:attribute name="Property" type="xs:string" use="required" />
Expand All @@ -145,6 +154,7 @@
<xs:element ref="WriteSpecificValue" />
<xs:element ref="Foreach" />
<xs:element ref="WriteExtensionData" />
<xs:element ref="WriteCustomCode" />
</xs:choice>
</xs:sequence>
</xs:complexType>
Expand Down
1 change: 1 addition & 0 deletions src/IxMilia.Dxf.Test/DxfCompatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ public void AutoCadReadAllEntitiesTest()
var unsupportedTypes = new[]
{
// unsupported because I need to write more information with them
typeof(DxfHatch), // need to fill in the data
typeof(DxfInsert), // need a block to insert
typeof(DxfLeader), // needs vertices
typeof(DxfMLine), // need to set MLINESTYLE and MLINESTYLE dictionary
Expand Down
Loading

0 comments on commit c658e7c

Please sign in to comment.