Civil 3D 删除不需要的标签表达式
正常情况下,
不需要的标签表达式应该能够手动删除,
不知道什么原因
有些表达式在创建后状态就成了“被引用”状态,
导致无法删除。
即使想修改名称也不行,
不得不采用编程的方式进行删除。
代码如下:
public void m_RemoveExpression() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.CurrentDocument; Database db = doc.Database; Transaction tr = db.TransactionManager.StartTransaction(); try { CivilDocument civilDoc = CivilDocument.GetCivilDocument(db); object obj = civilDoc.Styles; //获取所有样式 RemoveExpression(tr, obj, ref doc); //修改样式 tr.Commit(); tr.Dispose(); } catch (System.Exception ex) { tr.Commit(); tr.Dispose(); doc.Editor.WriteMessage("\n" + ex.Message); } }
递归方法,
遍历所有的样式....
找到需要删除的后删除之......
private void RemoveExpression(Transaction tr, object obj, ref Document doc) { Type objType = obj.GetType(); PropertyInfo[] properties = objType.GetProperties(); foreach (PropertyInfo prop in properties) { Object obj1 = prop.GetValue(obj); if (IsEnumberable(obj1)) { #region try try { PropertyInfo property = obj1.GetType().GetProperty("Expressions"); if (property != null) { #region ExpressionCollection exps = property.GetValue(obj1) as ExpressionCollection; List<int> indexs = new List<int>(); for (int i = 0; i < exps.Count; i++) { if (exps[i].Description .Contains("删除")) { indexs.Add(i); } } for (int j = indexs.Count - 1; j >= 0; j--) { exps.RemoveAt(indexs[j]); } #endregion } else { IEnumerable enumerable = obj1 as IEnumerable; IEnumerator enumerator = enumerable.GetEnumerator(); List<object> objs = new List<object>(); while (enumerator.MoveNext()) { object obj2 = enumerator.Current; objs.Add(obj2); } foreach (object obj2 in objs) { RemoveExpression(tr, obj2, ref doc); } } } catch (System.Exception ex) { //doc.Editor.WriteMessage("\n{0}\t{1}\t{2}", // ex.Message, objType.ToString(), obj.ToString()); } #endregion } else if (prop.PropertyType.ToString().Contains("Root")) { RemoveExpression(tr, obj1, ref doc); } } }
bool IsEnumberable(Object obj) { if (obj is string) return false; IEnumerable enumerable = obj as IEnumerable; return (enumerable != null); }
要处理Civil 3d样式相关的东西,
反射是个好工具,
可惜本人对此并不熟悉,
试验了很长时间才写出了上面的代码。
标签:
Civil3d
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?