乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 94|回复: 4

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

[复制链接]

8

主题

12

帖子

1

银币

初来乍到

Rank: 1

铜币
44
发表于 2012-7-7 09:38:00 | 显示全部楼层 |阅读模式
大家好。我想绘制一个矩形框,然后给这个矩形框添加一个名称和标记的扩展数据。然后返回这个矩形框的ObjectId。扩展数据使用DataTable。在绘制以后在通过这个矩形的ObjectId重新获得这个矩形的名称和标记。如何做到?下面是我写的一部分代码,没有完成,请大家帮忙了。
  1. '通过用户指定点后再绘制这个矩形,矩形的长和宽也作为参数
  2. Public Shared Function AddRectangle(ByVal cenPt As Point3d, ByVal Height As Double, _
  3.                                         ByVal Length As Double) As ObjectId
  4.         Dim Pt(4) As Point3d
  5.         Pt(0) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y - Height * 0.5, 0)
  6.         Pt(1) = New Point3d(cenPt.X + Length * 0.5, cenPt.Y - Height * 0.5, 0)
  7.         Pt(2) = New Point3d(cenPt.X + Length * 0.5, cenPt.Y + Height * 0.5, 0)
  8.         Pt(3) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y + Height * 0.5, 0)
  9.         Pt(4) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y - Height * 0.5, 0)
  10.         Dim Pts As New Point3dCollection(Pt)
  11.         ' 在内存中创建一个未经拟合的标准三维多段线对象.
  12.         Dim ent As New Polyline3d(Poly3dType.SimplePoly, Pts, False)
  13.         'Add the rectangle polyline entity to model space
  14.         Dim acEntId As ObjectId = AppendEntity(ent)
  15.         Dim dt As DataTable = New DataTable()
  16.         dt.TableName = "属性表"
  17.         dt.AppendColumn(CellType.CharPtr, "Name")
  18.         dt.AppendColumn(CellType.CharPtr, "Tag")
  19.         Dim row As DataCellCollection = New DataCellCollection()
  20.         Dim Name As DataCell = New DataCell()
  21.         Dim Tag As DataCell = New DataCell()
  22.         Name.SetString("矩形框")
  23.         Tag.SetString("矩形标记")
  24.         row.Add(Name)
  25.         row.Add(Tag)
  26.         dt.AppendRow(row, True)
  27.         ent.CreateExtensionDictionary()
  28.         Dim exDic As DBDictionary = New DBDictionary()  '如何将扩展数据记录下来啊?
  29.         exDic.SetAt("属性表", dt)
  30.         '返回ObjectId
  31.         Return acEntId
  32.     End Function
  33. ' 将图形对象加入到模型空间的函数.
  34.     Public Shared Function AppendEntity(ByVal ent As Entity) As ObjectId
  35.         ' 得到当前文档图形数据库.
  36.         Dim db As Database = HostApplicationServices.WorkingDatabase
  37.         Dim entId As ObjectId
  38.         Using trans As Transaction = db.TransactionManager.StartTransaction
  39.             ' 以读方式打开块表.
  40.             Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
  41.             ' 以写方式打开模型空间块表记录.
  42.             Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
  43.             ' 将图形对象的信息添加到块表记录中,并返回ObjectId对象.
  44.             entId = btr.AppendEntity(ent)
  45.             ' 把图形对象添加到事务处理中.
  46.             trans.AddNewlyCreatedDBObject(ent, True)
  47.             ' 提交事务处理.
  48.             trans.Commit()
  49.         End Using
  50.         Return entId
  51.     End Function
回复

使用道具 举报

0

主题

37

帖子

2

银币

初来乍到

Rank: 1

铜币
37
发表于 2012-7-7 09:54:00 | 显示全部楼层

