Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
[2020/11/16]
Browse files Browse the repository at this point in the history
* The AttributeDefiniton and Attribute Value property are now stored as strings.
	The user will be responsible on how to convert this value to and from the string.
	In the end this value is always stored in a DXF file as a string (code 1),
	and how the conversion is done from a generic object value may differ from user to user.
  • Loading branch information
haplokuon committed Nov 16, 2020
1 parent 1a35445 commit 8e9b814
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 114 deletions.
18 changes: 9 additions & 9 deletions TestDxfDocument/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ private static void TestModelSpaceBlock()
AttributeDefinition attdef = new AttributeDefinition("MyAttribute")
{
Prompt = "Enter a value:",
Value = 0,
Value = "0",
Position = Vector3.Zero,
Layer = new Layer("MyLayer")
{
Expand Down Expand Up @@ -1582,7 +1582,7 @@ private static void TestModelSpaceBlock()
AttributeDefinition def = new AttributeDefinition("AttDefOutsideBlock")
{
Prompt = "Enter value:",
Value = 0,
Value = "0",
Color = AciColor.Blue,
Position = new Vector3(0, 30, 0)
};
Expand Down Expand Up @@ -3693,7 +3693,7 @@ public static void LoadAndSaveBlocks()
AttributeDefinition attdef = new AttributeDefinition("MyAttribute")
{
Prompt = "Enter a value:",
Value = 0,
Value = "0",
Position = Vector3.Zero,
Layer = new Layer("MyLayer")
{
Expand Down Expand Up @@ -3753,7 +3753,7 @@ public static void LoadAndSaveBlocks()
AttributeDefinition def = new AttributeDefinition("AttDefOutsideBlock")
{
Prompt = "Enter value:",
Value = 0,
Value = "0",
Color = AciColor.Blue,
Position = new Vector3(0, 30, 0)
};
Expand Down Expand Up @@ -4304,7 +4304,7 @@ private static void BlockWithAttributes()
// this is the text prompt shown to introduce the attribute value when a new Insert entity is inserted into the drawing
attdef.Prompt = "InfoText";
// optionally we can set a default value for new Insert entities
attdef.Value = 0;
attdef.Value = "0";
// the attribute definition position is in local coordinates to the Insert entity to which it belongs
attdef.Position = new Vector3(1, 1, 0);

Expand Down Expand Up @@ -4355,7 +4355,7 @@ private static void BlockWithAttributes()
insert1.TransformAttributes();

// Once the insert has been created we can modify the attributes properties, the list cannot be modified only the items stored in it
insert1.Attributes[0].Value = 24;
insert1.Attributes[0].Value = 24.ToString();

// Modifying directly the layer might not get the desired results. Create one or get one from the layers table, modify it and assign it to the insert
// One thing to note, if there is already a layer with the same name, the existing one in the layers table will override the new one, when the entity is added to the document.
Expand All @@ -4378,7 +4378,7 @@ private static void BlockWithAttributes()
Insert insert2 = new Insert(block, new Vector3(10, 5, 0));

// as before now we can change the insert2 attribute value
insert2.Attributes[0].Value = 34;
insert2.Attributes[0].Value = 34.ToString();

// additionally we can insert extended data information
XData xdata1 = new XData(new ApplicationRegistry("netDxf"));
Expand Down Expand Up @@ -7503,10 +7503,10 @@ private static void WriteNestedInsert()
nestedBlock.AttributeDefinitions.Add(attdef);

Insert nestedInsert = new Insert(nestedBlock, new Vector3(0, 0, 0)); // the position will be relative to the position of the insert that nest it
nestedInsert.Attributes[0].Value = 24;
nestedInsert.Attributes[0].Value = 24.ToString();

Insert nestedInsert2 = new Insert(nestedBlock, new Vector3(-20, 0, 0)); // the position will be relative to the position of the insert that nest it
nestedInsert2.Attributes[0].Value = -20;
nestedInsert2.Attributes[0].Value = (-20).ToString();

Block block = new Block("MyBlock");
block.Entities.Add(new Line(new Vector3(-5, -5, 0), new Vector3(5, 5, 0)));
Expand Down
6 changes: 6 additions & 0 deletions doc/Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Change history

### [2020/11/16]
* The AttributeDefiniton and Attribute Value property are now stored as strings.
The user will be responsible on how to convert this value to and from the string.
In the end this value is always stored in a DXF file as a string (code 1),
and how the conversion is done from a generic object value may differ from user to user.

### [2020/11/14]
* Now it is possible to change the ImageDefinition of an Image entity.
* Now it is possible to change the UnderlayDefinition of an Underlay entity.
Expand Down
68 changes: 57 additions & 11 deletions netDxf/Entities/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected virtual TextStyle OnTextStyleChangedEvent(TextStyle oldTextStyle, Text

private AttributeDefinition definition;
private string tag;
private object attValue;
private string attValue;
private TextStyle style;
private Vector3 position;
private AttributeFlags flags;
Expand All @@ -122,9 +122,10 @@ protected virtual TextStyle OnTextStyleChangedEvent(TextStyle oldTextStyle, Text

#region constructor

internal Attribute()
internal Attribute(string tag)
: base(DxfObjectCode.Attribute)
{
this.tag = string.IsNullOrEmpty(tag) ? string.Empty : tag;
}

/// <summary>
Expand All @@ -135,7 +136,9 @@ public Attribute(AttributeDefinition definition)
: base(DxfObjectCode.Attribute)
{
if (definition == null)
{
throw new ArgumentNullException(nameof(definition));
}

this.color = definition.Color;
this.layer = definition.Layer;
Expand Down Expand Up @@ -175,7 +178,9 @@ public AciColor Color
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
this.color = value;
}
}
Expand All @@ -189,7 +194,9 @@ public Layer Layer
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
this.layer = this.OnLayerChangedEvent(this.layer, value);
}
}
Expand All @@ -203,7 +210,9 @@ public Linetype Linetype
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
this.linetype = this.OnLinetypeChangedEvent(this.linetype, value);
}
}
Expand All @@ -226,7 +235,9 @@ public Transparency Transparency
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
this.transparency = value;
}
}
Expand All @@ -240,7 +251,9 @@ public double LinetypeScale
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The line type scale must be greater than zero.");
}
this.linetypeScale = value;
}
}
Expand All @@ -264,7 +277,9 @@ public Vector3 Normal
{
this.normal = Vector3.Normalize(value);
if (Vector3.IsNaN(this.normal))
{
throw new ArgumentException("The normal can not be the zero vector.", nameof(value));
}
}
}

