Esempio n. 1
0
        /// <summary>Generates a source file from the given PlcBlock</summary>
        /// <param name="exportItem">The export item.</param>
        /// <param name="exportPath">The export path.</param>
        /// <param name="options"></param>
        /// <returns>String</returns>
        /// <exception cref="System.ArgumentNullException">Parameter is null;exportItem</exception>
        /// <exception cref="System.ArgumentException">Parameter is null or empty;exportPath</exception>
        /// <exception cref="Siemens.Engineering.EngineeringException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="System.UnauthorizedAccessException"></exception>
        /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
        public static string GenerateSourceFromBlock(PlcBlock exportItem, string exportPath, GenerateOptions options)
        {
            if (exportItem == null)
            {
                throw new ArgumentNullException(nameof(exportItem), "Parameter is null");
            }
            if (String.IsNullOrEmpty(exportPath))
            {
                throw new ArgumentException("Parameter is null or empty", nameof(exportPath));
            }

            var filePath = Path.GetFullPath(exportPath);

            if (!exportItem.IsKnowHowProtected)
            {
                Directory.CreateDirectory(filePath);
                switch (exportItem.ProgrammingLanguage)
                {
                case ProgrammingLanguage.DB:
                    filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(exportItem.Name) + ".db");
                    break;

                case ProgrammingLanguage.SCL:
                    filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(exportItem.Name) + ".scl");
                    break;

                case ProgrammingLanguage.STL:
                    filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(exportItem.Name) + ".awl");
                    break;

                default:
                    return(null);
                }

                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }

                IEngineeringInstance temp = exportItem;

                do
                {
                    temp = temp.Parent;
                }while (!(temp is PlcSoftware));

                (temp as PlcSoftware).ExternalSourceGroup.GenerateSource(new[] { exportItem }, new FileInfo(filePath), options);

                return(filePath);
            }
            throw new EngineeringException(string.Format(CultureInfo.InvariantCulture, "Block: '{0}' is Know-how protected! \r\n 'Generate source from block' is not possible on know how protected blocks!", exportItem.Name));
        }
Esempio n. 2
0
        /// <summary>
        /// Exports the given object with the give options to the defined path.
        /// </summary>
        /// <param name="exportItem">Object to export</param>
        /// <param name="exportOption">Export options</param>
        /// <param name="exportPath">Folder path in which to export</param>
        /// <returns>String</returns>
        /// <exception cref="System.ArgumentNullException">Parameter is null;exportItem</exception>
        /// <exception cref="System.ArgumentException">Parameter is null or empty;exportPath</exception>
        /// <exception cref="Siemens.Engineering.EngineeringException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="System.UnauthorizedAccessException"></exception>
        /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
        public static string ExportItem(IEngineeringObject exportItem, ExportOptions exportOption, string exportPath)
        {
            if (exportItem == null)
            {
                throw new ArgumentNullException(nameof(exportItem), "Parameter is null");
            }
            if (String.IsNullOrEmpty(exportPath))
            {
                throw new ArgumentException("Parameter is null or empty", nameof(exportPath));
            }

            var filePath = Path.GetFullPath(exportPath);

            if (exportItem is PlcBlock)
            {
                var    block     = exportItem as PlcBlock;
                string blockName = GetObjectName(exportItem);
                if (block.ProgrammingLanguage == ProgrammingLanguage.ProDiag || block.ProgrammingLanguage == ProgrammingLanguage.ProDiag_OB || block.ProgrammingLanguage == ProgrammingLanguage.SCL)
                {
                    return(null);
                }
                if (block.IsConsistent)
                {
                    blockName = XmlParser.RemoveWindowsUnallowedChars(blockName);

                    filePath = Path.Combine(filePath, blockName + ".xml");

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    (exportItem as PlcBlock).Export(new FileInfo(filePath), exportOption);

                    return(filePath);
                }
                else
                {
                    MessageBox.Show("Block: " + blockName + " is inconsistent! It will not be exported.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(null);
                }
            }

            if (exportItem is PlcTagTable || exportItem is PlcType || exportItem is ScreenOverview || exportItem is ScreenGlobalElements ||
                exportItem is Screen || exportItem is TagTable || exportItem is Connection || exportItem is GraphicList ||
                exportItem is TextList || exportItem is Cycle || exportItem is MultiLingualGraphic || exportItem is ScreenTemplate ||
                exportItem is VBScript || exportItem is ScreenPopup || exportItem is ScreenSlidein)
            {
                Directory.CreateDirectory(filePath);
                filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(GetObjectName(exportItem)) + ".xml");
                File.Delete(filePath);
                var parameter = new Dictionary <Type, object>();
                parameter.Add(typeof(FileInfo), new FileInfo(filePath));
                parameter.Add(typeof(ExportOptions), exportOption);
                exportItem.Invoke("Export", parameter);
                return(filePath);
            }

            if (exportItem is PlcExternalSource)
            {
                //Directory.CreateDirectory(filePath);
                //filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(GetObjectName(exportItem)));
                //File.Delete(filePath);
                //File.Create(filePath);
                //return filePath;
            }
            return(null);
        }