public PCF_Importer_form(ExternalCommandData cData, ref string message)
        {
            InitializeComponent();
            _commandData = cData;
            _uiapp       = _commandData.Application;
            _uidoc       = _uiapp.ActiveUIDocument;
            _doc         = _uidoc.Document;
            _message     = message;

            //Init saved values
            _excelPath = mySettings.Default.excelPath;
            _pcfPath   = mySettings.Default.pcfPath;

            //Init textboxes
            textBox1.Text = _pcfPath;
            textBox2.Text = _excelPath;
            textBox3.Text = mySettings.Default.numberOfPipelinesDetected.ToString();

            //Init radiobuttons
            iv.ConfigureAll = mySettings.Default.configureAll;
            if (iv.ConfigureAll)
            {
                radioButton1.Checked = true;
                radioButton2.Checked = false;
            }
            else
            {
                radioButton1.Checked = false;
                radioButton2.Checked = true;
            }

            PcfDict    = new PCF_Dictionary(new KeywordProcessor());
            PcfCreator = new PCF_Creator(new ProcessElements());
        }
Exemple #2
0
        public Result ExecuteMyCommand(UIApplication uiApp, ref string message)
        {
            UIDocument  uidoc = uiApp.ActiveUIDocument;
            Application app   = uiApp.Application;

            doc     = uidoc.Document;
            PcfDict = new PCF_Dictionary(new KeywordProcessor());

            ExtractedElementCollection = new ElementCollection();

            //Read the input PCF file
            FileReader fileReader = new FileReader();

            string[] readFile = fileReader.ReadFile(mySettings.Default.pcfPath);

            //This method collects all top-level element strings and creates ElementSymbols with data
            Parser.CreateInitialElementList(ExtractedElementCollection, readFile);

            //This method compares all element symbols and gets the number of lines for their definition
            Parser.IndexElementDefinitions(ExtractedElementCollection, readFile);

            //This method extracts element data from the file
            Parser.ExtractElementDefinition(ExtractedElementCollection, readFile);

            //This method processes elements
            foreach (ElementSymbol elementSymbol in ExtractedElementCollection.Elements)
            {
                PcfDict.ProcessTopLevelKeywords(elementSymbol);
            }

            #region CONFIGURATION

            DataSet dataSet = PCF_Configuration.ImportExcelToDataSet(mySettings.Default.excelPath);

            foreach (ElementSymbol es in
                     from ElementSymbol es in ExtractedElementCollection.Elements
                     where !( //Filter out non pipeline elements
                         string.Equals(es.PipelineReference, "PRE-PIPELINE") ||
                         string.Equals(es.PipelineReference, "MATERIALS") ||
                         string.Equals(es.ElementType, "PIPELINE-REFERENCE")
                         )
                     select es)

            {
                PCF_Configuration.ExtractElementConfiguration(dataSet, es);
            }

            #endregion

            #region Element creation

            using (TransactionGroup txGp = new TransactionGroup(doc))
            {
                txGp.Start("Create elements from PCF data");

                using (Transaction trans1 = new Transaction(doc))
                {
                    trans1.Start("Create elements");
                    PcfCreator = new PCF_Creator(new ProcessElements());
                    //This method creates elements
                    //First send pipes for creation, other elements after
                    //Filter for pipes
                    var pipeQuery = from ElementSymbol es in ExtractedElementCollection.Elements
                                    where string.Equals(es.ElementType, "PIPE")
                                    select es;
                    //Send pipes to creation
                    foreach (ElementSymbol es in pipeQuery)
                    {
                        PcfCreator.SendElementsToCreation(es);
                    }
                    //Regenerate document
                    doc.Regenerate();
                    //The rest of the elements are sent in waves, because fx. I determined, that CAPs must be sent later
                    //It depends on if the element can be created as standalone or it would need other elements to be present
                    //CAPS must be sent later
                    var firstWaveElementsQuery = from ElementSymbol es in ExtractedElementCollection.Elements
                                                 where
                                                 !( //Take care! ! operator has lower precedence than ||
                        string.Equals(es.ElementType, "PIPE") ||
                        string.Equals(es.ElementType, "CAP") ||
                        string.Equals(es.ElementType, "FLANGE-BLIND") ||
                        string.Equals(es.ElementType, "OLET") ||
                        string.Equals(es.ElementType, "VALVE")
                        )
                                                 select es;
                    //Send elements to creation
                    foreach (ElementSymbol es in firstWaveElementsQuery)
                    {
                        PcfCreator.SendElementsToCreation(es);
                    }
                    trans1.Commit();
                }

                using (Transaction trans2 = new Transaction(doc))
                {
                    trans2.Start("Create second wave of elements");
                    //Filter CAPs
                    var secondWaveElementsQuery = from ElementSymbol es in ExtractedElementCollection.Elements
                                                  where
                                                  string.Equals(es.ElementType, "CAP") ||
                                                  string.Equals(es.ElementType, "FLANGE-BLIND") ||
                                                  string.Equals(es.ElementType, "OLET")
                                                  select es;
                    //Send CAPs to creation
                    foreach (ElementSymbol es in secondWaveElementsQuery)
                    {
                        PcfCreator.SendElementsToCreation(es);
                    }
                    trans2.Commit();
                }

                using (Transaction trans3 = new Transaction(doc))
                {
                    trans3.Start("Create third wave of elements");
                    //Filter CAPs
                    var thirdWaveElementsQuery = from ElementSymbol es in ExtractedElementCollection.Elements
                                                 where string.Equals(es.ElementType, "VALVE")
                                                 select es;
                    //Send CAPs to creation
                    foreach (ElementSymbol es in thirdWaveElementsQuery)
                    {
                        PcfCreator.SendElementsToCreation(es);
                    }
                    trans3.Commit();
                }

                using (Transaction tx = new Transaction(doc))
                {
                    tx.Start("Delete dummy elements");
                    IEnumerable <Element> query = from ElementSymbol es in ExtractedElementCollection.Elements
                                                  where es.DummyToDelete != null
                                                  select es.DummyToDelete;
                    try
                    {
                        foreach (Element e in query)
                        {
                            doc.Delete(e.Id);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    tx.Commit();
                }
                txGp.Assimilate();
            }

            #endregion



            //Test
            //int test = ExtractedElementCollection.Elements.Count;

            //using (Transaction tx = new Transaction(doc))
            //{
            //    tx.Start("Transaction Name");
            //    tx.Commit();
            //}

            return(Result.Succeeded);
        }