beckhoff TwinCAT 3 — 16.4 Creating and handling TcCOM modules

Appendix | Creating and handling TcCOM modules uint objId = moduleInstance.oid; uint parentObjId = moduleInstance.ParentOID; } Code Snippet (Powershell): $moduleManager = $systemManager.GetModuleManager() ForEach( $moduleInstance in $moduleManager ) { $moduleType = $moduleInstance.ModuleTypeName $instanceName = $moduleInstance.ModuleInstanceName $classId = $moduleInstance.ClassID $objId = $moduleInstance.oid $parentObjId = $moduleInstance.ParentOID } Please note that every module object can also be interpreted as an ITcSmTreeItem, therefore the following type cast would be valid: Code Snippet (C#): ITcSmTreeItem treeItem = moduleInstance As ITcSmTreeItem; Please note:

Powershell uses dynamic data types by default. Setting CreateSymbol flag for parameters The CreateSymbol (CS) flag for parameters of a TcCOM module can be set via its XML description. The following code snippet demonstrates how to activate the CS flag for the Parameter “CallBy”. Code Snippet (C#): bool activateCS = true; // First step: Read all Parameters of TcCOM module instance string tempControllerXml = tempController.ProduceXml(); XmlDocument tempControllerDoc = new XmlDocument(); tempControllerDoc.LoadXml(tempControllerXml); XmlNode sourceParameters = tempControllerDoc.SelectSingleNode("TreeItem/TcModuleInstance/Module/ Parameters"); // Second step:

Build target XML (for later ConsumeXml()) XmlDocument targetDoc = new XmlDocument(); XmlElement treeItemElement = targetDoc.CreateElement("TreeItem"); XmlElement moduleInstanceElement = targetDoc.CreateElement("TcModuleInstance"); XmlElement moduleElement = targetDoc.CreateElement("Module"); XmlElement parametersElement = (XmlElement) targetDoc.ImportNode(sourceParameters, true); moduleElement.AppendChild(parametersElement); moduleInstanceElement.AppendChild(moduleElement); treeItemElement.AppendChild(moduleInstanceElement); targetDoc.AppendChild(treeItemElement); // Third step:

Look for specific parameter (in this case "CallBy") and read its CreateSymbol attribute XmlNode destModule = targetDoc.SelectSingleNode("TreeItem/TcModuleInstance/Module"); XmlNode callByParameter = destParameters.SelectSingleNode("Parameters/Parameter[Name='CallBy']"); XmlAttribute createSymbol = callByParameter.Attributes["CreateSymbol"]; createSymbol.Value = "true"; // Fifth step: Write prepared XML to configuration via ConsumeXml() string targetXml = targetDoc.OuterXml; tempController.ConsumeXml(targetXml); Code Snippet (Powershell):

$tempControllerXml = [Xml]$tempController.ProduceXml() $sourceParameters = $tempControllerXml.TreeItem.TcModuleInstance.Module.Parameters [System.XML.XmlDocument] $targetDoc = New-Object System.XML.XmlDocument [System.XML.XmlElement] $treeItemElement = $targetDoc.CreateElement("TreeItem") [System.XML.XmlElement] $moduleInstanceElement = $targetDoc.CreateElement("TcModuleInstance") [System.XML.XmlElement] $moduleElement = $targetDoc.CreateElement("Module") [System.XML.XmlElement] $parametersElement = $targetDoc.ImportNode($sourceParameters, $true) $moduleElement.AppendChild($parametersElement) $moduleInstanceElement.AppendChild($moduleElement) $treeItemElement.AppendChild($moduleInstanceElement) $targetDoc.AppendChild($treeItemElement) $destModule = $targetDoc.TreeItem.TcModuleInstance.Module TwinCAT 3 335 Version:

1.19.1