Exemplo n.º 1
0
 public void WriteDictionary(Entity ent, Dictionary <string, string> data)
 {
     using (var locker = Application.DocumentManager.MdiActiveDocument.LockDocument())
     {
         using (var tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
         {
             var obj = tr.GetObject(ent.ObjectId, OpenMode.ForWrite);
             if (!obj.ExtensionDictionary.IsValid)
             {
                 //创建扩展字典
                 obj.CreateExtensionDictionary();
             }
             using (var objdict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite, false))
             {
                 foreach (var kv in data)
                 {
                     Xrecord xrecord = new Xrecord()
                     {
                         Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, kv.Value))
                     };
                     objdict.SetAt(kv.Key, xrecord);
                     tr.AddNewlyCreatedDBObject(xrecord, true);
                     xrecord.Dispose();
                 }
             }
             tr.Commit();
         }
     }
 }
Exemplo n.º 2
0
        protected static void WriteDictionary(ObjectId oid, Dictionary <string, string> data, bool clear = false)
        {
            if (data != null && data.Any())
            {
                using (var locker = Application.DocumentManager.MdiActiveDocument.LockDocument())
                {
                    using (var tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
                    {
                        var obj = tr.GetObject(oid, OpenMode.ForWrite);
                        if (!obj.ExtensionDictionary.IsValid)
                        {
                            //创建扩展字典
                            obj.CreateExtensionDictionary();
                        }
                        using (var objdict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite, false))
                        {
                            if (clear)
                            {
                                foreach (var k in objdict)
                                {
                                    objdict.Remove(k.Key);
                                }
                            }

                            foreach (var kv in data)
                            {
                                Xrecord xrecord = new Xrecord()
                                {
                                    Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, kv.Value))
                                };
                                objdict.SetAt(kv.Key, xrecord);
                                tr.AddNewlyCreatedDBObject(xrecord, true);
                                xrecord.Dispose();
                                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($@"【{kv.Key}】{kv.Value}{Environment.NewLine}");
                            }
                        }
                        tr.Commit();
                    }
                }
            }
        }
Exemplo n.º 3
0
 public static void WriteDictionary(Entity ent, Dictionary <string, string> data, Transaction tr)
 {
     if (!ent.ExtensionDictionary.IsValid)
     {
         //创建扩展字典
         ent.CreateExtensionDictionary();
     }
     using (var objdict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite, false))
     {
         foreach (var kv in data)
         {
             Xrecord xrecord = new Xrecord()
             {
                 Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, kv.Value))
             };
             objdict.SetAt(kv.Key, xrecord);
             tr.AddNewlyCreatedDBObject(xrecord, true);
             xrecord.Dispose();
         }
     }
 }
Exemplo n.º 4
0
        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);
        }