public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { var uiApp = commandData.Application; var app = commandData.Application.Application; var uiDoc = commandData.Application.ActiveUIDocument; var doc = commandData.Application.ActiveUIDocument.Document; var elementIds = uiDoc.Selection.PickObjects(ObjectType.Element, new VLClassFilter(typeof(Pipe)), "选择要添加的构件").Select(c => c.ElementId); if (elementIds.Count() == 0) { return(Result.Cancelled); } List <Element> pickedElements = elementIds.Select(c => doc.GetElement(c)).ToList(); foreach (var pickedElement in pickedElements) { ElementIntersectsElementFilter filter = new ElementIntersectsElementFilter(pickedElement); var conflict = pickedElements.Where(c => filter.PassesFilter(c)).ToList(); var conflictIds = conflict.Select(c => c.Id); var conflictIds2 = string.Join(",", conflictIds); } return(Result.Succeeded); }
internal void SetConflictElements(List <AvoidElement> elementsToAvoid, List <ValuedConflictNode> conflictNodes) { ConflictElements = new List <ConflictElement>(); ElementIntersectsElementFilter filter = new ElementIntersectsElementFilter(MEPCurve); var elementsConflicted = elementsToAvoid.Where(c => filter.PassesFilter(c.MEPCurve)).ToList(); foreach (var elementConflicted in elementsConflicted) { var conflictLocation = GetConflictPoint(elementConflicted.MEPCurve); if (conflictLocation != null) { var conflictElement = new ConflictElement(this, conflictLocation, elementConflicted); ConflictElements.Add(conflictElement); if (conflictNodes.FirstOrDefault(c => c.ConflictLocation.VL_XYEqualTo(conflictLocation) && (c.ValueNode1.OrientAvoidElement == this || c.ValueNode2.OrientAvoidElement == this)) == null) { conflictNodes.Add(new ValuedConflictNode(this, conflictLocation, elementConflicted)); } } } SortConflictElements(); ////TEST //var conflictIds = string.Join(",", elementsConflicted.Select(c => c.MEPElement.Id)); }
private void JoinOrUnJoinElements(bool join) { if (uidoc != null) { if (joinSelectUI.currentFirstElementList != null && joinSelectUI.currentSecondElementList != null) { bool secondElementsWasEmpty = false; // if we the second elements were not selected, use the first list if (joinSelectUI.currentFirstElementList.Count == 0) { return; } if (joinSelectUI.currentSecondElementList.Count == 0) { secondElementsWasEmpty = true; joinSelectUI.currentSecondElementList = joinSelectUI.currentFirstElementList.ToList(); } string transactionName = join ? Properties.Messages.ElementJoinSelect_JoinTransaction : Properties.Messages.ElementJoinSelect_UnJoinTransaction; using (Transaction t = new Transaction(uidoc.Document, transactionName)) { t.Start(); FailureHandlingOptions joinFailOp = t.GetFailureHandlingOptions(); joinFailOp.SetFailuresPreprocessor(new JoinFailurAdvHandler()); t.SetFailureHandlingOptions(joinFailOp); foreach (Element currentElementToJoin in joinSelectUI.currentFirstElementList) { BoundingBoxXYZ bBox = currentElementToJoin.get_BoundingBox(null); bBox.Enabled = true; Outline outLine = new Outline(bBox.Min, bBox.Max); BoundingBoxIntersectsFilter bbIntersects = new BoundingBoxIntersectsFilter(outLine, Utils.ConvertM.cmToFeet(3)); ElementIntersectsElementFilter intersectFilter = new ElementIntersectsElementFilter(currentElementToJoin); foreach (Element currentElementToBeJoined in joinSelectUI.currentSecondElementList) { if (join) { if (currentElementToJoin.Id == currentElementToBeJoined.Id) { continue; } if (!bbIntersects.PassesFilter(currentElementToBeJoined)) { continue; } if (currentElementToJoin.Category.Id != currentElementToBeJoined.Category.Id) { if (!intersectFilter.PassesFilter(currentElementToBeJoined)) { continue; } } if (!JoinGeometryUtils.AreElementsJoined(uidoc.Document, currentElementToJoin, currentElementToBeJoined)) { try { JoinGeometryUtils.JoinGeometry(uidoc.Document, currentElementToJoin, currentElementToBeJoined); } catch (Exception e) { System.Diagnostics.Debug.Print(e.Message); } } } else { if (JoinGeometryUtils.AreElementsJoined(uidoc.Document, currentElementToJoin, currentElementToBeJoined)) { try { JoinGeometryUtils.UnjoinGeometry(uidoc.Document, currentElementToJoin, currentElementToBeJoined); } catch (Exception e) { System.Diagnostics.Debug.Print(e.Message); } } } } } if (secondElementsWasEmpty) { joinSelectUI.currentSecondElementList = new List <Element>(); } t.Commit(); } } } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Selection sel = uidoc.Selection; try { //IList<Level> projectLevelList = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).OfClass(typeof(Level)) // .Cast<Level>().OrderBy(l => l.Elevation).ToList(); IList <LevelInfo> projectLevelList = Utils.GetInformation.GetAllLevelsInfo(doc); ElementsJoinUIAdvanced currentUI = new ElementsJoinUIAdvanced(projectLevelList); currentUI.ShowDialog(); if (currentUI.DialogResult == false) { return(Result.Cancelled); } string firstCatName = (currentUI.comboFirstCategory.SelectedItem as ComboBoxItem).Content.ToString(); string seconCatName = (currentUI.comboSecondCategory.SelectedItem as ComboBoxItem).Content.ToString(); BuiltInCategory firstCat = checkCategory(currentUI.comboFirstCategory.SelectedIndex); BuiltInCategory secondCat = checkCategory(currentUI.comboSecondCategory.SelectedIndex); if (firstCat == BuiltInCategory.INVALID || secondCat == BuiltInCategory.INVALID) { return(Result.Cancelled); } Level lowerLevel = null; Level upperLevel = null; Element lowerLevelElement = null; Element upperLevelElement = null; if (currentUI.selectedLowerLevel != 0 && currentUI.selectedLowerLevel != -1) { lowerLevelElement = doc.GetElement(new ElementId(currentUI.selectedLowerLevel)); } if (currentUI.selectedUpperLevel != 0 && currentUI.selectedUpperLevel != -1) { upperLevelElement = doc.GetElement(new ElementId(currentUI.selectedUpperLevel)); } if (lowerLevelElement != null) { lowerLevel = lowerLevelElement as Level; } if (upperLevelElement != null) { upperLevel = GetNextLevel(upperLevelElement as Level); } IList <Element> ElementsToJoin = GetElementsOnLevels(doc, firstCat, lowerLevel, upperLevel); IList <ElementId> elementsIdsToJoin = new List <ElementId>(); foreach (Element currentElement in ElementsToJoin) { elementsIdsToJoin.Add(currentElement.Id); } IList <Element> elementsIntersecting = new List <Element>(); using (Transaction t = new Transaction(doc, Properties.Messages.ElementJoin_JoinTransaction)) { t.Start(); FailureHandlingOptions joinFailOp = t.GetFailureHandlingOptions(); joinFailOp.SetFailuresPreprocessor(new JoinFailurAdvHandler()); t.SetFailureHandlingOptions(joinFailOp); IList <Element> elementsIToBeJoin = GetElementsOnLevels(doc, secondCat, lowerLevel, upperLevel); IList <ElementId> elementsIdsToBeJoin = new List <ElementId>(); foreach (Element currentElement in elementsIToBeJoin) { elementsIdsToBeJoin.Add(currentElement.Id); } foreach (Element currentElementToJoin in ElementsToJoin) { BoundingBoxXYZ bBox = currentElementToJoin.get_BoundingBox(null); bBox.Enabled = true; Outline outlineToJoin = new Outline(bBox.Min, bBox.Max); BoundingBoxIntersectsFilter bbIntersects = new BoundingBoxIntersectsFilter(outlineToJoin, Utils.ConvertM.cmToFeet(0.5)); elementsIntersecting = new FilteredElementCollector(doc, elementsIdsToBeJoin).OfCategory(secondCat).WherePasses(bbIntersects).ToElements(); ElementIntersectsElementFilter intersectFilter = new ElementIntersectsElementFilter(currentElementToJoin); foreach (Element currentElementToBeJoined in elementsIntersecting) { if (currentUI.join) { if (currentElementToJoin.Id == currentElementToBeJoined.Id) { continue; } if (currentElementToJoin.Category.Id != currentElementToBeJoined.Category.Id) { if (!intersectFilter.PassesFilter(currentElementToBeJoined)) { continue; } } if (!JoinGeometryUtils.AreElementsJoined(doc, currentElementToJoin, currentElementToBeJoined)) { try { JoinGeometryUtils.JoinGeometry(doc, currentElementToJoin, currentElementToBeJoined); } catch (Exception e) { System.Diagnostics.Debug.Print(e.Message); } } } else { if (JoinGeometryUtils.AreElementsJoined(doc, currentElementToJoin, currentElementToBeJoined)) { try { JoinGeometryUtils.UnjoinGeometry(doc, currentElementToJoin, currentElementToBeJoined); } catch (Exception e) { System.Diagnostics.Debug.Print(e.Message); } } } } } t.Commit(); } } catch (Exception excep) { ExceptionManager eManager = new ExceptionManager(excep); } return(Result.Succeeded); }