private void showElementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UIDocument       uidoc      = m_app.ActiveUIDocument;
                Document         m_doc      = uidoc.Document;
                List <ElementId> elementIds = new List <ElementId>();

#if RELEASE2013 || RELEASE2014
                SelElementSet newSelection = SelElementSet.Create();

                foreach (DataGridViewRow row in dataGridViewElement.SelectedRows)
                {
                    if (null != row.Tag)
                    {
                        ElementProperties ep        = row.Tag as ElementProperties;
                        ElementId         elementId = new ElementId(ep.ElementId);
                        elementIds.Add(elementId);
                        newSelection.Add(ep.ElementObj);
                    }
                }
                uidoc.ShowElements(elementIds);
                uidoc.Selection.Elements = newSelection;
#elif RELEASE2015 || RELEASE2016
                Selection selection = uidoc.Selection;

                foreach (DataGridViewRow row in dataGridViewElement.SelectedRows)
                {
                    if (null != row.Tag)
                    {
                        ElementProperties ep        = row.Tag as ElementProperties;
                        ElementId         elementId = new ElementId(ep.ElementId);
                        elementIds.Add(elementId);
                    }
                }
                uidoc.ShowElements(elementIds);
                selection.SetElementIds(elementIds);
#endif
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to show elements.\n" + ex.Message, "Form_OverlapMass:buttonDetermine_Click", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LogFileManager.AppendLog("showElementToolStripMenuItem_Click", ex.Message);
            }
        }
        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var currentCell = dataGridViewElement.CurrentCellAddress;
                var sendingCB   = sender as DataGridViewComboBoxEditingControl;
                DataGridViewTextBoxCell cell = (DataGridViewTextBoxCell)dataGridViewElement.Rows[currentCell.Y].Cells["MassId"];
                int selectedIndex            = ((System.Windows.Forms.ComboBox)sender).SelectedIndex;

                int selectedMassId = 0;
                if (null != dataGridViewElement.Rows[currentCell.Y].Tag && selectedIndex != -1)
                {
                    if (selectedIndex == 0) //mass name: <None>
                    {
                        cell.Value = selectedMassId;
                    }
                    else if (selectedIndex > 0)
                    {
                        ElementProperties ep = dataGridViewElement.Rows[currentCell.Y].Tag as ElementProperties;
                        selectedMassId = ep.MassContainers.Keys.ToList()[selectedIndex - 1];
                        cell.Value     = selectedMassId;

                        if (checkBoxHost.Checked)
                        {
                            if (hostMaps.ContainsKey(ep.ElementId))
                            {
                                foreach (int elementId in hostMaps[ep.ElementId].Keys)
                                {
                                    int rowIndex = hostMaps[ep.ElementId][elementId];
                                    DataGridViewCell textCell = dataGridViewElement.Rows[rowIndex].Cells["MassId"];
                                    textCell.Value = selectedMassId;
                                    DataGridViewComboBoxCell comboCell = dataGridViewElement.Rows[rowIndex].Cells["ColumnSelection"] as DataGridViewComboBoxCell;
                                    comboCell.Value = massDictionary[selectedMassId].MassName;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to tag massId on the cell.\n" + ex.Message, "Form_OverlapMass:ComboBox_SelectedIndexChanged", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LogFileManager.AppendLog("ComboBox_SelectedIndexChanged", ex.Message);
            }
        }
        private void showElementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UIDocument       uidoc      = m_app.ActiveUIDocument;
                Document         m_doc      = uidoc.Document;
                List <ElementId> elementIds = new List <ElementId>();

#if RELEASE2013 || RELEASE2014
                SelElementSet newSelection = SelElementSet.Create();

                foreach (DataGridViewRow row in dataGridViewElement.SelectedRows)
                {
                    if (null != row.Tag)
                    {
                        ElementProperties ep        = row.Tag as ElementProperties;
                        ElementId         elementId = new ElementId(ep.ElementId);
                        elementIds.Add(elementId);
                        newSelection.Add(ep.ElementObj);
                    }
                }
                uidoc.ShowElements(elementIds);
                uidoc.Selection.Elements = newSelection;
#else
                Selection selection = uidoc.Selection;

                foreach (DataGridViewRow row in dataGridViewElement.SelectedRows)
                {
                    if (null != row.Tag)
                    {
                        ElementProperties ep        = row.Tag as ElementProperties;
                        ElementId         elementId = new ElementId(ep.ElementId);
                        elementIds.Add(elementId);
                    }
                }
                uidoc.ShowElements(elementIds);
                selection.SetElementIds(elementIds);
#endif
            }
            catch (Exception ex)
            {
                Log.AppendLog(LogMessageType.EXCEPTION, ex.Message);
            }
        }
        private void buttonApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckValidation())
                {
                    foreach (DataGridViewRow row in dataGridViewElement.Rows)
                    {
                        if (null != row.Tag)
                        {
                            ElementProperties ep = row.Tag as ElementProperties;
                            intersectingElements.Remove(ep.ElementId);

                            bool selected = Convert.ToBoolean(row.Cells[0].Value);
                            if (selected)
                            {
                                int massId = int.Parse(row.Cells["MassId"].Value.ToString());
                                ep.SelectedMassId = massId;
                                intersectingElements.Add(ep.ElementId, ep);

                                if (massId == 0)
                                {
                                    unassignedElements.Add(ep.ElementId, ep);
                                }
                            }
                        }
                    }
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to assign Mass elements to propagate.\n" + ex.Message, "Form_OverlapMass:buttonApply_Click", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LogFileManager.AppendLog("buttonApply_Click", ex.Message);
            }
        }