Expand Down Expand Up @@ -293,7 +308,6 @@ public AttributeDefinition Definition
public string Tag
{
get { return this.tag; }
internal set { this.tag = value; }
}

/// <summary>
Expand All @@ -309,7 +323,9 @@ public double Height
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The height should be greater than zero.");
}
this.height = value;
}
}
Expand All @@ -324,7 +340,9 @@ public double Width
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The Text width must be greater than zero.");
}
this.width = value;
}
}
Expand All @@ -342,7 +360,9 @@ public double WidthFactor
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The width factor should be greater than zero.");
}
this.widthFactor = value;
}
}
Expand All @@ -357,18 +377,20 @@ public double ObliqueAngle
set
{
if (value < -85.0 || value > 85.0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The oblique angle valid values range from -85 to 85.");
}
this.obliqueAngle = value;
}
}

/// <summary>
/// Gets or sets the attribute value.
/// </summary>
public object Value
public string Value
{
get { return this.attValue; }
set { this.attValue = value; }
set { this.attValue = string.IsNullOrEmpty(value) ? string.Empty : value; }
}

/// <summary>
Expand All @@ -383,7 +405,9 @@ public TextStyle Style
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
this.style = this.OnTextStyleChangedEvent(this.style, value);
}
}
Expand Down Expand Up @@ -456,15 +480,24 @@ public void TransformBy(Matrix3 transformation, Vector3 translation)
{
bool mirrText;
if (this.Owner == null)
{
mirrText = Text.DefaultMirrText;
else if(this.Owner.Owner == null)
}
else if (this.Owner.Owner == null)
{
mirrText = Text.DefaultMirrText;
}
else
{
mirrText = this.Owner.Owner.Record.Owner.Owner.DrawingVariables.MirrText;
}

Vector3 newPosition = transformation * this.Position + translation;
Vector3 newNormal = transformation * this.Normal;
if (Vector3.Equals(Vector3.Zero, newNormal)) newNormal = this.Normal;
if (Vector3.Equals(Vector3.Zero, newNormal))
{
newNormal = this.Normal;
}

Matrix3 transOW = MathHelper.ArbitraryAxis(this.Normal);

Expand Down Expand Up @@ -499,7 +532,10 @@ public void TransformBy(Matrix3 transformation, Vector3 translation)
if (Vector2.CrossProduct(newUvector, newVvector) < 0)
{
newObliqueAngle = 90 - (newRotation - newObliqueAngle);
if(!(this.Alignment == TextAlignment.Fit || this.Alignment == TextAlignment.Aligned)) newRotation += 180;
if (!(this.Alignment == TextAlignment.Fit || this.Alignment == TextAlignment.Aligned))
{
newRotation += 180;
}
this.IsBackward = !this.IsBackward;
}
else
Expand Down Expand Up @@ -579,22 +615,33 @@ public void TransformBy(Matrix3 transformation, Vector3 translation)
// the oblique angle is defined between -85 and 85 degrees
newObliqueAngle = MathHelper.NormalizeAngle(newObliqueAngle);
if (newObliqueAngle > 180)
{
newObliqueAngle = 180 - newObliqueAngle;
}

if (newObliqueAngle < -85)
{
newObliqueAngle = -85;
}
else if (newObliqueAngle > 85)
{
newObliqueAngle = 85;
}

// the height must be greater than zero, the cos is always positive between -85 and 85
double newHeight = newVvector.Modulus() * Math.Cos(newObliqueAngle * MathHelper.DegToRad);
newHeight = MathHelper.IsZero(newHeight) ? MathHelper.Epsilon : newHeight;

// the width factor is defined between 0.01 and 100
double newWidthFactor = newUvector.Modulus() / newHeight;
if(newWidthFactor<0.01)
if (newWidthFactor < 0.01)
{
newWidthFactor = 0.01;
}
else if (newWidthFactor > 100)
{
newWidthFactor = 100;
}

this.Position = newPosition;
this.Normal = newNormal;
Expand Down Expand Up @@ -629,7 +676,7 @@ public void TransformBy(Matrix4 transformation)
/// <returns>A new Attribute that is a copy of this instance.</returns>
public object Clone()
{
Attribute entity = new Attribute
Attribute entity = new Attribute(this.tag)
{
//Attribute properties
Layer = (Layer) this.Layer.Clone(),
Expand All @@ -641,7 +688,6 @@ public object Clone()
Normal = this.Normal,
IsVisible = this.isVisible,
Definition = (AttributeDefinition) this.definition?.Clone(),
Tag = this.tag,
Height = this.height,
Width = this.width,
WidthFactor = this.widthFactor,
Expand Down
Loading

0 comments on commit 8e9b814

Please sign in to comment.