介之推 发表于 2012-7-7 09:38:00

如何绘制一条PolyLine,添加扩展属性,并返回这个Polyline的ObjectId

大家好。我想绘制一个矩形框,然后给这个矩形框添加一个名称和标记的扩展数据。然后返回这个矩形框的ObjectId。扩展数据使用DataTable。在绘制以后在通过这个矩形的ObjectId重新获得这个矩形的名称和标记。如何做到?下面是我写的一部分代码,没有完成,请大家帮忙了。

'通过用户指定点后再绘制这个矩形,矩形的长和宽也作为参数
Public Shared Function AddRectangle(ByVal cenPt As Point3d, ByVal Height As Double, _
                                        ByVal Length As Double) As ObjectId
      Dim Pt(4) As Point3d
      Pt(0) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y - Height * 0.5, 0)
      Pt(1) = New Point3d(cenPt.X + Length * 0.5, cenPt.Y - Height * 0.5, 0)
      Pt(2) = New Point3d(cenPt.X + Length * 0.5, cenPt.Y + Height * 0.5, 0)
      Pt(3) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y + Height * 0.5, 0)
      Pt(4) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y - Height * 0.5, 0)
      Dim Pts As New Point3dCollection(Pt)
      ' 在内存中创建一个未经拟合的标准三维多段线对象.
      Dim ent As New Polyline3d(Poly3dType.SimplePoly, Pts, False)
      'Add the rectangle polyline entity to model space
      Dim acEntId As ObjectId = AppendEntity(ent)
      Dim dt As DataTable = New DataTable()
      dt.TableName = "属性表"
      dt.AppendColumn(CellType.CharPtr, "Name")
      dt.AppendColumn(CellType.CharPtr, "Tag")
      Dim row As DataCellCollection = New DataCellCollection()
      Dim Name As DataCell = New DataCell()
      Dim Tag As DataCell = New DataCell()
      Name.SetString("矩形框")
      Tag.SetString("矩形标记")
      row.Add(Name)
      row.Add(Tag)
      dt.AppendRow(row, True)
      ent.CreateExtensionDictionary()
      Dim exDic As DBDictionary = New DBDictionary()'如何将扩展数据记录下来啊?
      exDic.SetAt("属性表", dt)
      '返回ObjectId
      Return acEntId
    End Function
' 将图形对象加入到模型空间的函数.
    Public Shared Function AppendEntity(ByVal ent As Entity) As ObjectId
      ' 得到当前文档图形数据库.
      Dim db As Database = HostApplicationServices.WorkingDatabase
      Dim entId As ObjectId
      Using trans As Transaction = db.TransactionManager.StartTransaction
            ' 以读方式打开块表.
            Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
            ' 以写方式打开模型空间块表记录.
            Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            ' 将图形对象的信息添加到块表记录中,并返回ObjectId对象.
            entId = btr.AppendEntity(ent)
            ' 把图形对象添加到事务处理中.
            trans.AddNewlyCreatedDBObject(ent, True)
            ' 提交事务处理.
            trans.Commit()
      End Using
      Return entId
    End Function

sxpd 发表于 2012-7-7 09:54:00