用XDATA不是更简单
你的扩展数据用法有问题
贴个相关的程序你看看
  1.     public static class DBDictionaryEx
  2.     {
  3.         public static IEnumerable GetAllObjects(this DBDictionary dict, Transaction tr) where T : DBObject
  4.         {
  5.             foreach (DBDictionaryEntry e in dict)
  6.             {
  7.                 yield return
  8.                     tr.GetObject(e.Value, OpenMode.ForRead) as T;
  9.             }
  10.         }
  11.         
  12.         public static T GetAt(this DBDictionary dict, Transaction tr, string key) where T : DBObject
  13.         {
  14.             if (dict.Contains(key))
  15.             {
  16.                 ObjectId id = dict.GetAt(key);
  17.                 if (!id.IsNull)
  18.                 {
  19.                     return tr.GetObject(id, OpenMode.ForRead) as T;
  20.                 }
  21.             }
  22.             return null;
  23.         }
  24.         public static void SetAt(this DBDictionary dict, Transaction tr, string key, T obj) where T : DBObject
  25.         {
  26.             using (dict.UpgradeOpenAndRun())
  27.             {
  28.                 dict.SetAt(key, obj);
  29.                 tr.AddNewlyCreatedDBObject(obj, true);
  30.             }
  31.         }
  32.         #region GetSubDictionary
  33.         internal static List GetDictNames(string[] keys, out string key)
  34.         {
  35.             List dictNames = new List(keys);
  36.             if (dictNames.Count > 0)
  37.             {
  38.                 int index = dictNames.Count - 1;
  39.                 key = dictNames[index];
  40.                 dictNames.RemoveAt(index);
  41.             }
  42.             else
  43.             {
  44.                 key = "*";
  45.             }
  46.             return dictNames;
  47.         }
  48.         internal static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, IEnumerable dictNames)
  49.         {
  50.             Database db = dict.Database;
  51.             DBDictionary subdict;
  52.             if (createSubDictionary)
  53.             {
  54.                 foreach (string name in dictNames)
  55.                 {
  56.                     if (dict.Contains(name))
  57.                     {
  58.                         subdict = dict.GetAt(name).Open();
  59.                         dict.Close();
  60.                         dict.Dispose();
  61.                         dict = subdict;
  62.                     }
  63.                     else
  64.                     {
  65.                         using (dict.UpgradeOpenAndRun())
  66.                         {
  67.                             subdict = new DBDictionary();
  68.                             dict.SetAt(name, subdict);
  69.                             db.AddDBObject(subdict);
  70.                         }
  71.                         dict.Close();
  72.                         dict.Dispose();
  73.                         dict = subdict;
  74.                     }
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 foreach (string name in dictNames)
  80.                 {
  81.                     if (dict.Contains(name))
  82.                     {
  83.                         subdict = dict.GetAt(name).Open();
  84.                         dict.Close();
  85.                         dict.Dispose();
  86.                         dict = subdict;
  87.                     }
  88.                     else
  89.                         return null;
  90.                 }
  91.             }
  92.             return dict;
  93.         }
  94.         public static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, params string[] dictNames)
  95.         {
  96.             return
  97.                 GetSubDictionary(
  98.                     dict,
  99.                     createSubDictionary,
  100.                     (IEnumerable)dictNames);
  101.         }
  102.         internal static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, IEnumerable dictNames)
  103.         {
  104.             if (createSubDictionary)
  105.             {
  106.                 using (dict.UpgradeOpenAndRun())
  107.                     dict.TreatElementsAsHard = true;
  108.                 foreach (string name in dictNames)
  109.                 {
  110.                     if (dict.Contains(name))
  111.                     {
  112.                         dict = dict.GetAt(name).GetObject(tr);
  113.                     }
  114.                     else
  115.                     {
  116.                         DBDictionary subDict = new DBDictionary();
  117.                         dict.SetAt(tr, name, subDict);
  118.                         dict = subDict;
  119.                         dict.TreatElementsAsHard = true;
  120.                     }
  121.                 }
  122.             }
  123.             else
  124.             {
  125.                 foreach (string name in dictNames)
  126.                 {
  127.                     if (dict.Contains(name))
  128.                         dict = dict.GetAt(name).GetObject(tr);
  129.                     else
  130.                         return null;
  131.                 }
  132.             }
  133.             return dict;
  134.         }
  135.         internal static DBDictionary GetSubDictionary(this DBObject obj, Transaction tr, bool createSubDictionary, IEnumerable dictNames)
  136.         {
  137.             ObjectId id = obj.ExtensionDictionary;
  138.             if (id.IsNull)
  139.             {
  140.                 using (obj.UpgradeOpenAndRun())
  141.                 {
  142.                     obj.CreateExtensionDictionary();
  143.                 }
  144.                 id = obj.ExtensionDictionary;
  145.             }
  146.             DBDictionary dict = id.GetObject(tr);
  147.             return
  148.                 GetSubDictionary(
  149.                     dict,
  150.                     tr,
  151.                     createSubDictionary,
  152.                     dictNames);
  153.         }
  154.         ///
  155.         /// 获取子字典
  156.         ///
  157.         /// 根字典
  158.         /// 是否创建子字典
  159.         /// 键值列表
  160.         ///
  161.         public static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, params string[] dictNames)
  162.         {
  163.             return
  164.                 GetSubDictionary(
  165.                     dict,
  166.                     tr,
  167.                     createSubDictionary,
  168.                     (IEnumerable)dictNames);
  169.         }
  170.         ///
  171.         /// 获取实体字典的子字典
  172.         ///
  173.         /// 图元对象
  174.         /// 是否创建子字典
  175.         /// 键值列表
  176.         ///
  177.         public static DBDictionary GetSubDictionary(this DBObject obj, Transaction tr, bool createSubDictionary, params string[] dictNames)
  178.         {
  179.             return
  180.                 GetSubDictionary(
  181.                     obj,
  182.                     tr,
  183.                     createSubDictionary,
  184.                     (IEnumerable)dictNames);
  185.         }
  186.         #endregion    }

回复

使用道具 举报

0

主题

37

帖子

2

银币

初来乍到

Rank: 1

铜币
37
发表于 2012-7-7 09:58:00 | 显示全部楼层
扩展数据应该用XRecord保存在字典
回复

使用道具 举报

8

主题

12

帖子

1

银币

初来乍到

Rank: 1

铜币
44
发表于 2012-7-8 10:49:00 | 显示全部楼层

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

使用道具 举报

9

主题

30

帖子

5

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
66
发表于 2015-10-14 22:05:00 | 显示全部楼层
学习!!!!!!!!!!!!!!!!!!
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-14 13:24 , Processed in 0.560297 second(s), 62 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表