tnunnink / L5Sharp

A library for intuitively interacting with Rockwell's L5X import/export files.
MIT License
55 stars 6 forks source link

Tag Data Type Modification #14

Closed Ryushi5 closed 8 months ago

Ryushi5 commented 8 months ago

Hey, I just started checking out your project and it looks really great! I'm trying to use it for some simple things, and I've run into something that I can't quite figure out.

I am opening an existing L5X that has AOIs defined already. What I would like to do is simply create a tag, and set it's data type to that AOI. From what I can see, the DataType is just read-only for the tag. Looking into your code, it seems like I might have to replicate the AOI in C# class form to be able to get it to work is that right?

Is there a way to simply create a new tag and have it's DataType set to a string value?

tnunnink commented 8 months ago

Hi there,

Yes, you just need to set Value to an instance of a ComplexType.

var tag = new Tag { Name = "Test", Value = new ComplexType("My_AOI_Type_Name") };

This may look weird, but it keeps DataType in sync with the actual type of Value for a given tag (so ultimately you have less work to do). ComplexType is a "generic" data type class you can use to build up a data structure dynamically. Meaning, you can add/remove members to ComplexType in order to load data into Logix upon import.

var aoi = new ComplexType("MyAoi");
aoi.Add(new LogixMember("Member01", new DINT(100)));
aoi.Add(new LogixMember("Member02", new TIMER { PRE = 3000 }));
aoi.Add(new LogixMember("Member03", new ComplexType("SubType")));

Or you can do this more concisely from Tag itself.

var tag = new Tag { Name = "Test", Value = new ComplexType("My_AOI_Type_Name") }; 
tag.Add("Member01", new DINT(100));
tag.Add("Member02", new TIMER { PRE = 3000 });
tag.Add("Member03", new ComplexType("SubType"));

Of course, you could create your AOI type using a C# class and derive from ComplexType, but it is not strictly necessary. It just lets you new up instances of your custom type without have to always add all the members this way.

Hopefully this makes sense. Let me know if you need any further clarification.

Ryushi5 commented 8 months ago

5 Stars man! That was a very detailed answer in a very short time.

That's exactly what I'm looking for. Thank you very much!