用XDATA不是更简单
你的扩展数据用法有问题
贴个相关的程序你看看
    public static class DBDictionaryEx
    {
      public static IEnumerable GetAllObjects(this DBDictionary dict, Transaction tr) where T : DBObject
      {
            foreach (DBDictionaryEntry e in dict)
            {
                yield return
                  tr.GetObject(e.Value, OpenMode.ForRead) as T;
            }
      }
      
      public static T GetAt(this DBDictionary dict, Transaction tr, string key) where T : DBObject
      {
            if (dict.Contains(key))
            {
                ObjectId id = dict.GetAt(key);
                if (!id.IsNull)
                {
                  return tr.GetObject(id, OpenMode.ForRead) as T;
                }
            }
            return null;
      }
      public static void SetAt(this DBDictionary dict, Transaction tr, string key, T obj) where T : DBObject
      {
            using (dict.UpgradeOpenAndRun())
            {
                dict.SetAt(key, obj);
                tr.AddNewlyCreatedDBObject(obj, true);
            }
      }
      #region GetSubDictionary
      internal static List GetDictNames(string[] keys, out string key)
      {
            List dictNames = new List(keys);
            if (dictNames.Count > 0)
            {
                int index = dictNames.Count - 1;
                key = dictNames;
                dictNames.RemoveAt(index);
            }
            else
            {
                key = "*";
            }
            return dictNames;
      }
      internal static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, IEnumerable dictNames)
      {
            Database db = dict.Database;
            DBDictionary subdict;
            if (createSubDictionary)
            {
                foreach (string name in dictNames)
                {
                  if (dict.Contains(name))
                  {
                        subdict = dict.GetAt(name).Open();
                        dict.Close();
                        dict.Dispose();
                        dict = subdict;
                  }
                  else
                  {
                        using (dict.UpgradeOpenAndRun())
                        {
                            subdict = new DBDictionary();
                            dict.SetAt(name, subdict);
                            db.AddDBObject(subdict);
                        }
                        dict.Close();
                        dict.Dispose();
                        dict = subdict;
                  }
                }
            }
            else
            {
                foreach (string name in dictNames)
                {
                  if (dict.Contains(name))
                  {
                        subdict = dict.GetAt(name).Open();
                        dict.Close();
                        dict.Dispose();
                        dict = subdict;
                  }
                  else
                        return null;
                }
            }
            return dict;
      }
      public static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, params string[] dictNames)
      {
            return
                GetSubDictionary(
                  dict,
                  createSubDictionary,
                  (IEnumerable)dictNames);
      }
      internal static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, IEnumerable dictNames)
      {
            if (createSubDictionary)
            {
                using (dict.UpgradeOpenAndRun())
                  dict.TreatElementsAsHard = true;
                foreach (string name in dictNames)
                {
                  if (dict.Contains(name))
                  {
                        dict = dict.GetAt(name).GetObject(tr);
                  }
                  else
                  {
                        DBDictionary subDict = new DBDictionary();
                        dict.SetAt(tr, name, subDict);
                        dict = subDict;
                        dict.TreatElementsAsHard = true;
                  }
                }
            }
            else
            {
                foreach (string name in dictNames)
                {
                  if (dict.Contains(name))
                        dict = dict.GetAt(name).GetObject(tr);
                  else
                        return null;
                }
            }
            return dict;
      }
      internal static DBDictionary GetSubDictionary(this DBObject obj, Transaction tr, bool createSubDictionary, IEnumerable dictNames)
      {
            ObjectId id = obj.ExtensionDictionary;
            if (id.IsNull)
            {
                using (obj.UpgradeOpenAndRun())
                {
                  obj.CreateExtensionDictionary();
                }
                id = obj.ExtensionDictionary;
            }
            DBDictionary dict = id.GetObject(tr);
            return
                GetSubDictionary(
                  dict,
                  tr,
                  createSubDictionary,
                  dictNames);
      }
      ///
      /// 获取子字典
      ///
      /// 根字典
      /// 是否创建子字典
      /// 键值列表
      ///
      public static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, params string[] dictNames)
      {
            return
                GetSubDictionary(
                  dict,
                  tr,
                  createSubDictionary,
                  (IEnumerable)dictNames);
      }
      ///
      /// 获取实体字典的子字典
      ///
      /// 图元对象
      /// 是否创建子字典
      /// 键值列表
      ///
      public static DBDictionary GetSubDictionary(this DBObject obj, Transaction tr, bool createSubDictionary, params string[] dictNames)
      {
            return
                GetSubDictionary(
                  obj,
                  tr,
                  createSubDictionary,
                  (IEnumerable)dictNames);
      }
      #endregion    }

sxpd 发表于 2012-7-7 09:58:00

扩展数据应该用XRecord保存在字典

介之推 发表于 2012-7-8 10:49:00


多谢大师指导了啊。哈哈。

lionguns 发表于 2015-10-14 22:05:00

学习!!!!!!!!!!!!!!!!!!
页: [1]
查看完整版本: 如何绘制一条PolyLine,添加扩展属性,并返回这个Polyline的ObjectId