private static string GetStringValue(ResultBufferEnumerator tvEnumerator, bool hasCurrent) { var regex = new Regex(@"^\d{1,2}#"); string nextVal = null; do { if (!hasCurrent) { continue; } var tv = tvEnumerator.Current; if (tv.TypeCode == (short)DxfCode.ExtendedDataAsciiString) { nextVal = tv.Value?.ToString(); break; } }while (tvEnumerator.MoveNext()); if (nextVal == null) { return(null); } var match = regex.Match(nextVal); if (!match.Success) { return(nextVal); } var sb = new StringBuilder(nextVal.Substring(match.Length)); while (tvEnumerator.MoveNext()) { if (tvEnumerator.Current.TypeCode != (short)DxfCode.ExtendedDataAsciiString) { break; } var val = tvEnumerator.Current.Value?.ToString(); if (val == null) { break; } match = regex.Match(val); if (!match.Success) { break; } var valNext = val.Substring(match.Length); sb.Append(valNext); } return(sb.ToString()); }
public void mghlParam() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptResult pr = ed.GetString("\nEnter Parameter name: "); if (pr.Status != PromptStatus.OK) { return; } try { ObjectId variableId = AssocUtil.GetVariableByName(db.CurrentSpaceId, pr.StringResult, false); if (variableId == ObjectId.Null) { PromptKeywordOptions pko = new PromptKeywordOptions("\nThe Parameter doesn't exist, do you want to create it?:"); pko.AllowNone = false; pko.Keywords.Add("Yes"); pko.Keywords.Add("No"); pko.Keywords.Default = "Yes"; PromptResult pkr = ed.GetKeywords(pko); if (pkr.Status != PromptStatus.OK) { return; } if (pkr.StringResult == "No") { return; } variableId = AssocUtil.GetVariableByName(db.CurrentSpaceId, pr.StringResult, true); } //At this point parameter must exist string name; ResultBuffer value; string expression; string evaluatorId; AssocUtil.GetVariableValue(variableId, out name, out value, out expression, out evaluatorId); ResultBufferEnumerator iter = value.GetEnumerator(); iter.MoveNext(); ed.WriteMessage("\n - Current Value: {0} = {1} [Expression: {2}])", name, iter.Current.Value.ToString(), expression); PromptKeywordOptions pko2 = new PromptKeywordOptions("\n\nEnter an option:"); pko2.AllowNone = false; pko2.Keywords.Add("Rename"); pko2.Keywords.Add("Expression"); pko2.Keywords.Default = "Rename"; PromptResult pkr2 = ed.GetKeywords(pko2); if (pkr2.Status != PromptStatus.OK) { return; } if (pkr2.StringResult == "Rename") { PromptResult pr2 = ed.GetString("\nEnter new name: "); if (pr2.Status != PromptStatus.OK) { return; } AssocUtil.RenameVariable(variableId, pr2.StringResult, true); } if (pkr2.StringResult == "Expression") { PromptResult pr3 = ed.GetString("\nEnter new Expression: "); if (pr3.Status != PromptStatus.OK) { return; } string errMsg; AssocUtil.SetVariableValue(variableId, null, pr3.StringResult, evaluatorId, out errMsg); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("\nError: " + ex.ErrorStatus.ToString()); } }
public static System.Data.DataTable GetAllPolylineNums() { System.Data.DataTable table = new System.Data.DataTable("编码"); table.Columns.Add(new System.Data.DataColumn(("多段线id"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("个体编码"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("个体要素"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("个体名称"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("数量"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("个体阶段"), typeof(string))); System.Data.DataColumn column; System.Data.DataRow row; Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; DocumentLock m_DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument(); try { using (Transaction tr = db.TransactionManager.StartTransaction()) { foreach (ObjectId layerId in LayersToList(db)) { LayerTableRecord layer = tr.GetObject(layerId, OpenMode.ForRead) as LayerTableRecord; TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.LayerName, layer.Name) }; SelectionFilter sfilter = new SelectionFilter(filList); PromptSelectionResult result = ed.SelectAll(sfilter); if (result.Status == PromptStatus.OK) { SelectionSet acSSet = result.Value; foreach (ObjectId id in acSSet.GetObjectIds()) { DBObject obj = id.GetObject(OpenMode.ForRead); //以读的方式打开对象 ObjectId dictId = obj.ExtensionDictionary; //获取对象的扩展字典的id Entity ent1 = tr.GetObject(id, OpenMode.ForRead) as Entity; if (ent1 is Polyline && !dictId.IsNull) { DBDictionary dict = dictId.GetObject(OpenMode.ForRead) as DBDictionary;//获取对象的扩展字典 if (!dict.Contains("polylineNumber")) { continue;//如果扩展字典中没有包含指定关键 字的扩展记录,则返回null; } //先要获取对象的扩展字典或图形中的有名对象字典,然后才能在字典中获取要查询的扩展记录 ObjectId xrecordId = dict.GetAt("polylineNumber"); //获取扩展记录对象的id Xrecord xrecord = xrecordId.GetObject(OpenMode.ForRead) as Xrecord; //根据id获取扩展记录对象 ResultBuffer resBuf = xrecord.Data; ResultBufferEnumerator rator = resBuf.GetEnumerator(); int i = 0; List <string> values = new List <string>(); while (rator.MoveNext()) { TypedValue re = rator.Current; values.Add((string)re.Value); } bool hasData = false; foreach (System.Data.DataRow item in table.Rows) { if ((string)item["个体编码"] == values[0] && (string)item["个体要素"] == values[1] && (string)item["个体名称"] == values[2] && (string)item["个体阶段"] == values[3]) { item["多段线id"] += id.Handle.Value.ToString() + ","; int count = 0; int.TryParse((string)item["数量"], out count); item["数量"] = count + 1; hasData = true; break; } } if (!hasData) { row = table.NewRow(); row["多段线id"] = id.Handle.Value.ToString() + ","; row["数量"] = 1; while (rator.MoveNext()) { TypedValue re = rator.Current; if (i == 0) { row["个体编码"] = re.Value; } if (i == 1) { row["个体要素"] = re.Value; } if (i == 2) { row["个体名称"] = re.Value; } if (i == 3) { row["个体阶段"] = re.Value; } i++; } table.Rows.Add(row); } resBuf.Dispose(); xrecord.Dispose(); dict.Dispose(); } ent1.Dispose(); obj.Dispose(); } } layer.Dispose(); } tr.Commit(); } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.ToString()); } finally { m_DocumentLock.Dispose(); } return(table); }
public static System.Data.DataTable GetAllPolylineNumsEx(Polyline line) { System.Data.DataTable table = new System.Data.DataTable("编码"); table.Columns.Add(new System.Data.DataColumn(("多段线id"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("个体编码"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("个体要素"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("个体名称"), typeof(string))); table.Columns.Add(new System.Data.DataColumn(("个体阶段"), typeof(string))); System.Data.DataColumn column; System.Data.DataRow row; try { ObjectId dictId = line.ExtensionDictionary;//获取对象的扩展字典的id if (!dictId.IsNull) { DBDictionary dict = dictId.GetObject(OpenMode.ForRead) as DBDictionary;//获取对象的扩展字典 if (!dict.Contains("polylineNumber")) { return(null);//如果扩展字典中没有包含指定关键 字的扩展记录,则返回null; } //先要获取对象的扩展字典或图形中的有名对象字典,然后才能在字典中获取要查询的扩展记录 ObjectId xrecordId = dict.GetAt("polylineNumber"); //获取扩展记录对象的id Xrecord xrecord = xrecordId.GetObject(OpenMode.ForRead) as Xrecord; //根据id获取扩展记录对象 ResultBuffer resBuf = xrecord.Data; ResultBufferEnumerator rator = resBuf.GetEnumerator(); int i = 0; row = table.NewRow(); row["多段线id"] = line.Id.Handle.Value.ToString(); while (rator.MoveNext()) { TypedValue re = rator.Current; if (i == 0) { row["个体编码"] = re.Value; } if (i == 1) { row["个体要素"] = re.Value; } if (i == 2) { row["个体名称"] = re.Value; } if (i == 3) { row["个体阶段"] = re.Value; } i++; } table.Rows.Add(row); } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.ToString()); } return(table); }