static void AddECLASSAttributes(InternalElementType element, SystemUnitFamilyType eclass) { // add the class reference without any attributes element.SystemUnitClass = eclass; if (eclass.Attribute["ECLASS"].Value == "19234103") { // you need a ECLASS license to get the data, this is only an example element.SetAttributeValue("MC_Power", 220.0); } }
/// <summary> /// Shows the SystemUnitClass in the tree view. /// </summary> /// <param name="systemUnitClass">The system unit class.</param> internal void ShowClass(SystemUnitFamilyType systemUnitClass) { treeView1.Nodes.Clear(); var tn = treeView1.Nodes.Add(systemUnitClass.Node.Name.LocalName + ": " + systemUnitClass.Name); AddEIChilds(systemUnitClass, tn); AddIEChilds(systemUnitClass, tn); treeView1.ExpandAll(); }
private static void ShowMySystemUnitClasses(TreeNode node, SystemUnitFamilyType suc) { var childNode = node.Nodes.Add(suc.Name); childNode.Tag = suc; foreach (var ie in suc.InternalElement) { ShowMyInternalElement(childNode, ie); } foreach (var childSuc in suc.SystemUnitClass) { ShowMySystemUnitClasses(childNode, childSuc); } }
private void myShowSUC(TreeNode node, SystemUnitFamilyType SUC) { TreeNode childNode = node.Nodes.Add(SUC.Name.Value); childNode.Tag = SUC; foreach (InternalElementType IE in SUC.InternalElement) { myShowIE(childNode, IE); } foreach (SystemUnitFamilyType childSUC in SUC.SystemUnitClass) { myShowSUC(childNode, childSUC); } }
/// <summary> /// Create the AMLX File with the correct AML File and optional pictures /// </summary> /// <param name="device">The device which will be created</param> /// <param name="isEdit">true if an amlx file get update, false if a new file will be created</param> /// <returns></returns> public string CreateDevice(MWDevice device, bool isEdit) { CAEXDocument document = null; AutomationMLContainer amlx = null; // Init final .amlx Filepath //first of all create a folder on "Vendor Name" string vendorCompanyName = device.vendorName; string vendorCompanyNameFilePath = ""; string fileName = device.fileName; string amlFilePath = System.IO.Path.Combine(device.filepath, fileName + ".amlx"); FileInfo file = new FileInfo(amlFilePath); // Create directory if it's not existing file.Directory.Create(); // Init CAEX Document if (isEdit) { // Load the amlx file amlx = new AutomationMLContainer(amlFilePath, FileMode.Open); IEnumerable <PackagePart> rootParts = amlx.GetPartsByRelationShipType(AutomationMLContainer.RelationshipType.Root); // We expect the aml to only have one root part if (rootParts.First() != null) { PackagePart part = rootParts.First(); // load the aml file as an CAEX document document = CAEXDocument.LoadFromStream(part.GetStream()); } else { // the amlx contains no aml file document = CAEXDocument.New_CAEXDocument(); } } else { // create a new CAEX document document = CAEXDocument.New_CAEXDocument(); try { amlx = new AutomationMLContainer(amlFilePath, FileMode.Create); } catch (Exception) {} } // Init the default Libs AutomationMLBaseRoleClassLibType.RoleClassLib(document); AutomationMLInterfaceClassLibType.InterfaceClassLib(document); var structureRoleFamilyType = AutomationMLBaseRoleClassLibType.RoleClassLib(document).Structure; SystemUnitFamilyType systemUnitClass = null; // Create the SystemUnitClass for our device if (!isEdit) { systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(device.deviceName); device.listWithURIConvertedToString = new List <AttachablesDataGridViewParameters>(); foreach (AttachablesDataGridViewParameters eachparameter in device.dataGridAttachablesParametrsList) { if (eachparameter.FilePath.Contains("https://") || eachparameter.FilePath.Contains("http://") || eachparameter.FilePath.Contains("www") || eachparameter.FilePath.Contains("WWW")) { interneturl(eachparameter.FilePath, eachparameter.ElementName.ToString(), "ExternalDataConnector", systemUnitClass); } else { Boolean myBool; Boolean.TryParse(eachparameter.AddToFile, out myBool); if (myBool == true) { } Uri eachUri = null; AttachablesDataGridViewParameters par = new AttachablesDataGridViewParameters(); eachUri = createPictureRef(eachparameter.FilePath, eachparameter.ElementName.ToString(), "ExternalDataConnector", systemUnitClass); par.ElementName = eachUri.ToString(); par.FilePath = eachparameter.FilePath; device.listWithURIConvertedToString.Add(par); } } foreach (var pair in device.DictionaryForRoleClassofComponent) { SupportedRoleClassType supportedRoleClass = null; Match numberfromElectricalConnectorType = Regex.Match(pair.Key.ToString(), @"\((\d+)\)"); string initialnumberbetweenparanthesisofElectricalConnectorType = numberfromElectricalConnectorType.Groups[1].Value; // string stringinparanthesis = Regex.Match(pair.Key.ToString(), @"\{(\d+)\}").Groups[1].Value; string supportedRoleClassFromDictionary = Regex.Replace(pair.Key.ToString(), @"\(.+?\)", ""); supportedRoleClassFromDictionary = Regex.Replace(supportedRoleClassFromDictionary, @"\{.+?\}", ""); var SRC = systemUnitClass.SupportedRoleClass.Append(); var attributesOfSystemUnitClass = systemUnitClass.Attribute; foreach (var valueList in pair.Value) { foreach (var item in valueList) { if (item.AttributePath.Contains("/") || item.AttributePath.Contains(".")) { int count = 2; int counter = 0; Stack <char> stack = new Stack <char>(); string searchAttributeName = item.AttributePath.Substring(0, item.AttributePath.Length - item.Name.Length); foreach (var character in searchAttributeName.Reverse()) { if (!char.IsLetterOrDigit(character)) { counter++; if (counter == count) { break; } } if (char.IsLetterOrDigit(character)) { stack.Push(character); } } string finalAttributeName = new string(stack.ToArray()); foreach (var attribute in systemUnitClass.Attribute) { if (attribute.Name == finalAttributeName) { var eachattribute = attribute.Attribute.Append(item.Name.ToString()); eachattribute.Value = item.Value; eachattribute.DefaultValue = item.Default; eachattribute.Unit = item.Unit; eachattribute.AttributeDataType = item.DataType; eachattribute.Description = item.Description; eachattribute.Copyright = item.CopyRight; eachattribute.ID = item.ID; foreach (var val in item.RefSemanticList.Elements) { var refsem = eachattribute.RefSemantic.Append(); refsem.CorrespondingAttributePath = val.FirstAttribute.Value; } SRC.RefRoleClassPath = item.SupportesRoleClassType; } if (attribute.Attribute.Exists) { SearchForAttributesInsideAttributesofAutomationComponent(finalAttributeName, attribute, item, SRC); } } } else { var eachattribute = attributesOfSystemUnitClass.Append(item.Name.ToString()); eachattribute.Value = item.Value; eachattribute.DefaultValue = item.Default; eachattribute.Unit = item.Unit; eachattribute.AttributeDataType = item.DataType; eachattribute.Description = item.Description; eachattribute.Copyright = item.CopyRight; eachattribute.ID = item.ID; foreach (var val in item.RefSemanticList.Elements) { var refsem = eachattribute.RefSemantic.Append(); refsem.CorrespondingAttributePath = val.FirstAttribute.Value; } SRC.RefRoleClassPath = item.SupportesRoleClassType; } } } foreach (var pairofList in device.DictionaryForExternalInterfacesUnderRoleClassofComponent) { Match numberfromElectricalConnectorPins = Regex.Match(pairofList.Key.ToString(), @"\((\d+)\)"); string initialnumberbetweenparanthesisElectricalConnectorPins = numberfromElectricalConnectorPins.Groups[1].Value; string electricalConnectorPinName = Regex.Replace(pairofList.Key.ToString(), @"\(.*?\)", ""); electricalConnectorPinName = Regex.Replace(electricalConnectorPinName, @"\{.*?\}", ""); electricalConnectorPinName = electricalConnectorPinName.Replace(supportedRoleClassFromDictionary, ""); /*if (initialnumberbetweenparanthesisofElectricalConnectorType == initialnumberbetweenparanthesisElectricalConnectorPins) * { * supportedRoleClass.RoleReference = pairofList.Key.ToString(); * * systemUnitClass.SupportedRoleClass.Append(supportedRoleClass); * systemUnitClass.BaseClass.Name = supportedRoleClassFromDictionary; * * var attributesOfSystemUnitClassattributes = systemUnitClass.Attribute; * * foreach (var valueList in pairofList.Value) * { * foreach (var item in valueList) * { * var eachattribute = attributesOfSystemUnitClassattributes.Append(item.Name.ToString()); * eachattribute.Value = item.Value; * eachattribute.DefaultValue = item.Default; * eachattribute.Unit = item.Unit; * //eachattribute.AttributeDataType = * eachattribute.Description = item.Description; * eachattribute.Copyright = item.CopyRight; * * eachattribute.ID = item.ID; * * * * // systemUnitClass.BaseClass.Name = item.RefBaseClassPath; * } * } * }*/ } } } else { // check if our format is given in the amlx file if not: create it bool foundSysClassLib = false; foreach (var sysclasslib in document.CAEXFile.SystemUnitClassLib) { if (sysclasslib.Name.Equals("ComponentSystemUnitClassLib")) { bool foundSysClass = false; foreach (var sysclass in sysclasslib.SystemUnitClass) { if (sysclass.Name.Equals(device.deviceName)) { foundSysClass = true; systemUnitClass = sysclass; break; } } if (!foundSysClass) { systemUnitClass = sysclasslib.SystemUnitClass.Append(device.deviceName); } foundSysClassLib = true; } } if (!foundSysClassLib) { systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(device.deviceName); } } // Create the internalElement Electrical Interfaces if (device.vendorName != null) { InternalElementType electricalInterface = null; RoleRequirementsType roleRequirements = null; foreach (var internalElement in systemUnitClass.InternalElement) { if (internalElement.Name.Equals("ElectricalInterfaces")) { electricalInterface = internalElement; roleRequirements = electricalInterface.RoleRequirements.Append(); roleRequirements.RefBaseRoleClassPath = structureRoleFamilyType.CAEXPath(); break; } } if (electricalInterface == null) { electricalInterface = systemUnitClass.InternalElement.Append("ElectricalInterfaces"); } roleRequirements = electricalInterface.RoleRequirements.Append(); roleRequirements.RefBaseRoleClassPath = structureRoleFamilyType.CAEXPath(); foreach (var pair in device.DictionaryForInterfaceClassesInElectricalInterfaces) { InternalElementType internalElementofElectricalConnectorType = null; ExternalInterfaceType electricalConnectorType = null; ExternalInterfaceType electricalConnectorPins = null; Match numberfromElectricalConnectorType = Regex.Match(pair.Key.ToString(), @"\((\d+)\)"); string initialnumberbetweenparanthesisofElectricalConnectorType = numberfromElectricalConnectorType.Groups[1].Value; string electricalConnectorTypeName = Regex.Replace(pair.Key.ToString(), @"\(.+?\)", ""); electricalConnectorTypeName = Regex.Replace(electricalConnectorTypeName, @"\{.+?\}", ""); internalElementofElectricalConnectorType = electricalInterface.InternalElement.Append(electricalConnectorTypeName); electricalConnectorType = internalElementofElectricalConnectorType.ExternalInterface.Append(electricalConnectorTypeName); var attributesOfConnectorType = electricalConnectorType.Attribute; foreach (var valueList in pair.Value) { foreach (var item in valueList) { if (item.AttributePath.Contains("/") || item.AttributePath.Contains(".")) { int count = 2; int counter = 0; Stack <char> stack = new Stack <char>(); string searchAttributeName = item.AttributePath.Substring(0, item.AttributePath.Length - item.Name.Length); foreach (var character in searchAttributeName.Reverse()) { if (!char.IsLetterOrDigit(character)) { counter++; if (counter == count) { break; } } if (char.IsLetterOrDigit(character)) { stack.Push(character); } } string finalAttributeName = new string(stack.ToArray()); foreach (var attribute in electricalConnectorType.Attribute) { if (attribute.Name == finalAttributeName) { var eachattribute = attribute.Attribute.Append(item.Name.ToString()); eachattribute.Value = item.Value; eachattribute.DefaultValue = item.Default; eachattribute.Unit = item.Unit; eachattribute.AttributeDataType = item.DataType; eachattribute.Description = item.Description; eachattribute.Copyright = item.CopyRight; eachattribute.ID = item.ID; foreach (var val in item.RefSemanticList.Elements) { var refsem = eachattribute.RefSemantic.Append(); refsem.CorrespondingAttributePath = val.FirstAttribute.Value; } electricalConnectorType.RefBaseClassPath = item.RefBaseClassPath; } if (attribute.Attribute.Exists) { SearchAttributesInsideAttributesOFElectricConnectorType(finalAttributeName, attribute, item, electricalConnectorType); } } } else { var eachattribute = attributesOfConnectorType.Append(item.Name.ToString()); eachattribute.Value = item.Value; eachattribute.DefaultValue = item.Default; eachattribute.Unit = item.Unit; eachattribute.AttributeDataType = item.DataType; eachattribute.Description = item.Description; eachattribute.Copyright = item.CopyRight; eachattribute.ID = item.ID; foreach (var val in item.RefSemanticList.Elements) { var refsem = eachattribute.RefSemantic.Append(); refsem.CorrespondingAttributePath = val.FirstAttribute.Value; } electricalConnectorType.RefBaseClassPath = item.RefBaseClassPath; } } } foreach (var pairofList in device.DictionaryForExternalInterfacesUnderInterfaceClassInElectricalInterfaces) { Match numberfromElectricalConnectorPins = Regex.Match(pairofList.Key.ToString(), @"\((\d+)\)"); string initialnumberbetweenparanthesisElectricalConnectorPins = numberfromElectricalConnectorPins.Groups[1].Value; string electricalConnectorPinName = Regex.Replace(pairofList.Key.ToString(), @"\(.*?\)", ""); electricalConnectorPinName = Regex.Replace(electricalConnectorPinName, @"\{.*?\}", ""); electricalConnectorPinName = electricalConnectorPinName.Replace(electricalConnectorTypeName, ""); if (initialnumberbetweenparanthesisofElectricalConnectorType == initialnumberbetweenparanthesisElectricalConnectorPins) { electricalConnectorPins = electricalConnectorType.ExternalInterface.Append(electricalConnectorPinName); var attributesOfConnectorPins = electricalConnectorPins.Attribute; foreach (var valueList in pairofList.Value) { foreach (var item in valueList) { if (item.AttributePath.Contains("/") || item.AttributePath.Contains(".")) { int count = 2; int counter = 0; Stack <char> stack = new Stack <char>(); string searchAttributeName = item.AttributePath.Substring(0, item.AttributePath.Length - item.Name.Length); foreach (var character in searchAttributeName.Reverse()) { if (!char.IsLetterOrDigit(character)) { counter++; if (counter == count) { break; } } if (char.IsLetterOrDigit(character)) { stack.Push(character); } } string finalAttributeName = new string(stack.ToArray()); foreach (var attribute in electricalConnectorPins.Attribute) { if (attribute.Name == finalAttributeName) { var eachattribute = attribute.Attribute.Append(item.Name.ToString()); eachattribute.Value = item.Value; eachattribute.DefaultValue = item.Default; eachattribute.Unit = item.Unit; eachattribute.AttributeDataType = item.DataType; eachattribute.Description = item.Description; eachattribute.Copyright = item.CopyRight; eachattribute.ID = item.ID; foreach (var val in item.RefSemanticList.Elements) { var refsem = eachattribute.RefSemantic.Append(); refsem.CorrespondingAttributePath = val.FirstAttribute.Value; } electricalConnectorPins.RefBaseClassPath = item.RefBaseClassPath; } if (attribute.Attribute.Exists) { SearchAttributesInsideAttributesOFElectricConnectorType(finalAttributeName, attribute, item, electricalConnectorPins); } } } else { var eachattribute = attributesOfConnectorPins.Append(item.Name.ToString()); eachattribute.Value = item.Value; eachattribute.DefaultValue = item.Default; eachattribute.Unit = item.Unit; eachattribute.AttributeDataType = item.DataType; eachattribute.Description = item.Description; eachattribute.Copyright = item.CopyRight; eachattribute.ID = item.ID; foreach (var val in item.RefSemanticList.Elements) { var refsem = eachattribute.RefSemantic.Append(); refsem.CorrespondingAttributePath = val.FirstAttribute.Value; } electricalConnectorPins.RefBaseClassPath = item.RefBaseClassPath; } } } } } } } // create the PackageUri for the root aml file Uri partUri = PackUriHelper.CreatePartUri(new Uri("/" + fileName + "-root.aml", UriKind.Relative)); // tcreate the aml file as a temporary file string path = Path.GetTempFileName(); document.SaveToFile(path, true); if (isEdit) { // delete the old aml file amlx.Package.DeletePart(partUri); // delete all files in the amlx package. // Directory.Delete(Path.GetFullPath(amlx.ContainerFilename), true); } // write the new aml file into the package PackagePart root = amlx.AddRoot(path, partUri); if (!isEdit) { foreach (AttachablesDataGridViewParameters listWithUri in device.listWithURIConvertedToString) { if (listWithUri.ElementName != null) { Uri newuri = null; newuri = new Uri(listWithUri.ElementName, UriKind.Relative); amlx.AddAnyContent(root, listWithUri.FilePath.ToString(), newuri); } } } DirectoryInfo directory = new DirectoryInfo(Path.GetDirectoryName(amlFilePath)); foreach (FileInfo fileInfos in directory.GetFiles()) { if (fileInfos.Extension != ".amlx") { fileInfos.Delete(); } } amlx.Save(); amlx.Close(); if (isEdit) { return("Sucessfully updated device!\nFilepath " + amlFilePath); } else { return("Device description file created!\nFilepath " + amlFilePath); } }
public void StoreEachAttributeValueInListOfComponent(int i, List <List <ClassOfListsFromReferencefile> > list, AttributeType AttributeInAttribute, AttributeType attributeType, SupportedRoleClassType supportedRoleClass, SystemUnitFamilyType classType) { list = new List <List <ClassOfListsFromReferencefile> >(); List <ClassOfListsFromReferencefile> sublist = new List <ClassOfListsFromReferencefile>(); ClassOfListsFromReferencefile attributeparameters = new ClassOfListsFromReferencefile(); // In the following parameters on right hand side "attributeType" has been changed to "AttributeInAttribute" this has been repeated to all // methods of name "StoreEachAttributeValuesInList" with four parameters. attributeparameters.Name = AttributeInAttribute.Name; attributeparameters.Value = AttributeInAttribute.Value; attributeparameters.Default = AttributeInAttribute.DefaultValue; attributeparameters.Unit = AttributeInAttribute.Unit; attributeparameters.DataType = AttributeInAttribute.AttributeDataType; attributeparameters.Description = AttributeInAttribute.Description; attributeparameters.CopyRight = AttributeInAttribute.Copyright; attributeparameters.AttributePath = AttributeInAttribute.AttributePath; attributeparameters.RefSemanticList = AttributeInAttribute.RefSemantic; // attributeparameters.ReferencedClassName = externalInterface.BaseClass.ToString(); attributeparameters.SupportesRoleClassType = supportedRoleClass.RefRoleClassPath.ToString(); /*attributeparameters.RefBaseClassPath = externalInterface.RefBaseClassPath; * attributeparameters.ID = externalInterface.ID;*/ sublist.Add(attributeparameters); list.Add(sublist); try { if (DictionaryofRolesforAutomationComponenet.ContainsKey("(" + i + ")" + supportedRoleClass.RoleReference.ToString())) { DictionaryofRolesforAutomationComponenet["(" + i + ")" + supportedRoleClass.RoleReference.ToString()].AddRange(list); } else { DictionaryofRolesforAutomationComponenet.Add("(" + i + ")" + supportedRoleClass.RoleReference.ToString(), list); } } catch (Exception) { throw; } }
public void StoreEachAttributeValueInListOfComponent(int i, List <List <ClassOfListsFromReferencefile> > list, AttributeType attributeType, SupportedRoleClassType supportedRoleClass, SystemUnitFamilyType classType) { list = new List <List <ClassOfListsFromReferencefile> >(); List <ClassOfListsFromReferencefile> sublist = new List <ClassOfListsFromReferencefile>(); ClassOfListsFromReferencefile attributeparameters = new ClassOfListsFromReferencefile(); attributeparameters.Name = attributeType.Name; attributeparameters.Value = attributeType.Value; attributeparameters.Default = attributeType.DefaultValue; attributeparameters.Unit = attributeType.Unit; attributeparameters.DataType = attributeType.AttributeDataType; attributeparameters.Description = attributeType.Description; attributeparameters.CopyRight = attributeType.Copyright; attributeparameters.AttributePath = attributeType.AttributePath; attributeparameters.RefSemanticList = attributeType.RefSemantic; // attributeparameters.ReferencedClassName = externalInterface.BaseClass.ToString(); attributeparameters.SupportesRoleClassType = supportedRoleClass.RefRoleClassPath.ToString(); //attributeparameters.ID = supportedRoleClass.ID; sublist.Add(attributeparameters); list.Add(sublist); try { if (DictionaryofRolesforAutomationComponenet.ContainsKey("(" + i + ")" + supportedRoleClass.RoleReference.ToString())) { DictionaryofRolesforAutomationComponenet["(" + i + ")" + supportedRoleClass.RoleReference.ToString()].AddRange(list); } else { DictionaryofRolesforAutomationComponenet.Add("(" + i + ")" + supportedRoleClass.RoleReference.ToString(), list); } } catch (Exception) { throw; } }
public void CkeckForNestedAttributesOfComponent(int i, AttributeType attributeType, SupportedRoleClassType supportedRoleClass, SystemUnitFamilyType classType) { List <List <ClassOfListsFromReferencefile> > attributelist = new List <List <ClassOfListsFromReferencefile> >(); if (attributeType.Attribute.Exists) { foreach (var attributeinattribute in attributeType.Attribute) { StoreEachAttributeValueInListOfComponent(i, attributelist, attributeinattribute, attributeType, supportedRoleClass, classType); CkeckForNestedAttributesOfComponent(i, attributeinattribute, supportedRoleClass, classType); } } if (!attributeType.Attribute.Exists) { List <List <ClassOfListsFromReferencefile> > list = new List <List <ClassOfListsFromReferencefile> >(); List <ClassOfListsFromReferencefile> sublist = new List <ClassOfListsFromReferencefile>(); list.Add(sublist); try { if (DictionaryofRolesforAutomationComponenet.ContainsKey("(" + i + ")" + supportedRoleClass.RoleReference.ToString())) { DictionaryofRolesforAutomationComponenet["(" + i + ")" + supportedRoleClass.RoleReference.ToString()].AddRange(list); } else { DictionaryofRolesforAutomationComponenet.Add("(" + i + ")" + supportedRoleClass.RoleReference.ToString(), list); } } catch (Exception) { throw; } } }
/// <summary> /// Create a new amlx file using <paramref name="newInterface"/> /// </summary> /// <param name="newInterface">the object to create</param> /// <param name="isEdit">true if an amlx file get update, false if a new file will be created</param> /// <returns></returns> public string CreateInterface(MWInterface newInterface, bool isEdit) { // Anlegen des AML / XML's // Siehe TINF17C/software-engineering-1/modelling-wizard/modellingwizardplugin#25 AutomationMLContainer amlx = null; CAEXDocument document = null; // init the filepath string fileName = newInterface.numberOfInterface + "-V.1.0-" + DateTime.Now.Date.ToShortDateString(); string amlFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\modellingwizard\\" + newInterface.numberOfInterface + ".amlx"; FileInfo file = new FileInfo(amlFilePath); file.Directory.Create(); // Init new CAEX Document if (isEdit) { // Load the amlx file amlx = new AutomationMLContainer(amlFilePath, FileMode.Open); IEnumerable <PackagePart> rootParts = amlx.GetPartsByRelationShipType(AutomationMLContainer.RelationshipType.Root); // We expect the aml to only have one root part if (rootParts.First() != null) { PackagePart part = rootParts.First(); // load the aml file as an CAEX document document = CAEXDocument.LoadFromStream(part.GetStream()); } else { // the amlx contains no aml file document = CAEXDocument.New_CAEXDocument(); } } else { // create a new CAEX document document = CAEXDocument.New_CAEXDocument(); amlx = new AutomationMLContainer(amlFilePath, FileMode.Create); } // Init the default Libs AutomationMLBaseRoleClassLibType.RoleClassLib(document); AutomationMLInterfaceClassLibType.InterfaceClassLib(document); SystemUnitFamilyType systemUnitClass = null; // Create the SystemUnitClass for our device if (!isEdit) { systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(newInterface.numberOfInterface.ToString()); } else { bool foundSysClassLib = false; foreach (var sysclasslib in document.CAEXFile.SystemUnitClassLib) { if (sysclasslib.Name.Equals("ComponentSystemUnitClassLib")) { bool foundSysClass = false; foreach (var sysclass in sysclasslib.SystemUnitClass) { if (sysclass.Name.Equals(newInterface.numberOfInterface.ToString())) { foundSysClass = true; systemUnitClass = sysclass; break; } } if (!foundSysClass) { sysclasslib.SystemUnitClass.Append(newInterface.numberOfInterface.ToString()); } foundSysClassLib = true; } } if (!foundSysClassLib) { systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(newInterface.numberOfInterface.ToString()); } } // create the DeviceIdentification InternalElement InternalElementType ie = null; foreach (var internalelement in systemUnitClass.InternalElement) { if (internalelement.Name.Equals("DeviceIdentification")) { ie = internalelement; break; } } if (ie == null) { ie = systemUnitClass.InternalElement.Append("DeviceIdentification"); } // make sure that the attributes are initialized initCAEXAttribute("InterfaceNumber", "xs:integer", ie); initCAEXAttribute("Description", "xs:string", ie); initCAEXAttribute("ConnectorType", "xs:string", ie); initCAEXAttribute("PinCount", "xs:integer", ie); // special handling for the pinlist AttributeType pinlistAtt = null; if (ie.Attribute.GetCAEXAttribute("PinAttributes") == null) { pinlistAtt = ie.Attribute.Append("PinAttributes"); } else { pinlistAtt = ie.Attribute.GetCAEXAttribute("PinAttributes"); pinlistAtt.Attribute.Remove(); } // assign the values for the pinlist foreach (MWPin pin in newInterface.pinList) { pinlistAtt.Attribute.Append(pin.pinNumber.ToString()).Value = pin.attribute; } // assign the values for the 'normal' attributes writeIfNotNull(ie.Attribute.GetCAEXAttribute("InterfaceNumber"), newInterface.numberOfInterface); writeIfNotNull(ie.Attribute.GetCAEXAttribute("Description"), newInterface.interfaceDescription); writeIfNotNull(ie.Attribute.GetCAEXAttribute("ConnectorType"), newInterface.connectorType); writeIfNotNull(ie.Attribute.GetCAEXAttribute("PinCount"), newInterface.amountPins); // create the aml package part Uri partUri = PackUriHelper.CreatePartUri(new Uri("/" + fileName + "-root.aml", UriKind.Relative)); // create a temp file with the new aml string path = Path.GetTempFileName(); document.SaveToFile(path, true); if (isEdit) { // delete the old aml file amlx.Package.DeletePart(partUri); } // copy the new aml file into the package PackagePart root = amlx.AddRoot(path, partUri); amlx.Save(); amlx.Close(); if (isEdit) { return("Sucessfully updated interface!\nFilepath " + amlFilePath); } else { return("Sucessfully created interface!\nFilepath " + amlFilePath); } }
/// <summary> /// Create the AMLX File with the correct AML File and optional pictures /// </summary> /// <param name="device">The device which will be created</param> /// <param name="isEdit">true if an amlx file get update, false if a new file will be created</param> /// <returns></returns> public string CreateDevice(MWDevice device, bool isEdit) { Uri manuPart = null; Uri deviceIconPart = null; Uri devicePicPart = null; CAEXDocument document = null; AutomationMLContainer amlx = null; // Init final .amlx Filepath string fileName = device.vendorName + "-" + device.deviceName + "-V.1.0-" + DateTime.Now.Date.ToShortDateString(); string amlFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\modellingwizard\\" + fileName + ".amlx"; FileInfo file = new FileInfo(amlFilePath); // Create directory if it's not existing file.Directory.Create(); // Init CAEX Document if (isEdit) { // Load the amlx file amlx = new AutomationMLContainer(amlFilePath, FileMode.Open); IEnumerable <PackagePart> rootParts = amlx.GetPartsByRelationShipType(AutomationMLContainer.RelationshipType.Root); // We expect the aml to only have one root part if (rootParts.First() != null) { PackagePart part = rootParts.First(); // load the aml file as an CAEX document document = CAEXDocument.LoadFromStream(part.GetStream()); } else { // the amlx contains no aml file document = CAEXDocument.New_CAEXDocument(); } } else { // create a new CAEX document document = CAEXDocument.New_CAEXDocument(); amlx = new AutomationMLContainer(amlFilePath, FileMode.Create); } // Init the default Libs AutomationMLBaseRoleClassLibType.RoleClassLib(document); AutomationMLInterfaceClassLibType.InterfaceClassLib(document); SystemUnitFamilyType systemUnitClass = null; // Create the SystemUnitClass for our device if (!isEdit) { systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(device.deviceName); } else { // check if our format is given in the amlx file if not: create it bool foundSysClassLib = false; foreach (var sysclasslib in document.CAEXFile.SystemUnitClassLib) { if (sysclasslib.Name.Equals("ComponentSystemUnitClassLib")) { bool foundSysClass = false; foreach (var sysclass in sysclasslib.SystemUnitClass) { if (sysclass.Name.Equals(device.deviceName)) { foundSysClass = true; systemUnitClass = sysclass; break; } } if (!foundSysClass) { systemUnitClass = sysclasslib.SystemUnitClass.Append(device.deviceName); } foundSysClassLib = true; } } if (!foundSysClassLib) { systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(device.deviceName); } } // Convert picture paths to relativ package paths (if they are given) // Convert vendorLogo path if (device.vendorLogo != null && !device.vendorLogo.Equals("")) { try { // Create File Paths manuPart = createPictureRef(device.vendorLogo, "ManufacturerIcon", "ExternalDataConnector", systemUnitClass); } catch (Exception) { // No vendor Logo } } // Convert deviceIcon if (device.deviceIcon != null && !device.deviceIcon.Equals("")) { try { deviceIconPart = createPictureRef(device.deviceIcon, "ComponentIcon", "ExternalDataReference", systemUnitClass); } catch (Exception) { // No Device Icon } } // Convert devicePicture if (device.devicePicture != null && !device.devicePicture.Equals("")) { try { devicePicPart = createPictureRef(device.devicePicture, "ComponentPicture", "ExternalDataReference", systemUnitClass); } catch (Exception) { // No device Picture } } // Create the internalElement DeviceIdentification InternalElementType ie = null; foreach (var internalelement in systemUnitClass.InternalElement) { if (internalelement.Name.Equals("DeviceIdentification")) { ie = internalelement; break; } } if (ie == null) { ie = systemUnitClass.InternalElement.Append("DeviceIdentification"); } // Init the Attributes for our format and set the correct DataTypes initCAEXattributes(ie); // Set the correct values for the Attributes setCAEXattribute(ie, device); // create the PackageUri for the root aml file Uri partUri = PackUriHelper.CreatePartUri(new Uri("/" + fileName + "-root.aml", UriKind.Relative)); // tcreate the aml file as a temporary file string path = Path.GetTempFileName(); document.SaveToFile(path, true); if (isEdit) { // delete the old aml file amlx.Package.DeletePart(partUri); } // write the new aml file into the package PackagePart root = amlx.AddRoot(path, partUri); if (!isEdit) { // copy the images from disk into the package if (manuPart != null) { amlx.AddAnyContent(root, device.vendorLogo, manuPart); } if (deviceIconPart != null) { amlx.AddAnyContent(root, device.deviceIcon, deviceIconPart); } if (devicePicPart != null) { amlx.AddAnyContent(root, device.devicePicture, devicePicPart); } } amlx.Save(); amlx.Close(); if (isEdit) { return("Sucessfully updated device!\nFilepath " + amlFilePath); } else { return("Sucessfully created device!\nFilepath " + amlFilePath); } }