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; List <ElementId> elementIds = new List <ElementId>(); VLHookHelper.DelegateMouseHook(() => { elementIds = uiDoc.Selection.PickObjects(ObjectType.Element, new VLClassesFilter(false, typeof(Pipe), typeof(Duct), typeof(CableTray), typeof(Conduit) ), "选择要添加的构件").Select(c => c.ElementId).ToList(); }); if (elementIds == null || elementIds.Count() < 2) { return(Result.Failed); } var selectedElements = elementIds.Select(c => doc.GetElement(c)).ToList(); AvoidElementManager manager = new AvoidElementManager(uiApp); manager.AddElements(selectedElements); VLTransactionHelper.DelegateTransaction(doc, "调研_单点避让", () => { manager.CheckConflict(); manager.AutoAvoid(doc); return(true); }); VLTransactionHelper.DelegateTransaction(doc, "调研_单点避让", () => { var result = string.Join(",", manager.ConnectionNodes.Select(c => c.MEPCurve1.Id + "->" + c.MEPCurve2.Id)); var service = new MEPCurveConnectControlService(uiApp); foreach (var ConnectionNode in manager.ConnectionNodes) { try { service.NewTwoFitting(ConnectionNode.MEPCurve1, ConnectionNode.MEPCurve2, null); doc.Regenerate(); } catch (System.Exception ex) { VLLogHelper.Error(string.Format("Node1:{0},Node2:{1},Error:{2}", ConnectionNode.MEPCurve1.Id, ConnectionNode.MEPCurve2.Id, ex.Message)); var error = ex.ToString(); } } return(true); }); return(Result.Succeeded); }
public void Execute(UpdaterData updateData) { try { //if (PBPAContext.IsDeleting == true) //{ // PBPAContext.IsDeleting = false; // return; //} var doc = updateData.GetDocument(); var deletes = updateData.GetDeletedElementIds(); var collection = PBPAContext.GetCollection(doc); if (deletes.Count == 0) { return; } bool isDeleted = false; foreach (var deleteId in deletes) { var itemToDelete = collection.Data.FirstOrDefault(c => c.TargetId == deleteId); if (itemToDelete == null) { itemToDelete = collection.Data.FirstOrDefault(c => c.AnnotationId == deleteId); } if (itemToDelete == null) { itemToDelete = collection.Data.FirstOrDefault(c => c.LineIds.Contains(deleteId)); } if (itemToDelete != null) { itemToDelete.Document = doc; collection.Data.Remove(itemToDelete); itemToDelete.Clear(); isDeleted = true; } } if (isDeleted) { PBPAContext.SaveCollection(doc); } } catch (Exception ex) { VLLogHelper.Error(ex); } }
public void Execute(UpdaterData updateData) { try { var doc = updateData.GetDocument(); var deletes = updateData.GetDeletedElementIds(); var collection = CSAContext.GetCollection(doc); if (deletes.Count == 0) { return; } bool isDeleted = false; foreach (var deleteId in deletes) { var itemToDelete = collection.Data.FirstOrDefault(c => c.TargetId.IntegerValue == deleteId.IntegerValue); if (itemToDelete == null) { itemToDelete = collection.Data.FirstOrDefault(c => c.LineId.IntegerValue == deleteId.IntegerValue); } if (itemToDelete == null) { itemToDelete = collection.Data.FirstOrDefault(c => c.TextNoteIds.FirstOrDefault(p => p.IntegerValue == deleteId.IntegerValue) != null); } if (itemToDelete != null) { collection.Data.Remove(itemToDelete); var creater = CSAContext.Creator; creater.Clear(doc, itemToDelete); isDeleted = true; } } if (isDeleted) { CSAContext.Save(doc); } } catch (Exception ex) { VLLogHelper.Error(ex); } }
public void Execute(UpdaterData updateData) { try { //if (PBPAContext.IsEditing == true) //{ // PBPAContext.IsEditing = false; // return; //} var document = updateData.GetDocument(); var edits = updateData.GetModifiedElementIds(); var collection = PBPAContext.GetCollection(document); List <int> movedEntities = new List <int>(); foreach (var changeId in edits) { PBPAModel model = null; #region 单管 主体 重新生成 var targetMoved = collection.Data.FirstOrDefault(c => c.TargetId == changeId); if (targetMoved != null) { model = targetMoved; if (movedEntities.Contains(model.TargetId.IntegerValue)) { continue; } var creater = PBPAContext.Creator; var target = document.GetElement(model.TargetId);//标注主体失效时删除 var targetLocation = target.Location as LocationCurve; var p0 = targetLocation.Curve.GetEndPoint(0); var p1 = targetLocation.Curve.GetEndPoint(1); var pMiddle = new XYZ((p0.X + p1.X) / 2, (p0.Y + p1.Y) / 2, (p0.Z + p1.Z) / 2); var offset = pMiddle - model.TargetLocation; offset.ToSameZ(new XYZ(0, 0, 0)); model.BodyStartPoint += offset; model.BodyEndPoint += offset; model.LeafEndPoint += offset; //必要族 model.Document = document; model.IsRegenerate = true; if (!PBPAContext.Creator.Regenerate(model)) { collection.Data.Remove(model); } movedEntities.Add(model.TargetId.IntegerValue); //PBPAContext.IsEditing = true;//重新生成无需避免移动导致的重复触发 continue; } #endregion #region 根据 标注 重新生成 var textMoved = collection.Data.FirstOrDefault(c => c.AnnotationId == changeId); if (textMoved != null) { model = textMoved; if (movedEntities.Contains(model.TargetId.IntegerValue)) { continue; } var creater = PBPAContext.Creator; var annotation = document.GetElement(changeId) as IndependentTag; var offset = annotation.TagHeadPosition - model.AnnotationLocation; offset.ToSameZ(new XYZ(0, 0, 0)); model.BodyEndPoint += offset; model.LeafEndPoint += offset; model.Document = document; model.IsRegenerate = true; if (!PBPAContext.Creator.Regenerate(model)) { collection.Data.Remove(model); } movedEntities.Add(model.TargetId.IntegerValue); //PBPAContext.IsEditing = true;//重新生成无需避免移动导致的重复触发 continue; } #endregion } PBPAContext.SaveCollection(document); } catch (Exception ex) { VLLogHelper.Error(ex); } }
public void Execute(UpdaterData updateData) { try { if (PAAContext.IsDeleting == true) { PAAContext.IsDeleting = false; return; } var doc = updateData.GetDocument(); var deletes = updateData.GetDeletedElementIds(); var collection = PAAContext.GetCollection(doc); if (deletes.Count == 0) { return; } bool isDeleted = false; foreach (var deleteId in deletes) { var itemToDelete = collection.Data.FirstOrDefault(c => c.TargetId == deleteId); if (itemToDelete == null) { itemToDelete = collection.Data.FirstOrDefault(c => c.AnnotationId == deleteId); } if (itemToDelete == null) { itemToDelete = collection.Data.FirstOrDefault(c => c.LineId == deleteId); } if (itemToDelete != null) { itemToDelete.Document = doc; collection.Data.Remove(itemToDelete); itemToDelete.Clear(); isDeleted = true; } else { itemToDelete = collection.Data.FirstOrDefault(c => c.TargetIds != null && c.TargetIds.Contains(deleteId)); if (itemToDelete != null) { var index = itemToDelete.TargetIds.IndexOf(deleteId); itemToDelete.AnnotationIds.RemoveAt(index); itemToDelete.TargetIds.RemoveAt(index); if (itemToDelete.TargetIds.Count > 1) { itemToDelete.Document = doc; itemToDelete.IsRegenerate = true; if (!PAAContext.Creator.Regenerate(itemToDelete)) { collection.Data.Remove(itemToDelete); } } else { itemToDelete.Document = doc; collection.Data.Remove(itemToDelete); itemToDelete.Clear(); } } else { itemToDelete = collection.Data.FirstOrDefault(c => c.AnnotationIds != null && c.AnnotationIds.Contains(deleteId)); if (itemToDelete != null) { var index = itemToDelete.AnnotationIds.IndexOf(deleteId); itemToDelete.AnnotationIds.RemoveAt(index); itemToDelete.TargetIds.RemoveAt(index); if (itemToDelete.TargetIds.Count > 1) { itemToDelete.Document = doc; itemToDelete.IsRegenerate = true; if (!PAAContext.Creator.Regenerate(itemToDelete)) { collection.Data.Remove(itemToDelete); } } else { itemToDelete.Document = doc; collection.Data.Remove(itemToDelete); itemToDelete.Clear(); } } } } } if (isDeleted) { PAAContext.SaveCollection(doc); } } catch (Exception ex) { VLLogHelper.Error(ex); } }
public void Execute(UpdaterData updateData) { try { if (PAAContext.IsEditing == true) { PAAContext.IsEditing = false; return; } var document = updateData.GetDocument(); var edits = updateData.GetModifiedElementIds(); var collection = PAAContext.GetCollection(document); List <int> movedEntities = new List <int>(); foreach (var changeId in edits) { PAAModel model = null; #region 单管 主体 重新生成 var targetMoved = collection.Data.FirstOrDefault(c => c.TargetId == changeId); if (targetMoved != null) { model = targetMoved; if (movedEntities.Contains(model.TargetId.IntegerValue)) { continue; } var creater = PAAContext.Creator; var target = document.GetElement(model.TargetId);//标注主体失效时删除 var targetLocation = target.Location as LocationCurve; var p0 = targetLocation.Curve.GetEndPoint(0); var p1 = targetLocation.Curve.GetEndPoint(1); var pMiddle = new XYZ((p0.X + p1.X) / 2, (p0.Y + p1.Y) / 2, (p0.Z + p1.Z) / 2); var offset = pMiddle - model.TargetLocation; offset.ToSameZ(new XYZ(0, 0, 0)); model.BodyStartPoint += offset; model.BodyEndPoint += offset; model.LeafEndPoint += offset; //必要族 model.Document = document; model.IsRegenerate = true; model.RegenerateType = RegenerateType.BySingle; if (!PAAContext.Creator.Regenerate(model)) { collection.Data.Remove(model); } movedEntities.Add(model.TargetId.IntegerValue); //PAAContext.IsEditing = true;//重新生成无需避免移动导致的重复触发 continue; } #endregion #region 多管 主体 重新生成 targetMoved = collection.Data.FirstOrDefault(c => c.TargetIds != null && c.TargetIds.Contains(changeId)); if (targetMoved != null) { model = targetMoved; if (movedEntities.Contains(changeId.IntegerValue)) { continue; } model.Document = document; model.IsRegenerate = true; model.RegenerateType = RegenerateType.ByMultipleTarget; if (!PAAContext.Creator.Regenerate(model)) { collection.Data.Remove(model); } movedEntities.AddRange(model.TargetIds.Select(c => c.IntegerValue)); //PAAContext.IsEditing = true;//重新生成无需避免移动导致的重复触发 continue; } #endregion #region 根据 标注 重新生成 var textMoved = collection.Data.FirstOrDefault(c => c.AnnotationId == changeId); if (textMoved != null) { model = textMoved; if (movedEntities.Contains(model.TargetId.IntegerValue)) { continue; } var creater = PAAContext.Creator; var annotation = document.GetElement(changeId) as IndependentTag; var offset = annotation.TagHeadPosition - model.AnnotationLocation; offset.ToSameZ(new XYZ(0, 0, 0)); model.BodyEndPoint += offset; model.LeafEndPoint += offset; model.Document = document; model.IsRegenerate = true; model.RegenerateType = RegenerateType.BySingle; if (!PAAContext.Creator.Regenerate(model)) { collection.Data.Remove(model); } movedEntities.Add(model.TargetId.IntegerValue); //PAAContext.IsEditing = true;//重新生成无需避免移动导致的重复触发 continue; } #endregion #region 根据Line重新生成 var lineMoved = collection.Data.FirstOrDefault(c => c.LineId == changeId); if (lineMoved != null) { model = lineMoved; bool isExisted = false; foreach (var TargetId in model.TargetIds) { if (movedEntities.Contains(TargetId.IntegerValue)) { isExisted = true; break; } } if (isExisted) { continue; } model.Document = document; model.IsRegenerate = true; model.RegenerateType = RegenerateType.ByMultipleLine; if (PAAContext.Creator.Regenerate(model)) { PAAContext.IsEditing = true; PAAContext.IsDeleting = true; } else { collection.Data.Remove(model); } movedEntities.AddRange(model.TargetIds.Select(c => c.IntegerValue)); //var line0 = document.GetElement(model.LineIds[0]); //var pStart = (line0.Location as LocationCurve).Curve.GetEndPoint(0); //PAAContext.Creator.Regenerate(document, model, target, pStart - model.BodyStartPoint); //movedEntities.Add(model.TargetId.IntegerValue); ////CSAContext.IsEditing = true;//重新生成无需避免移动导致的重复触发 continue; } #endregion } PAAContext.SaveCollection(document); } catch (Exception ex) { VLLogHelper.Error(ex); } }
public void Execute(UpdaterData updateData) { try { var document = updateData.GetDocument(); var edits = updateData.GetModifiedElementIds(); var collection = CSAContext.GetCollection(document); if (CSAContext.IsEditing == true) { CSAContext.IsEditing = false; return; } List <int> movedEntities = new List <int>(); foreach (var changeId in edits) { CSAModel model = null; if (VLConstraintsForCSA.Doc == null) { VLConstraintsForCSA.Doc = document; } #region 根据Target重新生成 var targetMoved = collection.Data.FirstOrDefault(c => c.TargetId.IntegerValue == changeId.IntegerValue); if (targetMoved != null) { model = targetMoved; if (movedEntities.Contains(model.TargetId.IntegerValue)) { continue; } var creater = CSAContext.Creator; var target = document.GetElement(model.TargetId);//标注主体失效时删除 if (target == null) { collection.Data.Remove(model); continue; } CSAContext.Creator.Regenerate(document, model, target, new XYZ(0, 0, 0)); movedEntities.Add(model.TargetId.IntegerValue); CSAContext.IsEditing = true; } #endregion #region 根据Text重新生成 var textMoved = collection.Data.FirstOrDefault(c => c.TextNoteIds.FirstOrDefault(p => p.IntegerValue == changeId.IntegerValue) != null); if (textMoved != null) { model = textMoved; if (movedEntities.Contains(model.TargetId.IntegerValue)) { continue; } var creater = CSAContext.Creator; var target = document.GetElement(model.TargetId);//标注主体失效时删除 if (target == null) { collection.Data.Remove(model); continue; } //文本更改处理 var index = model.TextNoteIds.IndexOf(changeId); var newText = (document.GetElement(changeId) as TextNote).Text; if (model.Texts[index] != newText) { CompoundStructure compoundStructure = null; HostObjAttributes hoster = null; if (target is Wall) { hoster = (HostObjAttributes)((target as Wall).WallType); compoundStructure = hoster.GetCompoundStructure(); } if (target is Floor) { hoster = (HostObjAttributes)((target as Floor).FloorType); compoundStructure = hoster.GetCompoundStructure(); } if (target is RoofBase)//屋顶有多种类型 { hoster = (HostObjAttributes)((target as RoofBase).RoofType); compoundStructure = hoster.GetCompoundStructure(); } if (compoundStructure == null) { throw new NotImplementedException("关联更新失败,未获取有效的CompoundStructure类型"); } var layers = compoundStructure.GetLayers(); string pattern = @"([\d+\.]+)厚(.+)[\r]?"; Regex regex = new Regex(pattern); var match = regex.Match(newText); if (!match.Success) { PMMessageBox.ShowError("关联更新失败,文本不符合规范"); return; } var length = match.Groups[1].Value; var lengthFoot = UnitHelper.ConvertToFoot(Convert.ToDouble(length), VLUnitType.millimeter); var materialName = match.Groups[2].Value; var material = new FilteredElementCollector(document).OfClass(typeof(Material)) .FirstOrDefault(c => c.Name == materialName); if (material == null) { PMMessageBox.ShowError("关联更新失败,未获取到对应名称的材质"); return; } //更新 layers[index].Width = lengthFoot; layers[index].MaterialId = material.Id; compoundStructure.SetLayers(layers); IDictionary <int, CompoundStructureError> report = null; IDictionary <int, int> errorMap; try { compoundStructure.IsValid(document, out report, out errorMap); hoster.SetCompoundStructure(compoundStructure); } catch (Exception ex) { PMMessageBox.ShowError("材质设置失败,错误详情:" + (report != null ? string.Join(",", report.Select(c => c.Value)) : "")); throw ex; } ////报错This operation is valid only for non-vertically compound structures //layer = layers[index]; //layer.MaterialId = material.Id; ////compoundStructure.SetLayer(index, layer); } else { var textNote = document.GetElement(changeId) as TextNote; if (model.TextNoteTypeElementId.IntegerValue != textNote.TextNoteType.Id.IntegerValue) { model.TextNoteTypeElementId = textNote.TextNoteType.Id; CSAContext.Creator.Regenerate(document, model, target, new XYZ(0, 0, 0)); } else { //文本偏移处理 //var index = model.TextNoteIds.IndexOf(changeId); //var offset = (document.GetElement(changeId) as TextNote).Coord - model.TextLocations[index]; //CompoundStructureAnnotationContext.Creater.Regenerate(document, model, target, offset); //CSAContext.IsEditing = true;//移动会导致偏移 从而二次触发 } } movedEntities.Add(model.TargetId.IntegerValue); } #endregion #region 根据Line重新生成 var lineMoved = collection.Data.FirstOrDefault(c => c.LineId.IntegerValue == changeId.IntegerValue); if (lineMoved != null) { model = lineMoved; if (movedEntities.Contains(model.TargetId.IntegerValue)) { continue; } var creater = CSAContext.Creator; var target = document.GetElement(model.TargetId); if (target == null) { collection.Data.Remove(model); continue; } CSAContext.Creator.Regenerate(document, model, target); movedEntities.Add(model.TargetId.IntegerValue); CSAContext.IsEditing = true; } #endregion } CSAContext.Save(document); } catch (Exception ex) { VLLogHelper.Error(ex); } }