/// <summary>
        /// Get the current conector of the element
        /// </summary>
        /// <param name="elm"></param>
        /// <param name="conn"></param>
        private void CheckDoubleConn(ref int checkValue, ref ConnectorSet connectSet, ref Connector CurrentConnector)
        {
            List <Connector> connectorList   = new List <Connector>();
            List <Element>   adjacentElement = new List <Element>();


            foreach (Connector c in connectSet)
            {
                if (ConnectorsHelper.ConnStatus(c))
                {
                    ConnectorSet secondary = c.AllRefs;
                    foreach (Connector cone in secondary)
                    {
                        adjacentElement.Add(this._doc.GetElement(cone.Owner.Id));
                    }
                }
            }

            adjacentElement = adjacentElement.Distinct <Element>().ToList <Element>();
            Connector temConnector = null;
            int       count        = 0;

            foreach (Element elem in adjacentElement)
            {
                foreach (Connector c3 in connectorList)
                {
                    if (this.isAdjacentValidElmt(elem, c3))
                    {
                        count++;
                        temConnector = c3;
                    }
                }
            }

            if (count == 1)
            {
                checkValue       = 1;
                CurrentConnector = temConnector;
            }
        }
        private void NumberPartButton_Click(object sender, RoutedEventArgs el)
        {
            Connector        CurrentConnector = null;
            List <Element>   RunElements      = new List <Element>();
            List <ElementId> RunElementsId    = new List <ElementId>();

            base.Hide();
            Element e;


            //Select the element only if it is a part number
            using (Transaction AssingPartNumberT = new Transaction(tools.uidoc.Document, "Assing Part Number"))
            {
                AssingPartNumberT.Start();
                e = tools.NewSelection();
                AssingPartNumberT.Commit();
            }

            //Make sure that the user selected a valid element
            if (e == null)
            {
                goto CloseApp;
            }


            ConnectorSet connectors   = this.getConnectorSetFromElement(e);
            int          connectCount = 0;


            //Get the numbers of connectors of the element
            foreach (Connector c in connectors)
            {
                //Conector must be connected, must be a End conector and should have another element connected to it.
                if (c.IsConnected && c.ConnectorType.ToString() != "Curve" && this.ConnectionCheck(c) != 0)
                {
                    connectCount    += 1;
                    CurrentConnector = c;
                }
            }

            //Check if the element is isolated
            if (connectCount == 0 || SnglElmtButton)
            {
                RunElements.Add(e);
                connectCount = 0;
            }

            //Check if the element has more than 2 connections
            if (connectCount >= 2)
            {
                //check if the element is a false positive
                this.CheckDoubleConn(ref connectCount, ref connectors, ref CurrentConnector);

                if (connectCount >= 2)
                {
                    System.Windows.MessageBox.Show("The elements is not an endpoint", "warning");
                    goto CloseApp;
                }
            }

            //Check if the element has 1 connection
            if (connectCount == 1)
            {
                int              tempValue          = 1;
                ConnectorSet     TempConnectors     = null;
                List <Connector> TempConnectorsList = new List <Connector>();
                Element          tempElem           = e;
                int              iterationsCount    = 0;
                TempConnectors = connectors;

                while (tempValue == 1)
                {
                    int countConnect = 0;

                    if (checkRoundDucts(RoundDuctsBool, tempElem))
                    {
                        RunElements.Add(tempElem);
                    }

                    RunElementsId.Add(tempElem.Id);

                    //Make sure that the elements are conected if connectors
                    //are geometrically aligned
                    foreach (Connector fcon in TempConnectors)
                    {
                        if (ConnectorsHelper.ConnStatus(fcon))
                        {
                            if (this.ConnectionCheck(fcon) != 0)
                            {
                                countConnect++;
                            }
                        }
                    }

                    //Check if the element is a tee
                    if (ConnectorsHelper.TeeDetect(TempConnectors))
                    {
                        tempValue = 2;
                        goto EndLoop;
                    }

                    if (countConnect == 1 && iterationsCount != 0)
                    {
                        tempValue = 2;
                        goto EndLoop;
                    }
                    else
                    {
                        ConnectorSet secondary = CurrentConnector.AllRefs;
                        foreach (Connector cone in secondary)
                        {
                            if (!RunElementsId.Contains(cone.Owner.Id))
                            {
                                //Check if the following element is connected from the side
                                tempElem = this._doc.GetElement(cone.Owner.Id);

                                Category        cat     = tempElem.Category;
                                BuiltInCategory enumCat = (BuiltInCategory)cat.Id.IntegerValue;

                                if (enumCat.ToString() != "OST_FabricationDuctwork")
                                {
                                    tempValue = 2;
                                    goto EndLoop;
                                }
                            }
                        }



                        TempConnectors = this.getConnectorSetFromElement(tempElem);

                        foreach (Connector connec in TempConnectors)
                        {
                            //Check that the previous element is not connecting from side
                            if (!ConnectorsHelper.ConnStatus(connec))
                            {
                                foreach (Connector con in connec.AllRefs)
                                {
                                    if (RunElementsId.Contains(con.Owner.Id))
                                    {
                                        tempValue = 2;
                                        goto EndLoop;
                                    }
                                }
                            }

                            //Get the next useful connector
                            if (ConnectorsHelper.ConnStatus(connec))
                            {
                                foreach (Connector con in connec.AllRefs)
                                {
                                    if (!RunElementsId.Contains(con.Owner.Id))
                                    {
                                        CurrentConnector = connec;
                                    }
                                }
                            }
                        }
                    }
EndLoop:
                    iterationsCount++;
                }
            }


            List <string> listToStringComplete = this.listToString(RunElements);

            if (RunElements.Count != 0)
            {
                foreach (Element elmnt in RunElements)
                {
                    using (Transaction AssingPartNumberT2 = new Transaction(tools.uidoc.Document, "Assing Part Number"))
                    {
                        tools.selectedElements.Clear();
                        AssingPartNumberT2.Start();


                        //Find all the elements that are the same in the RunElements list
                        tools.AddToSelection(elmnt, listToStringComplete, RunElements, checkBoxDuplicates);

                        //Generate the part number
                        string partNumber = tools.createNumbering(this.PrefixBox.Text, this.SeparatorBox.Text, tools.count, this.NumberBox.Text.Length);

                        //Assign the part number to each selected element
                        foreach (Element x in tools.selectedElements)
                        {
                            tools.AssingPartNumber(x, partNumber);
                        }


                        if (tools.selectedElements.Count != 0)
                        {
                            tools.count++;
                        }

                        int leadingZeros = (this.NumberBox.Text.Length > 1) ? (this.NumberBox.Text.Length - tools.count.ToString().Length) : 0;

                        if (leadingZeros >= 0)
                        {
                            this.NumberBox.Text = new string('0', leadingZeros) + tools.count.ToString();
                        }
                        else
                        {
                            this.NumberBox.Text = tools.count.ToString();
                        }

                        tools.writeConfig(this.PrefixBox.Text, this.SeparatorBox.Text, this.NumberBox.Text);
                        Options options = this.uiApp.Application.Create.NewGeometryOptions();
                        AssingPartNumberT2.Commit();
                    }
                }
            }



            CloseApp :;
            base.ShowDialog();
        }