예제 #1
0
        private void bttnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                List <int> checkedRows = new List <int>();
                for (int i = 0; i < dataGridViewArea.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(dataGridViewArea.Rows[i].Cells[0].Value))
                    {
                        checkedRows.Add(i);
                    }
                }

                if (checkedRows.Count > 0)
                {
                    double height = 0;
                    if (checkBoxHeight.Checked)
                    {
                        if (!ValidateHeight(out height))
                        {
                            MessageBox.Show("Please enter a valid area height.", "Area Height Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        else if (height == 0)
                        {
                            MessageBox.Show("Please enter a valid area height.", "Area Height Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }

                    massCreator.DefDictionary = defDictionary;

                    SaveHeightValues();
                    Dictionary <int, AreaProperties> createdAreas = new Dictionary <int, AreaProperties>();
                    Dictionary <int, MassProperties> placedMasses = new Dictionary <int, MassProperties>();

                    statusLabel.Text             = "Updating Masses . . .";
                    toolStripProgressBar.Maximum = checkedRows.Count;
                    toolStripProgressBar.Visible = true;

                    StringBuilder resultMessage = new StringBuilder();

                    foreach (int index in checkedRows)
                    {
                        toolStripProgressBar.PerformStep();
                        DataGridViewRow row = dataGridViewArea.Rows[index];
                        if (null != row.Tag)
                        {
                            AreaProperties ap = row.Tag as AreaProperties;
                            MassProperties mp = new MassProperties();
                            mp.HostElementId = ap.ID;
                            if (placedAreas.Contains(ap.ID))
                            {
                                FamilyInstance instance = MassUtils.FindMassById(doc, ap.ID);
                                if (null != instance)
                                {
                                    mp.MassFamilyInstance = instance;

                                    if (!placedMasses.ContainsKey(ap.ID))
                                    {
                                        placedMasses.Add(ap.ID, mp);
                                    }
                                    resultMessage.AppendLine(ap.ID + "\t" + ap.Number + "\t" + ap.Name);
                                }
                                continue;
                            }

                            if (areaDiscrepancy.Contains(ap.ID))
                            {
                                FamilyInstance instance = MassUtils.FindMassById(doc, ap.ID);
                                if (null != instance)
                                {
                                    using (Transaction trans = new Transaction(doc))
                                    {
                                        trans.Start("Delete Element");
                                        doc.Delete(instance.Id);
                                        trans.Commit();
                                    }
                                }
                            }

                            FamilyInstance familyInstance = massCreator.CreateFamily(ap);
                            if (null != familyInstance)
                            {
                                mp.MassFamilyInstance = familyInstance;
                                if (!placedMasses.ContainsKey(ap.ID))
                                {
                                    placedMasses.Add(ap.ID, mp);
                                }
                            }
                            createdAreas.Add(ap.ID, ap);
                            resultMessage.AppendLine(ap.ID + "\t" + ap.Number + "\t" + ap.Name);
                        }
                    }

                    foreach (int areaId in placedAreas)
                    {
                        FamilyInstance instance = MassUtils.FindMassById(doc, areaId);
                        MassProperties mp       = new MassProperties();
                        if (null != instance)
                        {
                            mp.MassFamilyInstance = instance;
                            if (!placedMasses.ContainsKey(areaId))
                            {
                                placedMasses.Add(areaId, mp);
                            }
                        }
                    }

                    statusLabel.Text            = "Done";
                    iniDataManager.CreatedAreas = createdAreas;
                    iniDataManager.WriteINI();

                    if (massCreator.FailureMessage.Length > 0)
                    {
                        DialogResult dr = MessageBox.Show("Errors occured while creating extrusion forms.\n" + massCreator.FailureMessage.ToString(), "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        if (dr == DialogResult.OK)
                        {
                            using (Transaction trans = new Transaction(doc))
                            {
                                trans.Start("Set Shared Parameters");
                                m_app.Application.SharedParametersFilename = originalDefFile;
                                trans.Commit();
                            }
                            this.Close();
                        }
                    }
                    else
                    {
                        if (resultMessage.Length > 0)
                        {
                            string         header      = "Area Masses are sucessfully created. \n[Area ID], [Area Number], [Area Name]\n";
                            MessageBoxForm messageForm = new MessageBoxForm("Completion Messages", header + resultMessage.ToString(), "", false, false);
                            if (DialogResult.OK == messageForm.ShowDialog())
                            {
                                using (Transaction trans = new Transaction(doc))
                                {
                                    trans.Start("Set Shared parameters");
                                    m_app.Application.SharedParametersFilename = originalDefFile;
                                    trans.Commit();
                                }
                                this.Close();
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please select at leaset one Area item to proceed.", "Empty Selection", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create mass rooms.\n" + ex.Message, "Form_CreateMass:BttnCreate_Click", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
예제 #2
0
        private void bttnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                List <int> checkedRows = new List <int>();
                for (int i = 0; i < dataGridViewRoom.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(dataGridViewRoom.Rows[i].Cells[0].Value))
                    {
                        checkedRows.Add(i);
                    }
                }

                if (checkedRows.Count > 0)
                {
                    using (TransactionGroup tg = new TransactionGroup(doc))
                    {
                        tg.Start("Update Masses");
                        try
                        {
                            double height = 0;
                            if (checkBoxHeight.Checked)
                            {
                                if (!ValidateHeight(out height))
                                {
                                    MessageBox.Show("Please enter a valid room height.", "Room Height Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    return;
                                }
                                else if (height == 0)
                                {
                                    MessageBox.Show("Please enter a valid room height.", "Room Height Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    return;
                                }
                            }

                            massCreator.DefDictionary = defDictionary;

                            Dictionary <int, RoomProperties> createdRooms = new Dictionary <int, RoomProperties>(); //created rooms at this run
                            Dictionary <int, MassProperties> placedMasses = new Dictionary <int, MassProperties>();

                            statusLabel.Text             = "Updating Masses . . .";
                            toolStripProgressBar.Maximum = checkedRows.Count;
                            toolStripProgressBar.Visible = true;

                            StringBuilder resultMessage = new StringBuilder();

                            foreach (int index in checkedRows)
                            {
                                toolStripProgressBar.PerformStep();
                                DataGridViewRow row = dataGridViewRoom.Rows[index];
                                if (null != row.Tag)
                                {
                                    RoomProperties rp = row.Tag as RoomProperties;
                                    MassProperties mp = new MassProperties();
                                    mp.HostElementId   = rp.ID;
                                    rp.IsDefaultHeight = checkBoxHeight.Checked;
                                    rp.DefaultHeight   = height;

                                    if (placedRooms.Contains(rp.ID))
                                    {
                                        FamilyInstance instance = MassUtils.FindMassById(doc, rp.ID);
                                        if (null != instance)
                                        {
                                            mp.MassFamilyInstance = instance;
                                            double roomHeight = (rp.IsDefaultHeight == true) ? rp.DefaultHeight : rp.UnboundedHeight;
#if RELEASE2015 || RELEASE2016
                                            Parameter parameter = instance.LookupParameter("Height");
#elif RELEASE2013 || RELEASE2014
                                            Parameter parameter = instance.get_Parameter("Height");
#endif

                                            if (null != parameter)
                                            {
                                                using (Transaction trans = new Transaction(doc))
                                                {
                                                    trans.Start("Update Height");
                                                    try
                                                    {
                                                        parameter.Set(roomHeight);
                                                        trans.Commit();
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        string message = ex.Message;
                                                        trans.RollBack();
                                                    }
                                                }
                                            }

                                            if (!placedMasses.ContainsKey(rp.ID))
                                            {
                                                placedMasses.Add(rp.ID, mp);
                                            }

                                            resultMessage.AppendLine(rp.ID + "\t" + rp.Number + "\t" + rp.Name);
                                        }
                                        continue;
                                    }

                                    if (roomDiscrepancy.Contains(rp.ID))
                                    {
                                        FamilyInstance instance = MassUtils.FindMassById(doc, rp.ID);
                                        if (null != instance)
                                        {
                                            using (Transaction trans = new Transaction(doc))
                                            {
                                                trans.Start("Delete Element");
                                                doc.Delete(instance.Id);
                                                trans.Commit();
                                            }
                                        }
                                    }

                                    FamilyInstance familyInstance = massCreator.CreateFamily(rp);
                                    if (null != familyInstance)
                                    {
                                        mp.MassFamilyInstance = familyInstance;
                                        if (!placedMasses.ContainsKey(rp.ID))
                                        {
                                            placedMasses.Add(rp.ID, mp);
                                        }
                                    }

                                    createdRooms.Add(rp.ID, rp);
                                    resultMessage.AppendLine(rp.ID + "\t" + rp.Number + "\t" + rp.Name);
                                }
                            }
                            //to include placed masses from the previous run into the snapshot.png
                            foreach (int roomId in placedRooms)
                            {
                                FamilyInstance instance = MassUtils.FindMassById(doc, roomId);
                                MassProperties mp       = new MassProperties();
                                if (null != instance)
                                {
                                    mp.MassFamilyInstance = instance;
                                    if (!placedMasses.ContainsKey(roomId))
                                    {
                                        placedMasses.Add(roomId, mp);
                                    }
                                }
                            }

                            statusLabel.Text             = "Done";
                            roomDataManager.CreatedRooms = createdRooms;
                            roomDataManager.WriteINI();
                            if (massCreator.FailureMessage.Length > 0)
                            {
                                DialogResult dr = MessageBox.Show("Errors occured while creating extrusion forms.\n" + massCreator.FailureMessage.ToString(), "Warning Messages", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                if (dr == DialogResult.OK)
                                {
                                    using (Transaction trans = new Transaction(doc))
                                    {
                                        trans.Start("Set Shared Parameters");
                                        m_app.Application.SharedParametersFilename = originalDefFile;
                                        trans.Commit();
                                    }

                                    this.Close();
                                }
                            }
                            else
                            {
                                if (resultMessage.Length > 0)
                                {
                                    string         header      = "Room Masses are sucessfully updated. \n[Room ID], [Room Number], [Room Name]\n";
                                    MessageBoxForm messageForm = new MessageBoxForm("Completion Messages", header + resultMessage.ToString(), "", false, false);
                                    if (DialogResult.OK == messageForm.ShowDialog())
                                    {
                                        using (Transaction trans = new Transaction(doc))
                                        {
                                            trans.Start("Set Shared Parameters");
                                            m_app.Application.SharedParametersFilename = originalDefFile;
                                            trans.Commit();
                                        }
                                        this.Close();
                                    }
                                }
                            }
                            tg.Assimilate();
                        }
                        catch (Exception ex)
                        {
                            tg.RollBack();
                            MessageBox.Show("Failed to update masses from rooms.\n" + ex.Message, "Create Masses from Rooms", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please select at leaset one Room item to proceed.", "Empty Selection", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create mass rooms.\n" + ex.Message, "Form_CreateMass:BttnCreate_Click", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }