Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Overview

ScriptClasses allow you to create ScriptsClasses (Reference)  provide a repository of class reusable code libraries, methods, and functions that you can use across the application. You can call them from tasks, other classes, and the display's code behind functionality.  On accessible throughout your FrameworX solution. The platform automatically manages these classes as static objects with built-in exception handling. 

ScriptClasses enable code reuse across:

  • Script Tasks
  • Display Code Behind
  • Other ScriptClasses
  • Expressions throughout the solution

The platform provides automatic:

  • Static object instantiation
  • Exception protection
  • Cross-language interoperability (C#/Python/VB.NET)

In this page:

Table of Contents
maxLevel

3stylenone
Tip
titleSolution Example

Scripts Examples

2
minLevel2
indent10px
excludeSteps
stylenone


Creating ScriptClasses

Configuring ScriptClasses

To create and configure a new ScriptClass follow the steps below:

Access Scripts → Classes

Click

on the 

the plus icon

.Input

to create a

class name and description, select the desired language and choose the domain of the class.
  • Choose to either create a new code or import one from an existing library.

  • Choose the content type for the class.

  • Click Ok.

  • Now, you can double-click the newly created row to access the Code Editor, where you can insert the code for the ScriptClass.

    Info

    To edit a property of an existing object, click the desired property to select it and click it again after a second to edit it. See a list of available properties in the next section.

    Built-in ScriptClasses

    All solutions include the following built-in ScriptClasses:

    • ServerMain: The methods library available for all server tasks.
    • ClientMain: The methods library available for all clients.
     

    ScriptClasses Configuration Tables

    The following is a table describing each available property field for ScriptClasses:

    Property

    Description

    Name

    The name of the class.

    Code

    Specifies the language used for the code of this Class. By default, this is the language you selected when you created the solution.

    Domain

    The location where the class executes, there are two options:

    • Client: The class is executed on each client system. These are classes that apply locally (on the user's computer).
    • Server: The class is executed on the server system. These are classes that apply globally across the solution.

    ClassContent

    The type of the content in the class (Methods or Namespace)

    Edit Security

    The security to enable who can edit the tasks.

    Build Order

    The order to build the classes.

    Build Status

    The Status of the class code from the continuous compiling process.

    • Green check mark: The task runs without errors.
    • Red X: The task has warnings or errors, double-click to access the warning or error.
      • Warnings are information only.
      • Errors will prevent the code from running for that specific task. The rest of the solution will run.

    Build Errors

    Displays any errors encountered during the last build.

    Description

    The description of this class.

    Circular References

    A circular reference occurs when two classes mutually depend on each other. For example:

    • Class A has methods or attributes that reference Class B.
    • Class B, in turn, has methods or attributes that reference Class A.

    This structure creates a “circularity” between the classes, leading to compilation failures and complicating code maintenance.

    How Compilation Caching Works with Circular References:

    • When Saving a Class Individually: When a class (e.g., Class A) is saved, the system caches the last successfully compiled version. Therefore, if Class B references Class A, and Class A is then altered in a way that causes a compilation error, this error may not immediately appear in Class B because the cache retains the last valid version.

    • During a Complete Solution Build: During a full build, the cache is cleared, and all classes are recompiled from scratch. In this process:

      • If there are circular references and one of the classes contains a compilation error, none of the classes will compile successfully. To resolve this, one of the circular references should be temporarily removed (commented out or deleted), allowing the successful compilation of one of the classes by saving. Then, the removed reference can be reinserted in the other class, now correctly compiled.
    Note

    We emphasize that using circular references is not a recommended practice. This kind of mutual dependency compromises code stability and maintainability, complicates debugging, and leads to complete build failures.

    new class. Three options are available:

    1. Class with Methods - Standard reusable code library
    2. Full Namespace - Advanced .NET namespace (Visual Studio-like)
    3. MCP Tool - AI model integration via Model Context Protocol

    Configuration Properties

    PropertyDescription
    NameClass identifier used in code references
    CodeProgramming language (C#, Python, VB.NET)
    DomainExecution location: Server (global) or Client (local)
    ClassContentType: Methods or Namespace
    Edit SecurityPermission level required to modify
    Build OrderCompilation sequence when multiple classes exist
    Build StatusGreen check (success) or Red X (errors)
    Build ErrorsCompilation error details
    DescriptionDocumentation for the class purpose

    Code Structure Requirements

    <ac:structured-macro ac:name="warning"> ac:rich-text-body CRITICAL: Do not include class declarations or namespace definitions in ScriptClasses

    When creating a ScriptClass with Methods or MCP Tool:

    • ? DO NOT include: public class MyClass { }
    • ? DO NOT include: namespace MyNamespace { }
    • ? DO NOT include: using statements directly in code
    • ? DO include: Method definitions only
    • ? DO use: Namespace Declarations button for using statements

    The platform automatically wraps your code in the appropriate class structure using the Name from the configuration table. </ac:rich-text-body> </ac:structured-macro>

    Correct ScriptClass Format

    csharp

    // Methods directly - NO class wrapper, NO using statements
    public string ProcessData(double value)
    {
        return $"Processed: {value:F2}";
    }
    
    public int Calculate(int a, int b)
    {
        return a + b;
    }

    Adding Namespace Declarations

    To add using statements (C#) or Import statements (VB.NET):

    1. Open the Code Editor
    2. Click the Namespace Declarations button in toolbar
    3. Add required namespaces in the dialog
    4. Never put using statements directly in the code

    ScriptClass Types

    1. Class with Methods (Standard)

    Most common type for reusable code:

    • Platform manages as static object
    • Automatic exception handling
    • No instantiation required
    • Access via: @Script.Class.ClassName.MethodName()

    Example:

    csharp

    // In ScriptClass named "Calculations"
    public double CalculateEfficiency(double actual, double target)
    {
        if (target == 0) return 0;
        return (actual / target) * 100;
    }
    
    // Usage anywhere in solution:
    // double eff = @Script.Class.Calculations.CalculateEfficiency(95, 100);

    2. MCP Tool

    Special class type for AI integration:

    • Exposes methods to external Language Models
    • Requires decorator attributes
    • Methods without decorators behave as regular methods
    • Available in all editions including EdgeConnect

    Example:

    csharp

    [MCPMethod(Description = "Get tank level")]
    public double GetTankLevel(
        [MCPParameter(Description = "Tank ID")] string tankId)
    {
        return @Tag[$"Tank_{tankId}_Level"];
    }

    3. Full Namespace (Advanced)

    <ac:structured-macro ac:name="note"> ac:rich-text-body Advanced Feature Warning: Full Namespace is for experienced developers only. Most applications should use standard Class with Methods. This option requires manual object instantiation and exception handling like Visual Studio development. </ac:rich-text-body> </ac:structured-macro>

    Complete .NET namespace implementation:

    • Requires full namespace and class declarations
    • Manual object instantiation required
    • No automatic exception protection
    • Full control over implementation

    Example:

    csharp

    namespace MyCompany.Utilities
    {
        public class AdvancedProcessor
        {
            private int counter = 0;
            
            public void Process()
            {
                // Implementation
            }
        }
    }
    
    // Usage requires instantiation:
    // var processor = new MyCompany.Utilities.AdvancedProcessor();
    // processor.Process();

    Built-in Classes

    All solutions include:

    ServerMain

    Global methods library executed on server:

    • Available to all server-side scripts
    • Runs in TServer main thread
    • Pre-instantiated by platform
    • Special entry points for server initialization

    <ac:structured-macro ac:name="info"> ac:rich-text-body ServerMain has special integration with TServer process. Additional documentation on advanced ServerMain features will be provided in future updates. </ac:rich-text-body> </ac:structured-macro>

    ClientMain

    Local methods library executed on each client:

    • Runs independently per client
    • Pre-instantiated for each client connection
    • Access to local display context
    • Ideal for UI-specific operations

    Library Import/Export

    ScriptClasses can be shared between solutions using the Library feature:

    Export to Library

    1. Select ScriptClass in table
    2. Click Export to Library
    3. Class is saved to central Library.dbsln repository

    Import from Library

    1. When creating new class, select Get from Library
    2. Browse available classes in Library.dbsln
    3. Select and import desired class

    The Library serves as a centralized repository for reusable components across all your solutions.


    Cross-Language Interoperability

    FrameworX enables calling between languages transparently:

    csharp

    // C# ScriptClass calling Python method
    double result = @Script.Class.PythonClass.calculate_value(10, 20);

    python

    # Python ScriptClass calling C# method
    efficiency = TK.Script.Class.CSharpClass.CalculateEfficiency(95.5, 100.0)

    <ac:structured-macro ac:name="info"> ac:rich-text-body For detailed Python/.NET integration including type conversions and limitations, see [Python.NET Integration (Reference)] </ac:rich-text-body> </ac:structured-macro>


    Accessing Solution Objects

    ScriptClasses can directly access all solution namespaces:

    csharp

    public void UpdateProduction()
    {
        // Access Tags
        double rate = @Tag.ProductionRate;
        
        // Access Alarms  
        bool hasAlarm = @Alarm.HasActive("Line1");
        
        // Access Historian
        var history = @Historian.GetValues("Temperature", 100);
        
        // Access other Script Classes
        var result = @Script.Class.OtherClass.Method();
        
        // Access Script Tasks
        @Script.Task.MyTask.Run();
    }

    External References

    To use external .NET assemblies:

    1. Add Assembly Reference
      • Go to Scripts → References
      • Add external DLL location
      • See [Scripts References (Reference)] for details
    2. Add Namespace Declaration
      • Open Code Editor
      • Click Namespace Declarations button
      • Add namespaces from referenced assembly
    3. Use in Code
      • Call methods from external assembly
      • No using statements in code itself

    Compilation and Build Order

    Build Process

    1. Classes compile in Build Order sequence (lowest first)
    2. Cached compilation for unchanged classes
    3. Full rebuild clears cache

    Circular References

    <ac:structured-macro ac:name="warning"> ac:rich-text-body Avoid circular references between classes. While not blocked by the system, they cause:

    • Compilation failures during full builds
    • Maintenance difficulties
    • Debugging complications

    If circular references exist, temporarily comment one reference to allow compilation. </ac:rich-text-body> </ac:structured-macro>


    Best Practices

    1. Use descriptive names - Class and method names should indicate purpose
    2. Add XML comments - Document methods for IntelliSense
    3. Handle exceptions - Even with automatic protection, validate inputs
    4. Avoid circular references - Design clear dependency hierarchy
    5. Use standard classes - Only use Full Namespace when absolutely necessary
    6. Test incrementally - Verify Build Status after each change
    7. Use Namespace Declarations - Never put using statements in code

    Troubleshooting

    Red X Build Status

    • Double-click to see specific errors
    • Check for missing semicolons or braces
    • Verify all referenced tags/objects exist

    Methods not accessible

    • Confirm Build Status is green
    • Check Domain (Server/Client) matches usage location
    • Verify syntax: @Script.Class.ClassName.MethodName()

    Class wrapper errors

    • Remove any public class declarations
    • Remove namespace definitions (unless Full Namespace type)
    • Remove using statements from code (use Namespace Declarations button)

    Cannot find external types

    • Add assembly reference in Scripts → References
    • Add namespace via Namespace Declarations button
    • Verify DLL compatibility with target framework




    In this section...

    Page Tree
    root@parent
    spaces93DRAF

    Recommendations:

    • Guide users to avoid circular references between classes whenever possible, as this will simplify compilation and maintenance.
    • Include an explanatory note that, while the system does not block the creation of circular references, this practice is not recommended, as it often leads to compilation difficulties and stability issues.

    In this section:

    Page Tree
    rootV10:@parent
    spacesV10