明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2681|回复: 4

哪位大侠帮我弄成CAD可执行的程序,谢谢!

[复制链接]
发表于 2012-9-27 11:21:19 | 显示全部楼层 |阅读模式
本帖最后由 shuaier 于 2012-9-27 11:48 编辑

编好后发到我的邮箱414528096@qq.com,非常感谢!
  1. static AcDbObjectId LoadEntity(AcDbEntity* entity)
  2. {


  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. static void SetLineWeight(double& weight,bool& isCancel)
  15. {
  16. CString str;
  17. str.Format(_T("\n禁止开口带线宽: <%.2f>"),weight);


  18. acedInitGet(RSG_NONEG+RSG_NOZERO,NULL);
  19. int ret = acedGetReal(str,&weight);
  20. if(ret == RTCAN)
  21. {
  22. isCancel = true;
  23. }
  24. else
  25. {
  26. isCancel = false;
  27. }
  28. }

  29. static void TESTcornercmd()
  30. {
  31. //设置线宽-----------------------------------
  32. bool isCan = false;
  33. ads_real lineWeight = DEFAULT_WEIGHT;
  34. SetLineWeight(lineWeight,isCan);
  35. if(isCan)
  36. {
  37. return;
  38. }


  39. //选择一点-----------------------------------


  40. ads_point ptCorner;
  41. while(true)
  42. {
  43. acedInitGet(RSG_NONULL,_T("S"));
  44. int retPt = acedGetPoint(NULL,_T("\n点选地块转角[设置线宽(S)]:"),ptCorner);
  45. if(retPt == RTNORM)
  46. {
  47. break;
  48. }
  49. else if(retPt == RTKWORD)
  50. {
  51. CString strInput;
  52. acedGetInput(strInput.GetBuffer(10));
  53. strInput.ReleaseBuffer();
  54. if(strInput.CompareNoCase(_T("S")) == 0)
  55. {
  56. SetLineWeight(lineWeight,isCan);
  57. if(isCan)
  58. {
  59. return;
  60. }
  61. }
  62. }
  63. else
  64. {
  65. return;
  66. }
  67. }
  68. AcGePoint3d ptCorner3d = asPnt3d(ptCorner);


  69. //设置过滤器--------------------------------------
  70. resbuf* strFilter = NULL;
  71. strFilter = acutBuildList(-4,_T("<and"),
  72. -4,_T("<or"),
  73. 8,_T("C1"),//C1层
  74. 8,_T("C2"),//C2层
  75. 8,_T("C3"),//C3层
  76. 8,_T("R2"),//R2层
  77. -4,_T("or>"),
  78. RTDXF0,_T("LWPOLYLINE"),///多段线
  79. -4,_T("and>"),
  80. RTNONE);


  81. ads_name ssName;
  82. if(acedSSGet(_T("X"),NULL,NULL,strFilter,ssName) != RTNORM)
  83. {
  84. acutPrintf(_T("\n未找到符合条件的图形"));
  85. acutRelRb(strFilter);
  86. return;
  87. }


  88. long len = 0;
  89. acedSSLength(ssName,&len);
  90. if(len <=0)
  91. {
  92. acutPrintf(_T("\n图形数量是0"));
  93. acedSSFree(ssName);
  94. acutRelRb(strFilter);
  95. return;
  96. }


  97. //遍历选择集,找到距离点击位置最近的线段,计算出相邻线段的单位向量----------------------------------
  98. ads_name en;
  99. AcDbObjectId eId;
  100. AcDbPolyline* plMin = NULL;
  101. int plIndex = 0;
  102. AcDbPolyline::SegType segType;


  103. AcGePoint3d ptClosest;
  104. AcGePoint3d ptMinClose;
  105. double minDist = 0.0;


  106. for(int i = 0;i < len;i++)
  107. {


  108. int rName = acedSSName(ssName,i,en);
  109. if(rName != RTNORM)
  110. {
  111. acutPrintf(_T("\n获取名称失败"));
  112. continue;
  113. }


  114. Acad::ErrorStatus es = acdbGetObjectId(eId,en);
  115. if(es!=Acad::eOk)
  116. {
  117. acutPrintf(_T("\n获取ID失败"));
  118. continue;
  119. }
  120. AcDbEntity* pEnt = NULL;

  121. es = acdbOpenObject(pEnt,eId,AcDb::OpenMode::kForRead);
  122. if(es != Acad::eOk)
  123. {
  124. acutPrintf(_T("\n打开实体失败"));
  125. continue;
  126. }


  127. if(!pEnt->isKindOf(AcDbPolyline::desc()))
  128. {
  129. acutPrintf(_T("\n第%d个实体不是多段线"),i);
  130. pEnt->close();
  131. continue;
  132. }


  133. AcDbPolyline* pPoly = NULL;
  134. pPoly = (AcDbPolyline*)pEnt;


  135. es = pPoly->getClosestPointTo(ptCorner3d,ptClosest);
  136. if(es != Acad::eOk)
  137. {
  138. acutPrintf(_T("\n获取最小距离点出错"));
  139. pEnt->close();
  140. continue;
  141. }


  142. double minParam = 0.0;
  143. es = pPoly->getParamAtPoint(ptClosest,minParam);
  144. if(es != Acad::eOk)
  145. {
  146. pEnt->close();
  147. continue;
  148. }


  149. double dist = ptCorner3d.distanceTo(ptClosest);
  150. if(i == 0)
  151. {
  152. minDist = dist;
  153. }

  154. else if(dist >= minDist && i > 0)
  155. {
  156. pEnt->close();
  157. continue;
  158. }


  159. minDist = dist;
  160. AcDbPolyline::SegType st = pPoly->segType(minParam);
  161. if(st == AcDbPolyline::SegType::kArc)
  162. {
  163. AcGeCircArc3d arc3d;
  164. pPoly->getArcSegAt(minParam,arc3d);


  165. segType = st;
  166. plMin = AcDbPolyline::cast(pPoly->clone());
  167. plIndex = minParam;
  168. ptMinClose = ptClosest;


  169. }
  170. else if(st == AcDbPolyline::SegType::kLine)
  171. {
  172. AcGeLineSeg3d line3d;
  173. pPoly->getLineSegAt(minParam,line3d);


  174. segType = st;
  175. plMin = AcDbPolyline::cast(pPoly->clone());
  176. plIndex = minParam;
  177. ptMinClose = ptClosest;


  178. }
  179. pEnt->close();
  180. }


  181. //释放资源-------------------------------------------
  182. //pEnt->close();
  183. acutRelRb(strFilter);
  184. acedSSFree(ssName);


  185. //判断最小距离是否超出了系统允许的最大值
  186. if(minDist >MINDISTANCE)
  187. {
  188. acutPrintf(_T("\n最小距离至少为%.2f"),MINDISTANCE);
  189. delete plMin;
  190. return;
  191. }


  192. //绘制转角线段-------------------------------------------------
  193. AcDbPolyline * pLine = new AcDbPolyline();

  194. ////线段
  195. if(segType == AcDbPolyline::SegType::kLine)
  196. {
  197. AcGeLineSeg2d line2d;
  198. plMin->getLineSegAt(plIndex,line2d);


  199. pLine->addVertexAt(0,line2d.startPoint());
  200. pLine->addVertexAt(1,line2d.endPoint());
  201. }
  202. ////弧
  203. else if(segType == AcDbPolyline::SegType::kArc)
  204. {
  205. AcGeCircArc2d arc2d;
  206. plMin->getArcSegAt(plIndex,arc2d);
  207. double bulge = 0.0;
  208. plMin->getBulgeAt(plIndex,bulge);


  209. pLine->addVertexAt(0,arc2d.startPoint(),bulge);
  210. pLine->addVertexAt(1,arc2d.endPoint());
  211. }


  212. Acad::ErrorStatus es = pLine->setLayer(strLayer);
  213. if(Acad::eOk != es)
  214. {
  215. acutPrintf(_T("\n设置层名称出错"));
  216. }


  217. es = pLine->setConstantWidth(lineWeight);
  218. if(Acad::eOk != es)
  219. {
  220. acutPrintf(_T("\n设置曲线宽度出错"));
  221. }


  222. AcDbObjectIdArray idArr;
  223. AcDbObjectId idLine = LoadEntity(pLine);
  224. idArr.append(idLine);////记录ObjId


  225. //绘制禁止开口两边-------------------------


  226. ////第一个邻边
  227. //--------------箭头-----------------------

  228. AcGePoint3d pt3dS;
  229. AcGePoint3d pt3dE;
  230. AcGePoint2d pt2dS;
  231. AcGePoint2d pt2dE;

  232. bool gotonext = true;
  233. if(segType == AcDbPolyline::SegType::kArc)
  234. {
  235. AcGeCircArc3d arc3d;
  236. plMin->getArcSegAt(plIndex,arc3d);
  237. AcGeCircArc2d arc2d;
  238. plMin->getArcSegAt(plIndex,arc2d);
  239. pt3dS = arc3d.startPoint();
  240. pt3dE = arc3d.endPoint();
  241. pt2dS = arc2d.startPoint();
  242. pt2dE = arc2d.endPoint();


  243. }
  244. else
  245. {
  246. AcGeLineSeg3d line3d;
  247. plMin->getLineSegAt(plIndex,line3d);
  248. AcGeLineSeg2d line2d;
  249. plMin->getLineSegAt(plIndex,line2d);


  250. pt3dS = line3d.startPoint();
  251. pt3dE = line3d.endPoint();
  252. pt2dS = line2d.startPoint();
  253. pt2dE = line2d.endPoint();
  254. }


  255. AcDbObjectId idPointer1;
  256. DrawPointer(plMin,!gotonext,plIndex,pt3dS,idPointer1);


  257. ads_real dis=DEFAULT_LEN;


  258. acedInitGet(RSG_NOZERO+RSG_NONEG,_T("P"));
  259. CString strDis ;
  260. strDis.Format(_T("\n输入该方向的禁止开口长度[从点击位置算起(P)]<%.2f>:"),dis);
  261. int ret = acedGetReal(strDis,&dis);


  262. int polyIndex = 0;
  263. if(ret == RTCAN)
  264. {
  265. EraseIds(idArr);
  266. RemoveEntity(idPointer1);
  267. delete plMin;
  268. return;
  269. }

  270. else if(ret == RTKWORD)
  271. {
  272. CString str;
  273. acedGetInput(str.GetBuffer(2));
  274. str.ReleaseBuffer();
  275. double length = 0.0;
  276. EraseIds(idArr);
  277. if(str.CompareNoCase(_T("P")) == 0)
  278. {
  279. AcDbObjectIdArray pIdArr;


  280. strDis.Format(_T("\n输入该方向的禁止开口长度<%.2f>:"),dis);
  281. acedInitGet(RSG_NOZERO+RSG_NONEG,NULL);
  282. ret = acedGetReal(strDis,&dis);
  283. if(ret == RTCAN)
  284. {
  285. RemoveEntity(idPointer1);
  286. delete plMin;
  287. return;
  288. }

  289. double disS = 0.0;
  290. double disE = 0.0;
  291. double disC = 0.0;
  292. plMin->getDistAtPoint(ptMinClose,disC);
  293. plMin->getDistAtPoint(pt3dS,disS);
  294. plMin->getDistAtPoint(pt3dE,disE);


  295. AcGePoint2d ptClose2d;
  296. Pt3dTo2d(ptMinClose,ptClose2d);
  297. double curBulge = 0.0;
  298. plMin->getBulgeAt(plIndex,curBulge);


  299. ////向前面画
  300. length = abs(disS - disC);

  301. AcDbPolyline* pl1 = new AcDbPolyline();
  302. pl1->setConstantWidth(lineWeight);
  303. pl1->setLayer(strLayer);
  304. int pl1Index = 0;

  305. StartWithP(plMin,plIndex,dis,length,segType,ptClose2d,!gotonext,pl1,pl1Index);


  306. AcDbObjectId idPl1 = LoadEntity(pl1);
  307. pIdArr.append(idPl1);
  308. ////移除第1个箭头
  309. RemoveEntity(idPointer1);


  310. ////--------------------向后面画--先画箭头------------------------
  311. AcDbObjectId idPointer2;
  312. DrawPointer(plMin,gotonext,plIndex,pt3dE,idPointer2);

  313. int count = plMin->numVerts();


  314. AcDbPolyline* pl2 = new AcDbPolyline();
  315. pl2->setConstantWidth(lineWeight);
  316. pl2->setLayer(strLayer);
  317. int pl2Index = 0;
  318. if(plIndex == count - 1)
  319. {
  320. double total = 0.0;
  321. double endParam = 0.0;
  322. plMin->getEndParam(endParam);
  323. plMin->getDistAtParam(endParam,total);
  324. disE = total;
  325. }
  326. length = abs(disC - disE);


  327. strDis.Format(_T("\n输入该方向的禁止开口长度<%.2f>:"),dis);
  328. acedInitGet(RSG_NOZERO+RSG_NONEG,NULL);
  329. ret = acedGetReal(strDis,&dis);
  330. if(ret == RTCAN)
  331. {
  332. delete plMin;
  333. RemoveEntity(idPointer2);
  334. EraseIds(pIdArr);
  335. return;
  336. }

  337. StartWithP(plMin,plIndex,dis,length,segType,ptClose2d,gotonext,pl2,pl2Index);


  338. AcDbObjectId idPl2 = LoadEntity(pl2);
  339. pIdArr.append(idPl2);

  340. ////移除第二个箭头
  341. RemoveEntity(idPointer2);



  342. AcDbPolyline* plAll = new AcDbPolyline();
  343. plAll->setConstantWidth(lineWeight);
  344. plAll->setLayer(strLayer);

  345. int plAllIndex = 0;
  346. AppendPLinePoint(idPl1,!gotonext,plAll,plAllIndex);
  347. AppendPLinePoint(idPl2,gotonext,plAll,plAllIndex);
  348. LoadEntity(plAll);


  349. EraseIds(pIdArr);
  350. }


  351. delete plMin;
  352. return;
  353. }
  354. //不是点击位置开始的情况------------------------------------



  355. //第一条边

  356. AcDbPolyline* pLine1 = new AcDbPolyline();
  357. pLine1->setConstantWidth(lineWeight);
  358. pLine1->setLayer(strLayer);
  359. int pLine1Index = 0;
  360. AcGePoint2d ptNextS;
  361. AcGePoint2d ptNextE;
  362. GetNextPt(plMin,!gotonext,plIndex,ptNextS,ptNextE);


  363. DrawByLen(!gotonext,ptNextE,ptNextS,dis,plMin,pLine1,pLine1Index);

  364. AcDbObjectId idLine1 = LoadEntity(pLine1);
  365. idArr.append(idLine1);
  366. RemoveEntity(idPointer1);

  367. //第二条边

  368. strDis.Format(_T("\n输入该方向的禁止开口长度<%.2f>:"),dis);
  369. acedInitGet(RSG_NOZERO+RSG_NONEG,NULL);
  370. ret = acedGetReal(strDis,&dis);
  371. if(ret == RTCAN)
  372. {
  373. delete plMin;
  374. EraseIds(idArr);
  375. return;
  376. }


  377. AcDbObjectId idPointer2;
  378. DrawPointer(plMin,gotonext,plIndex,pt3dE,idPointer2);


  379. AcDbPolyline* pLine2 = new AcDbPolyline();
  380. pLine2->setConstantWidth(lineWeight);
  381. pLine2->setLayer(strLayer);


  382. int pLine2Index = 0;
  383. GetNextPt(plMin,gotonext,plIndex,ptNextS,ptNextE);
  384. DrawByLen(gotonext,ptNextS,ptNextE,dis,plMin,pLine2,pLine2Index);
  385. AcDbObjectId idLine2 = LoadEntity(pLine2);


  386. idArr.append(idLine2);
  387. RemoveEntity(idPointer2);
  388. delete plMin;


  389. //合并多段线----------------------------------------
  390. AcDbPolyline* pl = new AcDbPolyline();
  391. pl->setConstantWidth(lineWeight);
  392. pl->setLayer(strLayer);
  393. int plIndex1 = 0;
  394. AppendPLinePoint(idLine1,false,pl,plIndex1);
  395. AppendPLinePoint(idLine,true,pl,plIndex1);
  396. AppendPLinePoint(idLine2,true,pl,plIndex1);


  397. LoadEntity(pl);


  398. //////移除之前画上去的3条多段线
  399. EraseIds(idArr);

  400. }


  401. static void StartWithP(const AcDbPolyline* plMin,const int& plIndex,const double& dis,
  402. const double& length,const AcDbPolyline::SegType& segType,const AcGePoint2d& ptClose2d,const bool& gotonext,AcDbPolyline* pl1,int& pl1Index)
  403. {
  404. AcGePoint2d ptS;
  405. AcGePoint2d ptE;
  406. double curBulge = 0.0;
  407. plMin->getBulgeAt(plIndex,curBulge);
  408. double thisLen = 0.0;


  409. if(segType == AcDbPolyline::SegType::kArc)
  410. {
  411. AcGeCircArc2d arc2d;
  412. plMin->getArcSegAt(plIndex,arc2d);
  413. ptS = arc2d.startPoint();
  414. ptE = arc2d.endPoint();
  415. thisLen = arc2d.length(arc2d.paramOf(ptS),arc2d.paramOf(ptE));
  416. }
  417. else
  418. {
  419. AcGeLineSeg2d line2d;
  420. plMin->getLineSegAt(plIndex,line2d);
  421. ptS = line2d.startPoint();
  422. ptE = line2d.endPoint();
  423. }




  424. if(dis > length)
  425. {
  426. double bulge1 = 0.0;
  427. if(segType == AcDbPolyline::SegType::kArc)
  428. {
  429. bulge1 = tan(atan(curBulge) * length / thisLen);
  430. }

  431. if(!gotonext)
  432. {
  433. pl1->addVertexAt(pl1Index,ptClose2d,-bulge1);
  434. pl1Index++;
  435. pl1->addVertexAt(pl1Index,ptS);
  436. pl1Index ++;
  437. }
  438. else
  439. {
  440. pl1->addVertexAt(pl1Index,ptClose2d,bulge1);
  441. pl1Index++;
  442. pl1->addVertexAt(pl1Index,ptE);
  443. pl1Index ++;
  444. }


  445. AcGePoint2d ptNextS;
  446. AcGePoint2d ptNextE;
  447. GetNextPt(plMin,gotonext,plIndex,ptNextS,ptNextE);


  448. DrawByLen(gotonext,ptNextS,ptNextE,dis-length,plMin,pl1,pl1Index);
  449. }
  450. else
  451. {
  452. AcGePoint2d ptEnding;
  453. if(segType == AcDbPolyline::SegType::kArc)
  454. {
  455. AcGeCircArc2d arc2d;
  456. plMin->getArcSegAt(plIndex,arc2d);
  457. if(!gotonext)
  458. {
  459. GetPtAtDistOnCurve(&arc2d,ptClose2d,dis,ptEnding,Adesk::kFalse);
  460. }
  461. else
  462. {
  463. GetPtAtDistOnCurve(&arc2d,ptClose2d,dis,ptEnding,Adesk::kTrue);
  464. }
  465. }
  466. else
  467. {
  468. AcGeLineSeg2d line2d;
  469. plMin->getLineSegAt(plIndex,line2d);

  470. if(!gotonext)
  471. {
  472. GetPtAtDistOnCurve(&line2d,ptClose2d,dis,ptEnding,Adesk::kFalse);
  473. }
  474. else
  475. {
  476. GetPtAtDistOnCurve(&line2d,ptClose2d,dis,ptEnding,Adesk::kTrue);
  477. }
  478. }


  479. double bulge1 = 0.0;
  480. if(segType == AcDbPolyline::SegType::kArc)
  481. {
  482. bulge1 = tan(atan(curBulge) * dis / thisLen);
  483. }


  484. if(!gotonext)
  485. {
  486. pl1->addVertexAt(pl1Index,ptClose2d,-bulge1);
  487. }
  488. else
  489. {
  490. pl1->addVertexAt(pl1Index,ptClose2d,bulge1);
  491. }
  492. pl1Index++;
  493. pl1->addVertexAt(pl1Index,ptEnding);
  494. }

  495. }


  496. static void GetNextPt(const AcDbPolyline* plMin,const bool& gotonext,const int& plIndex,AcGePoint2d& ptNextS,AcGePoint2d& ptNextE)
  497. {
  498. int nextIndex = 0;
  499. int count = plMin->numVerts();
  500. if(!gotonext)
  501. {
  502. if(plIndex > 0)
  503. {
  504. nextIndex = plIndex - 1;
  505. }
  506. else
  507. {
  508. nextIndex = count - 1;
  509. }
  510. }
  511. else
  512. {
  513. if(plIndex < count - 1)
  514. {
  515. nextIndex = plIndex + 1;
  516. }
  517. else
  518. {
  519. nextIndex = 0;
  520. }
  521. }


  522. AcDbPolyline::SegType nextType = plMin->segType(nextIndex);


  523. if(nextType == AcDbPolyline::SegType::kArc)
  524. {
  525. AcGeCircArc2d arc2d;
  526. plMin->getArcSegAt(nextIndex,arc2d);
  527. ptNextS = arc2d.startPoint();
  528. ptNextE = arc2d.endPoint();
  529. }
  530. else
  531. {
  532. AcGeLineSeg2d line2d;
  533. plMin->getLineSegAt(nextIndex,line2d);
  534. ptNextS = line2d.startPoint();
  535. ptNextE = line2d.endPoint();
  536. }


  537. }


  538. ////合成多段线
  539. static void AppendPLinePoint(const AcDbObjectId& id,const bool& gotoNext,AcDbPolyline* pLine,int& plIndex)
  540. {
  541. AcDbEntity* pEnt = NULL;
  542. Acad::ErrorStatus es = acdbOpenObject(pEnt,id,AcDb::OpenMode::kForRead);
  543. if(es != Acad::eOk)
  544. {
  545. acutPrintf(_T("open object failed in combine pline"));
  546. return;
  547. }
  548. if(!pEnt->isKindOf(AcDbPolyline::desc()))
  549. {
  550. pEnt->close();
  551. return;
  552. }

  553. AcDbPolyline* pPoly = NULL;
  554. pPoly = (AcDbPolyline*)pEnt;
  555. AcGePoint2dArray ptArr;
  556. int count = pPoly->numVerts();
  557. AcGePoint2d pt ;
  558. double bulge = 0.0;
  559. if(gotoNext)
  560. {
  561. for(int i = 0;i < count ; i++)
  562. {
  563. pPoly->getPointAt(i,pt);
  564. pPoly->getBulgeAt(i,bulge);
  565. pLine->addVertexAt(plIndex,pt,bulge);
  566. plIndex++;
  567. }
  568. }
  569. else
  570. {
  571. for(int i = count - 1;i > 0; i--)
  572. {
  573. pPoly->getPointAt(i,pt);
  574. if(i > 0)
  575. {
  576. pPoly->getBulgeAt(i - 1,bulge);
  577. }
  578. else
  579. {
  580. pPoly->getBulgeAt(0,bulge);
  581. }
  582. pLine->addVertexAt(plIndex,pt,-bulge);
  583. plIndex++;
  584. }
  585. }

  586. pEnt->close();
  587. }


  588. static void EraseIds(AcDbObjectIdArray idArr)
  589. {
  590. if(idArr == NULL || idArr.length() == 0)
  591. {
  592. return;
  593. }
  594. for(int i = 0;i < idArr.length(); i++)
  595. {
  596. AcDbEntity* pDel = NULL;
  597. if(Acad::eOk != acdbOpenObject(pDel,idArr.at(i),AcDb::OpenMode::kForWrite))
  598. {
  599. continue;
  600. }
  601. if(Acad::eOk != pDel->erase())
  602. {
  603. acutPrintf(_T("\n删除第%d个实体失败"),i);
  604. }
  605. pDel->close();
  606. }
  607. }


  608. ////绘制箭头(ptStart 为转角线段的起点)
  609. static void DrawPointer(const AcDbPolyline* pl,bool gotonext,const int& plIndex,const AcGePoint3d& ptDraw,AcDbObjectId& idPointer)
  610. {
  611. //--当前边相关信息
  612. AcGePoint3d ptCurStart;
  613. AcGePoint3d ptCurEnd;
  614. AcGeCircArc3d arc3dCur;
  615. AcGeLineSeg3d line3dCur;


  616. AcDbPolyline::SegType curType = pl->segType(plIndex);
  617. if(curType == AcDbPolyline::SegType::kArc)
  618. {
  619. pl->getArcSegAt(plIndex,arc3dCur);
  620. ptCurStart = arc3dCur.startPoint();
  621. ptCurEnd = arc3dCur.endPoint();
  622. }
  623. else
  624. {
  625. pl->getLineSegAt(plIndex,line3dCur);
  626. ptCurStart = line3dCur.startPoint();
  627. ptCurEnd = line3dCur.endPoint();
  628. }

  629. double paramDraw = 0.0;
  630. if(pl->getParamAtPoint(ptDraw,paramDraw)!=Acad::eOk)
  631. {
  632. return;
  633. }
  634. AcGeVector3d v3d;
  635. pl->getFirstDeriv(paramDraw,v3d);
  636. AcGeVector2d v(v3d[X],v3d[Y]);
  637. AcGePoint2d pt0(ptDraw[X],ptDraw[Y]);

  638. v.normalize();
  639. if(!gotonext)
  640. {
  641. v = -v;
  642. }
  643. AcGeVector2d vVer = v;
  644. ////绘制箭头
  645. AcDbPolyline* pLine = new AcDbPolyline();
  646. pLine->addVertexAt(0,pt0);


  647. vVer.rotateBy(PI/2);
  648. AcGePoint2d pt1 = pt0 + vVer *POINTERWIDTH / 2;
  649. pLine->addVertexAt(1,pt1);


  650. vVer.rotateBy(-PI/2);
  651. AcGePoint2d pt2 = pt1 + vVer * POINTERLENGTH;
  652. pLine->addVertexAt(2,pt2);


  653. vVer.rotateBy(PI/2);
  654. AcGePoint2d pt3 = pt2 + vVer * POINTERWIDTH / 2;
  655. pLine->addVertexAt(3,pt3);


  656. vVer.rotateBy(- 3 * PI/4);
  657. AcGePoint2d pt4 = pt3 + vVer * sqrt(2.0) * POINTERWIDTH;
  658. pLine->addVertexAt(4,pt4);


  659. vVer.rotateBy(-PI/2);
  660. AcGePoint2d pt5 = pt4 + vVer * sqrt(2.0) * POINTERWIDTH;
  661. pLine->addVertexAt(5,pt5);


  662. vVer.rotateBy(-3 * PI/4);
  663. AcGePoint2d pt6 = pt5 + vVer * POINTERWIDTH / 2;
  664. pLine->addVertexAt(6,pt6);


  665. vVer.rotateBy(PI/2);
  666. AcGePoint2d pt7 = pt6 + vVer * POINTERLENGTH ;
  667. pLine->addVertexAt(7,pt7);


  668. vVer.rotateBy(-PI/2);
  669. AcGePoint2d pt8 = pt7 + vVer * POINTERWIDTH / 2;
  670. pLine->addVertexAt(8,pt8);
  671. idPointer = LoadEntity(pLine);
  672. }


  673. static void RemoveEntity(AcDbObjectId entId)
  674. {
  675. AcDbEntity* pEnt = NULL;
  676. if(acdbOpenObject(pEnt,entId,AcDb::OpenMode::kForWrite) == Acad::eOk)
  677. {
  678. if(pEnt->erase() != Acad::eOk)
  679. {
  680. acutPrintf(_T("\n移除对象失败"));
  681. return;
  682. }
  683. }
  684. else
  685. {
  686. acutPrintf(_T("\n打开对象失败"));
  687. }
  688. pEnt->close();
  689. }

  690. ////2d坐标转为3d
  691. static void Pt2dTo3d(AcGePoint2d& pt2d,AcGePoint3d& pt3d)
  692. {
  693. pt3d[X] = pt2d[X];
  694. pt3d[Y] = pt2d[Y];
  695. pt3d[Z] = 0;
  696. }


  697. ////3d坐标转化为X,Y轴上的2D坐标
  698. static void Pt3dTo2d(const AcGePoint3d& pt3d,AcGePoint2d& pt2d)
  699. {
  700. pt2d = pt3d.convert2d(AcGePlane(AcGePoint3d::kOrigin, AcGeVector3d(0,0, 1)));
  701. }


  702. //from:起点,to:终点(这两点要相邻)
  703. //paramDis:沿着多段线画多长
  704. //pl:多段线
  705. //pPoly:新的多段线
  706. static void DrawByLen(const bool& gotoNext ,const AcGePoint2d& from,const AcGePoint2d& to,const double& paramDis,const AcDbPolyline* pl,AcDbPolyline* pPoly,int& polyIndex)
  707. {
  708. if(paramDis <= 0)
  709. {
  710. return;
  711. }
  712. int len = pl->numVerts();

  713. AcGeCircArc2d arc2d;
  714. AcGeLineSeg2d line2d;


  715. AcGePoint2d ptS;
  716. AcGePoint2d ptE;
  717. bool isFind = false;
  718. int plIndex = 0;
  719. AcGeCurve2d* pCurve = NULL;

  720. for(int i = 0;i < len;i++)
  721. {
  722. AcDbPolyline::SegType st = pl->segType(i);

  723. if(st == AcDbPolyline::SegType::kArc)
  724. {
  725. pl->getArcSegAt(i,arc2d);
  726. pCurve = &arc2d;
  727. }
  728. else if(st == AcDbPolyline::SegType::kLine)
  729. {
  730. pl->getLineSegAt(i,line2d);
  731. pCurve = &line2d;
  732. }


  733. if(!pCurve->hasStartPoint(ptS) || !pCurve->hasEndPoint(ptE))
  734. {
  735. continue;
  736. }

  737. if(ptS == from && ptE == to || ptS == to && ptE == from)
  738. {
  739. plIndex = i;
  740. isFind = true;
  741. break;
  742. }
  743. }


  744. double sumDis = 0.0;
  745. if(isFind)
  746. {
  747. DrawIt(gotoNext,pl,paramDis,from,polyIndex,plIndex,sumDis,pPoly);
  748. }
  749. else
  750. {
  751. acutPrintf(_T("\nnot found"));
  752. }
  753. }


  754. //summary////
  755. //指定一个起点和一条多段线,沿着多段线画出指定距离,递归执行,每次往后(前)移动一个点,直到画完指定的距离,
  756. //pl:多段线
  757. //paramDis:画多长
  758. //ptStart:起始点
  759. //polyIndex:添加到第几个了
  760. //plIndex,遍历到多段线第几条线
  761. //isSToE,遍历的顺序1:从前向后  0:从后向前
  762. //sumDis,目前画的总长度
  763. //pPoly:画出来的多段线
  764. static void DrawIt(const bool& gotoNext,const AcDbPolyline* pl,const double& paramDis,const AcGePoint2d& ptStart,int& polyIndex,int& plIndex,double& sumDis,AcDbPolyline* pPoly)
  765. {

  766. AcDbPolyline::SegType st = pl->segType(plIndex);
  767. AcGePoint2d ptS;
  768. AcGePoint2d ptE;
  769. double leftDis = 0.0;
  770. double curveDis = 0.0;
  771. double bulge = 0.0;
  772. AcGeCurve2d* pCurve = NULL;
  773. AcGeCircArc2d arc2d;
  774. AcGeLineSeg2d line2d;
  775. int len = pl->numVerts();


  776. if(polyIndex == 2*(len - 2))
  777. {
  778. acutPrintf(_T("\nend poly is %d"),polyIndex);
  779. return;
  780. }


  781. if(st == AcDbPolyline::SegType::kArc)
  782. {
  783. pl->getArcSegAt(plIndex,arc2d);
  784. pCurve = &arc2d;////!!!注意:指针的生命周期一定要大于等于指向的变量的生命周期,否则变量release掉指针就空了,再次使用指针程序直接崩溃!!
  785. }
  786. else if(st == AcDbPolyline::SegType::kLine)
  787. {
  788. pl->getLineSegAt(plIndex,line2d);
  789. pCurve = &line2d;
  790. }
  791. if(!pCurve->hasStartPoint(ptS) || !pCurve->hasEndPoint(ptE))
  792. {
  793. return;
  794. }
  795. curveDis = pCurve->length(pCurve->paramOf(ptS),pCurve->paramOf(ptE));
  796. leftDis = paramDis - sumDis;


  797. pl->getBulgeAt(plIndex,bulge);


  798. if(curveDis > leftDis)
  799. {
  800. double paramEnding = 0.0;

  801. if(gotoNext)
  802. {
  803. AcGePoint2d ptEnding;
  804. AcGePoint2d ptS;
  805. pCurve->hasStartPoint(ptS);
  806. GetPtAtDistOnCurve(pCurve,ptS,leftDis,ptEnding,Adesk::kTrue);


  807. bulge = tan(atan(bulge) * leftDis/curveDis);


  808. pPoly->addVertexAt(polyIndex,ptS,bulge);
  809. polyIndex ++;
  810. pPoly->addVertexAt(polyIndex,ptEnding);
  811. }
  812. else
  813. {
  814. AcGePoint2d ptEnding;


  815. AcGePoint2d ptE;
  816. pCurve->hasEndPoint(ptE);
  817. GetPtAtDistOnCurve(pCurve,ptE,leftDis,ptEnding,Adesk::kFalse);


  818. bulge = tan(atan(bulge) * leftDis/curveDis);


  819. pPoly->addVertexAt(polyIndex,ptE,-bulge);
  820. polyIndex ++;
  821. pPoly->addVertexAt(polyIndex,ptEnding);
  822. }
  823. return;
  824. }
  825. else
  826. {
  827. if(gotoNext)
  828. {
  829. pPoly->addVertexAt(polyIndex,ptS,bulge);
  830. polyIndex ++;
  831. pPoly->addVertexAt(polyIndex,ptE);
  832. polyIndex ++;
  833. //acutPrintf(_T("\nplIndex is %d,poly is %d。is goto next,bulge is %.2f"),plIndex,polyIndex,bulge);
  834. }
  835. else
  836. {
  837. pPoly->addVertexAt(polyIndex,ptE,-bulge);
  838. polyIndex ++;
  839. pPoly->addVertexAt(polyIndex,ptS);
  840. polyIndex ++;

  841. }
  842. /*acutPrintf(_T("\nptS[X] :%.2f,ptS[Y]:%.2f,ptE[X]:%.2f,ptE[Y]:%.2f"),ptS[X],ptS[Y],ptE[X],ptE[Y]);*/
  843. sumDis += curveDis;

  844. }


  845. if(gotoNext)
  846. {
  847. plIndex = plIndex < len - 1  ? ++plIndex : 0;
  848. }
  849. else
  850. {
  851. plIndex = plIndex > 0 ? --plIndex : len - 1;
  852. }


  853. DrawIt(gotoNext,pl,paramDis,ptStart,polyIndex,plIndex,sumDis,pPoly);


  854. }

  855. ////反回曲线上一定距离的点(默认从起点开始计算)
  856. ////pCurve:曲线指针,dist:距离,point:要返回的点
  857. ////Adesk::Boolean isGotoNext  true:沿着正向寻找,false:沿着反方向寻找
  858. static void GetPtAtDistOnCurve(const AcGeCurve2d* pCurve,const AcGePoint2d& ptInput,double dist,AcGePoint2d& point,Adesk::Boolean isGotoNext)
  859. {
  860. if(pCurve == NULL)
  861. {
  862. return;
  863. }
  864. AcGePoint2d ptS;
  865. ptS = ptInput;
  866. double pa = 0.0;
  867. double datumParam = 0.0;
  868. //Adesk::Boolean posParamDir = Adesk::kTrue;

  869. datumParam = pCurve->paramOf(ptS);
  870. pa = pCurve->paramAtLength(datumParam,dist,isGotoNext);
  871. point = pCurve->evalPoint(pa);
  872. }

复制代码
发表于 2012-10-29 15:25:19 | 显示全部楼层
你要说明你的autocad版本, 不然别人编译链接后不一定能用

我是初学者,还没有入门,因为C基础差,学起来事倍功半
 楼主| 发表于 2012-12-21 09:12:42 | 显示全部楼层
sunny20102 发表于 2012-10-29 15:25
你要说明你的autocad版本, 不然别人编译链接后不一定能用

我是初学者,还没有入门,因为C基础差,学起来事倍 ...

CAD2006就可以了
发表于 2012-12-21 10:43:27 | 显示全部楼层
shuaier 发表于 2012-12-21 09:12
CAD2006就可以了

编译完了,你准备怎么用啊?
 楼主| 发表于 2012-12-26 12:55:32 | 显示全部楼层
齐天大圣3386 发表于 2012-12-21 10:43
编译完了,你准备怎么用啊?

直接拖到CAD窗口加载,然后使用命令操作
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 07:56 , Processed in 0.176827 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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