4using System.Runtime.CompilerServices;
5using MAC_use_cases.TiaImports;
6using Siemens.Automation.ModularApplicationCreator.Tia.Helper;
7using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block;
8using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block.XmlBlocks.BlockFrames;
9using Siemens.Automation.ModularApplicationCreator.Tia.Openness;
10using Siemens.Automation.ModularApplicationCreator.Tia.Openness.SoftwareUnit;
11using Siemens.Automation.ModularApplicationCreator.Tia.TiaAttributeFuncs;
12using ProgrammingLanguage =
13 Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block.ProgrammingLanguage;
56 ProgrammingLanguage programmingLanguage,
60 if (
string.IsNullOrEmpty(blockName))
62 throw new ArgumentNullException(nameof(blockName));
65 if (
string.IsNullOrEmpty(instanceDbName))
67 throw new ArgumentNullException(nameof(instanceDbName));
70 if (plcDevice ==
null)
72 throw new ArgumentNullException(nameof(plcDevice));
75 if (programmingLanguage != ProgrammingLanguage.LAD && programmingLanguage != ProgrammingLanguage.FBD &&
76 programmingLanguage != ProgrammingLanguage.SCL)
78 throw new ArgumentException(nameof(programmingLanguage));
82 var fbCall =
new BlockCall(instanceDbName, plcDevice) { [
"Input1"] =
"1", [
"Input2"] =
"0" };
85 var fbNetwork =
new BlockNetwork { NetworkTitles = { [TypeMapper.BaseCulture.Name] =
"myFB Network Title" } };
86 fbNetwork.Blocks.Add(fbCall);
89 var fb =
new XmlFB(blockName);
90 fb.Networks.Add(fbNetwork);
91 fb.BlockAttributes.ProgrammingLanguage = programmingLanguage;
92 fb.BlockComments[TypeMapper.BaseCulture.Name] =
"myFB Block Comment";
93 fb.BlockTitles[TypeMapper.BaseCulture.Name] =
"myFB Block Title";
96 if (programmingLanguage == ProgrammingLanguage.SCL)
98 fb.GenerateSclBlock(plcDevice);
102 fb.GenerateXmlBlock(plcDevice);
132 ProgrammingLanguage programmingLanguage,
136 if (
string.IsNullOrEmpty(blockName))
138 throw new ArgumentNullException(nameof(blockName));
141 if (
string.IsNullOrEmpty(instanceDbName))
143 throw new ArgumentNullException(nameof(instanceDbName));
146 if (plcDevice ==
null)
148 throw new ArgumentNullException(nameof(plcDevice));
151 if (programmingLanguage != ProgrammingLanguage.F_LAD && programmingLanguage != ProgrammingLanguage.F_FBD)
153 throw new ArgumentException(nameof(programmingLanguage));
157 var fbCall =
new BlockCall(instanceDbName, plcDevice) { [
"Input1"] =
"1", [
"Input2"] =
"0" };
160 var fbNetwork =
new BlockNetwork { NetworkTitles = { [TypeMapper.BaseCulture.Name] =
"myFB Network Title" } };
161 fbNetwork.Blocks.Add(fbCall);
164 var failSafeBlock =
new XmlFailSafeFB(blockName);
165 failSafeBlock.Networks.Add(fbNetwork);
166 failSafeBlock.BlockAttributes.ProgrammingLanguage = programmingLanguage;
167 failSafeBlock.BlockComments[TypeMapper.BaseCulture.Name] =
"myFB Block Comment";
168 failSafeBlock.BlockTitles[TypeMapper.BaseCulture.Name] =
"myFB Block Title";
171 failSafeBlock.GenerateXmlBlock(plcDevice, programmingLanguage);
185 var dataBlock =
new XmlGlobalDB(dbName);
186 var itf = dataBlock.Interface[InterfaceSections.Static];
189 var standardParam =
new InterfaceParameter(
ParameterName,
"Int");
190 var customParam1 =
new InterfaceParameter(
"var_struct",
"Struct");
191 customParam1.SubParameter.Add(
new InterfaceParameter(
"Var1",
"Bool"));
192 customParam1.SubParameter.Add(
new InterfaceParameter(
"Var2",
"Int"));
193 var customParam2 =
new InterfaceParameter(
"datatyp_from_lib",
"\"" +
"myDataType" +
"\"");
196 itf.Add(standardParam);
197 itf.Add(customParam1);
198 itf.Add(customParam2);
200 dataBlock.GenerateXmlBlock(plcDevice);
226 ProgrammingLanguage programmingLanguage, PlcDevice plcDevice)
228 var fb =
new XmlFB(fbName);
230 fb.Interface[InterfaceSections.Input].Add(
new InterfaceParameter(
"Input_1",
"Bool")
232 Remanence = RemanenceSettings.IgnoreRemanence
234 fb.Interface[InterfaceSections.Input].Add(
new InterfaceParameter(
"Input_2",
"Bool")
236 Remanence = RemanenceSettings.IgnoreRemanence
238 fb.Interface[InterfaceSections.Output].Add(
new InterfaceParameter(
"Output",
"Bool")
240 Remanence = RemanenceSettings.IgnoreRemanence
244 if (programmingLanguage == ProgrammingLanguage.SCL)
246 fb.GenerateSclBlock(softwareUnitBase);
250 fb.GenerateXmlBlock(softwareUnitBase, programmingLanguage);
252 var block = softwareUnitBase.Blocks.ToList().FirstOrDefault(b => b.Value.Name == fbName);
253 block.Value.Namespace =
"Namespace_Block";
263 private static void GenerateBlock(
string code, PlcDevice targetDevice, ITarget targetContainer =
null,
267 var extension =
".scl";
274 var filePath = Path.ChangeExtension(Path.GetTempFileName(), extension);
277 File.WriteAllText(filePath, code);
280 var block = OpennessFuncs.ImportBlockToPlc(filePath, targetDevice, targetContainer);
283 File.Delete(filePath);
295 public static void SetDefaultValue(
string dbName,
string parameterName, TIATYPE dataType,
string value,
298 var myParameterNameAttribute = TiaAttributeProvider.CreateAttribute(dataType, SOURCETYPE.BLOCKVAR,
299 dbName, parameterName, parent);
300 myParameterNameAttribute.Value = value;
312 var myFb =
new XmlFB(
"MyFunctionBlock_MultiInstance")
314 BlockAttributes = { ProgrammingLanguage = ProgrammingLanguage.LAD }
317 var multiInstanceDataBlockName = myFb.Name +
"_Instance";
318 var myMultiInstanceFbCall =
319 new MultiInstanceCall(multiInstanceDataBlockName, module.ResourceManagement.MyFunctionBlock.Name, plcDevice)
326 var interfaceParameters = myFb.Interface[InterfaceSections.Static];
330 new InterfaceParameter(multiInstanceDataBlockName,
331 module.ResourceManagement.MyFunctionBlock.Name) { Remanence = RemanenceSettings.IgnoreRemanence };
334 interfaceParameters.Add(customParam);
336 var networkMultiInstance =
new BlockNetwork();
337 networkMultiInstance.Blocks.Add(myMultiInstanceFbCall);
338 networkMultiInstance.GenerationLabel =
339 new GenerationLabel(module.Name,
"Generated by MAC", module.ModuleID.ToString());
340 myFb.Networks.Add(networkMultiInstance);
341 AddCodeBlockToOB(myFb, module.ResourceManagement, languageSettings, plcDevice);
355 var mainOb =
new XmlOB(
"Main");
356 mainOb.BlockAttributes.ProgrammingLanguage = ProgrammingLanguage.LAD;
357 ((OB_BlockAttributes)mainOb.BlockAttributes).BlockSecondaryType =
"ProgramCycle";
359 var itf = mainOb.Interface[InterfaceSections.Temp];
360 var temp =
new InterfaceParameter(
"Temp",
"Bool");
361 var tempInt =
new InterfaceParameter(
"TempInt",
"Int");
366 var myFBCall =
new BlockCall(instanceDbName, plcDevice)
368 [
"Input1"] =
"#Temp",
374 var blockNetwork =
new BlockNetwork();
375 blockNetwork.Blocks.Add(myFBCall);
376 blockNetwork.GenerationLabel =
377 new GenerationLabel(module.Name,
"Generated by MAC", module.ModuleID.ToString());
378 mainOb.Networks.Add(blockNetwork);
381 var content =
"\"MyDataBlock_Global\".myParameterName := 100;\n";
382 content +=
"IF #Temp THEN\n";
383 content +=
"#TempInt := 5;\n";
385 content +=
"TempInt := 1;\n";
386 content +=
"END_IF;\n\n";
387 var sclContent = content;
388 var sclcode_network = ParseSingleSCLCall(sclContent, plcDevice, mainOb);
389 sclcode_network.GenerationLabel =
390 new GenerationLabel(module.Name,
"Virtual_Master_scl_code", module.ModuleID.ToString());
391 mainOb.Networks.Add(sclcode_network);
393 AddCodeBlockToOB(mainOb, module.ResourceManagement, languageSettings, plcDevice);
404 public static void AddCodeBlockToOB(XmlLogicalBlock codeBlock, ResourceManagement resourceManagement,
405 string languageSettings, PlcDevice plcDevice)
407 var existingOB = OpennessFuncs.TryGetOBFromPlcDevice(plcDevice, codeBlock.Name);
409 if (existingOB ==
null)
411 if (codeBlock.BlockAttributes.ProgrammingLanguage == ProgrammingLanguage.SCL)
413 codeBlock.GenerateSclBlock(plcDevice);
417 codeBlock.GenerateXmlBlock(plcDevice,
null, languageSettings);
424 OpennessFuncs.ExportBlockAsXml(existingOB.Name, plcDevice, OpennessFuncs.CachingOptions.DISABLED);
425 var parser =
new Parser();
426 var xmlOB = parser.ParseXml(blockAsDoc);
427 xmlOB.MergeBlock(codeBlock);
429 if (xmlOB.BlockAttributes.ProgrammingLanguage == ProgrammingLanguage.SCL)
431 xmlOB.GenerateSclBlock(plcDevice);
435 xmlOB.GenerateXmlBlock(plcDevice,
null, languageSettings);
452 var myOb =
new XmlOB(name);
453 myOb.BlockAttributes.BlockAutoNumber =
false;
454 myOb.BlockAttributes.BlockNumber = 555.ToString();
456 for (var i = 1; i <= numberOfCalls; i++)
458 var blockName = $
"{nameof(module.ResourceManagement.MyFunctionBlock)}DB" + i;
460 blockName, module.ResourceManagement.ModuleBlocksRootGroup);
461 var myOBCall =
new BlockCall(blockName, plcDevice)
467 var myObBlockNetwork =
new BlockNetwork();
468 myObBlockNetwork.Blocks.Add(myOBCall);
469 myObBlockNetwork.GenerationLabel =
new GenerationLabel(blockName,
470 "Generated by MAC. Blockname: \"" + blockName +
"\"", module.ModuleID.ToString());
471 myOb.Networks.Add(myObBlockNetwork);
474 AddCodeBlockToOB(myOb, module.ResourceManagement, languageSettings, plcDevice);
477 private static INetwork ParseSingleSCLCall(
string sclCall, PlcDevice plc, XmlBlock xmlBlock)
479 var parsed =
new Parser().ParseSclSnippet(sclCall, xmlBlock, plc, GroupBlockCalls.NOGROUPING)
481 if (parsed is BlockNetwork blockNetwork)
486 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 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 CreateFunctionBlockInSoftwareUnit(ISoftwareUnitBase softwareUnitBase, string fbName, ProgrammingLanguage programmingLanguage, PlcDevice plcDevice)
Creates a new Function Block (FB) within the specified software unit with predefined interface parame...
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)