Esempio n. 1
0
        private void ButtonRozdziel_Click(object sender, EventArgs e)
        {
            // nazwa pliku wejściowego DGN
            string inputFileName = textBoxFileName.Text;

            // nazwa pliku LOG
            string logFileName = inputFileName.Substring(0, inputFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".log";

            // lista wartstw z pliku DGN
            List <string> levelsList = new List <string>();

            StreamWriter logFile = new StreamWriter(new FileStream(logFileName, FileMode.Create), Encoding.UTF8);

            logFile.WriteLine("Rozdzielanie pliku: {0}\n", inputFileName);

            DesignFile inputFile = _oMstn.OpenDesignFile(inputFileName, false);

            String outputPath = inputFile.Path + "\\" + inputFile.Name.Substring(0, inputFile.Name.LastIndexOf(".", StringComparison.Ordinal));

            #region Tworzenie katalogu i kasownie plików

            if (Directory.Exists(outputPath))
            {
                DirectoryInfo di = new DirectoryInfo(outputPath);

                foreach (FileInfo file in di.GetFiles())
                {
                    try
                    {
                        file.Delete();
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show(ex.Message, @"Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            else
            {
                Directory.CreateDirectory(outputPath);
            }

            #endregion

            foreach (Level levelIn in inputFile.Levels)
            {
                string levelInName = levelIn.Name;

                if (levelIn.IsInUse)
                {
                    levelsList.Add(levelInName);
                    File.Copy(inputFileName, outputPath + "\\" + levelInName + ".dgn");
                    logFile.WriteLine("warstwa: {0}\t+", levelInName);
                }
                else
                {
                    logFile.WriteLine("warstwa: {0}", levelInName);
                }
            }

            inputFile.Close();

            List <string> levelToRemove = new List <string>();

            foreach (string levelName in levelsList)
            {
                inputFile = _oMstn.OpenDesignFile(outputPath + "\\" + levelName + ".dgn", false);

                _oMstn.CadInputQueue.SendKeyin("delete unused levels");

                foreach (Level levelIn in inputFile.Levels)
                {
                    if (levelIn.Name != levelName)
                    {
                        _oMstn.CadInputQueue.SendKeyin("level element delete \"" + levelIn.Name + "\"");
                    }
                }

                _oMstn.CadInputQueue.SendKeyin("delete unused levels");

                inputFile.Levels.Rewrite();

                inputFile.Save();

                inputFile.Close();
            }

            logFile.Close();
        }
Esempio n. 2
0
        private void ButtonRozdziel_Click(object sender, EventArgs e)
        {
            string iFileName = textBoxFileName.Text;

            StreamWriter logFile = new StreamWriter(new FileStream(iFileName.Substring(0, iFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".log", FileMode.Create), Encoding.UTF8);

            logFile.WriteLine("Rozdielanie pliku: {0}\n", iFileName);

            MicroStationDGN.Application oMstn = new MicroStationDGN.Application();

            DesignFile iFile = oMstn.OpenDesignFile(iFileName, true);

            if (iFile.Models.Count > 1)
            {
                MessageBox.Show(@"Ilość modeli w pliku większa niż 1 !");
                Application.Exit();
            }

            ModelReference modelIn = iFile.Models[1];

            foreach (Level levelIn in iFile.Levels)
            {
                string levelInName = levelIn.Name;

                if (levelIn.IsInUse)
                {
                    logFile.WriteLine("warstwa {0}: uzywana", levelInName);

                    File.Copy(iFileName, @"C:\temp\" + levelIn.Name + ".dgn", true);

                    DesignFile oFile = oMstn.OpenDesignFileForProgram(@"C:\temp\" + levelIn.Name + ".dgn", false);

                    ModelReference modelOut = oFile.Models[1];

                    foreach (Level levelOut in oFile.Levels)
                    {
                        string levelOutName = levelOut.Name;

                        if (levelOutName != levelInName)
                        {
                            if (levelOut.IsInUseWithinModel(modelOut))
                            {
                                ElementScanCriteria elementScanCriteriaOut = new ElementScanCriteriaClass();
                                elementScanCriteriaOut.ExcludeAllLevels();
                                elementScanCriteriaOut.IncludeLevel(levelOut);

                                ElementEnumerator elementEnumeratorOut = modelOut.Scan(elementScanCriteriaOut);

                                //Array elementOut = elementEnumeratorOut.BuildArrayFromContents();

                                while (elementEnumeratorOut.MoveNext())
                                {
                                    modelOut.RemoveElement(elementEnumeratorOut.Current);
                                }
                            }

                            if (!levelOut.IsInUse && levelOut.Number != 0)
                            {
                                oFile.DeleteLevel(levelOut);
                            }
                        }
                    }

                    oFile.RewriteLevels();

                    oFile.Save();

                    oFile.Close();
                }
                else
                {
                    logFile.WriteLine("warstwa {0}: brak", levelInName);
                }
            }

            logFile.Close();

            iFile.Close();

            oMstn.Quit();
        }