4using MAC_use_cases.TiaImports;
5using Siemens.Automation.ModularApplicationCreator.Tia.Helper;
6using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block;
7using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block.XmlBlocks.BlockFrames;
8using Siemens.Automation.ModularApplicationCreator.Tia.Openness;
9using Siemens.Automation.ModularApplicationCreator.Tia.Openness.SoftwareUnit;
10using Siemens.Automation.ModularApplicationCreator.Tia.TiaAttributeFuncs;
11using ProgrammingLanguage =
12 Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block.ProgrammingLanguage;
53 ProgrammingLanguage programmingLanguage,
57 if (
string.IsNullOrEmpty(blockName))
59 throw new ArgumentNullException(nameof(blockName));
62 if (
string.IsNullOrEmpty(instanceDbName))
64 throw new ArgumentNullException(nameof(instanceDbName));
67 if (plcDevice ==
null)
69 throw new ArgumentNullException(nameof(plcDevice));
72 if (programmingLanguage != ProgrammingLanguage.LAD && programmingLanguage != ProgrammingLanguage.FBD &&
73 programmingLanguage != ProgrammingLanguage.SCL)
75 throw new ArgumentException(nameof(programmingLanguage));
79 var fbCall =
new BlockCall(instanceDbName, plcDevice) { [
"Input1"] =
"1", [
"Input2"] =
"0" };
82 var fbNetwork =
new BlockNetwork { NetworkTitles = { [TypeMapper.BaseCulture.Name] =
"myFB Network Title" } };
83 fbNetwork.Blocks.Add(fbCall);
86 var fb =
new XmlFB(blockName);
87 fb.Networks.Add(fbNetwork);
88 fb.BlockAttributes.ProgrammingLanguage = programmingLanguage;
89 fb.BlockComments[TypeMapper.BaseCulture.Name] =
"myFB Block Comment";
90 fb.BlockTitles[TypeMapper.BaseCulture.Name] =
"myFB Block Title";
93 if (programmingLanguage == ProgrammingLanguage.SCL)
95 fb.GenerateSclBlock(plcDevice);
99 fb.GenerateXmlBlock(plcDevice);
129 ProgrammingLanguage programmingLanguage,
133 if (
string.IsNullOrEmpty(blockName))
135 throw new ArgumentNullException(nameof(blockName));
138 if (
string.IsNullOrEmpty(instanceDbName))
140 throw new ArgumentNullException(nameof(instanceDbName));
143 if (plcDevice ==
null)
145 throw new ArgumentNullException(nameof(plcDevice));
148 if (programmingLanguage != ProgrammingLanguage.F_LAD && programmingLanguage != ProgrammingLanguage.F_FBD)
150 throw new ArgumentException(nameof(programmingLanguage));
154 var fbCall =
new BlockCall(instanceDbName, plcDevice) { [
"Input1"] =
"1", [
"Input2"] =
"0" };
157 var fbNetwork =
new BlockNetwork { NetworkTitles = { [TypeMapper.BaseCulture.Name] =
"myFB Network Title" } };
158 fbNetwork.Blocks.Add(fbCall);
161 var failSafeBlock =
new XmlFailSafeFB(blockName);
162 failSafeBlock.Networks.Add(fbNetwork);
163 failSafeBlock.BlockAttributes.ProgrammingLanguage = programmingLanguage;
164 failSafeBlock.BlockComments[TypeMapper.BaseCulture.Name] =
"myFB Block Comment";
165 failSafeBlock.BlockTitles[TypeMapper.BaseCulture.Name] =
"myFB Block Title";
168 failSafeBlock.GenerateXmlBlock(plcDevice, programmingLanguage);
182 var dataBlock =
new XmlGlobalDB(dbName);
183 var itf = dataBlock.Interface[InterfaceSections.Static];
186 var standardParam =
new InterfaceParameter(
ParameterName,
"Int");
187 var customParam1 =
new InterfaceParameter(
"var_struct",
"Struct");
188 customParam1.SubParameter.Add(
new InterfaceParameter(
"Var1",
"Bool"));
189 customParam1.SubParameter.Add(
new InterfaceParameter(
"Var2",
"Int"));
190 var customParam2 =
new InterfaceParameter(
"datatyp_from_lib",
"\"" +
"myDataType" +
"\"");
193 itf.Add(standardParam);
194 itf.Add(customParam1);
195 itf.Add(customParam2);
197 dataBlock.GenerateXmlBlock(plcDevice);
223 ProgrammingLanguage programmingLanguage)
225 var fb =
new XmlFB(fbName);
227 fb.Interface[InterfaceSections.Input].Add(
new InterfaceParameter(
"Input_1",
"Bool")
229 Remanence = RemanenceSettings.IgnoreRemanence
231 fb.Interface[InterfaceSections.Input].Add(
new InterfaceParameter(
"Input_2",
"Bool")
233 Remanence = RemanenceSettings.IgnoreRemanence
235 fb.Interface[InterfaceSections.Output].Add(
new InterfaceParameter(
"Output",
"Bool")
237 Remanence = RemanenceSettings.IgnoreRemanence
242 fb.GenerateXmlBlock(softwareUnitBase, programmingLanguage);
252 private static void GenerateBlock(
string code, PlcDevice targetDevice, ITarget targetContainer =
null,
256 var extension =
".scl";
263 var filePath = Path.ChangeExtension(Path.GetTempFileName(), extension);
266 File.WriteAllText(filePath, code);
269 var block = OpennessFuncs.ImportBlockToPlc(filePath, targetDevice, targetContainer);
272 File.Delete(filePath);
284 public static void SetDefaultValue(
string dbName,
string parameterName, TIATYPE dataType,
string value,
287 var myParameterNameAttribute = TiaAttributeProvider.CreateAttribute(dataType, SOURCETYPE.BLOCKVAR,
288 dbName, parameterName, parent);
289 myParameterNameAttribute.Value = value;
301 var myFb =
new XmlFB(
"MyFunctionBlock_MultiInstance")
303 BlockAttributes = { ProgrammingLanguage = ProgrammingLanguage.LAD }
306 var multiInstanceDataBlockName = myFb.Name +
"_Instance";
307 var myMultiInstanceFbCall =
308 new MultiInstanceCall(multiInstanceDataBlockName, module.ResourceManagement.MyFunctionBlock.Name, plcDevice)
315 var interfaceParameters = myFb.Interface[InterfaceSections.Static];
319 new InterfaceParameter(multiInstanceDataBlockName,
320 module.ResourceManagement.MyFunctionBlock.Name) { Remanence = RemanenceSettings.IgnoreRemanence };
323 interfaceParameters.Add(customParam);
325 var networkMultiInstance =
new BlockNetwork();
326 networkMultiInstance.Blocks.Add(myMultiInstanceFbCall);
327 networkMultiInstance.GenerationLabel =
328 new GenerationLabel(module.Name,
"Generated by MAC", module.ModuleID.ToString());
329 myFb.Networks.Add(networkMultiInstance);
330 AddCodeBlockToOB(myFb, module.ResourceManagement, languageSettings, plcDevice);
344 var mainOb =
new XmlOB(
"Main");
345 mainOb.BlockAttributes.ProgrammingLanguage = ProgrammingLanguage.LAD;
346 ((OB_BlockAttributes)mainOb.BlockAttributes).BlockSecondaryType =
"ProgramCycle";
348 var itf = mainOb.Interface[InterfaceSections.Temp];
349 var temp =
new InterfaceParameter(
"Temp",
"Bool");
350 var tempInt =
new InterfaceParameter(
"TempInt",
"Int");
355 var myFBCall =
new BlockCall(instanceDbName, plcDevice)
357 [
"Input1"] =
"#Temp",
363 var blockNetwork =
new BlockNetwork();
364 blockNetwork.Blocks.Add(myFBCall);
365 blockNetwork.GenerationLabel =
366 new GenerationLabel(module.Name,
"Generated by MAC", module.ModuleID.ToString());
367 mainOb.Networks.Add(blockNetwork);
370 var content =
"\"MyDataBlock_Global\".myParameterName := 100;\n";
371 content +=
"IF #Temp THEN\n";
372 content +=
"#TempInt := 5;\n";
374 content +=
"TempInt := 1;\n";
375 content +=
"END_IF;\n\n";
376 var sclContent = content;
377 var sclcode_network = ParseSingleSCLCall(sclContent, plcDevice, mainOb);
378 sclcode_network.GenerationLabel =
379 new GenerationLabel(module.Name,
"Virtual_Master_scl_code", module.ModuleID.ToString());
380 mainOb.Networks.Add(sclcode_network);
382 AddCodeBlockToOB(mainOb, module.ResourceManagement, languageSettings, plcDevice);
393 public static void AddCodeBlockToOB(XmlLogicalBlock codeBlock, ResourceManagement resourceManagement,
394 string languageSettings, PlcDevice plcDevice)
396 var existingOB = OpennessFuncs.TryGetOBFromPlcDevice(plcDevice, codeBlock.Name);
398 if (existingOB ==
null)
400 if (codeBlock.BlockAttributes.ProgrammingLanguage == ProgrammingLanguage.SCL)
402 codeBlock.GenerateSclBlock(plcDevice);
406 codeBlock.GenerateXmlBlock(plcDevice,
null, languageSettings);
413 OpennessFuncs.ExportBlockAsXml(existingOB.Name, plcDevice, OpennessFuncs.CachingOptions.DISABLED);
414 var parser =
new Parser();
415 var xmlOB = parser.ParseXml(blockAsDoc);
416 xmlOB.MergeBlock(codeBlock);
418 if (xmlOB.BlockAttributes.ProgrammingLanguage == ProgrammingLanguage.SCL)
420 xmlOB.GenerateSclBlock(plcDevice);
424 xmlOB.GenerateXmlBlock(plcDevice,
null, languageSettings);
441 var myOb =
new XmlOB(name);
442 myOb.BlockAttributes.BlockAutoNumber =
false;
443 myOb.BlockAttributes.BlockNumber = 555.ToString();
445 for (var i = 1; i <= numberOfCalls; i++)
447 var blockName = $
"{nameof(module.ResourceManagement.MyFunctionBlock)}DB" + i;
449 blockName, module.ResourceManagement.ModuleBlocksRootGroup);
450 var myOBCall =
new BlockCall(blockName, plcDevice)
456 var myObBlockNetwork =
new BlockNetwork();
457 myObBlockNetwork.Blocks.Add(myOBCall);
458 myObBlockNetwork.GenerationLabel =
new GenerationLabel(blockName,
459 "Generated by MAC. Blockname: \"" + blockName +
"\"", module.ModuleID.ToString());
460 myOb.Networks.Add(myObBlockNetwork);
463 AddCodeBlockToOB(myOb, module.ResourceManagement, languageSettings, plcDevice);
466 private static INetwork ParseSingleSCLCall(
string sclCall, PlcDevice plc, XmlBlock xmlBlock)
468 var parsed =
new Parser().ParseSclSnippet(sclCall, xmlBlock, plc, GroupBlockCalls.NOGROUPING)
470 if (parsed is BlockNetwork blockNetwork)
475 if (parsed is FixNetwork fixNetwork)
This is the main class in which the workflow starts. Here are all sections for creating or generating...
All the functions to configure and generate Blocks are defined here.
static XmlGlobalDB GenerateGlobalDataBlock(string dbName, PlcDevice plcDevice, MAC_use_casesEM module)
This function creates a Data Block with interface (sub-)parameters in a desired folder....
static void GenerateMultiInstanceFB(PlcDevice plcDevice, string languageSettings, MAC_use_casesEM module)
This function creates an multiInstanceFB.
static void SetDefaultValue(string dbName, string parameterName, TIATYPE dataType, string value, MAC_use_casesEM parent)
This function sets the default value of a parameter in a DB.
static void CreateFunctionBlockInSoftwareUnit(ISoftwareUnitBase softwareUnitBase, string fbName, ProgrammingLanguage programmingLanguage)
Creates a new Function Block (FB) within the specified software unit with predefined interface parame...
static void GenerateOB_Main(string instanceDbName, MAC_use_casesEM module, string languageSettings, PlcDevice plcDevice)
This function generates an OB and creates a block call in FBD and SCL.
static string ParameterName
This is the name of the generated parameter.
static void CreateFailSafeFunctionBlock(string blockName, string instanceDbName, ProgrammingLanguage programmingLanguage, PlcDevice plcDevice)
Creates a Fail-Safe Function Block (F-FB) in the target PLC's program blocks folder.
static void GenerateOBWithMultipleCalls(string name, int numberOfCalls, string languageSettings, PlcDevice plcDevice, MAC_use_casesEM module)
This function Generates the OB with the given number of calls.
static void CreateFunctionBlock(string blockName, string instanceDbName, ProgrammingLanguage programmingLanguage, PlcDevice plcDevice)
Creates a Function Block (FB) in the target PLC's program blocks folder.
static void AddCodeBlockToOB(XmlLogicalBlock codeBlock, ResourceManagement resourceManagement, string languageSettings, PlcDevice plcDevice)
This function checks if OB is already in TIA Project and merge the new one into the existing one depe...
static void GenerateBlock(string code, PlcDevice targetDevice, ITarget targetContainer=null, bool isScl=true)
This function creates a Block on the targetDevice in the targetContainer (folder under program blocks...
All the functions to configure and generate parts of the library are defined here.
static DataBlock CreateInstanceDataBlock(MAC_use_casesEM module, FBMasterCopy masterCopy, string instanceName, BlockGroup target)
This function creates an instance DB in the target folder (folder under program blocks)