Ejemplo n.º 1
0
        public override string GetSourceBlock(bool useSymbols = false)
        {
            StringBuilder retVal = new StringBuilder();

            string name = this.BlockName;

            if (useSymbols && SymbolTableEntry != null)
            {
                name = SymbolTableEntry.Symbol;
            }

            if (this.BlockType == PLCBlockType.UDT)
            {
                retVal.AppendLine("TYPE " + name);
            }
            else
            {
                retVal.AppendLine("DATA_BLOCK " + name);
            }
            retVal.AppendLine("TITLE =" + this.Title);

            if (!string.IsNullOrEmpty(this.Author))
            {
                retVal.AppendLine("AUTHOR : " + this.Author);
            }
            if (!string.IsNullOrEmpty(this.Name))
            {
                retVal.AppendLine("NAME : " + this.Name);
            }
            if (!string.IsNullOrEmpty(this.Version))
            {
                retVal.AppendLine("VERSION : " + this.Version);
            }
            retVal.AppendLine();
            retVal.AppendLine();


            if (this.Structure.Children != null && !this.IsInstanceDB)
            {
                retVal.AppendLine("  STRUCT");
                retVal.Append(AWLToSource.DataRowToSource(((S7DataRow)this.Structure), "    "));
                retVal.AppendLine("  END_STRUCT ;");
            }
            else if (this.IsInstanceDB)
            {
                if (useSymbols)
                {
                    if (SymbolTable.GetEntryFromOperand("FB" + this.FBNumber) != null)
                    {
                        retVal.AppendLine(" " + SymbolTable.GetEntryFromOperand("FB" + this.FBNumber).Symbol);
                    }
                    else
                    {
                        retVal.AppendLine(" FB " + this.FBNumber);
                    }
                }
                else
                {
                    retVal.AppendLine(" FB " + this.FBNumber);
                }
            }
            if (this.BlockType != PLCBlockType.UDT)
            {
                retVal.AppendLine("BEGIN");
                retVal.Append(AWLToSource.DataRowValueToSource(((S7DataRow)this.Structure), "    "));
                //db data???
            }

            if (this.BlockType == PLCBlockType.UDT)
            {
                retVal.AppendLine("END_TYPE");
            }
            else
            {
                retVal.AppendLine("END_DATA_BLOCK");
            }

            return(retVal.ToString());
        }
Ejemplo n.º 2
0
        public override string GetSourceBlock(bool useSymbols = false)
        {
            StringBuilder retVal = new StringBuilder();

            string name = this.BlockName;

            if (useSymbols && SymbolTableEntry != null)
            {
                name = "\"" + SymbolTableEntry.Symbol + "\"";
            }

            if (this.BlockType == PLCBlockType.FC)
            {
                retVal.AppendLine("FUNCTION " + name + " : VOID");
            }
            else if (this.BlockType == PLCBlockType.OB)
            {
                retVal.AppendLine("ORGANIZATION_BLOCK " + name);
            }
            else
            {
                retVal.AppendLine("FUNCTION_BLOCK " + name);
            }

            retVal.Append("TITLE =" + this.Title + Environment.NewLine);

            if (!String.IsNullOrEmpty(this.Description))
            {
                retVal.AppendLine("//" + this.Description.Replace(Environment.NewLine, Environment.NewLine + "//"));
            }
            if (!string.IsNullOrEmpty(this.Author))
            {
                retVal.AppendLine("AUTHOR : " + this.Author);
            }
            if (!string.IsNullOrEmpty(this.Name))
            {
                retVal.AppendLine("NAME : " + this.Name);
            }
            if (!string.IsNullOrEmpty(this.Version))
            {
                retVal.AppendLine("VERSION : " + this.Version);
            }
            retVal.AppendLine();
            retVal.AppendLine();


            if (this.Parameter.Children != null)
            {
                foreach (S7DataRow s7DataRow in this.Parameter.Children)
                {
                    if (s7DataRow.Children.Count > 0)
                    {
                        string parnm = s7DataRow.Name;
                        string ber   = "VAR_" + parnm;
                        if (parnm == "IN")
                        {
                            ber = "VAR_INPUT";
                        }
                        else if (parnm == "OUT")
                        {
                            ber = "VAR_OUTPUT";
                        }
                        else if (parnm == "STATIC")
                        {
                            ber = "VAR";
                        }
                        retVal.AppendLine(ber);
                        string vars = AWLToSource.DataRowToSource(s7DataRow, "  ", ((this.BlockType != PLCBlockType.FB && this.BlockType != PLCBlockType.SFB) || parnm == "TEMP"));
                        if (useSymbols)
                        {
                            foreach (string dependency in Dependencies)
                            {
                                if (dependency.Contains("SFC") || dependency.Contains("SFB"))
                                {
                                    continue;
                                }
                                try
                                {
                                    string depSymbol = "\"" + SymbolTable.GetEntryFromOperand(dependency).Symbol + "\"";
                                    vars = vars.Replace(dependency, SymbolTable.GetEntryFromOperand(dependency).Symbol);
                                }
                                catch { }
                            }
                        }
                        retVal.Append(vars);
                        retVal.AppendLine("END_VAR");
                    }
                }
            }
            retVal.AppendLine("BEGIN");
            foreach (Network network in this.Networks)
            {
                retVal.AppendLine("NETWORK");
                retVal.AppendLine("TITLE = " + network.Name);
                if (!String.IsNullOrEmpty(network.Comment))
                {
                    retVal.AppendLine("//" + network.Comment.Replace(Environment.NewLine, Environment.NewLine + "//"));
                }
                else
                {
                    retVal.AppendLine();
                }
                foreach (S7FunctionBlockRow functionBlockRow in network.AWLCode)
                {
                    string awlCode = functionBlockRow.ToString(useSymbols, true);
                    if (awlCode == "" || awlCode == ";")
                    {
                        retVal.AppendLine();
                    }
                    else
                    {
                        retVal.AppendLine(awlCode);
                    }
                }
            }

            if (this.BlockType == PLCBlockType.FC)
            {
                retVal.Append("END_FUNCTION");
            }
            else if (this.BlockType == PLCBlockType.OB)
            {
                retVal.AppendLine("END_ORGANIZATION_BLOCK");
            }
            else
            {
                retVal.Append("END_FUNCTION_BLOCK");
            }
            //retVal.Append("END_FUNCTION");

            return(retVal.ToString());
        }