private void mnu_CreateMaterialFromElement_Click(object sender, RoutedEventArgs e)
        {
            List <CarboElement> selectedCarboElementList = new List <CarboElement>();

            selectedCarboElementList = dgv_Elements.SelectedItems.Cast <CarboElement>().ToList();

            if (selectedCarboElementList.Count == 1)
            {
                CarboElement newBufferElement = selectedCarboElementList[0];
                string       materialName     = newBufferElement.MaterialName;
                if (materialName != "")
                {
                    CarboLifeProject.CarboDatabase.AddMaterial(new CarboMaterial(materialName));
                }
            }
            else
            {
                MessageBox.Show("Please select a element from the list");
            }
        }
        private void Mnu_MoveToNewGroup_Click(object sender, RoutedEventArgs e)
        {
            //IList<DataGridCellInfo> selectedElementList = dgv_Elements.SelectedCells;
            try
            {
                List <CarboElement> selectedCarboElementList = new List <CarboElement>();
                selectedCarboElementList = dgv_Elements.SelectedItems.Cast <CarboElement>().ToList();

                if (selectedCarboElementList.Count > 0)
                {
                    CarboGroup selectedCarboGroup = (CarboGroup)dgv_Overview.SelectedItem;

                    int carbogroupId = selectedCarboGroup.Id;
                    List <CarboElement> allCarboElementList = selectedCarboGroup.AllElements;

                    CarboGroup newGroup = new CarboGroup();
                    newGroup.AllElements  = selectedCarboElementList;
                    newGroup.Category     = selectedCarboGroup.Category;
                    newGroup.SubCategory  = selectedCarboGroup.SubCategory;
                    newGroup.Description  = "A new Group";
                    newGroup.Material     = allCarboElementList[0].Material;
                    newGroup.MaterialName = allCarboElementList[0].MaterialName;


                    int delcounter = 0;

                    foreach (CarboElement ce in selectedCarboElementList)
                    {
                        for (int i = 0; i >= 0; i--)
                        {
                            CarboElement oldce = allCarboElementList[i];
                            if (ce.Id == oldce.Id)
                            {
                                allCarboElementList.RemoveAt(i);
                                delcounter++;
                            }
                        }
                    }
                    //Now there should be two lists, one with the selcted items and one without.
                    foreach (CarboGroup cg in CarboLifeProject.getGroupList)
                    {
                        if (cg.Id == carbogroupId)
                        {
                            cg.AllElements = allCarboElementList;
                        }
                    }
                    CarboLifeProject.AddGroup(newGroup);

                    MessageBox.Show(delcounter + " Elements moved to new group", "Warning", MessageBoxButton.OK);

                    CarboLifeProject.CalculateProject();
                    refreshData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Warning", MessageBoxButton.OK);
            }

            //List<CarboElement> selectedElement = dgv_Elements.SelectedCells;
        }
Exemple #3
0
        public static CarboElement getNewCarboElement(Document doc, Element el, ElementId materialIds, CarboRevitImportSettings settings)
        {
            CarboElement newCarboElement = new CarboElement();

            try
            {
                int    setId;
                string setName;
                string setImportedMaterialName;
                string setCategory;
                string setSubCategory;
                double setVolume;
                double setLevel;
                bool   setIsDemolished;
                bool   setIsSubstructure;
                bool   setIsExisting;
                //int layernr;

                // Material material = doc.GetElement(materialIds) as Material;
                //Id:
                setId = el.Id.IntegerValue;

                //Name (Type)
                ElementId   elId = el.GetTypeId();
                ElementType type = doc.GetElement(elId) as ElementType;
                setName = type.Name;

                //MaterialName
                setImportedMaterialName = doc.GetElement(materialIds).Name.ToString();

                //CarboMaterial carboMaterial = new CarboMaterial(setMaterialName);

                //GetDensity
                Parameter paramMaterial = el.get_Parameter(BuiltInParameter.STRUCTURAL_MATERIAL_PARAM);
                if (paramMaterial != null)
                {
                    Material material = doc.GetElement(paramMaterial.AsElementId()) as Material;
                    if (material != null)
                    {
                        PropertySetElement property = doc.GetElement(material.StructuralAssetId) as PropertySetElement;
                        if (property != null)
                        {
                            Parameter paramDensity = property.get_Parameter(BuiltInParameter.PHY_MATERIAL_PARAM_STRUCTURAL_DENSITY);
                            if (paramDensity != null)
                            {
                                double density = paramDensity.AsDouble();
                                newCarboElement.Density = density;
                            }
                        }
                    }
                }


                //Category
                setCategory = getValueFromList(el, type, settings.MainCategory, doc);

                //SubCategory
                setSubCategory = getValueFromList(el, type, settings.SubCategory, doc);

                //Volume

                double volumeCubicFt = el.GetMaterialVolume(materialIds);
                setVolume = Utils.convertToCubicMtrs(volumeCubicFt);

                newCarboElement.isDemolished = false;

                Level lvl = doc.GetElement(el.LevelId) as Level;
                if (lvl != null)
                {
                    setLevel = Convert.ToDouble((lvl.Elevation) * 304.8);
                }
                else
                {
                    setLevel = 0;
                }

                if (setLevel <= settings.CutoffLevelValue)
                {
                    setIsSubstructure = true;
                }
                else
                {
                    setIsSubstructure = false;
                }

                //Get Phasing;
                Phase elCreatedPhase = doc.GetElement(el.CreatedPhaseId) as Phase;
                Phase elDemoPhase    = doc.GetElement(el.DemolishedPhaseId) as Phase;


                if (elDemoPhase != null)
                {
                    setIsDemolished = true;
                }
                else
                {
                    setIsDemolished = false;
                }

                if (elCreatedPhase.Name == "Existing")
                {
                    setIsExisting = true;
                }
                else
                {
                    setIsExisting = false;
                }

                //Makepass;

                //Is existing and retained
                if (setIsExisting == true && setIsDemolished == false)
                {
                    if (settings.IncludeExisting == false)
                    {
                        return(null);
                    }
                }

                //Is demolished
                if (setIsDemolished == true)
                {
                    if (settings.IncludeDemo == false)
                    {
                        return(null);
                    }
                }

                //If it passed it is either proposed, or demolished and retained.

                newCarboElement.Id           = setId;
                newCarboElement.Name         = setName;
                newCarboElement.MaterialName = setImportedMaterialName;
                newCarboElement.Category     = setCategory;
                newCarboElement.SubCategory  = setSubCategory;
                newCarboElement.Volume       = Math.Round(setVolume, 4);
                //newCarboElement.Material = carboMaterial; //Material removed
                newCarboElement.Level          = Math.Round(setLevel, 3);
                newCarboElement.isDemolished   = setIsDemolished;
                newCarboElement.isExisting     = setIsExisting;
                newCarboElement.isSubstructure = setIsSubstructure;

                if (newCarboElement.Volume != 0)
                {
                    return(newCarboElement);
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                //TaskDialog.Show("Error", ex.Message);
                return(null);
            }
        }
Exemple #4
0
        public static void ImportElements(UIApplication app, CarboRevitImportSettings settings, string updatePath)
        {
            UIDocument uidoc = app.ActiveUIDocument;
            Document   doc   = uidoc.Document;

            string MyAssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string MyAssemblyDir  = Path.GetDirectoryName(MyAssemblyPath);
            bool   updateFile     = false;

            if (File.Exists(updatePath))
            {
                updateFile = true;
            }
            //Create a new project
            CarboProject myProject = new CarboProject();

            myProject.Name   = doc.ProjectInformation.Name.Trim();
            myProject.Number = doc.ProjectInformation.Number;

            ICollection <ElementId> selectionList = uidoc.Selection.GetElementIds();

            double area = 0;

            #region buildQuantitiestable

            if (selectionList.Count == 0)
            {
                //No elements are selected, all elements will be parsed
                View activeView = doc.ActiveView;

                FilteredElementCollector coll = new FilteredElementCollector(app.ActiveUIDocument.Document, activeView.Id);

                coll.WherePasses(new LogicalOrFilter(new ElementIsElementTypeFilter(false),
                                                     new ElementIsElementTypeFilter(true)));

                //Now cast them as elements into a container
                IList <Element> collection = coll.ToElements();
                string          name       = "";

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

                foreach (Element el in collection)
                {
                    name = el.Id.ToString();

                    try
                    {
                        if (CarboRevitUtils.isElementReal(el) == true)
                        {
                            ICollection <ElementId> MaterialIds = el.GetMaterialIds(false);

                            foreach (ElementId materialIds in MaterialIds)
                            {
                                CarboElement carboElement = CarboRevitUtils.getNewCarboElement(doc, el, materialIds, settings);

                                if (carboElement != null)
                                {
                                    myProject.AddElement(carboElement);
                                }
                            }
                            //See if is floor(then count area)
                            area += getFloorarea(el);
                        }
                    }
                    catch
                    {
                        IdsNotFound.Add(name);

                        //TaskDialog.Show("Error", ex.Message);
                    }
                }
                if (IdsNotFound.Count > 0)
                {
                    string message = "One or more elements weren't processed, most likely because they didn't contain any volume the element ids of these elements are: ";

                    foreach (string id in IdsNotFound)
                    {
                        message += "\n" + id;
                    }

                    MessageBox.Show(message, "Warning", MessageBoxButton.OK);
                }
            }
            else
            {
                try
                {
                    foreach (ElementId elid in selectionList)
                    {
                        Element el = doc.GetElement(elid);
                        ICollection <ElementId> MaterialIds = el.GetMaterialIds(false);

                        if (CarboRevitUtils.isElementReal(el) == true)
                        {
                            foreach (ElementId materialIds in MaterialIds)
                            {
                                CarboElement carboElement = CarboRevitUtils.getNewCarboElement(doc, el, materialIds, settings);

                                if (carboElement != null)
                                {
                                    myProject.AddElement(carboElement);
                                }
                            }
                            //See if is floor(then count area)
                            area += getFloorarea(el);
                        }
                    }
                }
                catch (Exception ex)
                {
                    TaskDialog.Show("Error", ex.Message);
                }
            }

            #endregion

            //All element have been mapped, here the code will be split between an update or a new one.
            if (myProject.getAllElements.Count > 0)
            {
                CarboProject projectToOpen = new CarboProject();

                if (updateFile == false)
                {
                    //Create groups from all the individual elements
                    myProject.CreateGroups();
                    projectToOpen = myProject;
                }
                else //upadte an existing file:
                {
                    CarboProject projectToUpdate = new CarboProject();

                    CarboProject buffer = new CarboProject();
                    projectToUpdate = buffer.DeSerializeXML(updatePath);

                    projectToUpdate.Audit();
                    projectToUpdate.UpdateProject(myProject);
                    projectToUpdate.CalculateProject();

                    projectToOpen = projectToUpdate;
                }

                double areaMSqr = Math.Round((area * (0.3048 * 0.3048)), 2);
                projectToOpen.Area = areaMSqr;

                CarboLifeMainWindow carboCalcProgram = new CarboLifeMainWindow(projectToOpen);
                carboCalcProgram.IsRevit = true;

                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

                carboCalcProgram.ShowDialog();
                //Create a visual
                if (carboCalcProgram.createHeatmap == true)
                {
                    CreateHeatMap(app, carboCalcProgram.carboLifeProject);
                }
            }
            else
            {
                MessageBox.Show("No elements could be found to be calculated, please make sure you have a 3D view active and the building volume is clearly visible ", "Warning", MessageBoxButton.OK);
            }
            //When assembly cant be find bind to current
            System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                System.Reflection.Assembly ayResult = null;
                string sShortAssemblyName           = args.Name.Split(',')[0];

                System.Reflection.Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                foreach (System.Reflection.Assembly ayAssembly in ayAssemblies)
                {
                    if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0])
                    {
                        ayResult = ayAssembly;
                        break;
                    }
                }
                return(ayResult);
            }
        }
