枚举,工序,工位

为工序添加工位的一种方式,

优点;

1.当一个工序添加完所需要的工位,他是自动排序的。

2.当产品执行流转逻辑时,对工位的状态更新更加方便

 

[Flags]
public enum Workstation
{
    None = 0,      // 无工位
    OP01 = 1,      // 工位 1 (二进制: 0000000000000001)
    OP02 = 2,      // 工位 2 (二进制: 0000000000000010)
    OP03 = 4,      // 工位 3 (二进制: 0000000000000100)
    OP04 = 8,      // 工位 4 (二进制: 0000000000001000)
    OP05 = 16,     // 工位 5 (二进制: 0000000000010000)
    OP06 = 32,     // 工位 6 (二进制: 0000000000100000)
    OP07 = 64,     // 工位 7 (二进制: 0000000001000000)
    OP08 = 128,    // 工位 8 (二进制: 0000000010000000)
    OP09 = 256,    // 工位 9 (二进制: 0000000100000000)
    OP10 = 512,    // 工位 10 (二进制: 0000001000000000)
    OP11 = 1024,   // 工位 11 (二进制: 0000010000000000)
    OP12 = 2048,   // 工位 12 (二进制: 0000100000000000)
    OP13 = 4096,   // 工位 13 (二进制: 0001000000000000)
    OP14 = 8192,   // 工位 14 (二进制: 0010000000000000)
    OP15 = 16384,  // 工位 15 (二进制: 0100000000000000)
    OP16 = 32768   // 工位 16 (二进制: 1000000000000000)
}
using System;

public class Process
{
    public Workstation Stations { get; private set; } // 工位组合

    public Process()
    {
        Stations = Workstation.None; // 初始化工位组合
    }

    // 添加工位
    public void AddWorkstation(Workstation workstation)
    {
        Stations |= workstation; // 使用按位或操作符添加工位
    }

    // 移除工位
    public void RemoveWorkstation(Workstation workstation)
    {
        Stations &= ~workstation; // 使用按位与和取反操作符移除工位
    }

    // 检查是否包含某个工位
    public bool ContainsWorkstation(Workstation workstation)
    {
        return Stations.HasFlag(workstation);
    }

    // 获取 16 位二进制表示
    public string GetBinaryString()
    {
        return Convert.ToString((int)Stations, 2).PadLeft(16, '0');
    }

    // 从二进制字符串解析工位组合
    public static Workstation FromBinaryString(string binaryString)
    {
        int value = Convert.ToInt32(binaryString, 2); // 将二进制字符串转换为整数
        return (Workstation)value; // 将整数转换为枚举值
    }

    // 输出工序信息
    public void PrintProcessInfo()
    {
        Console.WriteLine("Stations: " + Stations);
        Console.WriteLine("Binary (16-bit): " + GetBinaryString());
    }
}

class Program
{
    static void Main()
    {
        // 创建一个工序
        Process process = new Process();

        // 添加工位
        process.AddWorkstation(Workstation.OP01);
        process.AddWorkstation(Workstation.OP03);
        process.AddWorkstation(Workstation.OP05);
        process.AddWorkstation(Workstation.OP16);

        // 输出工序信息
        process.PrintProcessInfo();

        // 检查是否包含某个工位
        if (process.ContainsWorkstation(Workstation.OP03))
        {
            Console.WriteLine("Process contains OP03.");
        }

        // 移除工位
        process.RemoveWorkstation(Workstation.OP01);
        Console.WriteLine("After removing OP01:");
        process.PrintProcessInfo();

        // 从二进制字符串解析工位组合
        string binaryString = "1000000000010101"; // 二进制表示的工位组合
        Workstation parsedStations = Process.FromBinaryString(binaryString);
        Console.WriteLine($"Parsed Stations from binary '{binaryString}': " + parsedStations);
    }
}

输出结果:

Stations: OP01, OP03, OP05, OP16
Binary (16-bit): 1000000000010101
Process contains OP03.
After removing OP01:
Stations: OP03, OP05, OP16
Binary (16-bit): 1000000000010100
Parsed Stations from binary '1000000000010101': OP01, OP03, OP05, OP16

posted @ 2025-03-04 11:04  -Timosthetic  阅读(10)  评论(0)    收藏  举报