找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1360|回复: 0

[分享] Converting Geometry objects to Database entity

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-26 23:33:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Converting Geometry objects to Database entity                                                        By Balaji Ramamoorthy
Most often, you will work with geometry objects (AcGeXXXX classes) to perform common 2D and 3D geometric operations. To convert them to an entity that can be appended to the AutoCAD database, there is an ObjectARX / AutoCAD .Net API method that is very handy.
The "acdbConvertGelibCurveToAcDbCurve" is a method that was introduced in AutoCAD 2012 which can do this. The equivalent of this method in the AutoCAD .Net API was introduced in AutoCAD 2013.
Here is an example demonstrating the use of the method in ObjectARX / AutoCAD .Net API :
  1. // This sample uses "acdbConvertGelibCurveToAcDbCurve"
  2. // Other methods that you might be interested in :
  3. // AcDbCurve::getAcGeCurve()
  4. // AcDbCurve::setFromAcGeCurve()

  5. static void AdskMyTest1_Ge2Db(void)
  6. {
  7.     AcGeEllipArc3d ellipticArc(AcGePoint3d::kOrigin,
  8.         AcGeVector3d::kXAxis,
  9.         AcGeVector3d::kYAxis,
  10.         2.0,
  11.         0.5);
  12.     GetCurveObjectId(ellipticArc);
  13. }

  14. static AcDbObjectId GetCurveObjectId(AcGeCurve3d &geCurve3d)
  15. {
  16.     AcDbObjectId oid = AcDbObjectId::kNull;
  17.     Acad::ErrorStatus es = Acad::eOk;
  18.     AcDbCurve *pDbCurve = NULL;
  19.     es = acdbConvertGelibCurveToAcDbCurve(geCurve3d, pDbCurve);
  20.     if(es == Acad::eOk)
  21.     {
  22.     postToDb(pDbCurve, oid);
  23.     }
  24.     return oid;
  25. }

  26. static Acad::ErrorStatus postToDb(
  27. AcDbEntity* pEnt, AcDbObjectId &oid)
  28. {
  29.     AcDbBlockTable* pBlockTable;
  30.     AcDbDatabase *pDb
  31.     = acdbHostApplicationServices()->workingDatabase();
  32.     pDb = ->getBlockTable(pBlockTable, AcDb::kForRead);
  33.     AcDbBlockTableRecord* pModelSpaceBTR = NULL;
  34.     pBlockTable->getAt(ACDB_MODEL_SPACE,
  35.     pModelSpaceBTR,
  36.     AcDb::kForWrite);

  37.     Acad::ErrorStatus es;
  38.     es = pModelSpaceBTR->appendAcDbEntity(oid, pEnt);
  39.     pEnt->close();
  40.     pModelSpaceBTR->close();
  41.     pBlockTable->close();
  42.     return es;
  43. }

  1. using Autodesk.AutoCAD.Geometry;
  2. [CommandMethod("ge2dbnet")]
  3. public void Ge2DbMethod()
  4. {
  5.      EllipticalArc3d arc1 =
  6.          new EllipticalArc3d(Point3d.Origin,
  7.      Vector3d.XAxis,
  8.      Vector3d.YAxis,
  9.      2.0, 0.5);
  10.     GetCurveObjectId(arc1);
  11. }

  12. static ObjectId GetCurveObjectId(Curve3d geCurve3d)
  13. {
  14.     Document doc  = Application.DocumentManager.MdiActiveDocument;
  15.     Editor ed = doc.Editor;
  16.     Database db = doc.Database;
  17.     ObjectId oid = ObjectId.Null;
  18.     try
  19.     {
  20.         Curve dbCurve = Curve.CreateFromGeCurve(geCurve3d);
  21.         if (dbCurve != null)
  22.         {
  23.             using (Transaction tr =
  24.                 db.TransactionManager.StartTransaction())
  25.                 {
  26.                     BlockTable bt = tr.GetObject(db.BlockTableId,
  27.                         OpenMode.ForRead) as BlockTable;
  28.                 BlockTableRecord btr =
  29.                     tr.GetObject(db.CurrentSpaceId,
  30.                     OpenMode.ForWrite) as BlockTableRecord;
  31.                 btr.AppendEntity(dbCurve);
  32.                 tr.AddNewlyCreatedDBObject(dbCurve, true);
  33.                 tr.Commit();
  34.                 }
  35.          }
  36.     }
  37.     catch (System.Exception ex)
  38.     {
  39.         ed.WriteMessage(ex.Message);
  40.     }
  41.     return oid;
  42. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-22 22:47 , Processed in 0.366474 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表