Skip to content

Commit

Permalink
Moved L5X back up to LogixElement.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
tnunnink committed Jul 5, 2024
1 parent e73f2e0 commit 5f4b937
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 86 deletions.
60 changes: 9 additions & 51 deletions src/.idea/.idea.L5Sharp/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions src/L5Sharp.Core/L5Sharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<Title>L5Sharp</Title>
<Authors>Timothy Nunnink</Authors>
<Version>3.0.0</Version>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion>
<Version>3.1.0</Version>
<AssemblyVersion>3.1.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>
<Description>A library for intuitively interacting with Rockwell's L5X import/export files.</Description>
<RepositoryUrl>https://github.com/tnunnink/L5Sharp</RepositoryUrl>
<PackageTags>csharp allen-bradely l5x logix plc-programming rockwell-automation logix5000</PackageTags>
Expand All @@ -18,10 +18,7 @@
<!--<PackageIcon>icon.png</PackageIcon>-->
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>
LogixContainer now implements IList{T} and ICollection, slightly changes the API.
LogixObject{T}, LogixComponent{T} created and add ILogixParsable{T} making all components and some elements able to be parsed dynamically using LogixParser extensions.
Update TagName Concat() to reduce memory consumption.
Added methods to AddOnInstruction for generating NeutralText and Instruction instances.
Moved L5X property back up to root LogixElement class (from LogixObject).
</PackageReleaseNotes>
<Copyright>Copyright (c) Timothy Nunnink 2022</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
30 changes: 28 additions & 2 deletions src/L5Sharp.Core/LogixElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ protected LogixElement(XElement element)
/// in the underlying parent element.
/// </summary>
protected virtual List<string> ElementOrder { get; } = [];

/// <summary>
/// Indicates whether this element is attached to an L5X document.
/// </summary>
/// <value><c>true</c> if this is an attached element; Otherwise, <c>false</c>.</value>
/// <remarks>
/// This simply looks to see if the element has a ancestor with the root RSLogix5000Content element or not.
/// If so we will assume this element is attached to an L5X document.
/// </remarks>
public bool IsAttached => Element.Ancestors(L5XName.RSLogix5000Content).Any();

/// <summary>
/// Returns the <see cref="L5X"/> instance this <see cref="LogixElement"/> is attached to if it is attached.
/// </summary>
/// <returns>
/// If the current element is attached to a L5X document (i.e. has the root content element),
/// then the <see cref="L5X"/> instance; Otherwise, <c>null</c>.
/// </returns>
/// <remarks>
/// This allows attached logix elements to reach up to the L5X root in order to traverse or retrieve
/// other elements in the L5X. This is helpful/used for other extensions and cross-referencing functions.
/// </remarks>
public L5X? L5X => Element.Ancestors(L5XName.RSLogix5000Content).FirstOrDefault()?.Annotation<L5X>();

/// <summary>
/// Returns the name of the L5XType for this <see cref="LogixElement"/> object.
Expand Down Expand Up @@ -622,11 +645,14 @@ protected void SetContainer<TObject>(LogixContainer<TObject>? value, [CallerMemb
/// <param name="value">The value to set true/false, which will be converted to 0/1.</param>
/// <param name="name">The name of the bit attribute.</param>
/// <remarks>
/// This is a specialized helper since Logix uses 0/1 instead of true/false for some properties/attributes. Properties
/// that need to write a 0/1 to correctly import L5X content should use this method.
/// This is a specialized helper since Logix uses 0/1 instead of true/false for some properties/attributes.
/// Properties that need to write a 0/1 to correctly import L5X content should use this method.
/// </remarks>
protected void SetBit(bool? value, [CallerMemberName] string? name = null)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("Name can not be null or empty", nameof(name));

if (value is null)
{
Element.Attribute(name)?.Remove();
Expand Down
23 changes: 0 additions & 23 deletions src/L5Sharp.Core/LogixObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,6 @@ protected LogixObject(XElement element) : base(element)
{
}

/// <summary>
/// Indicates whether this element is attached to an L5X document.
/// </summary>
/// <value><c>true</c> if this is an attached element; Otherwise, <c>false</c>.</value>
/// <remarks>
/// This simply looks to see if the element has a ancestor with the root RSLogix5000Content element or not.
/// If so we will assume this element is attached to an L5X document.
/// </remarks>
public bool IsAttached => Element.Ancestors(L5XName.RSLogix5000Content).Any();

/// <summary>
/// Returns the <see cref="L5X"/> instance this <see cref="LogixElement"/> is attached to if it is attached.
/// </summary>
/// <returns>
/// If the current element is attached to a L5X document (i.e. has the root content element),
/// then the <see cref="L5X"/> instance; Otherwise, <c>null</c>.
/// </returns>
/// <remarks>
/// This allows attached logix elements to reach up to the L5X file in order to traverse or retrieve
/// other elements in the L5X. This is helpful/used for other extensions and cross-referencing functions.
/// </remarks>
public L5X? L5X => Element.Ancestors(L5XName.RSLogix5000Content).FirstOrDefault()?.Annotation<L5X>();

/// <summary>
/// The scope of the element, indicating whether it is a globally scoped controller element,
/// a locally scoped program or instruction element, or neither (not attached to L5X tree).
Expand Down
4 changes: 2 additions & 2 deletions src/L5Sharp.Core/LogixParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public static object Parse(this string input, Type type)
/// </remarks>
private static Func<string, object> GetParser(Type type)
{
if (Parsers.Value.TryGetValue(type, out var parsers))
return parsers.Parse;
if (Parsers.Value.TryGetValue(type, out var parser))
return parser.Parse;

//Intercept any .NET bool type because we want to handle 1/0 case as Logix sometimes uses those values instead of true/false.
if (type == typeof(bool) || Nullable.GetUnderlyingType(type) == typeof(bool))
Expand Down
11 changes: 10 additions & 1 deletion tests/L5Sharp.Tests/ProofTesting.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.ComponentModel;
using System.Reflection;
using FluentAssertions;


Expand Down Expand Up @@ -47,6 +48,14 @@ public void GetParseMethod()
method.Should().NotBeNull();
}

[Test]
public void ParseString()
{
var result = "this is a test".Parse<string>();

result.Should().Be("this is a test");
}

private static bool IsParseFunctionFor(Type type, MethodInfo info)
{
var parameters = info.GetParameters();
Expand Down

0 comments on commit 5f4b937

Please sign in to comment.