/// <summary> /// 外部强行取消模型线的绘制,由于外部并不知道当前的模型线是否符合要求,所以要最后检查一次 /// </summary> public void CancelExternally() { // 最后再检测一次 bool blnContinueDraw = false; CurvesState cs = ValidateCurves(AddedModelCurvesId, out blnContinueDraw); RefreshUiAfterValidation(cs, continueDraw: false); // if (cs == CurvesState.Validated) { this.FinishOnce(finishedExternally: true, succeeded: true); } else { // 不管是 Validating 还是 Invalid,说明在此刻都没有成功 this.FinishOnce(finishedExternally: true, succeeded: false); } // Dispose(); }
/// <summary> /// 根据当前曲线的连续性状态,以及是否可以继续绘制,来作出相应的UI更新 /// </summary> /// <param name="cs"></param> /// <param name="continueDraw"></param> private void RefreshUiAfterValidation(CurvesState cs, bool continueDraw) { switch (cs) { case CurvesState.Validated: if (continueDraw) // 说明是绘制连续线时满足条件 { // 继续绘制即可 } else // 说明是绘制封闭线时终于封闭成功了 { // 此时直接结束绘制就可以了,而不用考虑撤消的问题 this.FinishOnce(false, true); return; } break; case CurvesState.Validating: if (continueDraw) // 说明是绘制封闭线时还未封闭,但是所绘制的曲线都是连续的 { // 继续绘制即可 } else // 暂时没有考虑到何时会出现此种情况 { // 不需要任何实现方法 } return; case CurvesState.Invalid: if (InquireUndo()) { RvtTools.Undo(); } else { // 结束绘制 this.FinishOnce(false, false); } return; } }
/// <summary> /// 检测当前集合中的曲线是否符合指定的连续性要求 /// </summary> /// <param name="curveIds">被检验的模型线集合</param> /// <param name="continueDraw">在检查连续性后是否要继续绘制</param> /// <returns></returns> private CurvesState ValidateCurves(List <ElementId> curveIds, out bool continueDraw) { CurvesState cs = CurvesState.Invalid; continueDraw = false; if (curveIds.Any()) { List <Curve> curves = new List <Curve>(); //将ElementId转换为对应的 Curve 对象 foreach (var id in curveIds) { curves.Add(((ModelCurve)doc.GetElement(id)).GeometryCurve); } // 根据不同的模式进行不同的检测 if (this.CheckMode == CurveCheckMode.Connected) // 一条连续曲线链 { if (CurvesFormator.GetContiguousCurvesFromCurves(curves) != null) { cs = CurvesState.Validated; continueDraw = true; } else // 说明根本不连续 { cs = CurvesState.Invalid; continueDraw = false; } } else if (this.CheckMode == CurveCheckMode.Closed) { IList <Curve> CurveChain = default(List <Curve>); // MessageBox.Show(GeoHelper.GetCurvesEnd(curves)); CurveChain = CurvesFormator.GetContiguousCurvesFromCurves(curves); if (CurveChain == null) // 说明根本就不连续 { cs = CurvesState.Invalid; continueDraw = false; } else // 说明起码是连续的 { if (CurveChain.First().GetEndPoint(0).DistanceTo(CurveChain.Last().GetEndPoint(1)) < GeoHelper.VertexTolerance) { // 说明整个连续曲线是首尾相接,即是闭合的。此时就不需要再继续绘制下去了 cs = CurvesState.Validated; continueDraw = false; } else { // 说明整个曲线是连续的,但是还没有闭合。此时就可以继续绘制下去 cs = CurvesState.Validating; continueDraw = true; } } } else if (this.CheckMode == (CurveCheckMode.HorizontalPlan | CurveCheckMode.Closed)) { if (CurvesFormator.IsInOnePlan(curves, new XYZ(0, 0, 1))) { } return(CurvesState.Invalid); } else if (this.CheckMode == CurveCheckMode.Seperated) { // 不用检测,直接符合 cs = CurvesState.Validated; continueDraw = true; } } else // 说明当前集合中一个模型线元素都没有 { } return(cs); }
/// <summary> /// DocumentChanged事件,并针对不同的绘制情况而进行不同的处理 /// </summary> /// <param name="sender">Application对象</param> /// <param name="e"></param> private void app_DocumentChanged(object sender, DocumentChangedEventArgs e) { if (e.Operation == UndoOperation.TransactionCommitted || e.Operation == UndoOperation.TransactionUndone || e.Operation == UndoOperation.TransactionRedone) { doc = e.GetDocument(); bool blnContinueDraw = false; // 在检查连续性后是否要继续绘制 try { // 先考察添加的对象:如果添加了新对象,则要么是 DrawNewLines ,要么是 DrawOtherObjects int addedCount = 0; Element addedElement = default(Element); foreach (ElementId eid in e.GetAddedElementIds()) { addedElement = doc.GetElement(eid); if (addedElement is ModelCurve) { AddedModelCurvesId.Add(eid); addedCount++; } } if (addedCount > 0) // 说明绘制了新的模型线 { // 检测当前集合中的曲线是否符合指定的连续性要求 if (this.CheckInTime) { CurvesState cs = ValidateCurves(AddedModelCurvesId, out blnContinueDraw); RefreshUiAfterValidation(cs, blnContinueDraw); } else // 说明不进行实时检测,而直接继续绘制 { } // return; } // // 再考察删除对象的情况 List <ElementId> deleted = e.GetDeletedElementIds().ToList(); if (deleted.Count > 0) { // 先将被删除的曲线从曲线链集合中剔除掉 int id_Chain = 0; // 曲线链中的元素下标 int id_deleted = 0; // 删除的模型线集合中的元素下标 for (id_Chain = AddedModelCurvesId.Count - 1; id_Chain >= 0; id_Chain--) // 曲线链中的元素下标 { // id_deleted = deleted.IndexOf(AddedModelCurvesId[id_Chain]); // 找到对应的项 // if (id_deleted >= 0) { deleted.RemoveAt(id_deleted); AddedModelCurvesId.RemoveAt(id_Chain); } } // 检测剔除后的集合中的曲线是否符合指定的连续性要求 if (this.CheckInTime) { if (this.CheckInTime) { CurvesState cs = ValidateCurves(AddedModelCurvesId, out blnContinueDraw); RefreshUiAfterValidation(cs, blnContinueDraw); } else // 说明不进行实时检测,而直接继续绘制 { } } else // 说明不进行实时检测,而直接继续绘制 { } // return; } // 再考察修改对象的情况(因为在添加对象或者删除对象时,都有可能伴随有修改对象):在没有添加新对象,只作了修改的情况下,要么是对 int modifiedCount = 0; Element modifiedCountElement = default(Element); foreach (ElementId eid in e.GetModifiedElementIds()) { modifiedCountElement = doc.GetElement(eid); if (modifiedCountElement is ModelCurve) { modifiedCount++; } } if (modifiedCount > 0) { // 检测剔除后的集合中的曲线是否符合指定的连续性要求 if (this.CheckInTime) { CurvesState cs = ValidateCurves(AddedModelCurvesId, out blnContinueDraw); RefreshUiAfterValidation(cs, blnContinueDraw); } else // 说明不进行实时检测,而直接继续绘制 { } // return; } } catch (Exception ex) { MessageBox.Show("在绘制模型线及连续性判断时出问题啦~~~" + "\r\n" + ex.Message + ex.GetType().FullName + "\r\n" + ex.StackTrace); // 结束绘制 this.FinishOnce(false, false); } } }