private void runComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     try
     {
         Runs r = e.AddedItems[0] as Runs;
         selectedRun = r;
         exampleLabel.Content = r.FamilyType;
         qtyLabel.Content = r.ElementIds.Count.ToString();
     }
     catch { }
 }
Exemple #2
0
        bool ILyrebirdService.CreateOrModify(List<RevitObject> incomingObjs, Guid uniqueId, string nickName)
        {
            lock (_locker)
            {
                TaskContainer.Instance.EnqueueTask(uiApp =>
                {
                    try
                    {
                        // Set the DisplayUnitTypes
                        Units units = uiApp.ActiveUIDocument.Document.GetUnits();
                        FormatOptions fo = units.GetFormatOptions(UnitType.UT_Length);
                        lengthDUT = fo.DisplayUnits;
                        fo = units.GetFormatOptions(UnitType.UT_Area);
                        areaDUT = fo.DisplayUnits;
                        fo = units.GetFormatOptions(UnitType.UT_Volume);
                        volumeDUT = fo.DisplayUnits;

                        // Find existing elements
                        List<ElementId> existing = FindExisting(uiApp.ActiveUIDocument.Document, uniqueId, incomingObjs[0].CategoryId, -1);
                        
                        // find if there's more than one run existing
                        Schema instanceSchema = Schema.Lookup(instanceSchemaGUID);
                        List<int> runIds = new List<int>();
                        List<Runs> allRuns = new List<Runs>();
                        if (instanceSchema != null)
                        {
                            foreach (ElementId eid in existing)
                            {
                                Element e = uiApp.ActiveUIDocument.Document.GetElement(eid);
                                // Find the run ID
                                Entity entity = e.GetEntity(instanceSchema);
                                if (entity.IsValid())
                                {
                                    Field f = instanceSchema.GetField("RunID");
                                    int tempId = entity.Get<int>(f);
                                    if (!runIds.Contains(tempId))
                                    {
                                        runIds.Add(tempId);
                                        string familyName = string.Empty;
                                        if (e.Category.Id.IntegerValue == -2000011)
                                        {
                                            Wall w = e as Wall;
                                            familyName = w.Category.Name + " : " + w.WallType.Name;
                                        }
                                        else if (e.Category.Id.IntegerValue == -2000032)
                                        {
                                            Floor flr = e as Floor;
                                            familyName = flr.Category.Name + " : " + flr.FloorType.Name;
                                        }
                                        else if (e.Category.Id.IntegerValue == -2000035)
                                        {
                                            RoofBase r = e as RoofBase;
                                            familyName = r.Category.Name + " : " + r.RoofType.Name;
                                        }
                                        else if (e.Category.Id.IntegerValue == -2000240)
                                        {
                                            Level lvl = e as Level;
                                            familyName = lvl.Category.Name + " : " + lvl.LevelType.Name;
                                        }
                                        else if (e.Category.Id.IntegerValue == -2000220)
                                        {
                                            Grid g = e as Grid;
                                            familyName = g.Category.Name + " : " + g.GridType.Name;
                                        }
                                        else
                                        {
                                            FamilyInstance famInst = e as FamilyInstance;
                                            familyName = famInst.Symbol.Family.Name + " : " + famInst.Symbol.Name;
                                        }
                                        Runs run = new Runs(tempId, "Run" + tempId.ToString(), familyName);
                                        allRuns.Add(run);
                                    }
                                }
                            }
                        }


                        if (runIds != null && runIds.Count > 0)
                        {
                            runIds.Sort((x, y) => x.CompareTo(y));
                            int lastId = 0;
                            lastId = runIds.Last();
                            ModifyForm mform = new ModifyForm(this, allRuns);
                            mform.ShowDialog();

                            // Get the set of existing elements to reflect the run choice.
                            List<ElementId> existingRunEID = FindExisting(uiApp.ActiveUIDocument.Document, uniqueId, incomingObjs[0].CategoryId, runId);

                            // modBehavior = 0, Modify the selected run
                            if (modBehavior == 0)
                            {
                                if (existingRunEID.Count == incomingObjs.Count)
                                {
                                    // just modify
                                    ModifyObjects(incomingObjs, existingRunEID, uiApp.ActiveUIDocument.Document, uniqueId, true, nickName, runId);
                                }
                                else if (existingRunEID.Count > incomingObjs.Count)
                                {
                                    // Modify and Delete
                                    List<ElementId> modObjects = new List<ElementId>();
                                    List<ElementId> removeObjects = new List<ElementId>();

                                    int i = 0;
                                    while (i < incomingObjs.Count)
                                    {
                                        modObjects.Add(existingRunEID[i]);
                                        i++;
                                    }
                                    while (existingRunEID != null && i < existingRunEID.Count)
                                    {
                                        Element e = uiApp.ActiveUIDocument.Document.GetElement(existing[i]);
                                        // Find the run ID
                                        Entity entity = e.GetEntity(instanceSchema);
                                        if (entity.IsValid())
                                        {
                                            removeObjects.Add(existingRunEID[i]);
                                            //Field f = instanceSchema.GetField("RunID");
                                            //int tempId = entity.Get<int>(f);
                                            //if (tempId == runId)
                                            //{
                                            //    removeObjects.Add(existing[i]);
                                            //}
                                        }
                                        i++;
                                    }
                                    try
                                    {
                                        ModifyObjects(incomingObjs, modObjects, uiApp.ActiveUIDocument.Document, uniqueId, true, nickName, runId);
                                        DeleteExisting(uiApp.ActiveUIDocument.Document, removeObjects);
                                    }
                                    catch (Exception ex)
                                    {
                                        Debug.WriteLine(ex.Message);
                                    }
                                }
                                else if (existingRunEID.Count < incomingObjs.Count)
                                {
                                    // modify and create
                                    // create and modify
                                    List<RevitObject> existingObjects = new List<RevitObject>();
                                    List<RevitObject> newObjects = new List<RevitObject>();

                                    int i = 0;
                                    Debug.Assert(existing != null, "existing != null");
                                    while (i < existingRunEID.Count)
                                    {
                                        existingObjects.Add(incomingObjs[i]);
                                        i++;
                                    }
                                    while (i < incomingObjs.Count)
                                    {
                                        newObjects.Add(incomingObjs[i]);
                                        i++;
                                    }
                                    try
                                    {
                                        ModifyObjects(existingObjects, existingRunEID, uiApp.ActiveUIDocument.Document, uniqueId, true, nickName, runId);
                                        CreateObjects(newObjects, uiApp.ActiveUIDocument.Document, uniqueId, runId, nickName);
                                    }
                                    catch (Exception ex)
                                    {
                                        Debug.WriteLine(ex.Message);
                                    }
                                }
                            }
                            // modBehavior = 1, Create a new run
                            else if (modBehavior == 1)
                            {
                                // Just send everything to create a new Run.
                                try
                                {
                                    CreateObjects(incomingObjs, uiApp.ActiveUIDocument.Document, uniqueId, lastId + 1, nickName);
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine("Error", ex.Message);
                                }
                            }

                            // modBehavior = 3, cancel/ignore
                        }
                        else
                        {
                            TaskDialog dlg = new TaskDialog("Warning") { MainInstruction = "Incoming Data" };
                            RevitObject existingObj1 = incomingObjs[0];
                            bool profileWarning1 = (existingObj1.CategoryId == -2000011 && existingObj1.Curves.Count > 1) || existingObj1.CategoryId == -2000032 || existingObj1.CategoryId == -2000035;
                            if (existing == null || existing.Count == 0)
                            {
                                dlg.MainContent = "Data is being sent to Revit from another application using Lyrebird." +
                                    " This data will be used to create " + incomingObjs.Count.ToString(CultureInfo.InvariantCulture) + " elements.  How would you like to proceed?";
                                dlg.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Create new elements");
                                dlg.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Cancel");
                            }

                            TaskDialogResult result1 = dlg.Show();
                            if (result1 == TaskDialogResult.CommandLink1)
                            {
                                // Create new
                                try
                                {
                                    CreateObjects(incomingObjs, uiApp.ActiveUIDocument.Document, uniqueId, 0, nickName);
                                }
                                catch (Exception ex)
                                {
                                    TaskDialog.Show("Error", ex.Message);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        TaskDialog.Show("Test", ex.Message);
                        Debug.WriteLine(ex.Message);
                    }
                    finally
                    {
                        Monitor.Pulse(_locker);
                    }
                });
                Monitor.Wait(_locker, Properties.Settings.Default.infoTimeout);
            }
            return true;
        }
        private List<RunCollection> FindExisting()
        {
            // Find existing elements with a matching GUID from the GH component.
            List<RunCollection> collectedRuns = new List<RunCollection>();

            Schema instanceSchema = Schema.Lookup(instanceSchemaGUID);
            if (instanceSchema == null)
            {
                return collectedRuns;
            }

            ElementIsElementTypeFilter filter = new ElementIsElementTypeFilter(false);

            FilteredElementCollector fiCollector = new FilteredElementCollector(doc);
            fiCollector.OfClass(typeof(FamilyInstance));
            fiCollector.ToElements();

            FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
            wallCollector.OfCategory(BuiltInCategory.OST_Walls);
            wallCollector.OfClass(typeof(Wall));
            wallCollector.ToElements();

            FilteredElementCollector floorCollector = new FilteredElementCollector(doc);
            floorCollector.OfCategory(BuiltInCategory.OST_Floors);
            floorCollector.OfClass(typeof(Floor));
            floorCollector.ToElements();

            FilteredElementCollector roofCollector = new FilteredElementCollector(doc);
            roofCollector.OfCategory(BuiltInCategory.OST_Roofs);
            roofCollector.OfClass(typeof(RoofBase));
            roofCollector.ToElements();

            List<Element> elemCollector = new List<Element>();
            foreach (Element e in fiCollector)
            {
                elemCollector.Add(e);
            }
            foreach (Element e in wallCollector)
            {
                elemCollector.Add(e);
            }
            foreach (Element e in floorCollector)
            {
                elemCollector.Add(e);
            }
            foreach (Element e in roofCollector)
            {
                elemCollector.Add(e);
            }

            //FilteredElementCollector elemCollector = new FilteredElementCollector(doc);
            //elemCollector.WherePasses(filter).ToElements();
            
            // First, find all of the unique componentGUID's that are in the Revit file.
            List<string> instanceIds = new List<string>();
            foreach (Element e in elemCollector)
            {
                if (e.Category.Id.IntegerValue == -2000011)
                {
                    try
                    {
                        Wall w = e as Wall;
                        if (w != null)
                        {
                            Entity entity = w.GetEntity(instanceSchema);
                            if (entity.IsValid())
                            {
                                Field f = instanceSchema.GetField("InstanceID");
                                string tempId = entity.Get<string>(f);
                                if (!instanceIds.Contains(tempId))
                                {
                                    instanceIds.Add(tempId);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error", ex.Message);
                    }
                }
                else if (e.Category.Id.IntegerValue == -2000032)
                {
                    try
                    {
                        Floor flr = e as Floor;
                        if (flr != null)
                        {
                            Entity entity = flr.GetEntity(instanceSchema);
                            if (entity.IsValid())
                            {
                                Field f = instanceSchema.GetField("InstanceID");
                                string tempId = entity.Get<string>(f);
                                if (!instanceIds.Contains(tempId))
                                {
                                    instanceIds.Add(tempId);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error", ex.Message);
                    }
                }
                else if (e.Category.Id.IntegerValue == -2000035)
                {
                    try
                    {
                        RoofBase r = e as RoofBase;
                        if (r != null)
                        {
                            Entity entity = r.GetEntity(instanceSchema);
                            if (entity.IsValid())
                            {
                                Field f = instanceSchema.GetField("InstanceID");
                                string tempId = entity.Get<string>(f);
                                if (!instanceIds.Contains(tempId))
                                {
                                    instanceIds.Add(tempId);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error", ex.Message);
                    }
                }
                else
                {
                    try
                    {
                        FamilyInstance fi = e as FamilyInstance;
                        if (fi != null)
                        {
                            Entity entity = fi.GetEntity(instanceSchema);
                            if (entity.IsValid())
                            {
                                Field f = instanceSchema.GetField("InstanceID");
                                string tempId = entity.Get<string>(f);
                                if (!instanceIds.Contains(tempId))
                                {
                                    instanceIds.Add(tempId);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error", ex.Message);
                    }
                }
            }
            
            // Create a runCollection for each guid
            foreach (string id in instanceIds)
            {
                RunCollection rc = new RunCollection();
                List<Runs> tempRuns = new List<Runs>();
                
                // Find the number of runs per instanceId
                List<int> runIds = new List<int>();
                foreach (Element e in elemCollector)
                {
                    if (e.Category.Id.IntegerValue == -2000011)
                    {
                        // Walls
                        try
                        {
                            Wall w = e as Wall;
                            if (w != null)
                            {
                                Entity entity = w.GetEntity(instanceSchema);
                                if (entity.IsValid())
                                {
                                    Field f = instanceSchema.GetField("InstanceID");
                                    string tempId = entity.Get<string>(f);
                                    if (tempId == id)
                                    {
                                        f = instanceSchema.GetField("NickName");
                                        string nickName = entity.Get<string>(f);
                                        rc.ComponentGuid = new Guid(tempId);
                                        rc.NickName = nickName;
                                        f = instanceSchema.GetField("RunID");
                                        int tempRunId = entity.Get<int>(f);

                                        if (!runIds.Contains(tempRunId))
                                        {
                                            runIds.Add(tempRunId);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Error", ex.Message);
                        }
                    }
                    else if (e.Category.Id.IntegerValue == -2000032)
                    {
                        // Floors
                        try
                        {
                            Floor flr = e as Floor;
                            if (flr != null)
                            {
                                Entity entity = flr.GetEntity(instanceSchema);
                                if (entity.IsValid())
                                {
                                    Field f = instanceSchema.GetField("InstanceID");
                                    string tempId = entity.Get<string>(f);
                                    if (tempId == id)
                                    {
                                        f = instanceSchema.GetField("NickName");
                                        string nickName = entity.Get<string>(f);
                                        rc.ComponentGuid = new Guid(tempId);
                                        rc.NickName = nickName;
                                        f = instanceSchema.GetField("RunID");
                                        int tempRunId = entity.Get<int>(f);

                                        if (!runIds.Contains(tempRunId))
                                        {
                                            runIds.Add(tempRunId);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Error", ex.Message);
                        }
                    }
                    else if (e.Category.Id.IntegerValue == -2000035)
                    {
                        // Roofs
                        try
                        {
                            RoofBase r = e as RoofBase;
                            if (r != null)
                            {
                                Entity entity = r.GetEntity(instanceSchema);
                                if (entity.IsValid())
                                {
                                    Field f = instanceSchema.GetField("InstanceID");
                                    string tempId = entity.Get<string>(f);
                                    if (tempId == id)
                                    {
                                        f = instanceSchema.GetField("NickName");
                                        string nickName = entity.Get<string>(f);
                                        rc.ComponentGuid = new Guid(tempId);
                                        rc.NickName = nickName;
                                        f = instanceSchema.GetField("RunID");
                                        int tempRunId = entity.Get<int>(f);

                                        if (!runIds.Contains(tempRunId))
                                        {
                                            runIds.Add(tempRunId);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Error", ex.Message);
                        }
                    }
                    else
                    {
                        // Other non-system families
                        try
                        {
                            FamilyInstance fi = e as FamilyInstance;
                            if (fi != null)
                            {
                                Entity entity = fi.GetEntity(instanceSchema);
                                if (entity.IsValid())
                                {
                                    Field f = instanceSchema.GetField("InstanceID");
                                    string tempId = entity.Get<string>(f);
                                    if (tempId == id)
                                    {
                                        f = instanceSchema.GetField("NickName");
                                        string nickName = entity.Get<string>(f);
                                        rc.ComponentGuid = new Guid(tempId);
                                        rc.NickName = nickName;
                                        f = instanceSchema.GetField("RunID");
                                        int tempRunId = entity.Get<int>(f);

                                        if (!runIds.Contains(tempRunId))
                                        {
                                            runIds.Add(tempRunId);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Error", ex.Message);
                        }
                    }
                }
                
                foreach (int i in runIds)
                {
                    List<int> runElemIds = new List<int>();
                    Runs run = new Runs();
                    foreach (Element e in elemCollector)
                    {
                        try
                        {
                            if (e.Category.Id.IntegerValue == -2000011)
                            {
                                // Walls
                                try
                                {
                                    Wall w = e as Wall;
                                    if (w != null)
                                    {
                                        Entity entity = w.GetEntity(instanceSchema);
                                        if (entity.IsValid())
                                        {
                                            Field f = instanceSchema.GetField("InstanceID");
                                            string tempId = entity.Get<string>(f);
                                            if (tempId == id)
                                            {
                                                f = instanceSchema.GetField("RunID");
                                                int tempRunId = entity.Get<int>(f);

                                                if (tempRunId == i)
                                                {
                                                    if (run.RunName == null || run.RunName == string.Empty)
                                                    {
                                                        run.RunId = tempRunId;
                                                        run.RunName = "Run" + tempRunId.ToString();
                                                        run.FamilyType = w.Category.Name + " : " + w.WallType.Name;
                                                    }
                                                    runElemIds.Add(w.Id.IntegerValue);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine("Error", ex.Message);
                                }
                            }
                            else if (e.Category.Id.IntegerValue == -2000032)
                            {
                                // Floors
                                try
                                {
                                    Floor flr = e as Floor;
                                    if (flr != null)
                                    {
                                        Entity entity = flr.GetEntity(instanceSchema);
                                        if (entity.IsValid())
                                        {
                                            Field f = instanceSchema.GetField("InstanceID");
                                            string tempId = entity.Get<string>(f);
                                            if (tempId == id)
                                            {
                                                f = instanceSchema.GetField("RunID");
                                                int tempRunId = entity.Get<int>(f);

                                                if (tempRunId == i)
                                                {
                                                    if (run.RunName == null || run.RunName == string.Empty)
                                                    {
                                                        run.RunId = tempRunId;
                                                        run.RunName = "Run" + tempRunId.ToString();
                                                        run.FamilyType = flr.Category.Name + " : " + flr.FloorType.Name;
                                                    }
                                                    runElemIds.Add(flr.Id.IntegerValue);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine("Error", ex.Message);
                                }
                            }
                            else if (e.Category.Id.IntegerValue == -2000035)
                            {
                                // Roofs
                                try
                                {
                                    RoofBase r = e as RoofBase;
                                    if (r != null)
                                    {
                                        Entity entity = r.GetEntity(instanceSchema);
                                        if (entity.IsValid())
                                        {
                                            Field f = instanceSchema.GetField("InstanceID");
                                            string tempId = entity.Get<string>(f);
                                            if (tempId == id)
                                            {
                                                f = instanceSchema.GetField("RunID");
                                                int tempRunId = entity.Get<int>(f);

                                                if (tempRunId == i)
                                                {
                                                    if (run.RunName == null || run.RunName == string.Empty)
                                                    {
                                                        run.RunId = tempRunId;
                                                        run.RunName = "Run" + tempRunId.ToString();
                                                        run.FamilyType = r.Category.Name + " : " + r.RoofType.Name;
                                                    }
                                                    runElemIds.Add(r.Id.IntegerValue);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine("Error", ex.Message);
                                }
                            }
                            else
                            {
                                // Other non-system families
                                try
                                {
                                    FamilyInstance fi = e as FamilyInstance;
                                    if (fi != null)
                                    {
                                        Entity entity = fi.GetEntity(instanceSchema);
                                        if (entity.IsValid())
                                        {
                                            Field f = instanceSchema.GetField("InstanceID");
                                            string tempId = entity.Get<string>(f);
                                            if (tempId == id)
                                            {
                                                f = instanceSchema.GetField("RunID");
                                                int tempRunId = entity.Get<int>(f);

                                                if (tempRunId == i)
                                                {
                                                    if (run.RunName == null || run.RunName == string.Empty)
                                                    {
                                                        run.RunId = tempRunId;
                                                        run.RunName = "Run" + tempRunId.ToString();
                                                        run.FamilyType = fi.Symbol.Family.Name + " : " + fi.Symbol.Name;
                                                    }
                                                    runElemIds.Add(fi.Id.IntegerValue);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine("Error", ex.Message);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Error", ex.Message);
                        }
                    }
                    run.ElementIds = runElemIds;
                    tempRuns.Add(run);
                }
                rc.Runs = tempRuns;
                collectedRuns.Add(rc);
            }
            return collectedRuns;
        }