Exemple #5
0
        private void Mnu_MoveToNewGroup_Click(object sender, RoutedEventArgs e)
        {
            //IList<DataGridCellInfo> selectedElementList = dgv_Elements.SelectedCells;
            try
            {
                List <CarboElement> selectedCarboElementList = new List <CarboElement>();
                selectedCarboElementList = dgv_Elements.SelectedItems.Cast <CarboElement>().ToList();

                if (selectedCarboElementList.Count > 0)
                {
                    CarboGroup selectedCarboGroup = (CarboGroup)dgv_Overview.SelectedItem;

                    //Reset all findme flags.
                    CarboLifeProject.ResetElementFlags();

                    //Flag the elements that require updating
                    foreach (CarboElement ce in selectedCarboElementList)
                    {
                        ce.isUpdated = true;
                    }

                    int carbogroupId = selectedCarboGroup.Id;
                    List <CarboElement> allCarboElementList = selectedCarboGroup.AllElements;
                    CarboGroup          newGroup            = selectedCarboGroup.Copy();

                    //move all elements to the new group
                    newGroup.AllElements = selectedCarboElementList;
                    newGroup.Description = "A new Group";

                    int delcounter = 0;

                    //remove the old ones from the list
                    foreach (CarboElement ce in selectedCarboElementList)
                    {
                        for (int i = 0; i >= 0; i--)
                        {
                            CarboElement oldce = allCarboElementList[i];
                            if (oldce.isUpdated == true)
                            {
                                allCarboElementList.RemoveAt(i);
                                delcounter++;
                            }
                        }
                    }
                    //Now there should be two lists, one with the selcted items and one without.
                    foreach (CarboGroup cg in CarboLifeProject.getGroupList)
                    {
                        if (cg.Id == carbogroupId)
                        {
                            cg.AllElements = allCarboElementList;
                        }
                    }
                    CarboLifeProject.AddGroup(newGroup);

                    MessageBox.Show(delcounter + " Elements moved to new group", "Message", MessageBoxButton.OK);

                    CarboLifeProject.CalculateProject();
                    refreshData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Warning", MessageBoxButton.OK);
            }

            //List<CarboElement> selectedElement = dgv_Elements.SelectedCells;
        }
        public static void ImportElements(UIApplication app, CarboRevitImportSettings settings)
        {
            UIDocument uidoc = app.ActiveUIDocument;
            Document   doc   = uidoc.Document;

            string MyAssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string MyAssemblyDir  = Path.GetDirectoryName(MyAssemblyPath);

            CarboProject myProject = new CarboProject();

            myProject.Name   = doc.ProjectInformation.Name.Trim();
            myProject.Number = doc.ProjectInformation.Number;

            ICollection <ElementId> selectionList = uidoc.Selection.GetElementIds();

            double area = 0;

            #region buildQuantitiestable

            if (selectionList.Count == 0)
            {
                //No elements are selected:
                View activeView = doc.ActiveView;

                FilteredElementCollector coll = new FilteredElementCollector(app.ActiveUIDocument.Document, activeView.Id);

                coll.WherePasses(new LogicalOrFilter(new ElementIsElementTypeFilter(false),
                                                     new ElementIsElementTypeFilter(true)));

                //Now cast them as elements into a container
                IList <Element> collection = coll.ToElements();

                try
                {
                    foreach (Element el in collection)
                    {
                        if (CarboRevitUtils.isElementReal(el) == true)
                        {
                            ICollection <ElementId> MaterialIds = el.GetMaterialIds(false);
                            foreach (ElementId materialIds in MaterialIds)
                            {
                                CarboElement carboElement = CarboRevitUtils.getNewCarboElement(doc, el, materialIds, settings);

                                if (carboElement != null)
                                {
                                    myProject.AddElement(carboElement);
                                }
                            }
                        }

                        //See if is floor(then count area)
                        area += getFloorarea(el);
                    }
                }
                catch (Exception ex)
                {
                    TaskDialog.Show("Error", ex.Message);
                }
            }
            else
            {
                try
                {
                    foreach (ElementId elid in selectionList)
                    {
                        Element el = doc.GetElement(elid);
                        ICollection <ElementId> MaterialIds = el.GetMaterialIds(false);

                        if (CarboRevitUtils.isElementReal(el) == true)
                        {
                            foreach (ElementId materialIds in MaterialIds)
                            {
                                CarboElement carboElement = CarboRevitUtils.getNewCarboElement(doc, el, materialIds, settings);
                            }

                            //See if is floor(then count area)
                            area += getFloorarea(el);
                        }
                    }
                }
                catch (Exception ex)
                {
                    TaskDialog.Show("Error", ex.Message);
                }
            }

            #endregion

            if (myProject.getAllElements.Count > 0)
            {
                myProject.CreateGroups();

                double areaMSqr = Math.Round((area * (0.3048 * 0.3048)), 2);
                myProject.Area = areaMSqr;

                CarboLifeMainWindow carboCalcProgram = new CarboLifeMainWindow(myProject);
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

                carboCalcProgram.ShowDialog();
            }

            //When assembly cant be find bind to current
            System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                System.Reflection.Assembly ayResult = null;
                string sShortAssemblyName           = args.Name.Split(',')[0];

                System.Reflection.Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                foreach (System.Reflection.Assembly ayAssembly in ayAssemblies)
                {
                    if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0])
                    {
                        ayResult = ayAssembly;
                        break;
                    }
                }
                return(ayResult);
            }
        }