Exemple #5
0
        //find elements which intersect more than two mass elements.
        private void FindOverlaps()
        {
            try
            {
                if (elementDictionary.Count > 0)
                {
                    progressForm            = new Form_ProgressBar();
                    progressForm.Text       = "Finding Overlapping Conditions";
                    progressForm.LabelText  = "Finding elements intersecting with more than two mass elements...";
                    progressForm.LabelCount = elementDictionary.Count.ToString() + " elements found";
                    progressForm.MaxValue   = elementDictionary.Count;
                    progressForm.Show();
                    progressFormOpend = true;
                    progressForm.Refresh();
                }

                List <int> elementIds = elementDictionary.Keys.ToList();
                foreach (int elementId in elementIds)
                {
                    progressForm.PerformStep();
                    ElementProperties ep = elementDictionary[elementId];
                    if (ep.MassContainers.Count > 0)
                    {
                        Solid   unionSolid = null;
                        Options opt        = m_app.Application.Create.NewGeometryOptions();
                        opt.ComputeReferences = true;
                        opt.DetailLevel       = Autodesk.Revit.DB.ViewDetailLevel.Fine;

                        GeometryElement geomElement = ep.ElementObj.get_Geometry(new Options(opt));
                        geomElement = geomElement.GetTransformed(ep.TransformValue);

                        foreach (GeometryObject obj in geomElement)
                        {
                            Solid solid = obj as Solid;
                            if (null != solid)
                            {
                                if (solid.Volume > 0)
                                {
                                    if (unionSolid == null)
                                    {
                                        unionSolid = solid;
                                    }
                                    else
                                    {
                                        try { unionSolid = BooleanOperationsUtils.ExecuteBooleanOperation(solid, unionSolid, BooleanOperationsType.Union); }
                                        catch { }
                                    }
                                }
                            }
                        }
                        ep.ElementSolid = unionSolid;

                        Dictionary <int, double> overlapMap = new Dictionary <int, double>();
                        if (unionSolid != null)
                        {
                            if (unionSolid.Volume > 0)
                            {
                                foreach (int massId in ep.MassContainers.Keys)
                                {
                                    Solid massSolid      = ep.MassContainers[massId];
                                    Solid intersectSolid = null;
                                    try { intersectSolid = BooleanOperationsUtils.ExecuteBooleanOperation(massSolid, unionSolid, BooleanOperationsType.Intersect); }
                                    catch { intersectSolid = null; }
                                    if (null != intersectSolid)
                                    {
                                        if (intersectSolid.Volume > 0)
                                        {
                                            double ratio = intersectSolid.Volume / unionSolid.Volume;
                                            if (!overlapMap.ContainsKey(massId))
                                            {
                                                overlapMap.Add(massId, ratio);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        ep.OpverappingMaps = overlapMap;
                        elementDictionary.Remove(ep.ElementId);
                        elementDictionary.Add(ep.ElementId, ep);
                    }
                }

                if (progressFormOpend)
                {
                    progressForm.Close();
                    progressFormOpend = false;
                }
            }
            catch (Exception ex)
            {
                Log.AppendLog(LogMessageType.EXCEPTION, ex.Message);
            }
        }
Exemple #6
0
        private List <Element> FindElementsInLInkedFiles(Element massInstance, GeometryElement geomElement)
        {
            List <Element> foundElements = new List <Element>();

            try
            {
                Options opt = m_app.Application.Create.NewGeometryOptions();
                opt.ComputeReferences = true;
                opt.DetailLevel       = Autodesk.Revit.DB.ViewDetailLevel.Fine;

                FilteredElementCollector collector = new FilteredElementCollector(m_doc);
                collector.OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType();
                List <RevitLinkInstance> revitLinkInstances = collector.ToElements().Cast <RevitLinkInstance>().ToList();

                foreach (RevitLinkInstance linkInstance in revitLinkInstances)
                {
                    ElementId     typeId      = linkInstance.GetTypeId();
                    RevitLinkType linkType    = m_doc.GetElement(typeId) as RevitLinkType;
                    string        linkDocName = linkType.Name;

                    Document linkedDoc = null;
                    foreach (Document document in m_app.Application.Documents)
                    {
                        if (linkDocName.Contains(document.Title))
                        {
                            linkedDoc = document; break;
                        }
                    }

                    Transform transformValue = linkInstance.GetTotalTransform();

                    if (null != linkedDoc && null != transformValue)
                    {
                        GeometryElement transformedElement = geomElement.GetTransformed(transformValue.Inverse);
                        Solid           originalSolid      = GetSolid(geomElement);
                        Solid           transformedSolid   = GetSolid(transformedElement);

                        if (null != transformedSolid)
                        {
                            FilteredElementCollector elementCollector = new FilteredElementCollector(linkedDoc);
                            elementCollector.WherePasses(new ElementIntersectsSolidFilter(transformedSolid)).WhereElementIsNotElementType();
                            List <Element> elementList = elementCollector.ToElements().ToList();

                            foreach (Element element in elementList)
                            {
                                int elementId = element.Id.IntegerValue;
                                if (!elementDictionary.ContainsKey(elementId))
                                {
                                    ElementProperties ep = new ElementProperties(element);
                                    ep.Doc            = linkedDoc;
                                    ep.TransformValue = transformValue;
                                    ep.LinkedElement  = true;

                                    CollectParameterValues(element);

                                    FamilyInstance fi = element as FamilyInstance;
                                    if (null != fi)
                                    {
                                        if (null != fi.Host)
                                        {
                                            ep.HostElementId = fi.Host.Id.IntegerValue;
                                        }
                                    }

                                    Dictionary <int, Solid> massContainers = new Dictionary <int, Solid>();
                                    massContainers.Add(massInstance.Id.IntegerValue, originalSolid); //adjusted to the host coordinate system
                                    ep.MassContainers = massContainers;
                                    elementDictionary.Add(ep.ElementId, ep);

                                    if (!categoryIds.Contains(element.Category.Id.IntegerValue) && null != element.Category.Name)
                                    {
                                        elementCategories.Add(element.Category, false);
                                        categoryIds.Add(element.Category.Id.IntegerValue);
                                    }
                                }
                                else
                                {
                                    ElementProperties ep = elementDictionary[elementId];
                                    if (!ep.MassContainers.ContainsKey(massInstance.Id.IntegerValue))
                                    {
                                        ep.MassContainers.Add(massInstance.Id.IntegerValue, originalSolid);
                                        elementDictionary.Remove(elementId);
                                        elementDictionary.Add(elementId, ep);
                                    }
                                }
                            }
                            foundElements.AddRange(elementList);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to find elements in mass.\n" + ex.Message, "FindElementsInMass", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(foundElements);
        }
        private List <Element> FindElementsInMass(Document doc, Solid massSolid, int massId, bool linkedElement)
        {
            List <Element> elementList = new List <Element>();

            try
            {
                FilteredElementCollector elementCollector = new FilteredElementCollector(doc);
                ElementCategoryFilter    catFilter        = new ElementCategoryFilter(BuiltInCategory.OST_RvtLinks, true);
                elementCollector.WherePasses(catFilter).WherePasses(new ElementIntersectsSolidFilter(massSolid)).WhereElementIsNotElementType();
                elementList = elementCollector.ToElements().ToList();

                foreach (Element element in elementList)
                {
                    int elementId = element.Id.IntegerValue;
                    if (!elementDictionary.ContainsKey(elementId))
                    {
                        ElementProperties ep = new ElementProperties(element);
                        ep.LinkedElement = linkedElement;
                        CollectParameterValues(element);

                        FamilyInstance fi = element as FamilyInstance;
                        if (null != fi)
                        {
                            if (null != fi.Host)
                            {
                                ep.HostElementId = fi.Host.Id.IntegerValue;
                            }
                        }

                        Dictionary <int, Solid> massContainers = new Dictionary <int, Solid>();
                        massContainers.Add(massId, massSolid);
                        ep.MassContainers = massContainers;
                        elementDictionary.Add(ep.ElementId, ep);

                        if (!categoryIds.Contains(element.Category.Id.IntegerValue) && null != element.Category.Name)
                        {
                            elementCategories.Add(element.Category, false);
                            categoryIds.Add(element.Category.Id.IntegerValue);
                        }
                    }
                    else
                    {
                        ElementProperties ep = elementDictionary[elementId];
                        if (!ep.MassContainers.ContainsKey(massId))
                        {
                            ep.MassContainers.Add(massId, massSolid);
                            elementDictionary.Remove(elementId);
                            elementDictionary.Add(elementId, ep);
                        }
                    }
                }

                return(elementList);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to find mass elements in mass.\n" + ex.Message, "LinkedModelManager:FindElementsInMass", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LogFileManager.AppendLog("FindElementsInMass", ex.Message);
                return(elementList);
            }
        }
        private void DisplayIntersectingMasses()
        {
            try
            {
                if (massIds.Count > 1)
                {
                    int colWidth = (massIds.Count - 1) * 120;
                    if (splitOption)
                    {
                        colWidth += 120;
                    }

                    this.Size = new Size(this.Size.Width + colWidth, this.Size.Height);
                }

                if (!splitOption)
                {
                    dataGridViewElement.Columns[0].Visible = false;
                    bttnCheckAll.Enabled  = false;
                    bttnCheckNone.Enabled = false;
                }

                foreach (int massId in massIds)
                {
                    if (massDictionary.ContainsKey(massId))
                    {
                        DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                        column.Name        = massId.ToString();
                        column.HeaderText  = massDictionary[massId].MassName;
                        column.ToolTipText = massId.ToString();
                        column.ReadOnly    = true;
                        column.Width       = 110;

                        dataGridViewElement.Columns.Add(column);
                    }
                }

                DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
                comboColumn.Name         = "ColumnSelection";
                comboColumn.HeaderText   = "Selection";
                comboColumn.Width        = 110;
                comboColumn.FlatStyle    = FlatStyle.Flat;
                comboColumn.AutoComplete = true;
                dataGridViewElement.Columns.Add(comboColumn);

                //hidden massId column to create elementId-massId map
                DataGridViewTextBoxColumn massIdColumn = new DataGridViewTextBoxColumn();
                massIdColumn.Name       = "MassId";
                massIdColumn.HeaderText = "MassId";
                massIdColumn.Visible    = false;
                dataGridViewElement.Columns.Add(massIdColumn);

                int index = 0;
                foreach (int elementId in intersectingElements.Keys)
                {
                    ElementProperties ep = intersectingElements[elementId];
                    index = dataGridViewElement.Rows.Add();
                    dataGridViewElement.Rows[index].Tag            = ep;
                    dataGridViewElement.Rows[index].Cells[0].Value = true;
                    dataGridViewElement.Rows[index].Cells[1].Value = ep.ElementId.ToString();
                    dataGridViewElement.Rows[index].Cells[2].Value = ep.ElementName;
                    dataGridViewElement.Rows[index].Cells[3].Value = ep.CategoryName;

                    List <string> massNames = new List <string>();
                    massNames.Add("<Unassigned>");
                    double maxVal       = 0;
                    string selectedName = "";
                    int    maxId        = 0;
                    foreach (int massId in ep.MassContainers.Keys)
                    {
                        if (massDictionary.ContainsKey(massId))
                        {
                            string massName = massDictionary[massId].MassName;
                            if (ep.OpverappingMaps.Count > 0)
                            {
                                if (ep.OpverappingMaps.ContainsKey(massId))
                                {
                                    double ratio = ep.OpverappingMaps[massId];

                                    dataGridViewElement.Rows[index].Cells[massId.ToString()].Value = Math.Round(ratio, 2);
                                    if (ratio > maxVal)
                                    {
                                        maxVal       = ratio;
                                        selectedName = massName;
                                        maxId        = massId;
                                    }
                                }
                            }
                            massNames.Add(massName);
                        }
                    }

                    DataGridViewComboBoxCell comboCell = dataGridViewElement.Rows[index].Cells["ColumnSelection"] as DataGridViewComboBoxCell;
                    comboCell.DataSource = massNames;
                    if (ep.OpverappingMaps.Count > 0)
                    {
                        comboCell.Value = selectedName;
                    }
                    else
                    {
                        comboCell.Value = massNames[0]; maxId = 0;
                    }

                    dataGridViewElement.Rows[index].Cells["MassId"].Value = maxId;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to display elements and intersecting masses.\n" + ex.Message, "Form_OverlapMass:DisplayIntersectingMasses", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LogFileManager.AppendLog("DisplayIntersectingMasses", ex.Message);
            }
        }