/// <summary>
        /// Create an AMLX file with the aml file as string input
        /// </summary>
        /// <param name="caex">the complete aml file as a string</param>
        /// <param name="filename">the path to the original gsdml/iodd file</param>
        /// <returns>the result message as a string</returns>
        private string createAMLXFromString(string caex, string filename)
        {
            // create the CAEXDocument from byte string
            byte[]       bytearray = System.Text.Encoding.Unicode.GetBytes(caex);
            CAEXDocument document  = CAEXDocument.LoadFromBinary(bytearray);

            // create the amlx file
            string name = Path.GetFileNameWithoutExtension(filename);
            AutomationMLContainer amlx = new AutomationMLContainer(".\\modellingwizard\\" + name + ".amlx", FileMode.Create);

            // create the aml package path
            Uri partUri = PackUriHelper.CreatePartUri(new Uri("/" + name + "-root.aml", UriKind.Relative));

            // create a temp aml file
            string path = Path.GetTempFileName();

            document.SaveToFile(path, true);

            // copy the new aml into the package
            PackagePart root = amlx.AddRoot(path, partUri);

            // copy the original file into the package
            Uri gsdURI     = new Uri(new FileInfo(filename).Name, UriKind.Relative);
            Uri gsdPartURI = PackUriHelper.CreatePartUri(gsdURI);

            amlx.AddAnyContent(root, filename, gsdPartURI);

            amlx.Save();
            amlx.Close();

            return("Sucessfully imported device!\nCreated File " + Path.GetFullPath(".\\modellingwizard\\" + name + ".amlx"));
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an AMLX package.
        /// </summary>
        /// <param name="inputPath">The path to the IODD file.</param>
        /// <param name="outputPath">The path to the AML file.</param>
        /// <param name="amlxOutputPath">The output path for the AMLX package.</param>
        public void CreatePackage(string inputPath, string outputPath, string amlxOutputPath)
        {
            PathToIodd = inputPath;
            PathToAml  = outputPath;

            var pathToSourceFolder = Directory.GetParent(inputPath);
            // Set path for AMLX package to IODD file path if amlxOutoutPath is null.
            var pathToAmlxPackage = amlxOutputPath ?? CreateAmlxPackageName(PathToIodd);

            var files = pathToSourceFolder.GetFiles();

            // Deletes an existing AMLX package which would cause errors.
            if (File.Exists(pathToAmlxPackage))
            {
                File.Delete(pathToAmlxPackage);
            }

            // Gets all paths of images needed for the AMLX package.
            foreach (var file in files)
            {
                if (file.Extension.Contains("jpeg") || file.Extension.Contains("jpg") || file.Extension.Contains("png"))
                {
                    PathsToImages.Add(file.FullName);
                }
            }
            // Creates a new AutomationMLContainer
            using (var container = new AutomationMLContainer(pathToAmlxPackage))
            {
                // Sets the root of the container to the AML file, adds the schema, the iodd and the icons.
                var root = container.AddRoot(PathToAml, new Uri("/" + Path.GetFileName(PathToAml), UriKind.RelativeOrAbsolute));
                container.AddCAEXSchema(Assembly.GetExecutingAssembly().GetManifestResourceStream("Iodd2AmlConverter.Library.Resources.CAEX_ClassModel_V2.15.xsd"), new Uri("/CAEX_ClassModel_V2.15.xsd", UriKind.RelativeOrAbsolute));

                foreach (var pathToImage in PathsToImages)
                {
                    container.AddAnyContent(root, pathToImage, new Uri("/" + Path.GetFileName(pathToImage), UriKind.RelativeOrAbsolute));
                }

                container.AddAnyContent(root, PathToIodd, new Uri("/" + Path.GetFileName(PathToIodd), UriKind.RelativeOrAbsolute));
                container.Close();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Load the amlx container and try to load it as an <see cref="MWObject"/>
        /// </summary>
        /// <param name="file">The full path to the amlx file</param>
        /// <returns></returns>
        public MWObject loadObject(string file)
        {
            try
            {
                FileInfo fileInfo   = new FileInfo(file);
                string   objectName = fileInfo.Name;

                // Load the amlx container from the given filepath
                AutomationMLContainer amlx = new AutomationMLContainer(file);

                // Get the root path -> main .aml file
                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
                    CAEXDocument document = CAEXDocument.LoadFromStream(part.GetStream());


                    // Iterate over all SystemUnitClassLibs and SystemUnitClasses and scan if it matches our format
                    // since we expect only one device per aml(x) file, return after on is found
                    foreach (SystemUnitClassLibType classLibType in document.CAEXFile.SystemUnitClassLib)
                    {
                        foreach (SystemUnitFamilyType classLib in classLibType.SystemUnitClass)
                        {
                            // check if it matches our format
                            foreach (InternalElementType internalElement in classLib.InternalElement)
                            {
                                // is the DeviceIdentification there?
                                if (internalElement.Name.Equals("DeviceIdentification"))
                                {
                                    // is it an interface or a device?
                                    if (internalElement.Attribute.GetCAEXAttribute("InterfaceNumber") != null)
                                    {
                                        MWInterface mWInterface = new MWInterface();
                                        mWInterface.numberOfInterface = Convert.ToInt32(internalElement.Attribute.GetCAEXAttribute("InterfaceNumber").Value);

                                        // read the attributes and write them directly into the interface
                                        fillInterfaceWithData(mWInterface, internalElement.Attribute);

                                        amlx.Close();
                                        return(mWInterface);
                                    }
                                    else if (internalElement.Attribute.GetCAEXAttribute("DeviceName") != null)
                                    {
                                        MWDevice mWDevice = new MWDevice();

                                        // read the attributes and write them directly into the device
                                        fillDeviceWithData(mWDevice, internalElement.Attribute);

                                        // check if there are pictures provided
                                        foreach (InternalElementType ie in classLib.InternalElement)
                                        {
                                            switch (ie.Name)
                                            {
                                            case "ManufacturerIcon":
                                                try
                                                {
                                                    mWDevice.vendorLogo = ie.ExternalInterface.First().Attribute.GetCAEXAttribute("refURI").Value;
                                                }
                                                catch (Exception)
                                                {
                                                    // No vendorLogo
                                                }
                                                break;

                                            case "ComponentPicture":
                                                try
                                                {
                                                    mWDevice.devicePicture = ie.ExternalInterface.First().Attribute.GetCAEXAttribute("refURI").Value;
                                                }
                                                catch (Exception)
                                                {
                                                    // No vendorLogo
                                                }
                                                break;

                                            case "ComponentIcon":
                                                try
                                                {
                                                    mWDevice.deviceIcon = ie.ExternalInterface.First().Attribute.GetCAEXAttribute("refURI").Value;
                                                }
                                                catch (Exception)
                                                {
                                                    // No vendorLogo
                                                }
                                                break;
                                            }
                                        }
                                        amlx.Close();
                                        return(mWDevice);
                                    }
                                }
                            }
                        }
                    }
                }
                amlx.Close();
                return(null);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error while loading the AMLX-File", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return(null);
            }
        }