按步骤和流程自动化流程

在这里插入图片描述

应用MotionFlow和MotionStep定义完成所有自动化动作,用记忆状态

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeletStateMode
{
    public delegate int DeletAction();
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

             checkBox1.Checked = MyWs.bStep;
             checkBox2.Checked= MyWs.bquit;
        }
        WorkStation MyWs = new WorkStation("左料仓");
        private void button1_Click(object sender, EventArgs e)
        {
            MyWs.FuncFlowFeedFlow();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            MyWs.InitStep();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            MyWs.bStep = checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            MyWs.bquit = checkBox2.Checked;
        }
    }
    public class WorkStation
    {
        public string Name;
        public DeletAction CurAct;
        public MotionFlow CurMoFlow;
        public bool bquit = true;
        public bool bStep = true;
        public MotionStep MoFirstStep, MoSecondStep,MoThirdStep,MoFourthStep;
        public MotionFlow FlowFeedUp, FlowBackDown;
        public WorkStation(string name)
        {
            Name = name;
            InitStep();
        }      
        public void InitStep()
        {
            CurAct = FirstStep;
            MoFourthStep=new MotionStep("回到安全位", 1, 4, LastStep);
            MoThirdStep = new MotionStep("关闭夹爪", 1, 3, ThirdStep);
            MoSecondStep = new MotionStep("定位准备位", 1, 2, SecondStep);
            MoFirstStep = new MotionStep("检测安全", 1, 1, FirstStep);
            FlowFeedUp = new MotionFlow(Name+"-"+"上料流程", 1, MoFirstStep);
            CurMoFlow = FlowFeedUp;

        }
        private int FirstStep()
        {
            FlowFeedUp.CurMoStep = MoSecondStep;
            return 0;
        }
        private int SecondStep()
        {
            FlowFeedUp.CurMoStep = MoThirdStep;
            return 0;
        }
        private int ThirdStep()
        {
            if (bquit)
            {
                return -1;
            }
            FlowFeedUp.CurMoStep = MoFourthStep;
            return 0;
        }
        private int LastStep()
        {
            FlowFeedUp.CurMoStep = null;
            return 0;
        }

        public int FuncFlowFeedFlow()
        {
            if (FlowFeedUp == null) { MessageBox.Show(Name + FlowFeedUp.ActionDesc + "流程任务空!"); return 1; }
            int ret = 0;
            ret = FlowFeedUp.Func(bStep);
            if (ret != 0)
            {
                if (ret == 1) return ret;
                return ret;
            }
            CurMoFlow = null;
            if (bStep) return 1;
            return 0;
        }
        


    }
    public class MotionStep
    {
        public string StepDesc;
        private uint WsId, StepId;
        public DeletAction CurAct;
        public uint ID
        {
            get
            {                
                return WsId * 100 + StepId;
            }
        }
        public MotionStep(string StepDesc,uint WsId,uint StepId, DeletAction CurAct)
        {
            this.StepDesc = StepDesc;
            this.WsId = WsId;
            this.StepId = StepId;
            this.CurAct = CurAct;
        }
    }
    public class MotionFlow
    {
        public string ActionDesc;
        private uint WsId;
        public MotionFlow  CurFlow;
       public  MotionStep CurMoStep;
        public MotionFlow(string ActionDesc,uint WsId, MotionStep CurMoStep = null, MotionFlow NextFlow=null)
        {
            this.ActionDesc = ActionDesc;
            this.WsId = WsId;
            this.CurMoStep = CurMoStep;
            CurFlow = this;
        }
        public int Func(bool bStep=true,bool bShowStepMsg=true)
        {
            int ret = 0;
           
            if (CurMoStep == null)
            {
                MessageBox.Show(ActionDesc + "已经工作完成!"); return 1;
            }
            while (CurMoStep != null)
            {
                string StepDesc = CurMoStep.ID + ":" + ActionDesc + "-" + CurMoStep.StepDesc + "-";
                ret = CurMoStep.CurAct();
                if (ret != 0)
                {
                    MessageBox.Show(StepDesc + "执行失败!");
                    return ret;
                }
                if(bShowStepMsg)
                MessageBox.Show(StepDesc + "执行成功!");
                if (bStep) return 1;
            }
            MessageBox.Show(ActionDesc + "工作成功!");
            return 0;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值