当前位置:   article > 正文

OBJECT ARX 绘制常用图形 总结_objectarx acdbregion图形

objectarx acdbregion图形
  1. 将图形加载显示到CAD===================
  2. static AcDbObjectId LoadEntity(AcDbEntity* entity){
  3. AcDbBlockTable* pBlockTable;
  4. acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable,AcDb::kForRead);
  5. AcDbBlockTableRecord* pBlockTableRecord;
  6. pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
  7. AcDbObjectId Id;
  8. pBlockTableRecord->appendAcDbEntity(Id,entity);
  9. pBlockTable->close();
  10. pBlockTableRecord->close();
  11. entity->close();
  12. return Id;
  13. }
  14. 为图形添加标注
  15. static void AddOrdinate(AcGePoint3d ptStart,AcGePoint3d ptEnd,
  16. CString text){
  17. AcDbOrdinateDimension* pDim = new
  18. AcDbOrdinateDimension(true,ptStart,ptEnd,text);
  19. LoadEntity(pDim);
  20. }
  21. 画线====================
  22. static void TESTlineCmd()
  23. {
  24. //acutPrintf(_T("\nHello"));
  25. ads_point inputStart;
  26. ads_point inputEnd;
  27. AcGePoint3d ptStart;
  28. AcGePoint3d ptEnd;
  29. 用户输入要画的坐标
  30. if(acedGetPoint(NULL,_T("start point"),inputStart) != RTNORM){
  31. return;
  32. }
  33. ptStart[X] = inputStart[X];
  34. ptStart[Y] = inputStart[Y];
  35. ptStart[Z] = inputStart[Z];
  36. if(acedGetPoint(NULL,_T("end point"),inputEnd) != RTNORM){
  37. return;
  38. }
  39. ptEnd[X] = inputEnd[X];
  40. ptEnd[Y] = inputEnd[Y];
  41. ptEnd[Z] = inputEnd[Z];
  42. AcDbLine* pLine = new AcDbLine(ptStart,ptEnd);
  43. pLine->setColorIndex(2);
  44. LoadEntity(pLine);
  45. AddOrdinate(ptStart,ptEnd,_T("hi"));
  46. //AfxMessageBox(_T("命令画了一条线"));
  47. }
  48. 画圆形==========================
  49. static void TESTcirclecmd(){
  50. 垂直平面法向量
  51. AcGeVector3d vec(0,0,1);
  52. 圆心
  53. AcGePoint3d ptCenter(100,100,100);
  54. AcDbCircle* pCircle = new AcDbCircle(ptCenter,vec,1000);
  55. LoadEntity(pCircle);
  56. }
  57. 画圆弧================================
  58. static void TESTarccmd(){
  59. AcGeVector3d vec(0,0,1);
  60. AcGePoint3d ptCenter(500,500,500);
  61. AcDbArc* pArc = new AcDbArc(ptCenter,vec,700,0,90);
  62. LoadEntity(pArc);
  63. }
  64. 画多线段============================
  65. static void TESTpolycmd(){
  66. 构造多点数组
  67. AcGePoint2dArray points;
  68. AcGePoint2d pt1(1000, 1000);
  69. AcGePoint2d pt2(2000, 1800);
  70. AcGePoint2d pt3(500, 2000);
  71. AcGePoint2d pt4(3000, 5000);
  72. points.append(pt1);
  73. points.append(pt2);
  74. points.append(pt3);
  75. points.append(pt4);
  76. 绘制多点线段
  77. AcDbPolyline* pl = new AcDbPolyline(points.length());
  78. for(int i = 0;i < points.length();i ++){
  79. pl->addVertexAt(i,points.at(i),0,0.5,0.5);
  80. }
  81. LoadEntity(pl);
  82. }
  83. 绘制POLY3d线段====================
  84. static void TESTpoly3dcmd(){
  85. 构造多点数组
  86. AcGePoint3dArray points;
  87. AcGePoint3d pt1(10, 10,10);
  88. AcGePoint3d pt2(20, 10,20);
  89. AcGePoint3d pt3(20, 20,30);
  90. points.append(pt1);
  91. points.append(pt2);
  92. points.append(pt3);
  93. 绘制多点线段
  94. AcDb3dPolyline* pl = new AcDb3dPolyline(AcDb::Poly3dType::k3dSimplePoly,points);
  95. LoadEntity(pl);
  96. }
  97. 绘制椭圆=========================
  98. static void TESTellipsecmd(){
  99. 椭圆焦点
  100. /* AcGePoint2d p1(400,400);
  101. AcGePoint2d p2(800,400);*/
  102. 中心点
  103. AcGePoint3d ptCenter(600,400,0);
  104. AcGeVector3d vecNormal(0,0,1);
  105. 长轴
  106. AcGeVector3d majorAxis(1000,0,0);
  107. 通过曲率确定短轴长度
  108. double ratio = 0.6;
  109. AcDbEllipse* ae = new AcDbEllipse(/*AcGePoint3d::kOrigin*/ptCenter,vecNormal,majorAxis,
  110. ratio);
  111. LoadEntity(ae);
  112. }
  113. 创建样条曲线================
  114. static void TESTsplinecmd(){
  115. AcGePoint3d p1(0,0,0),p2(200,600,0),p3(1200,1600,0),
  116. p4(2000,2000,0);
  117. AcGePoint3dArray points;
  118. points.append(p1);
  119. points.append(p2);
  120. points.append(p3);
  121. points.append(p4);
  122. AcDbSpline* as = new AcDbSpline(points);
  123. LoadEntity(as);
  124. }
  125. 创建面域(把用直线创建的闭合图形,生成一个图形的对象)
  126. static void TESTregioncmd(){
  127. ads_name ss;
  128. 提示用户选择对象
  129. int rt = acedSSGet(NULL,NULL,NULL,NULL,ss);
  130. AcDbObjectIdArray ids;
  131. if(rt == RTNORM){
  132. long len;
  133. 获得对象个数
  134. acedSSLength(ss,&len);
  135. for(int i = 0;i < len;i ++){
  136. 先从对象中得到名字
  137. ads_name ent;
  138. acedSSName(ss,i,ent);
  139. 再从名字中取出ID
  140. AcDbObjectId objId;
  141. acdbGetObjectId(objId,ent);
  142. ids.append(objId);
  143. }
  144. }
  145. acedSSFree(ss);
  146. 得到objId数组之后,开始画区域
  147. CreateRegion(ids);
  148. }
  149. 绘制面域
  150. static AcDbObjectIdArray CreateRegion(AcDbObjectIdArray ids){
  151. AcDbObjectIdArray regionIds;
  152. AcDbVoidPtrArray curves;
  153. AcDbVoidPtrArray regions;
  154. AcDbEntity* pEnt;
  155. AcDbRegion* pRegion;
  156. for(int i = 0;i < ids.length();i ++){
  157. acdbOpenAcDbEntity(pEnt,ids.at(i),AcDb::kForRead);
  158. if(pEnt->isKindOf(AcDbCurve::desc())){
  159. curves.append(static_cast<void*>(pEnt));
  160. }
  161. }
  162. Acad::ErrorStatus es = AcDbRegion::createFromCurves(curves,regions);
  163. if(es == Acad::eOk){
  164. for(int i = 0;i < regions.length();i ++){
  165. pRegion = static_cast<AcDbRegion*>(regions[i]);
  166. pRegion->setDatabaseDefaults();
  167. AcDbObjectId regionId = LoadEntity(pRegion);
  168. regionIds.append(regionId);
  169. }
  170. CString tipStr ;
  171. tipStr.Format(_T("\n已成功创建面域:%d个"),regions.length());
  172. acutPrintf(tipStr);
  173. }
  174. else{
  175. for(int i = 0;i < regions.length();i ++){
  176. delete (AcRxObject*)regions[i];
  177. }
  178. }
  179. for(int i = 0;i < ids.length();i ++){
  180. pEnt = static_cast<AcDbEntity*>(curves[i]);
  181. pEnt->close();
  182. }
  183. return regionIds;
  184. }
  185. //绘制文字===================
  186. static void TESTtextcmd(){
  187. AcGePoint3d ptInsert(0,400,0);
  188. AcDbMText* pMText = new AcDbMText();
  189. 设置多行文字特性
  190. pMText->setContents(_T("hello!!"));
  191. pMText->setLocation(ptInsert);
  192. pMText->setTextHeight(1000);
  193. pMText->setWidth(1000);
  194. pMText->setAttachment(AcDbMText::kBottomLeft);
  195. LoadEntity(pMText);
  196. }
  197. 创建填充======================
  198. static void TESThatchcmd(){
  199. 提示用户选择填充边界
  200. ads_name ss;
  201. int rt = acedSSGet(NULL,NULL,NULL,NULL,ss);
  202. AcDbObjectIdArray objIds;
  203. if(rt == RTNORM){
  204. long len;
  205. acedSSLength(ss,&len);
  206. for(int i = 0;i < len;i ++){
  207. ads_name ent;
  208. acedSSName(ss,i,ent);
  209. AcDbObjectId objId;
  210. acdbGetObjectId(objId,ent);
  211. objIds.append(objId);
  212. }
  213. }
  214. acedSSFree(ss);
  215. 选择完毕,绘制填充
  216. Acad::ErrorStatus es;
  217. AcDbHatch* pHatch = new AcDbHatch();
  218. 选择填充平面
  219. AcGeVector3d normal(0,0,1);
  220. pHatch->setNormal(normal);
  221. pHatch->setElevation(0);
  222. 设置关联性
  223. pHatch->setAssociative(true);
  224. 设置填充图案
  225. pHatch->setPattern(AcDbHatch::kPreDefined,_T(/*"SOLID"*/"ANSI31"));
  226. 设置填充角度
  227. pHatch->setPatternAngle(45);
  228. 设置填充边界
  229. es=pHatch->appendLoop(AcDbHatch::kExternal,objIds);
  230. 显示填充对象
  231. es=pHatch->evaluateHatch();
  232. LoadEntity(pHatch);
  233. }
  234. 交互函数练习==================
  235. static void TESTinputcmd(){
  236. CString outStr;
  237. //acedGetReal
  238. /*ads_real realNum;
  239. acedGetReal(_T("输入一个实数"),&realNum);
  240. outStr.Format(_T("%lf"),realNum);
  241. acutPrintf(outStr);*/
  242. //acedGetInt
  243. /*int iNum;
  244. acedGetInt(_T("输入一个整数"),&iNum);
  245. outStr.Format(_T("%d"),iNum);
  246. acutPrintf(outStr);*/
  247. //acedGetString
  248. /*TCHAR str[256];
  249. acedGetString(0,_T("输入一个字符串"),str);
  250. acutPrintf(str);*/
  251. //acedGetKword
  252. /* TCHAR kw[256];
  253. acedGetKword(_T("输入一个关键字"),kw);
  254. acutPrintf(kw);*/
  255. //acedGetPoint
  256. /* ads_point pt;
  257. acedGetPoint(NULL,_T("输入一个点"),pt);
  258. outStr.Format(_T("x:%f,y:%f"),pt[X],pt[Y]);
  259. acutPrintf(outStr);*/
  260. //acedGetCorner(要与ads_point联合使用,完成画矩形)
  261. /*ads_point ptCorner;
  262. acedGetCorner(pt,_T("输入对角点"),ptCorner);
  263. outStr.Format(_T("x:%f,y:%f"),ptCorner[X],ptCorner[Y]);
  264. acutPrintf(outStr);*/
  265. //acedGetDist(和ads_point结合使用,实现画线)
  266. /*ads_real ptReal;
  267. acedGetDist(pt,_T("输入目标值"),&ptReal);
  268. outStr.Format(_T("%lf"),ptReal);
  269. acutPrintf(outStr);*/
  270. //acedGetAngle(和ads_point结合使用,实现画线)
  271. /* ads_real agReal;
  272. acedGetAngle(pt,_T("输入一个角度"),&agReal);
  273. outStr.Format(_T("%lf"),agReal);
  274. acutPrintf(outStr);*/
  275. }
  276. //获得一层的所有对象,变色===============
  277. static void TESTgetlayerobjcmd(){
  278. AcDbLayerTable *pLayerTbl;
  279. acdbHostApplicationServices()->workingDatabase()->getSymbolTable(
  280. pLayerTbl,AcDb::kForRead);
  281. if(!pLayerTbl->has(_T("测试"))){
  282. acutPrintf(_T("\n当前图形未包含'测试'图层"));
  283. pLayerTbl->close();
  284. return;
  285. }
  286. AcDbObjectId layerId;
  287. pLayerTbl->getAt(_T("测试"),layerId);
  288. pLayerTbl->close();
  289. 获得当前数据库的块表
  290. AcDbBlockTable* pBlkTbl;
  291. acdbHostApplicationServices()->workingDatabase()->getBlockTable(
  292. pBlkTbl,AcDb::kForRead);
  293. 获得模型空间的块表记录
  294. AcDbBlockTableRecord* pBlkTblRcd;
  295. pBlkTbl->getAt(ACDB_MODEL_SPACE,pBlkTblRcd,AcDb::kForRead);
  296. pBlkTbl->close();
  297. 创建块表记录遍历器
  298. AcDbBlockTableRecordIterator* pIter;
  299. pBlkTblRcd->newIterator(pIter);
  300. AcDbEntity* pEnt;
  301. for(pIter->start();!pIter->done();pIter->step()){
  302. pIter->getEntity(pEnt,AcDb::kForWrite);
  303. if(pEnt->layerId() == layerId){
  304. /*AcDbLine* pLine = AcDbLine::cast(pEnt);
  305. if(pLine != NULL){*/
  306. pEnt->setColorIndex(1);
  307. //}
  308. }
  309. pEnt->close();
  310. }
  311. delete pIter;
  312. pBlkTblRcd->close();
  313. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/114409
推荐阅读
相关标签
  

闽ICP备14008679号