找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 5797|回复: 6

[分享] Object_Arx实用函数收集汇总

[复制链接]
发表于 2013-9-12 09:48:53 | 显示全部楼层 |阅读模式

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

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

×
[ 本帖最后由 Love-Lisp 于 2013-9-12 09:55 编辑 ]\n\n搜集的一些Object_Arx实用函数,初学者学习arx代码的好范例!
  1. //添加Hatch
  2. AcDbObjectId CreateHatch(
  3. AcDbObjectId dbOId,
  4. char cLayer[],
  5. char cPattern[] = "SOLID",
  6. int nColor = 256,
  7. double dAngle = 0.0,
  8. double dScale = 1.0,
  9. AcDbDatabase * pDbDatab = acdbHostApplicationServices()->workingDatabase())
  10. {
  11. AcCmColor CmC;
  12. AcDbObjectId DbOId;
  13. AcDbObjectIdArray DbOIdA(0, 2);
  14. AcDbBlockTable * pDbBT;
  15. AcDbBlockTableRecord * pDbBTR;
  16. AcGeVector3d normal(0.0, 0.0, 1.0);

  17. DbOIdA.append(dbOId);

  18. AcDbHatch* pDbHat = new AcDbHatch();

  19. pDbHat->setDatabaseDefaults();

  20. pDbHat->setAssociative(Adesk::kTrue); // BUG: doesn't do squat! have to set the reactor yourself to get associativity!

  21. pDbHat->appendLoop(AcDbHatch::kExternal, DbOIdA);

  22. pDbHat->setPatternScale(dScale);

  23. pDbHat->setPatternAngle(dAngle);

  24. pDbHat->setPattern(AcDbHatch::kPreDefined, cPattern);

  25. pDbHat->setNormal(normal);

  26. pDbHat->evaluateHatch(); // crucial call or nothing gets displayed!

  27. pDbDatab->getSymbolTable(pDbBT, AcDb::kForRead);

  28. pDbBT->getAt(ACDB_MODEL_SPACE, pDbBTR, AcDb::kForWrite);

  29. pDbBTR->appendAcDbEntity(DbOId, pDbHat);

  30. pDbHat->setLayer(cLayer);

  31. CmC.setColorIndex(nColor);

  32. ((AcDbEntity *)pDbHat)->setColor(CmC);

  33. pDbBT->close();

  34. pDbBTR->close();

  35. pDbHat->close();

  36. return DbOId;

  37. }

  38. //************************************************************************
  39. //函数名称:drawDonutAtPoint
  40. //函数类型:void
  41. //返回值:
  42. //功能描述:
  43. //函数作者:Darcy
  44. //创建日期:2003-x-x
  45. //参数列表:
  46. //变量名:nPt 变量类型:const AcGePoint2d& 变量说明:中点
  47. //变量名:nRadius 变量类型:const double 变量说明:半径
  48. //************************************************************************
  49. void drawDonutAtPoint(const AcGePoint2d& nPt,const double nRadius)
  50. {
  51. AcGePoint2d pt;
  52. AcDbPolyline * pPline = new AcDbPolyline(2);

  53. pt = nPt;

  54. pt.x -= nRadius;

  55. pPline->addVertexAt( 0 , pt , 1.0 , nRadius*2.0 , nRadius*2.0);

  56. pt.x += nRadius * 2.0;

  57. pPline->addVertexAt( 1 , pt , 1.0 , nRadius*2.0 , nRadius*2.0);

  58. pPline->setClosed(Adesk::kTrue);

  59. //添加到当前数据库:
  60. addToCurrentSpaceAndClose(pPline);

  61. }

  62. 我也跟几贴,得点爱心币,互相交流
  63. //得到指定层上的所有实体
  64. Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray& IdArr,
  65. const AcArray& layerNameArr)
  66. {
  67. Acad::ErrorStatus es=Acad::eOk;
  68. ASSERT(pDb);
  69. if(pDb==NULL)
  70. return Acad::eInvalidInput;
  71. AcDbBlockTable *pBlkTable=NULL;
  72. if((es=pDb->getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表
  73. {
  74. acedAlert("打开块表失败");
  75. return es;
  76. }
  77. AcDbBlockTableRecord *pBlkTableRecord=NULL;
  78. if((es=pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录
  79. {
  80. acedAlert("打开块表记录失败");
  81. pBlkTable->close();
  82. return es;
  83. }
  84. pBlkTable->close();//关闭块表

  85. AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器
  86. if((es=pBlkTableRecord->newIterator(pIterator))!=Acad::eOk)
  87. {
  88. pBlkTableRecord->close();
  89. return es;
  90. }

  91. CAcModuleResourceOverride resoverride;
  92. CProgressDlg progress;
  93. progress.Create();
  94. progress.SetPos(0);
  95. progress.SetWindowText("正在检测图形中所有实体...");

  96. for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
  97. {
  98. AcDbEntity *entity=NULL;
  99. es=pIterator->getEntity(entity,AcDb::kForRead); //打开实体

  100. if(es==Acad::eLockViolation)
  101. {
  102. acedAlert("内存锁定");
  103. }
  104. else if(es==Acad::eWasOpenForWrite)
  105. {
  106. acedAlert("实体以写方式打开");
  107. }
  108. else if(es==Acad::eWasOpenForRead)
  109. {
  110. acedAlert("实体以读方式打开");
  111. }
  112. else
  113. {
  114. if(layerNameArr.contains(entity->layer()))
  115. IdArr.append(entity->objectId());
  116. entity->close();
  117. }
  118. progress.StepIt();
  119. }
  120. delete pIterator;pIterator=NULL;
  121. pBlkTableRecord->close();
  122. acutPrintf("eend");
  123. return es;
  124. }

  125. //得到指定层上指定颜色的所有实体
  126. Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray& IdArr,
  127. const AcArray& layerNameArr,Adesk::UInt16 colorIndex)
  128. {
  129. Acad::ErrorStatus es=Acad::eOk;
  130. ASSERT(pDb);
  131. if(pDb==NULL)
  132. return Acad::eInvalidInput;
  133. AcDbBlockTable *pBlkTable=NULL;
  134. if((es=pDb->getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表
  135. {
  136. acedAlert("打开块表失败");
  137. return es;
  138. }
  139. AcDbBlockTableRecord *pBlkTableRecord=NULL;
  140. if((es=pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录
  141. {
  142. acedAlert("打开块表记录失败");
  143. pBlkTable->close();
  144. return es;
  145. }
  146. pBlkTable->close();//关闭块表

  147. AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器
  148. if((es=pBlkTableRecord->newIterator(pIterator))!=Acad::eOk)
  149. {
  150. pBlkTableRecord->close();
  151. return es;
  152. }

  153. CAcModuleResourceOverride resoverride;
  154. CProgressDlg progress;
  155. progress.Create();
  156. progress.SetPos(0);
  157. progress.SetWindowText("正在检测图形中所有实体...");

  158. for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
  159. {
  160. AcDbEntity *entity=NULL;
  161. if((es=pIterator->getEntity(entity,AcDb::kForRead))!=Acad::eOk)//打开实体
  162. {
  163. delete pIterator;pIterator=NULL;
  164. pBlkTableRecord->close();
  165. return es;
  166. }
  167. if(layerNameArr.contains(entity->layer())&&entity->colorIndex()==colorIndex)
  168. IdArr.append(entity->objectId());
  169. entity->close();
  170. progress.StepIt();
  171. }
  172. delete pIterator;pIterator=NULL;
  173. pBlkTableRecord->close();
  174. return es;
  175. }

  176. //得到图形中所有的层
  177. Acad::ErrorStatus CDrawFunction::getAllLayerName(AcDbDatabase *pDb,CStringArray& layerArray)
  178. {
  179. Acad::ErrorStatus es=Acad::eOk;
  180. if(pDb==NULL)
  181. return Acad::eInvalidInput;
  182. layerArray.RemoveAll();
  183. AcDbLayerTable *pLayerTable=NULL;
  184. if((es=pDb->getSymbolTable(pLayerTable,AcDb::kForRead))!=Acad::eOk)
  185. {
  186. pLayerTable->close();
  187. return es;
  188. }
  189. //创建一个层表迭代器
  190. AcDbLayerTableIterator *pLayerTableIterator;
  191. pLayerTable->newIterator(pLayerTableIterator);
  192. pLayerTable->close();

  193. char *pLayerName=NULL;
  194. CString name;
  195. for(int i=0;!pLayerTableIterator->done();pLayerTableIterator->step(),i++)
  196. {
  197. AcDbLayerTableRecord *pLayerTableRecord=NULL;
  198. pLayerTableIterator->getRecord(pLayerTableRecord,AcDb::kForRead);
  199. pLayerTableRecord->getName(pLayerName);
  200. name.Format("%s",pLayerName);
  201. pLayerTableRecord->close();
  202. layerArray.Add(name);

  203. }

  204. if(pLayerName) acutDelString(pLayerName);
  205. delete pLayerTableIterator;pLayerTableIterator=NULL;
  206. return es;
  207. }


  208. //*************根据线形名得到线型ID********************//
  209. //******************************************************//
  210. BOOL CDrawFunction::getLinetypeIdFromString(const char* str, AcDbObjectId& id)
  211. {
  212. //----查找安装目录----//
  213. CString AcadInstallPath;
  214. FindAcadInstallPath(AcadInstallPath);
  215. CString File=AcadInstallPath+"\\linetype\\user.lin";
  216. //-----查找完毕--------//
  217. ASSERT(str!=NULL);
  218. AcDbLinetypeTable *pLinetypeTable;
  219. acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
  220. Acad::ErrorStatus mess;
  221. mess=pLinetypeTable->getAt(str,id);
  222. if(mess==Acad::eKeyNotFound||mess==Acad::ePermanentlyErased)
  223. {
  224. pLinetypeTable->close();
  225. Acad::ErrorStatus error;

  226. error=acdbLoadLineTypeFile(str,File.GetBuffer(0),acdbHostApplicationServices()->workingDatabase());
  227. if(error==Acad::eNullObjectPointer)
  228. {
  229. AcDbLinetypeTable *pLinetypeTable;
  230. acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
  231. pLinetypeTable->getAt("CONTINUOUS", id);
  232. pLinetypeTable->close();
  233. return FALSE;
  234. }
  235. else if(error==Acad::eFileSystemErr)
  236. {
  237. AfxMessageBox("the specified file cannot be opened");
  238. return FALSE;
  239. }
  240. else if(error==Acad::eUndefinedLineType)
  241. {
  242. AcDbLinetypeTable *pLinetypeTable;
  243. acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
  244. AfxMessageBox("the linetype name specified by ltname is not found in the file");
  245. pLinetypeTable->getAt("CONTINUOUS", id);
  246. pLinetypeTable->close();
  247. return TRUE;
  248. }
  249. AcDbLinetypeTable *pLinetypeTable;
  250. acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
  251. pLinetypeTable->getAt(str,id);
  252. pLinetypeTable->close();
  253. return TRUE;
  254. }
  255. pLinetypeTable->close();
  256. return TRUE;
  257. }



  258. void CDrawFunction::FindAcadInstallPath(CString &AcadInstallPath)
  259. {
  260. //查找样式目录安装路径
  261. TCHAR AcadPath[255];
  262. HKEY hKey;
  263. if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\线路处开发组\\RDS2002\\1.00"),0,KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
  264. {
  265. ::AfxMessageBox("注册路径不对");
  266. return ;
  267. }
  268. DWORD dwDataType=REG_SZ;
  269. DWORD dwLength=255;
  270. LONG lRet=RegQueryValueEx(hKey,TEXT("path"),NULL,NULL,(LPBYTE)AcadPath,&dwLength);
  271. RegCloseKey(hKey);
  272. if(lRet!=ERROR_SUCCESS)
  273. {
  274. acutPrintf("Read failed\n");
  275. return ;
  276. }
  277. //-----查找完毕--------//
  278. AcadInstallPath.Format("%s",AcadPath);
  279. }

  280. AcDbObjectId CDrawFunction::createTextStyle(CString fontName,CString bigFontName,CString textStyleName)
  281. {
  282. AcGiTextStyle *TextStyle=new AcGiTextStyle
  283. (fontName,
  284. bigFontName,
  285. 0,
  286. 0.67,
  287. 0,
  288. 0,
  289. Adesk::kFalse,
  290. Adesk::kFalse,
  291. Adesk::kFalse,
  292. Adesk::kFalse,
  293. Adesk::kFalse,
  294. textStyleName); //字体名
  295. AcDbObjectId textStyleId;
  296. toAcDbTextStyle(*TextStyle,textStyleId);
  297. return textStyleId;
  298. }



  299. //*******************wuweifan2002.12.12*************************//
  300. //********************插入多行文本***************************//
  301. //************************************************************//
  302. AcDbObjectId CDrawFunction::createMutiText(AcGePoint3d BasePoint,AcDb::TextHorzMode hMode,AcDb::TextVertMode vMode,CString Text,double texthight,double widthfactor,double angle,int color,CString smallFontName,CString bigFontName,CString layerName)
  303. {
  304. ASSERT(Text!=NULL);
  305. AcDbMText *pMText=new AcDbMText();
  306. if(pMText==NULL)
  307. throw Acad::eOutOfMemory;
  308. AcDbObjectId TextStyleId;
  309. TextStyleId=createTextStyle(smallFontName,bigFontName,"xianlu");
  310. pMText->setTextStyle(TextStyleId);
  311. pMText->setContents(Text.GetBuffer(Text.GetLength()));
  312. pMText->setTextHeight(texthight);
  313. pMText->setRotation(angle);
  314. pMText->setLineSpacingFactor(0.8);
  315. pMText->setColorIndex(color);
  316. if(layerName!="")
  317. pMText->setLayer(layerName.GetBuffer(0));
  318. AcDbObjectId MTextId;
  319. addToModelSpace(MTextId, pMText);
  320. pMText->close();
  321. return MTextId;
  322. }

  323. //********************插入单行文本***************************//
  324. //************************************************************//
  325. AcDbObjectId CDrawFunction::createtextAll(AcGePoint3d pt,char *text,AcDb::TextHorzMode hMode,AcDb::TextVertMode vMode,double hight,double widthFactor,double rotation,int color,CString smallFontName,CString bigFontName,CString layerName)
  326. {
  327. ASSERT(text!=NULL);
  328. AcDbText *pText=NULL;
  329. pText=new AcDbText;
  330. if(pText==NULL)
  331. throw Acad::eOutOfMemory;
  332. AcDbObjectId textId;
  333. textId=createTextStyle(smallFontName,bigFontName,"xianlu");
  334. pText->setTextStyle(textId);
  335. pText->setTextString(text);
  336. pText->setHeight(hight);
  337. pText->setColorIndex(color);
  338. pText->setRotation(rotation);
  339. pText->setWidthFactor(widthFactor);
  340. pText->setPosition(pt);
  341. if(layerName!="")
  342. pText->setLayer(layerName.GetBuffer(0));
  343. addToModelSpace(textId, pText);
  344. pText->close();
  345. return textId;
  346. }

  347. //设置尺寸文本样式
  348. void CDrawFunction::setDimTextStyle(AcDbObjectId dimId,AcDbObjectId textStyleId,
  349. int colorIndex,double textHeight,double textScator,
  350. double textGap,bool align)
  351. {
  352. AcDbDimension *dimText;
  353. acdbOpenObject(dimText,dimId,AcDb::kForWrite);
  354. dimText->setDimtxsty(textStyleId); //文本字体DIMTXSTY
  355. AcCmColor color;
  356. color.setColorIndex(colorIndex);
  357. dimText->setDimclrt(color);//文本颜色DIMCLRT
  358. dimText->setDimtxt(textHeight); //文本高度DIMTXT
  359. dimText->setDimtfac(textScator);//文本高宽比DIMTFAC
  360. dimText->setDimgap(textGap);//文本距尺寸距离DIMGAP
  361. dimText->setDimtoh(align);//文本标注DIMTOH
  362. dimText->close();
  363. }

  364. //设置尺寸延伸线类型
  365. Acad::ErrorStatus CDrawFunction::setextensionlineStyle(AcDbObjectId dimId,int colorIndex,double length,
  366. double offLength,bool v1,bool v2)
  367. {
  368. Acad::ErrorStatus es=Acad::eOk;
  369. AcDbDimension *dimText=NULL;
  370. if((es=acdbOpenObject(dimText,dimId,AcDb::kForWrite))!=Acad::eOk)
  371. return es;
  372. AcCmColor color;
  373. if((es=color.setColorIndex(colorIndex))!=Acad::eOk)
  374. {
  375. dimText->close();return es;
  376. }
  377. dimText->setDimclre(color);//设置颜色DIMCLRE
  378. dimText->setDimexe(length);//设置超出长度DIMEXE
  379. dimText->setDimexo(offLength);//尺寸偏离长度DIMEXO
  380. dimText->setDimse1(v1);//是否注第一条线DIMSE1
  381. dimText->setDimse2(v2);//是否注第二条线DIMSE2
  382. dimText->close();
  383. return es;
  384. }
  385. //绘制对齐尺寸线
  386. AcDbObjectId CDrawFunction::drawDimension(AcGePoint3d xLine1Point,AcGePoint3d xLine2Point,
  387. double fwj,int direction,double distance,CString dimText,CString m_cLayerName)
  388. {

  389. AcDbAlignedDimension *dimension=new AcDbAlignedDimension;
  390. AcGePoint3d dimLinePoint;
  391. // CCalcuMethod *calcu=new CCalcuMethod();
  392. // calcu->calEndZbSelf(xLine2Point,distance*direction,fwj,dimLinePoint);
  393. // delete calcu;calcu=NULL;
  394. dimension->setXLine1Point(xLine1Point);
  395. dimension->setDimLinePoint(dimLinePoint);
  396. dimension->setXLine2Point(xLine2Point);
  397. dimension->setDimensionText(dimText.GetBuffer(0));
  398. dimension->setLayer(m_cLayerName.GetBuffer(0));
  399. AcDbObjectId dimId;
  400. addToModelSpace(dimId,dimension);
  401. dimension->close();
  402. return dimId;
  403. }

  404. Acad::ErrorStatus CDrawFunction::createLine(AcDbObjectId &lineId,AcGePoint3d startPt,AcGePoint3d endPt,int color,CString Layer,char *linetype)
  405. {
  406. Acad::ErrorStatus es=Acad::eOk;
  407. ASSERT(linetype!=NULL);
  408. AcDbLine *pLine = new AcDbLine(startPt, endPt);
  409. if((es=pLine->setColorIndex(color))!=Acad::eOk)
  410. {
  411. pLine->close();return es;
  412. }
  413. if(Layer!="")
  414. {
  415. if(pLine->setLayer(Layer)==Acad::eKeyNotFound \
  416. ||pLine->setLayer(Layer)==Acad::eDeletedEntry )
  417. {
  418. createNewLayer(Layer);
  419. if((es=pLine->setLayer(Layer.GetBuffer(0)))!=Acad::eOk)
  420. {
  421. pLine->close();return es;
  422. }
  423. }
  424. }
  425. if(linetype!=NULL)
  426. {
  427. AcDbObjectId lineTypeId;
  428. if(getLinetypeIdFromString(linetype,lineTypeId))
  429. {
  430. if((es=pLine->setLinetype(lineTypeId))!=Acad::eOk)
  431. {
  432. pLine->close();return es;
  433. }
  434. if((es=pLine->setLinetypeScale(1))!=Acad::eOk)
  435. {
  436. pLine->close();return es;
  437. }
  438. }
  439. }
  440. es=addToModelSpace(lineId,pLine);
  441. return es;
  442. }

  443. Acad::ErrorStatus CDrawFunction::createCircle(AcDbObjectId& circleId,AcGePoint3d center,double radius,int color,CString layer)
  444. {
  445. Acad::ErrorStatus es=Acad::eOk;
  446. AcGeVector3d normal(0,0,1);
  447. AcDbCircle *circle=new AcDbCircle(center,normal,radius);
  448. if((es=circle->setColorIndex(color))!=Acad::eOk)
  449. {
  450. circle->close();return es;
  451. }
  452. if(layer!="")
  453. {
  454. if(circle->setLayer(layer)==Acad::eKeyNotFound \
  455. ||circle->setLayer(layer)==Acad::eDeletedEntry )
  456. {
  457. createNewLayer(layer);
  458. if((es=circle->setLayer(layer.GetBuffer(0)))!=Acad::eOk)
  459. {
  460. circle->close();return es;
  461. }
  462. }
  463. }
  464. es=addToModelSpace(circleId,circle);
  465. return es;
  466. }


  467. Acad::ErrorStatus CDrawFunction::DrawPolyline(AcDbObjectId& polylineId, AcGePoint3dArray ptArr, int Color, double Width,bool IsClose,CString Layer,char *linetype)
  468. {
  469. Acad::ErrorStatus es=Acad::eOk;
  470. AcDb2dPolyline *pNewPline;
  471. if(IsClose)pNewPline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kTrue,Width,Width);
  472. else pNewPline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kFalse,Width,Width);
  473. if((es=pNewPline->setColorIndex(Color))!=Acad::eOk)
  474. {
  475. pNewPline->close();return es;
  476. }
  477. if(Layer!="")
  478. {
  479. if(pNewPline->setLayer(Layer)==Acad::eKeyNotFound \
  480. ||pNewPline->setLayer(Layer)==Acad::eDeletedEntry )
  481. {
  482. createNewLayer(Layer);
  483. if((es=pNewPline->setLayer(Layer))!=Acad::eOk)
  484. {
  485. pNewPline->close();return es;
  486. }
  487. }
  488. }
  489. if(linetype!=NULL)
  490. {
  491. AcDbObjectId lineTypeId;
  492. if(getLinetypeIdFromString(linetype,lineTypeId))
  493. {
  494. if((es=pNewPline->setLinetype(lineTypeId))!=Acad::eOk)
  495. {
  496. pNewPline->close();return es;
  497. }
  498. if((es=pNewPline->setLinetypeScale(1))!=Acad::eOk)
  499. {
  500. pNewPline->close();return es;
  501. }
  502. }
  503. }
  504. if(!pNewPline->isLinetypeGenerationOn())
  505. {
  506. if((es=pNewPline->setLinetypeGenerationOn())!=Acad::eOk)
  507. {
  508. pNewPline->close();return es;
  509. }
  510. }
  511. es=addToModelSpace(polylineId,pNewPline);
  512. return es;
  513. }

  514. Acad::ErrorStatus CDrawFunction::DrawSplinePolyline(AcDbObjectId& polylineId, AcGePoint3dArray ptArr, int Color, double Width,bool IsClose,CString Layer,char *linetype)
  515. {
  516. Acad::ErrorStatus es=Acad::eOk;
  517. AcDb2dPolyline *pNewPline;
  518. if(IsClose)pNewPline=new AcDb2dPolyline(AcDb::k2dQuadSplinePoly,ptArr,0,Adesk::kTrue,Width,Width);
  519. else pNewPline=new AcDb2dPolyline(AcDb::k2dQuadSplinePoly,ptArr,0,Adesk::kFalse,Width,Width);
  520. if((es=pNewPline->setColorIndex(Color))!=Acad::eOk)
  521. {
  522. pNewPline->close();return es;
  523. }
  524. if(Layer!="")
  525. {
  526. if(pNewPline->setLayer(Layer)==Acad::eKeyNotFound \
  527. ||pNewPline->setLayer(Layer)==Acad::eDeletedEntry )
  528. {
  529. createNewLayer(Layer);
  530. if((es=pNewPline->setLayer(Layer))!=Acad::eOk)
  531. {
  532. pNewPline->close();return es;
  533. }
  534. }
  535. }
  536. if(linetype!=NULL)
  537. {
  538. AcDbObjectId lineTypeId;
  539. if(getLinetypeIdFromString(linetype,lineTypeId))
  540. {
  541. if((es=pNewPline->setLinetype(lineTypeId))!=Acad::eOk)
  542. {
  543. pNewPline->close();return es;
  544. }
  545. if((es=pNewPline->setLinetypeScale(1))!=Acad::eOk)
  546. {
  547. pNewPline->close();return es;
  548. }
  549. }
  550. }
  551. if(!pNewPline->isLinetypeGenerationOn())
  552. {
  553. if((es=pNewPline->setLinetypeGenerationOn())!=Acad::eOk)
  554. {
  555. pNewPline->close();return es;
  556. }
  557. }
  558. es=addToModelSpace(polylineId,pNewPline);
  559. return es;
  560. }


  561. //得到文本边界
  562. void CDrawFunction::getTextBoundary(AcDbObjectId objectId,double offset,AcDbObjectId &textBoundaryId)
  563. {
  564. AcDbExtents Ext;
  565. AcDbEntity *pEnt;
  566. acdbOpenObject(pEnt,objectId,AcDb::kForWrite);
  567. if(pEnt->isKindOf(AcDbText::desc()))
  568. {
  569. AcDbText *pText=AcDbText::cast(pEnt);
  570. AcGePoint3d basePoint;
  571. basePoint=pText->position();
  572. double rotateAngle=pText->rotation();
  573. pText->setRotation(0);
  574. pText->getGeomExtents(Ext);
  575. AcGePoint3d minPt,maxPt;
  576. minPt=Ext.minPoint();
  577. maxPt=Ext.maxPoint();
  578. AcGePoint3dArray pointArray;
  579. AcGePoint3d point1,point2,point3,point4;
  580. point1.x=minPt.x-offset;point1.y=minPt.y-offset;point1.z=0;
  581. pointArray.append(point1);
  582. point2.x=maxPt.x+offset;point2.y=minPt.y-offset;point2.z=0;
  583. pointArray.append(point2);
  584. point3.x=maxPt.x+offset;point3.y=maxPt.y+offset;point3.z=0;
  585. pointArray.append(point3);
  586. point4.x=minPt.x-offset;point4.y=maxPt.y+offset;point4.z=0;
  587. pointArray.append(point4);
  588. DrawPolyline(textBoundaryId,pointArray,1,0,TRUE,"0","CONTINUOUS");
  589. AcGeMatrix3d matrix;
  590. AcGeVector3d axis;
  591. ident_init(matrix);
  592. axis.set(0,0,1);
  593. matrix=matrix.rotation(rotateAngle,axis,basePoint); //旋转矩阵
  594. AcDbEntity *BounEnt;
  595. acdbOpenObject(BounEnt,textBoundaryId,AcDb::kForWrite);
  596. BounEnt->transformBy(matrix);
  597. BounEnt->close();
  598. pText->setRotation(rotateAngle);
  599. }
  600. else if(pEnt->isKindOf(AcDbMText::desc()))
  601. {
  602. AcDbMText *pMtext=AcDbMText::cast(pEnt);
  603. AcGePoint3d basePoint;
  604. basePoint=pMtext->location();
  605. double rotateAngle=pMtext->rotation();
  606. pMtext->setRotation(0);
  607. AcGePoint3dArray pointArray;
  608. double width=pMtext->actualWidth();
  609. double height=pMtext->actualHeight();
  610. AcGePoint3d point1,point2,point3,point4;
  611. point1.x=basePoint.x-offset;point1.y=basePoint.y+offset;point1.z=0;
  612. pointArray.append(point1);
  613. point2.x=basePoint.x+width+offset;point2.y=basePoint.y+offset;point2.z=0;
  614. pointArray.append(point2);
  615. point3.x=basePoint.x+width+offset;point3.y=basePoint.y-height-offset;point3.z=0;
  616. pointArray.append(point3);
  617. point4.x=basePoint.x-offset;point4.y=basePoint.y-height-offset;point4.z=0;
  618. pointArray.append(point4);
  619. DrawPolyline(textBoundaryId,pointArray,1,0,TRUE,"0","CONTINUOUS");
  620. AcGeMatrix3d matrix;
  621. AcGeVector3d axis;
  622. ident_init(matrix);
  623. axis.set(0,0,1);
  624. matrix=matrix.rotation(rotateAngle,axis,basePoint); //旋转矩阵
  625. AcDbEntity *BounEnt;
  626. acdbOpenObject(BounEnt,textBoundaryId,AcDb::kForWrite);
  627. BounEnt->transformBy(matrix);
  628. BounEnt->close();
  629. pMtext->setRotation(rotateAngle);
  630. }
  631. pEnt->close();
  632. return;
  633. }


  634. AcDbObjectId CDrawFunction::createNewLayer(CString LayerName)
  635. {
  636. AcDbLayerTable *LayerTable;
  637. acdbHostApplicationServices()->workingDatabase()->getSymbolTable(LayerTable,AcDb::kForWrite);
  638. AcDbObjectId LayerId;
  639. if(!LayerTable->has(LayerName))
  640. {
  641. AcDbLayerTableRecord *LayerTableRecord=new AcDbLayerTableRecord;
  642. LayerTableRecord->setName(LayerName);
  643. LayerTable->add(LayerId,LayerTableRecord);
  644. LayerTableRecord->close();
  645. }
  646. else
  647. {
  648. LayerTable->getAt(LayerName,LayerId,FALSE);
  649. }
  650. LayerTable->close();
  651. return LayerId;
  652. }

  653. bool CDrawFunction::insertBlock(AcDbObjectId &newEntId,CString BlockName,double fwj,AcGePoint3d basePoint,double scalex,
  654. CString Text1,CString Text2,CString Text3,
  655. int text1color,int text2color,int text3color,
  656. double text1height,double text2height,double text3height,
  657. double text1scator,double text2scator,double text3scator,
  658. CString littleFont,CString bigFont,CString layerName,double dx,double dy) //每块可有三个属性定义
  659. {
  660. AcDbObjectId blockId;
  661. double x,y;
  662. x=basePoint[X];
  663. y=basePoint[Y];
  664. bool a=AcDbSymbolUtilities::hasBlock(BlockName.GetBuffer(BlockName.GetLength()),acdbHostApplicationServices()->workingDatabase());
  665. if(!a)return FALSE;
  666. AcDbSymbolUtilities::getBlockId(blockId,BlockName.GetBuffer(BlockName.GetLength()),acdbHostApplicationServices()->workingDatabase());
  667. AcDbBlockReference *pBlkRef = new AcDbBlockReference;
  668. // pBlkRef->treatAsAcDbBlockRefForExplode();///////////
  669. pBlkRef->setBlockTableRecord(blockId);
  670. pBlkRef->setLayer(layerName);//设置层/////////////////////////////
  671. struct resbuf to, from;
  672. from.restype = RTSHORT;
  673. from.resval.rint = 1; // UCS
  674. to.restype = RTSHORT;
  675. to.resval.rint = 0; // WCS
  676. AcGeVector3d normal(0, 0, 1);
  677. acedTrans(&(normal.x), &from, &to, Adesk::kTrue,&(normal.x));
  678. AcGeScale3d scale(scalex,scalex,scalex);
  679. pBlkRef->setScaleFactors(scale);
  680. AcGePoint3d insertPoint;
  681. insertPoint=basePoint;
  682. pBlkRef->setPosition(basePoint);
  683. pBlkRef->setRotation(fwj);
  684. pBlkRef->setNormal(normal);
  685. pBlkRef->setColorIndex(text1color);//按文本颜色设置改变颜色
  686. pBlkRef->setLayer(layerName);
  687. AcDbBlockTable *pBlockTable;
  688. acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable,AcDb::kForWrite);
  689. AcDbBlockTableRecord *pBlockTableRecord;
  690. pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,AcDb::kForWrite);
  691. pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef);
  692. pBlockTable->close();
  693. pBlockTableRecord->close();

  694. acdbOpenObject(pBlockTableRecord, blockId, AcDb::kForRead);
  695. AcDbBlockTableRecordIterator *pIterator;
  696. pBlockTableRecord->newIterator(pIterator);
  697. AcDbEntity *pEnt;
  698. AcDbAttributeDefinition *pAttdef;
  699. for (pIterator->start(); !pIterator->done();pIterator->step())
  700. {
  701. pIterator->getEntity(pEnt, AcDb::kForWrite);
  702. //pEnt->setColorIndex(text1color);//按文本颜色设置改变颜色
  703. //pEnt->setLayer(layerName);//设置层
  704. if(pEnt->isKindOf(AcDbAttributeDefinition::desc()))
  705. {
  706. pAttdef = AcDbAttributeDefinition::cast(pEnt);
  707. if (pAttdef != NULL && !pAttdef->isConstant())
  708. {
  709. AcDbAttribute *pAtt = new AcDbAttribute();
  710. pAtt->setPropertiesFrom(pAttdef);
  711. pAtt->setInvisible(pAttdef->isInvisible());
  712. AcGePoint3d alignpoint;
  713. alignpoint=pAttdef->alignmentPoint();
  714. alignpoint+=pBlkRef->position().asVector();
  715. AcDbObjectId TextStyleId;
  716. TextStyleId=createTextStyle(littleFont,bigFont,"xianlu");
  717. pAtt->setTextStyle(TextStyleId);
  718. pAtt->setHeight(pAttdef->height());
  719. pAtt->setTag("Tag");
  720. pAtt->setFieldLength(25);
  721. pAtt->setLayer(layerName);
  722. char *pStr = pAttdef->tag();
  723. pAtt->setTag(pStr);
  724. free(pStr);
  725. pAtt->setFieldLength(pAttdef->fieldLength());
  726. if(strcmp(pAtt->tag(),"标注1")==0)
  727. {
  728. AcGePoint3d gg(dx/scalex,0,0);
  729. alignpoint=alignpoint+gg.asVector();
  730. AcGeVector3d vet(0,0,1);
  731. alignpoint=alignpoint.scaleBy(scalex,insertPoint);
  732. alignpoint=alignpoint.rotateBy(fwj,vet,insertPoint);
  733. pAtt->setTextString(Text1.GetBuffer(Text2.GetLength()));
  734. pAtt->setRotation(PI/2+fwj);
  735. pAtt->setHorizontalMode(AcDb::kTextCenter);
  736. pAtt->setVerticalMode(AcDb::kTextBottom);
  737. pAtt->setAlignmentPoint(alignpoint);
  738. pAtt->setColorIndex(text1color);
  739. pAtt->setHeight(text1height*scalex);
  740. pAtt->setWidthFactor(text1scator);
  741. }
  742. if(strcmp(pAtt->tag(),"标注2")==0)
  743. {
  744. AcGePoint3d gg(dx/scalex,0,0);
  745. alignpoint=alignpoint-gg.asVector();
  746. AcGeVector3d vet(0,0,1);
  747. alignpoint=alignpoint.scaleBy(scalex,insertPoint);
  748. alignpoint=alignpoint.rotateBy(fwj,vet,insertPoint);
  749. pAtt->setTextString(Text2.GetBuffer(Text2.GetLength()));
  750. pAtt->setRotation(PI/2+fwj);
  751. pAtt->setHorizontalMode(AcDb::kTextCenter);
  752. pAtt->setVerticalMode(AcDb::kTextBottom);
  753. pAtt->setAlignmentPoint(alignpoint);
  754. pAtt->setColorIndex(text2color);
  755. pAtt->setHeight(text2height*scalex);
  756. pAtt->setWidthFactor(text2scator);
  757. }
  758. if(strcmp(pAtt->tag(),"标注3")==0)
  759. {
  760. AcGePoint3d gg(0,dy/scalex,0);
  761. alignpoint=alignpoint+gg.asVector();
  762. AcGeVector3d vet(0,0,1);
  763. alignpoint=alignpoint.scaleBy(scalex,insertPoint);
  764. alignpoint=alignpoint.rotateBy(fwj,vet,insertPoint);
  765. pAtt->setTextString(Text3.GetBuffer(Text3.GetLength()));
  766. pAtt->setRotation(fwj);
  767. pAtt->setHorizontalMode(AcDb::kTextCenter);
  768. pAtt->setVerticalMode(AcDb::kTextBottom);
  769. pAtt->setAlignmentPoint(alignpoint);
  770. pAtt->setColorIndex(text3color);
  771. pAtt->setHeight(text3height*scalex);
  772. pAtt->setWidthFactor(text3scator);
  773. }
  774. AcDbObjectId attId;
  775. pBlkRef->appendAttribute(attId, pAtt);
  776. pAtt->close();
  777. }
  778. }
  779. // pEnt->setColorIndex(text1color);//按文本颜色设置改变颜色
  780. pEnt->close();
  781. }
  782. delete pIterator;
  783. pBlockTableRecord->close();
  784. pBlkRef->close();
  785. return TRUE;
  786. }


  787. //遍历多义线顶点坐标
  788. Acad::ErrorStatus CDrawFunction::IteratorPolyline(AcDbObjectId polylineID,AcGePoint3dArray& pointArray)
  789. {
  790. Acad::ErrorStatus es=Acad::eOk;
  791. AcDbObject* pObject=NULL;
  792. AcDbPolyline *pline=NULL;
  793. if((es=acdbOpenObject(pObject,polylineID,AcDb::kForRead))!=Acad::eOk)
  794. return es;
  795. if(!pObject->isKindOf(AcDbPolyline::desc()))
  796. {
  797. pObject->close();
  798. return Acad::eInvalidInput;
  799. }
  800. pline=AcDbPolyline::cast(pObject);
  801. int num=pline->numVerts();
  802. for (int i=0; i< num; i++)
  803. {
  804. AcGePoint3d temPt;
  805. if((es=pline->getPointAt(i, temPt))!=Acad::eOk)
  806. {
  807. pObject->close();
  808. return Acad::eInvalidInput;
  809. }
  810. pointArray.append(temPt);
  811. }
  812. pObject->close();
  813. return es;
  814. }


  815. Acad::ErrorStatus CDrawFunction::createGroup(CString groupname,AcDbObjectIdArray IdArray)
  816. {
  817. Acad::ErrorStatus es=Acad::eOk;
  818. AcDbDictionary *pGroupDict=NULL;
  819. AcDbGroup *pGroup=NULL;
  820. if((es=acdbHostApplicationServices()->workingDatabase()->getGroupDictionary(pGroupDict,AcDb::kForWrite))!=Acad::eOk)
  821. {
  822. return es;
  823. }
  824. AcDbObjectId groupId;
  825. es=pGroupDict->getAt(groupname,groupId);
  826. if(es==Acad::eInvalidKey)
  827. {
  828. acutPrintf("\n输入的词典名无效!");
  829. pGroupDict->close();
  830. return es;
  831. }
  832. else if(es==Acad::eKeyNotFound)
  833. {
  834. pGroup=new AcDbGroup("GroupDiscription");
  835. if((es=pGroupDict->setAt(groupname,pGroup,groupId))!=Acad::eOk)
  836. {
  837. pGroup->close();pGroupDict->close();return es;
  838. }
  839. }
  840. else if(es==Acad::eOk )
  841. {
  842. if((es=acdbOpenObject(pGroup,groupId,AcDb::kForWrite))!=Acad::eOk)
  843. {
  844. pGroupDict->close();return es;
  845. }
  846. }
  847. for(int i=0;i<IDARRAY.LENGTH();I++)
  848. pGroup->append(IdArray[i]);
  849. pGroup->setSelectable(FALSE);
  850. pGroupDict->close();
  851. pGroup->close();
  852. return es;
  853. }

  854. double CDrawFunction::getTextLength(AcDbObjectId textId)
  855. {
  856. Acad::ErrorStatus es=Acad::eOk;
  857. AcDbEntity *pEnt=NULL;
  858. if((es=acdbOpenObject(pEnt,textId,AcDb::kForRead))!=Acad::eOk)
  859. return -1;
  860. AcDbExtents Ext;
  861. pEnt->getGeomExtents(Ext);
  862. pEnt->close();
  863. AcGePoint3d minPt,maxPt;
  864. minPt=Ext.minPoint();
  865. maxPt=Ext.maxPoint();
  866. return acutDistance(asDblArray(minPt),asDblArray(maxPt));
  867. }



  868. void CDrawFunction::highlightEdge(const AcDbObjectId& objId,const int marker)
  869. {
  870. char dummy[133];
  871. AcDbEntity *pEnt;
  872. acdbOpenObject(pEnt,objId,AcDb::kForRead);
  873. AcGePoint3d pickpnt;
  874. AcGeMatrix3d xform;
  875. int numIds;
  876. AcDbFullSubentPath *subentIds;
  877. pEnt->getSubentPathsAtGsMarker(AcDb::kEdgeSubentType,marker,pickpnt,xform,numIds,subentIds);
  878. if(numIds>0)
  879. {
  880. pEnt->highlight(subentIds[0]);
  881. ads_getstring(0,"\npressto continue...",dummy);
  882. pEnt->unhighlight(subentIds[0]);

  883. }
  884. delete []subentIds;
  885. pEnt->close();
  886. }


  887. Acad::ErrorStatus CDrawFunction::readXrecord(CString dictName,CString xrecordName,CString &message)
  888. {
  889. AcDbDictionary *pDict=NULL;
  890. pDict=openDictionaryForRead(dictName,acdbHostApplicationServices()->workingDatabase());
  891. if(pDict)
  892. {
  893. AcDbXrecord *pXrec;
  894. pDict->getAt(xrecordName, (AcDbObject*&) pXrec,AcDb::kForRead);
  895. pDict->close();
  896. struct resbuf *pRbList;
  897. pXrec->rbChain(&pRbList);
  898. pXrec->close();
  899. message=pRbList->resval.rstring;
  900. acutRelRb(pRbList);
  901. return Acad::eOk;
  902. }
  903. else
  904. {
  905. return Acad::eInvalidInput;
  906. }
  907. }


  908. //**********************扩展记录操作(2002.12.12)***************//
  909. //****************打开扩展记录为写******************************//
  910. //**************************************************************//
  911. AcDbDictionary* CDrawFunction::openDictionaryForWrite(LPCTSTR dictName,
  912. bool createIfNotFound,AcDbDictionary* parentDict)
  913. {
  914. ASSERT(dictName != NULL);
  915. ASSERT(parentDict != NULL);
  916. ASSERT(parentDict->isWriteEnabled());
  917. AcDbDictionary* dict = NULL;
  918. AcDbObject* obj;
  919. Acad::ErrorStatus es;
  920. es = parentDict->getAt(dictName, obj, AcDb::kForWrite);
  921. if (es == Acad::eOk)
  922. {
  923. dict = AcDbDictionary::cast(obj);
  924. }
  925. else if (es == Acad::eKeyNotFound)
  926. {
  927. if (createIfNotFound)
  928. {
  929. dict = new AcDbDictionary;
  930. AcDbObjectId dictId;
  931. es = parentDict->setAt(dictName, dict, dictId);
  932. if (es != Acad::eOk)
  933. {
  934. delete dict;dict = NULL;
  935. }
  936. }
  937. }
  938. return dict;
  939. }



  940. /******************************************************************************/
  941. /* 関数名 : SetOSMODE */
  942. /* 機能 : 設置OSMODE */
  943. /* 入力引数 : int OSMODE値 */
  944. /* 出力引数 : 無 */
  945. /* 返り値 : 無 */
  946. /* 備考 : 無 */
  947. /******************************************************************************/
  948. void SetOSMODE(int nOSMODE)
  949. {
  950. struct resbuf rebNewOSMODE;
  951. rebNewOSMODE.restype = RTSHORT;
  952. rebNewOSMODE.resval.rint = nOSMODE;
  953. /*設置系統變量OSNAP的値。*/
  954. acedSetVar("OSMODE",&rebNewOSMODE);
  955. }

  956. /******************************************************************************/
  957. /* 関数名 : GetCrossPoint */
  958. /* 機能 : 得到交叉點的位置和交點坐標。 */
  959. /* 入力引数 : AcGePoint3dArray Polyline的點列。 */
  960. /* AcGePoint3d 選擇点。 */
  961. /* double PickBox的長度。 */
  962. /* Adesk::Boolean 閉合屬性。 */
  963. /* 出力引数 : int 交叉點的位置(前)。 */
  964. /* int 交叉點的位置(後)。 */
  965. /* AcGePoint3d 交點坐標 */
  966. /* 返り値 : TRUE 正常。 */
  967. /* FALSE 異常。 */
  968. /* 備考 : 無 */
  969. /******************************************************************************/
  970. BOOL GetCrossPoint(AcGePoint3dArray gePoi3dA, AcGePoint3d gePoi3d, double dPickLen, int & iCrsID1,
  971. int & iCrsID2, AcGePoint3d & gePoi3dCrs, Adesk::Boolean bIsClosed = Adesk::kTrue)
  972. {
  973. ads_point spt1, spt2, spt3, spt4;
  974. AcGePoint3d GePoi3dStart, GePoi3dNext;
  975. int iReturn, iNum;

  976. iNum = gePoi3dA.length();
  977. if (iNum < 2) {
  978. return FALSE;
  979. }

  980. /*得到交叉點的位置*/
  981. for (int i = 0; i < iNum; i++) {
  982. iCrsID1 = i;

  983. //得到前一個點
  984. asPnt3d(spt3) = gePoi3dA[iCrsID1];

  985. //得到後一個點
  986. if (i == iNum - 1) {
  987. //實體是否是閉合。
  988. if (!bIsClosed) {
  989. break;
  990. }
  991. else {
  992. iCrsID2 = 0;
  993. }
  994. }
  995. else {
  996. iCrsID2 = i + 1;
  997. }
  998. asPnt3d(spt4) = gePoi3dA[iCrsID2];

  999. spt3[Z] = 0;
  1000. spt4[Z] = 0;

  1001. /*X方向是否有交叉點。*/
  1002. spt1[X] = gePoi3d.x + (dPickLen / 2); spt2[X] = gePoi3d.x - (dPickLen / 2);
  1003. spt1[Y] = gePoi3d.y; spt2[Y] = gePoi3d.y;
  1004. spt1[Z] = 0; spt2[Z] = 0;
  1005. iReturn = acdbInters(spt1, spt2, spt3, spt4, 1, asDblArray(gePoi3dCrs));
  1006. if (iReturn == RTNORM) {
  1007. return TRUE;
  1008. }

  1009. /*Y方向是否有交叉點。*/
  1010. spt1[X] = gePoi3d.x; spt2[X] = gePoi3d.x;
  1011. spt1[Y] = gePoi3d.y + (dPickLen / 2); spt2[Y] = gePoi3d.y - (dPickLen / 2);
  1012. spt1[Z] = 0; spt2[Z] = 0;
  1013. iReturn = acdbInters(spt1, spt2, spt3, spt4, 1, asDblArray(gePoi3dCrs));
  1014. if (iReturn == RTNORM) {
  1015. return TRUE;
  1016. }
  1017. }
  1018. return FALSE;
  1019. }


  1020. 前阵困扰我的问题解决了,现在共享给大家:
  1021. //************************************************************************
  1022. //函数名称:getPointAtDistInGeCurve
  1023. //函数类型:Acad::ErrorStatus
  1024. //返回值:
  1025. //功能描述:返回曲线上距起点某距离值处的点。
  1026. //函数作者:Darcy
  1027. //创建日期:2003-XX-XX
  1028. //参数列表:
  1029. //变量名:pGeCurve 变量类型:const AcGeCurve3d * 变量说明:
  1030. //变量名:dist 变量类型:double 变量说明:
  1031. //变量名:point 变量类型:AcGePoint3d& 变量说明:
  1032. //备 注:
  1033. //************************************************************************
  1034. Acad::ErrorStatus getPointAtDistInGeCurve(const AcGeCurve3d * pGeCurve,double dist,AcGePoint3d& point)
  1035. {
  1036. Acad::ErrorStatus es = Acad::eOk;

  1037. if ( pGeCurve != NULL )
  1038. {
  1039. AcGePoint3d spt;
  1040. double pa=0.,datumParam=0.;
  1041. //距离从起点起算!
  1042. Adesk::Boolean posParamDir=Adesk::kTrue;

  1043. pGeCurve->hasStartPoint(spt);

  1044. datumParam = pGeCurve->paramOf(spt);;

  1045. pa = pGeCurve->paramAtLength(
  1046. datumParam,
  1047. dist,
  1048. posParamDir
  1049. );

  1050. point=pGeCurve->evalPoint(pa);
  1051. }
  1052. else
  1053. es = Acad::eInvalidInput;

  1054. return es;
  1055. }



  1056. bool
  1057. IsAtLine(CAD_POINT StartPt,CAD_POINT EndPt,CAD_POINT thePt)
  1058. /* 判断某点是否在直线上
  1059. StartPt 直线起点
  1060. EndPt 直线终点
  1061. thePt 所判断点
  1062. 返回:TRUE__在直线上 FALSE__不在直线上
  1063. */
  1064. {
  1065. //为保证计算精度,首先对点的坐标数据降级
  1066. CAD_VECTOR vec(StartPt.x,StartPt.y,StartPt.z);
  1067. StartPt=StartPt-vec;
  1068. EndPt=EndPt-vec;
  1069. thePt=thePt-vec;

  1070. // 直线方程系数
  1071. double A,B,C;
  1072. A = EndPt.y - StartPt.y;
  1073. B = StartPt.x - EndPt.x;
  1074. C = -1.0*(A*StartPt.x+B*StartPt.y);

  1075. if(StartPt.x==EndPt.x)
  1076. {
  1077. if(thePt.y>max(StartPt.y,EndPt.y) || thePt.y<MIN(STARTPT.Y,ENDPT.Y))
  1078. return FALSE;
  1079. }
  1080. else if(StartPt.y==EndPt.y)
  1081. {
  1082. if(thePt.x>max(StartPt.x,EndPt.x) || thePt.x<MIN(STARTPT.X,ENDPT.X))
  1083. return FALSE;
  1084. }
  1085. else
  1086. {
  1087. if( thePt.x>max(StartPt.x,EndPt.x) || thePt.x
  1088. thePt.y>max(StartPt.y,EndPt.y) || thePt.y
  1089. return FALSE;
  1090. }

  1091. if(fabs(A*thePt.x+B*thePt.y+C)>1.0E-6)
  1092. {
  1093. return FALSE;
  1094. }

  1095. return TRUE;
  1096. }

  1097. /*****************************************************/
  1098. BOOL
  1099. IsAtArc(CAD_POINT firstPt,CAD_POINT secondPt,
  1100. double radius,double direct,int More,CAD_POINT thePt)
  1101. /* 判断某点是否在圆弧上
  1102. firstPt 圆弧起点
  1103. secondPt 圆弧终点
  1104. radius 半径
  1105. direct 偏向( >=0__左偏 <0__右偏 )
  1106. More (More<0__小圆弧,More>0__大圆弧)
  1107. thePt 判断点
  1108. 返回:TRUE__在圆弧上 FALSE__不在圆弧上
  1109. */
  1110. {
  1111. CAD_POINT centerPt,sectionPt;
  1112. CAD_POINT startPt,endPt;
  1113. double startAngle,endAngle,chordAngle,vertAngle;
  1114. double intLine,chordLine;
  1115. /* centerPt 圆弧圆心
  1116. sectionPt 弦线中心点
  1117. startAngle 圆弧起点切线角度(弧度)
  1118. endAngle 圆弧终点切线角度(弧度)
  1119. chordAngle 弦线角度(弧度)
  1120. vertAngle 与弦线垂直的垂线角度(弧度)
  1121. intLine 弦线中心至圆心距离
  1122. chordLine 弦线长度
  1123. */
  1124. sectionPt.x = (firstPt.x + secondPt.x)/2;
  1125. sectionPt.y = (firstPt.y + secondPt.y)/2;

  1126. chordLine = sqrt( pow( (secondPt.x-firstPt.x),2 ) + pow( (secondPt.y-firstPt.y),2 ) );
  1127. intLine = sqrt((radius*radius-chordLine*chordLine/4) );

  1128. chordAngle = ads_angle(asDblArray(firstPt),asDblArray(secondPt)); //弦线的角度

  1129. if(direct>=0)//左偏
  1130. {
  1131. startPt=firstPt;
  1132. endPt=secondPt;
  1133. vertAngle=chordAngle+Pai/2;
  1134. }
  1135. else if(direct<0)//右偏
  1136. {
  1137. startPt=secondPt;
  1138. endPt=firstPt;
  1139. vertAngle=chordAngle-Pai/2;
  1140. }

  1141. if(More<=0)//小圆弧
  1142. {
  1143. centerPt.x=sectionPt.x+intLine*cos(vertAngle);
  1144. centerPt.y=sectionPt.y+intLine*sin(vertAngle);
  1145. }
  1146. else//大圆弧
  1147. {
  1148. centerPt.x=sectionPt.x-intLine*cos(vertAngle);
  1149. centerPt.y=sectionPt.y-intLine*sin(vertAngle);
  1150. }

  1151. if(fabs(centerPt.distanceTo(thePt)-radius)>1.0E-8)
  1152. return FALSE;
  1153. startAngle = ads_angle(asDblArray(centerPt),asDblArray(startPt));
  1154. endAngle = ads_angle(asDblArray(centerPt),asDblArray(endPt));

  1155. AcDbArc *pArc=new AcDbArc(centerPt,radius,startAngle,endAngle);
  1156. AcDbLine *pLine=new AcDbLine(centerPt,thePt);
  1157. AcGePoint3dArray Points;
  1158. pLine->intersectWith(pArc,AcDb::kOnBothOperands,Points);
  1159. if(Points.isEmpty()) return FALSE;

  1160. return TRUE;
  1161. }


  1162. 有时候,你需要这样调用Autocad命令
  1163. void SendCommandToAutoCad(CString cmd)
  1164. {
  1165. COPYDATASTRUCT cmdMsg;
  1166. cmdMsg.dwData = (DWORD)1;
  1167. cmdMsg.cbData = (DWORD)_tcslen(cmd) + 1;
  1168. cmdMsg.lpData = cmd.GetBuffer(cmd.GetLength()+1) ;
  1169. SendMessage(acedGetAcadFrame()->m_hWnd, WM_COPYDATA, NULL, (LPARAM)&cmdMsg);
  1170. }


  1171. 向AcDbObject添加扩展数据Xdata
  1172. 代码:

  1173. void affixXdata(char *appName, char *xData, AcDbObject *pObj)
  1174. {
  1175.   //向AcDbObject添加扩展数据Xdata
  1176.   struct resbuf *pRb, *pTemp;

  1177.   acdbRegApp(appName);
  1178.   pRb = acutNewRb(AcDb::kDxfRegAppName);
  1179.   pTemp = pRb;
  1180.   pTemp->resval.rstring = new char[strlen(appName)+1];
  1181.   strcpy(pTemp->resval.rstring, appName);

  1182.   pTemp->rbnext = acutNewRb(AcDb::kDxfXdAsciiString);
  1183.   pTemp = pTemp->rbnext;
  1184.   pTemp->resval.rstring = new char[strlen(xData)+1];
  1185.   strcpy(pTemp->resval.rstring, xData);

  1186.   pObj->setXData(pRb);
  1187.   acutRelRb(pRb);
  1188. }


  1189. //添加扩展数据
  1190. //实体添加扩展数据(字符串)
  1191. bool AddXData(CString appName, AcDbObjectId entId,CString data)
  1192. {
  1193. //open entity for read
  1194. AcDbEntity*pEnt;
  1195. Acad::ErrorStatus es=acdbOpenAcDbEntity(pEnt,entId,AcDb::kForRead);
  1196. if(es!=Acad::eOk)
  1197. {
  1198. ads_printf("error in open entity\n");
  1199. return false;
  1200. }
  1201. //get XData buffer
  1202. struct resbuf*pRb,*pTemp;
  1203. pRb=pEnt->xData(appName);
  1204. if(pRb!=NULL)//have XData
  1205. {
  1206. //pTemp移到表尾
  1207. pTemp=pRb;
  1208. for(pTemp=pRb;pTemp->rbnext!=NULL;pTemp=pTemp->rbnext){;}
  1209. }
  1210. else//NOT have XData
  1211. {
  1212. //create new xData
  1213. ads_regapp(appName);
  1214. pRb=ads_newrb(AcDb::kDxfRegAppName);
  1215. pRb->resval.rstring=(char*)malloc(appName.GetLength()+1);
  1216. strcpy(pRb->resval.rstring,appName);
  1217. pTemp=pRb;
  1218. }
  1219. //fill xData string
  1220. pTemp->rbnext=ads_newrb(AcDb::kDxfXdAsciiString);
  1221. pTemp=pTemp->rbnext;
  1222. pTemp->resval.rstring=(char*)malloc(data.GetLength()+1);
  1223. strcpy(pTemp->resval.rstring,data);
  1224. //add xData
  1225. es=pEnt->upgradeOpen();
  1226. if(es!=Acad::eOk)
  1227. {
  1228. ads_printf("\nError occur in updateOpen.");
  1229. pEnt->close();
  1230. ads_relrb(pRb);
  1231. return false;
  1232. }
  1233. es=pEnt->setXData(pRb);
  1234. if(es!=Acad::eOk)
  1235. {
  1236. ads_printf("\nError occur in setXData.");
  1237. pEnt->close();
  1238. ads_relrb(pRb);
  1239. return false;
  1240. }
  1241. //
  1242. pEnt->close();
  1243. ads_relrb(pRb);
  1244. return true;
  1245. }


  1246. //发命令前加按了两个ESCAPE
  1247. void SendCommand(char *cmd)
  1248. {
  1249. HWND wnd;
  1250. char cp[3];

  1251. wnd = adsw_acadMainWnd();
  1252. if(!wnd) return;

  1253. COPYDATASTRUCT cmddata;
  1254. cp[0] = VK_ESCAPE;
  1255. cp[1] = VK_ESCAPE;
  1256. cp[2] = NULL;
  1257. cmddata.dwData = (DWORD)1;
  1258. cmddata.cbData = (DWORD)strlen(cp)+1;
  1259. cmddata.lpData = cp;
  1260. SendMessage(wnd,WM_COPYDATA,(WPARAM)cp,(LPARAM)&cmddata);

  1261. cmddata.dwData = (DWORD)1;
  1262. cmddata.cbData = (DWORD)strlen(cmd)+1;
  1263. cmddata.lpData = cmd;
  1264. SendMessage(wnd,WM_COPYDATA,(WPARAM)wnd,(LPARAM)&cmddata);
  1265. }

  1266. //函数功能:根据用户指定的两点,自动创建破断线
  1267. void CAD_EXTBreakLine()
  1268. {
  1269. acutPrintf("指定两点,自动创建折线破断线\n");

  1270. ads_point StartPoint,EndPoint;
  1271. if(acedGetPoint(NULL,"\n请指定破断线的起点:",StartPoint)!=RTNORM) return;
  1272. if(acedGetPoint(StartPoint,"\n请指定破断线的终点:",EndPoint)!=RTNORM) return;
  1273. AcGePoint3d Start,End;
  1274. End = AcGePoint3d(EndPoint[X],EndPoint[Y],0);
  1275. Start = AcGePoint3d(StartPoint[X],StartPoint[Y],0);
  1276. float Length = Start.distanceTo(End);

  1277. AcGeVector3d Normal = End-Start;
  1278. Normal = Normal.normal(AcGeContext::gTol);

  1279. AcGePoint3d Point1(Start-Length*Normal*0.15);
  1280. AcGePoint3d Point2(Start+Length*Normal*0.45);
  1281. AcGePoint3d Point5(End-Length*Normal*0.45);
  1282. AcGePoint3d Point6(End+Length*Normal*0.15);

  1283. AcGeVector3d Normal2(-Normal.y,Normal.x,0);
  1284. AcGePoint3d Point3(Start+Length*Normal*0.5+Length*Normal2*0.10);
  1285. AcGePoint3d Point4(Start+Length*Normal*0.5-Length*Normal2*0.10);

  1286. AcGePoint3dArray vertices;
  1287. vertices.append(Point1);
  1288. vertices.append(Point2);
  1289. vertices.append(Point3);
  1290. vertices.append(Point4);
  1291. vertices.append(Point5);
  1292. vertices.append(Point6);
  1293. AddNewLayer("COMMANTARY");
  1294. AcDb2dPolyline* pBreakLine = new AcDb2dPolyline(AcDb::k2dSimplePoly,vertices,0,Adesk::kTrue,0,0,NULL);
  1295. pBreakLine->setLayer("COMMANTARY",TRUE);

  1296. AcGeMatrix3d mat;
  1297. acdbUcsMatrix(mat,acdbHostApplicationServices()->workingDatabase());
  1298. pBreakLine->transformBy(mat);

  1299. pBreakLine->makeOpen();
  1300. AddEntityToDb(pBreakLine);

  1301. }

  1302. //******************生成回转体**********************
  1303. /* pt -- 旋转基点
  1304. ver -- 旋转轴
  1305. angle -- 旋转角度(角度制)
  1306. 注意: 旋转轴不能垂直于面域平面、不能穿过面域*/
  1307. //**************************************************
  1308. void CreatRevolve(AcDbObjectId entid,
  1309. AcGeVector3d normal,
  1310. AcGePoint3d pt,
  1311. AcGeVector3d ver,
  1312. double angle)
  1313. {
  1314. Acad::ErrorStatus es;
  1315. AcDbCurve *curve;
  1316. AcDbObjectId tm;
  1317. if (acdbOpenObject(curve,entid,AcDb::kForWrite)!=Acad::eOk)
  1318. {
  1319. acutPrintf("打开实体失败!");
  1320. return ;
  1321. }
  1322. AcDbVoidPtrArray lines,regions1;
  1323. lines.append((void*)curve);
  1324. curve->close();
  1325. es = AcDbRegion::createFromCurves(lines,regions1);
  1326. if(es != Acad::eOk)
  1327. {
  1328. acutPrintf("获得面域失败!");
  1329. return ;
  1330. }
  1331. angle = angle*PI/180;
  1332. AcDbRegion *pregion1=AcDbRegion::cast((AcRxObject*)regions1[0]);
  1333. AcDb3dSolid *p3dobj = new AcDb3dSolid;
  1334. es = p3dobj->revolve(pregion1,pt,ver,angle);
  1335. if (es != Acad::eOk)
  1336. {
  1337. acutPrintf("建立回转体失败!请检查回转轴和基准点是否正确!");
  1338. }
  1339. pBlockTableRecord->appendAcDbEntity(tm,p3dobj);
  1340. p3dobj->close();
  1341. delete pregion1;
  1342. }

  1343. //复制对象
  1344. void cloneSameOwnerObjects()
  1345. {
  1346. // Step 1: Obtain the set of objects to be cloned.
  1347. ads_name sset;

  1348. if (acedSSGet(NULL, NULL, NULL, NULL, sset) != RTNORM) {
  1349. acutPrintf("\nNothing selected");
  1350. return;
  1351. }

  1352. // Step 2: Add obtained object IDs to list of objects
  1353. // to be cloned.
  1354. long length;
  1355. acedSSLength(sset, &length);
  1356. AcDbObjectIdArray objList;
  1357. AcDbObjectId ownerId = AcDbObjectId::kNull;

  1358. for (int i = 0; i < length; i++) {
  1359. ads_name ent;
  1360. acedSSName(sset, i, ent);
  1361. AcDbObjectId objId;
  1362. acdbGetObjectId(objId, ent);

  1363. // Check to be sure this has the same owner as the first
  1364. // object.
  1365. //
  1366. AcDbObject *pObj;
  1367. acdbOpenObject(pObj, objId, AcDb::kForRead);

  1368. if (pObj->ownerId() == ownerId)
  1369. objList.append(objId);
  1370. else if (i == 0) {
  1371. ownerId = pObj->ownerId();
  1372. objList.append(objId);
  1373. }
  1374. pObj->close();
  1375. }

  1376. acedSSFree(sset);

  1377. // Step 3: Get the object ID of the desired owner for
  1378. // the cloned objects. We'll use model space for
  1379. // this example.
  1380. //
  1381. AcDbBlockTable *pBlockTable;
  1382. acdbHostApplicationServices()->workingDatabase()
  1383. ->getSymbolTable(pBlockTable, AcDb::kForRead);

  1384. AcDbObjectId modelSpaceId;
  1385. pBlockTable->getAt(ACDB_MODEL_SPACE, modelSpaceId);
  1386. pBlockTable->close();

  1387. // Step 4: Create a new ID map.
  1388. //
  1389. AcDbIdMapping idMap;

  1390. // Step 5: Call deepCloneObjects().
  1391. //
  1392. acdbHostApplicationServices()->workingDatabase()
  1393. ->deepCloneObjects(objList, modelSpaceId, idMap);

  1394. // Now we can go through the ID map and do whatever we'd
  1395. // like to the original and/or clone objects.
  1396. //
  1397. // For this example, we'll print out the object IDs of
  1398. // the new objects resulting from the cloning process.
  1399. //
  1400. AcDbIdMappingIter iter(idMap);
  1401. for (iter.start(); !iter.done(); iter.next()) {
  1402. AcDbIdPair idPair;
  1403. iter.getMap(idPair);
  1404. if (!idPair.isCloned())
  1405. continue;
  1406. acutPrintf("\nObjectId is: %Ld",

  1407. idPair.value().asOldId());
  1408. }

  1409. }



  1410. 线和面求交,线和三角形交.

  1411. bool InterSection(XRay& ray,XPlan& plan,XPoint& point,float & t)
  1412. {
  1413. t = 0;

  1414. /***

  1415. -D - P0 * N(A,B,C)
  1416. t = --------------------
  1417. d * N(A,B,C)

  1418. 其中D 为平面方程中的D,
  1419. P0为射线的起点。而 d 为射线的方向

  1420. **/
  1421. t= -(plan.D + ray.m_Point.x * plan.A + ray.m_Point.y * plan.B + ray.m_Point.z * plan.C);
  1422. float t2 = (ray.m_Dir.x * plan.A + ray.m_Dir.y * plan.B + ray.m_Dir.z * plan.C) ;

  1423. if(t2 == 0.00)
  1424. return false;

  1425. t /= t2;


  1426. //求出交点
  1427. point = ray.m_Point + ray.m_Dir * t;

  1428. if( t < 0)
  1429. return false;

  1430. return true;
  1431. }


  1432. bool InterSection(XRay& ray,XTriangle& tri,XPoint& point,float & t)
  1433. {

  1434. XVector3D v1 = tri.m_points[1] - tri.m_points[0];
  1435. XVector3D v2 = tri.m_points[2] - tri.m_points[0];

  1436. XVector3D n = v1.cp(v2);
  1437. /**********************************************************************
  1438. ray = p0 + D * t
  1439. Tri : p1 - p3

  1440. (P2-P1) X (P3-P1) = N

  1441. N * (P - P1) = 0 ===>
  1442. N * (P0 + D * t - P1) = 0

  1443. ==>

  1444. N * (P0 - P1) + N * D * t = 0;
  1445. (P1-P0) * N
  1446. t = ----------------------
  1447. D * N
  1448. ******************************************************************/
  1449. t = n.dp(tri.m_points[0] - ray.m_Point);
  1450. t /= (ray.m_Dir.dp(n));
  1451. if( t < 0)
  1452. return false;

  1453. //求出交点
  1454. point = ray.m_Point + ray.m_Dir * t;

  1455. //判断点是不是在三角内部
  1456. v1 = point - tri.m_points[0];
  1457. v2 = point - tri.m_points[1];
  1458. XVector3D v3 = point - tri.m_points[2];

  1459. n = v1.cp(v2);
  1460. XVector3D n1 = v2.cp(v3);

  1461. if(n.isZero())
  1462. return true;
  1463. if(n1.isZero())
  1464. return true;

  1465. if( n.x * n1.x <= 0 )
  1466. return false;
  1467. if( n.y * n1.y <= 0 )
  1468. return false;
  1469. if( n.z * n1.z <= 0 )
  1470. return false;

  1471. return true;
  1472. }


  1473. //命名对象词典中无所需生成的词典(dictName)时生成词典,当DdictName存在时直接返回
  1474. void createDictionary(char *dictName)
  1475. {
  1476. AcDbDictionary *pNameDict;
  1477. acdbHostApplicationServices()->workingDatabase()->
  1478. getNamedObjectsDictionary(pNameDict, AcDb::kForWrite);


  1479. AcDbDictionary *pDict;
  1480. if (pNameDict->getAt(dictName, (AcDbObject*&) pDict,
  1481. AcDb::kForWrite) == Acad::eKeyNotFound)
  1482. {
  1483. pDict = new AcDbDictionary;
  1484. AcDbObjectId DictId;
  1485. pNameDict->setAt(dictName, pDict, DictId);
  1486. }
  1487. pNameDict->close();
  1488. pDict->close();
  1489. }

  1490. //将自定义对象pObj加入词典dictName,对象名为objectName。词典dictName不存在时,调用creatDictionary()生成。
  1491. void addObjectToDict(AcDbObject *pObj,char *objectName,char *dictName)
  1492. {
  1493. createDictionary(dictName);

  1494. AcDbDictionary *pNamedobj;
  1495. acdbHostApplicationServices()->workingDatabase()
  1496. ->getNamedObjectsDictionary(pNamedobj, AcDb::kForRead);

  1497. Acad::ErrorStatus es;
  1498. AcDbDictionary *pDict;
  1499. es=pNamedobj->getAt(dictName, (AcDbObject*&)pDict,
  1500. AcDb::kForWrite);

  1501. pNamedobj->close();

  1502. if (es==Acad::eOk) //使用if(pDict)不能起到判断的作用。
  1503. {
  1504. // add new objects to the dictionary,
  1505. // then close them.
  1506. AcDbObjectId rId1;
  1507. pDict->setAt(objectName, pObj, rId1);
  1508. pDict->close(); //不能放在该处{}之外,即未能正常获取,就无所谓关闭,否则出错。
  1509. }
  1510. // pDict->close();

  1511. }

  1512. //根据CAD两点坐标及对应的大地坐标(经纬距)计算两坐标系的夹角angle及比例转换系数K
  1513. //计算大地坐标系原点在CAD世界坐标系中的原点坐标earthorg
  1514. //函数return夹角,其单位为弧度
  1515. float EonAndXoyAngle(ads_point cadPt1,ads_point cadPt2,ads_point earthPt1,ads_point earthPt2,double &k,ads_point &earthOrg)
  1516. {
  1517. float angle;
  1518. double n,e,x,y;
  1519. float ax,ae;


  1520. n=(earthPt2[Y]-earthPt1[Y]);
  1521. e=(earthPt2[X]-earthPt1[X]);
  1522. x=(cadPt2[X]-cadPt1[X]);
  1523. y=(cadPt2[Y]-cadPt1[Y]);

  1524. k=sqrt((n*n+e*e)/(x*x+y*y));

  1525. ax=atan2(y,x); //线段与X轴的夹角
  1526. ae=atan2(n,e); //线段与E轴的夹角

  1527. angle=(ax-ae);

  1528. earthOrg[X]=cadPt1[X]-(cos(angle)*earthPt1[X]-sin(angle)*earthPt1[Y])/k;
  1529. earthOrg[Y]=cadPt1[Y]-(cos(angle)*earthPt1[Y]+sin(angle)*earthPt1[X])/k;
  1530. return angle;
  1531. }
  1532. //将CAD坐标转换为大地坐标
  1533. void xyToEn(ads_point cadPt,ads_point& earthPt,ads_point earthOrg,float angle, double k)
  1534. {
  1535. earthPt[X]=k*(cos(angle)*(cadPt[X]-earthOrg[X])+sin(angle)*(cadPt[Y]-earthOrg[Y]));
  1536. earthPt[Y]=k*(-sin(angle)*(cadPt[X]-earthOrg[X])+cos(angle)*(cadPt[Y]-earthOrg[Y]));
  1537. }
  1538. //将大地坐标转换为CAD坐标
  1539. void enToXY(ads_point earthPt,ads_point& cadPt,ads_point earthOrg,float angle, double k)
  1540. {
  1541. cadPt[X]=earthOrg[X]+(cos(angle)*earthPt[X]-sin(angle)*earthPt[Y])/k;
  1542. cadPt[Y]=earthOrg[Y]+(cos(angle)*earthPt[Y]+sin(angle)*earthPt[X])/k;
  1543. }
  1544. // 判定当前图形中有无给定块名的块表记录,有返回true、同时返回其id,
  1545. //否则返回flase
  1546. bool existBlock(char* pBlockName,AcDbObjectId &blockId)
  1547. {
  1548. AcDbBlockTable* pBlockTable;

  1549. acdbHostApplicationServices()->workingDatabase()
  1550. ->getSymbolTable(pBlockTable,AcDb::kForRead);
  1551. if(pBlockTable->has(pBlockName))
  1552. {
  1553. pBlockTable->getAt(pBlockName,blockId);
  1554. pBlockTable->close();
  1555. return true;
  1556. }
  1557. else
  1558. {
  1559. pBlockTable->close();
  1560. return false;
  1561. }
  1562. }
  1563. // 把一图形文件dwg插入当前图形作为一块,并给予块名。若有同名块就不再插入。
  1564. bool insertDwgToCurDwg(char* pFullPathFile,char* pBlockName,AcDbObjectId &blockId)
  1565. {
  1566. AcDbDatabase *pNewDb,*pCurDb;
  1567. Acad::ErrorStatus es;

  1568. if(existBlock(pBlockName,blockId))
  1569. {
  1570. return true;
  1571. }


  1572. pNewDb=new AcDbDatabase(Adesk::kFalse);
  1573. es=pNewDb->readDwgFile(pFullPathFile, _SH_DENYNO,false);
  1574. if (es!=Acad::eOk)
  1575. {
  1576. acutPrintf("读文件错");
  1577. delete pNewDb;
  1578. return false;
  1579. }

  1580. pCurDb =acdbHostApplicationServices()->workingDatabase();
  1581. if((es=pCurDb->insert(blockId, pBlockName,pNewDb, true))==Acad::eOk)
  1582. {

  1583. delete pNewDb;
  1584. return true;
  1585. }
  1586. else
  1587. {
  1588. delete pNewDb;
  1589. return false;
  1590. }

  1591. }


  1592. AcGePlane
  1593. CRegionGraph::getUcsPlane(AcDbDatabase* db)
  1594. {
  1595. ASSERT(db != NULL);

  1596. AcGeMatrix3d m;

  1597. if (acdbUcsMatrix(m, db)) {
  1598. AcGePoint3d origin;
  1599. AcGeVector3d xDir, yDir, zDir;

  1600. m.getCoordSystem(origin, xDir, yDir, zDir);
  1601. AcGePlane ucsPlane(origin, xDir, yDir);
  1602. return ucsPlane;
  1603. }
  1604. else {
  1605. ASSERT(0);
  1606. AcGePlane ucsPlane(AcGePoint3d::kOrigin, AcGeVector3d::kIdentity);
  1607. return ucsPlane;
  1608. }
  1609. }

  1610. //*****************************
  1611. AcGeVector3d
  1612. CRegionGraph::getUcsXAxis(AcDbDatabase* db)
  1613. {
  1614. ASSERT(db != NULL);

  1615. AcGeMatrix3d m;

  1616. if (acdbUcsMatrix(m, db)) {
  1617. AcGePoint3d origin;
  1618. AcGeVector3d xDir, yDir, zDir;

  1619. m.getCoordSystem(origin, xDir, yDir, zDir);
  1620. return xDir;
  1621. }
  1622. else {
  1623. ASSERT(0);
  1624. return AcGeVector3d::kXAxis;
  1625. }
  1626. }

  1627. /****************************************************************************
  1628. **
  1629. ** CRegionGraph::getUcsYAxis
  1630. ** returns current UCS Y Axis, even if currently the paperSpace viewport
  1631. **
  1632. ** **jma
  1633. */
  1634. //*************************************/

  1635. AcGeVector3d
  1636. CRegionGraph::getUcsYAxis(AcDbDatabase* db)
  1637. {
  1638. ASSERT(db != NULL);

  1639. AcGeMatrix3d m;

  1640. if (acdbUcsMatrix(m, db)) {
  1641. AcGePoint3d origin;
  1642. AcGeVector3d xDir, yDir, zDir;

  1643. m.getCoordSystem(origin, xDir, yDir, zDir);
  1644. return yDir;
  1645. }
  1646. else {
  1647. ASSERT(0);
  1648. return AcGeVector3d::kYAxis;
  1649. }
  1650. }

  1651. /****************************************************************************
  1652. **
  1653. ** CRegionGraph::getUcsZAxis
  1654. **
  1655. ** **jma
  1656. **
  1657. *************************************/

  1658. AcGeVector3d
  1659. CRegionGraph::getUcsZAxis(AcDbDatabase* db)
  1660. {
  1661. ASSERT(db != NULL);

  1662. AcGeMatrix3d m;

  1663. if (acdbUcsMatrix(m, db)) {
  1664. AcGePoint3d origin;
  1665. AcGeVector3d xDir, yDir, zDir;

  1666. m.getCoordSystem(origin, xDir, yDir, zDir);
  1667. return zDir;
  1668. }
  1669. else {
  1670. ASSERT(0);
  1671. return AcGeVector3d::kZAxis;
  1672. }
  1673. }


  1674. //指定两点Pnt3d1,Pnt3d2,查询Pnt3d3是否在由前两点组成的线的左边,坐标被投影成二维
  1675. BOOL CCommonHanlder::QueryPntIsOnLineLeft(AcGePoint3d Pnt3d1,AcGePoint3d Pnt3d2,AcGePoint3d Pnt3d3)
  1676. {
  1677. AcGePlane plane;
  1678. AcGePoint2d Pnt1,Pnt2,Pnt3;
  1679. Pnt1=Pnt3d1.convert2d(plane);
  1680. Pnt2=Pnt3d2.convert2d(plane);
  1681. Pnt3=Pnt3d3.convert2d(plane);
  1682. AcGeVector2d vec1,vec2;
  1683. AcGeLineSeg2d lin1,lin2;
  1684. lin1.set(Pnt2,Pnt1);
  1685. vec1=lin1.direction();
  1686. lin2.set(Pnt1,Pnt3);
  1687. vec2=lin2.direction();
  1688. if(sin(vec1.angle()-vec2.angle())>0)
  1689. return FALSE;
  1690. else
  1691. return TRUE;
  1692. }


  1693. /////////////////////创建窗体图层"Window_Layer"/////////////////
  1694. AcDbObjectId
  1695. createWindowsLayer()
  1696. {
  1697. //打开层表,打开方式为只写///
  1698. AcDbLayerTable *pLayerTable;
  1699. acdbHostApplicationServices()->workingDatabase()
  1700. ->getSymbolTable(pLayerTable,AcDb::kForWrite);
  1701. //初始化层表记录对象,并设定层表记录的名称“Window_Layer”

  1702. AcDbLayerTableRecord *pLayerTableRecord=new AcDbLayerTableRecord;
  1703. pLayerTableRecord->setName("Window_Layer");

  1704. AcCmColor color;
  1705. color.setColorIndex(1); // set color to red

  1706. pLayerTableRecord->setColor(color);
  1707. //层的其他属性(线型等)都用缺省值////

  1708. //将新建的层表记录添加到层表中,并将层表记录的ID保存到pLayerId作为函数的返回值////

  1709. AcDbObjectId pLayerId;
  1710. pLayerTable->add(pLayerId,pLayerTableRecord);
  1711. //关闭层表和层表记录对象///
  1712. pLayerTable->close();
  1713. pLayerTableRecord->close();

  1714. return pLayerId;
  1715. }



  1716. ///*************在指定点插入指定块*******************\\\
  1717. void CTestPlate::OnBlockInsert()
  1718. {
  1719. // TODO: Add your control notification handler code here
  1720. acDocManager->lockDocument(curDoc());
  1721. AcDbObjectId blockId; //要插入的块的Id值
  1722. AcDbBlockTable *pBlockTable;
  1723. acdbHostApplicationServices()->workingDatabase()
  1724. ->getSymbolTable(pBlockTable,AcDb::kForRead);
  1725. Acad::eOk!=pBlockTable->getAt("ASDK-NO-ATTR",
  1726. blockId,AcDb::kForRead)//根据块名获得要插入的块的ID值
  1727. AcDbBlockReference *pBlkRef=new AcDbBlockReference;//插入块实质是插入块的引用
  1728. pBlkRef->setBlockTableRecord(blockId);//指定所引用的图块表记录的对象ID
  1729. resbuf to,from;
  1730. from.restype=RTSHORT;//插入图块要进行用户坐标与世界坐标的转换
  1731. from.resval.rint=1;//UCS
  1732. to.restype=RTSHORT;
  1733. to.resval.rint=0;//WCS
  1734. AcGeVector3d normal(0.0,0.0,1.0);
  1735. acedTrans(&(normal.x),&from,&to,Adesk::kTrue,&(normal.x));//转换函数
  1736. AcGePoint3d basePoint(12,23,0);//指定的插入点(可以根据需要输入)
  1737. //acedGetPoint(NULL,"\nEnter insertion point:",asDblArray(basePoint));
  1738. pBlkRef->setPosition(basePoint);
  1739. pBlkRef->setRotation(0.0);
  1740. pBlkRef->setNormal(normal);
  1741. AcDbBlockTableRecord *pBlockTableRecord;
  1742. pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
  1743. pBlockTable->close();
  1744. AcDbObjectId newEntId;
  1745. pBlockTableRecord->appendAcDbEntity(newEntId,pBlkRef);
  1746. pBlockTableRecord->close();
  1747. pBlkRef->close();
  1748. acDocManager->lockDocument(curDoc());
  1749. }


  1750. //點遷多義線中之一直線,獲得線段之位置(圓弧可自行補上)
  1751. void GetPolyLineSelectLineIndex(AcDbPolyline *Poly,AcGePoint3d SelectPt,int Index)
  1752. {
  1753. AcGePoint2d tempPt;
  1754. double Distance=11111111;
  1755. tempPt.x=SelectPt.x;
  1756. tempPt.y=SelectPt.y;
  1757. AcDbVoidPtrArray entitySet;
  1758. Poly->explode(entitySet);
  1759. for(int i=0;i<ENTITYSET.LENGTH();I++)
  1760. {
  1761. AcDbEntity *pEnt;
  1762. pEnt=AcDbPolyline::cast((AcRxObject*)entitySet[i]);
  1763. if(pEnt->isA()==AcDbLine::desc())
  1764. {
  1765. AcDbLine *pLine=AcDbLine::cast(pEnt);
  1766. AcGePoint2d StPt,EndPt;
  1767. StPt.x=pLine->startPoint().x;
  1768. StPt.y=pLine->startPoint().y;
  1769. EndPt.x=pLine->endPoint().x;
  1770. EndPt.y=pLine->endPoint().y;
  1771. pLine->close();
  1772. AcGeLine2d GeLine(StPt,EndPt);
  1773. double pama=0;
  1774. if((GeLine.isOn(tempPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0)
  1775. {
  1776. Index=i;
  1777. break;
  1778. }
  1779. else
  1780. {
  1781. if(GeLine.distanceTo(tempPt)<DISTANCE)
  1782. {
  1783. AcGeLine2d tempLine;
  1784. GeLine.getPerpLine(tempPt,tempLine);
  1785. AcGePoint2d InsertPt;
  1786. GeLine.intersectWith(tempLine,InsertPt);
  1787. double pama=0;
  1788. if((GeLine.isOn(InsertPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0)
  1789. {
  1790. Distance=GeLine.distanceTo(tempPt);
  1791. Index=i;
  1792. }
  1793. }
  1794. }
  1795. }
  1796. pEnt->close();
  1797. }
  1798. }

  1799. int unionSolid(AcDbObjectId extrudeId,AcDb3dSolid *&pSolid)
  1800. {
  1801. //打开切弧实体 pExtrude
  1802. AcDb3dSolid *pExtrude;
  1803. if (Acad::eOk!=acdbOpenObject(pExtrude,extrudeId,AcDb::kForWrite)){
  1804. acutPrintf("\nError: Invalid extrude solid! ");
  1805. if(pExtrude!=NULL) pExtrude->close();
  1806. return RTERROR;
  1807. }
  1808. if(pExtrude==NULL){
  1809. acutPrintf("\nError: Invalid extrude solid! ");
  1810. return RTERROR;
  1811. }

  1812. // 实体
  1813. if(pSolid==NULL){
  1814. acutPrintf("\nError: Invalid extrude solid! ");
  1815. return RTERROR;
  1816. }
  1817. //Unit
  1818. if(Acad::eOk!=pSolid->booleanOper(AcDb::kBoolUnite,pExtrude))
  1819. {
  1820. acutPrintf("\nError: substract error!");
  1821. pExtrude->close();
  1822. return RTERROR;
  1823. }
  1824. pExtrude->close();
  1825. return RTNORM;
  1826. }

  1827. Acad::ErrorStatus DyZoom(AcGePoint2d CenterPt,double Height,double Width)
  1828. {
  1829. Acad::ErrorStatus es;
  1830. AcDbViewTableRecord *view=new AcDbViewTableRecord();
  1831. view->setCenterPoint(CenterPt);
  1832. view->setFrontClipAtEye(false);
  1833. view->setHeight(Height);
  1834. view->setWidth(Width);
  1835. es=acedSetCurrentView(view,NULL);
  1836. view->close();
  1837. delete view;
  1838. view=NULL;
  1839. return es;
  1840. }

  1841. 创建circle和newlayer(加了错误检查的代码)
  1842. Acad::ErrorStatus
  1843. createCircle(AcDbObjectId& circleId)
  1844. {
  1845. circleId = AcDbObjectId::kNull;

  1846. AcGePoint3d center(9.0,3.0,0.0);
  1847. AcGeVector3d normal(0.0,0.0,0.1);
  1848. AcDbCircle*pCirc=new AcDbCircle(center,normal,2.0);

  1849. if(pCirc==NULL)
  1850. return Acad::eOutOfMemory;

  1851. AcDbBlockTable*pBlockTable;
  1852. Acad::ErrorStatus es=
  1853. acdbHostApplicationServices()->workingDatabase()->
  1854. getSymbolTable(pBlockTable,AcDb::kForRead);
  1855. if(es!=Acad::eOk)
  1856. {
  1857. delete pCirc;
  1858. return es;
  1859. }

  1860. AcDbBlockTableRecord*pBlockTableRecord;
  1861. es=pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
  1862. if(es!=Acad::eOk){
  1863. Acad::ErrorStatus es2=pBlockTable->close();
  1864. if (es2!=Acad::eOk){
  1865. acrx_abort("\n应用程序X关闭边失败,错误码为:%d",
  1866. acadErrorStatusText(es2));
  1867. }
  1868. delete pCirc;
  1869. return es;
  1870. }

  1871. es=pBlockTable->close();
  1872. if (es!=Acad::eOk)
  1873. {
  1874. acrx_abort("\n应用程序X关闭边失败,错误码为:%d",
  1875. acadErrorStatusText(es));
  1876. }
  1877. es=pBlockTableRecord->appendAcDbEntity(circleId,pCirc);
  1878. if(es!=Acad::eOk){
  1879. Acad::ErrorStatus es2=pBlockTableRecord->close();
  1880. if (es2!=Acad::eOk){
  1881. acrx_abort("\n应用程序X关闭模型空间块表记录失败,错误码为:%s",
  1882. acadErrorStatusText(es2));
  1883. }
  1884. delete pCirc;
  1885. return es;
  1886. }

  1887. es=pBlockTableRecord->close();
  1888. if(es!=Acad::eOk)
  1889. {
  1890. acrx_abort("\n应用程序X关闭模型空间块表记录失败,错误码为:%d",
  1891. acadErrorStatusText(es));
  1892. }

  1893. es=pCirc->close();
  1894. if(es!=Acad::eOk){
  1895. acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
  1896. acadErrorStatusText(es));
  1897. }
  1898. return es;
  1899. }


  1900. Acad::ErrorStatus
  1901. createNewLayer()
  1902. {
  1903. AcDbLayerTableRecord*pLayerTableRecord=new AcDbLayerTableRecord;

  1904. if (pLayerTableRecord==NULL)
  1905. return Acad::eOutOfMemory;

  1906. Acad::ErrorStatus es
  1907. =pLayerTableRecord->setName("ASDK_MYLAYER");
  1908. if(es!=Acad::eOk)
  1909. {
  1910. delete pLayerTableRecord;
  1911. return es;
  1912. }

  1913. AcDbLayerTable*pLayerTable;
  1914. es=acdbHostApplicationServices()->workingDataBase()->
  1915. getSymbolTable(pLayerTable,AcDb::kForWrite);
  1916. if(es!=Acad::eOk){
  1917. delete pLayerTableRecord;
  1918. return es;
  1919. }
  1920. //由于先型对象ID的缺省直是0,不是有效的ID,必须将其设置为有效的ID,
  1921. //即CONTINUOUS线型。其他数据成员都为有效缺省值,因此不须对其进行设置
  1922. AcDbLinetypeTable*pLinetypeTbl;
  1923. es=acdbHostApplicationServices()->workingDatabase()->
  1924. getSymbolTable(pLinetypeTbl,AcDb::kForRead);
  1925. if (es!=Acad::eOk){
  1926. delete pLayerTableRecord;
  1927. es=pLayerTable->close();

  1928. if(es!=Acad::eOk){
  1929. acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
  1930. acadErrorStatusText(es));
  1931. }
  1932. return es;
  1933. }

  1934. AcDbObjectId ltypeobjId;
  1935. es=pLinetypeTbl->getAt("CONTINUOUS",ltypeObjId);
  1936. if (es!=Acad::eOk){
  1937. delete pLayerTableRecord;
  1938. es=pLayerTable->close();
  1939. if (es!=Acad::eOk){
  1940. acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",acadErrorStatusText(es));
  1941. }
  1942. return es;
  1943. }

  1944. pLayerTableRecord->setLinetypeObjectId(ltypeObjId);
  1945. es=pLayerTable->add(pLayerTableRecord);
  1946. if (es!=Acad::eOk)
  1947. {
  1948. Acad::ErrorStatus es2=pLayerTable->close();
  1949. if (es2!=Acad::eOk)
  1950. {

  1951. acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
  1952. acadErrorStatusText(es2));
  1953. }
  1954. delete pLayerTableRecord;
  1955. return es;
  1956. }

  1957. es=pLayerTable->close();
  1958. if(es!=Acad::eOk)
  1959. {
  1960. acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
  1961. acadErrorStatusText(es));
  1962. }
  1963. es=pLayerTableRecord->close();
  1964. if(es!=Acad::eOk)
  1965. {
  1966. acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
  1967. acadErrorStatusText(es));
  1968. }
  1969. return es;
  1970. }


  1971. Function name: setEntityElevation
  1972. Parameters: ads_name, double
  1973. Return value: False or True

  1974. According to the Entity name setting the elevation value of text,polyline,2dpolyline,block,hatch which is given
  1975. BOOL setEntityElevation(ads_name en, double elevation)
  1976. { AcDbEntity* pEnt;
  1977. AcDbObjectId eId;

  1978. if(acdbGetObjectId(eId, en) != Acad::eOk) return FALSE;
  1979. if(acdbOpenObject(pEnt,eId,AcDb::kForWrite) != Acad::eOk) return FALSE;
  1980. if(!pEnt) { acutPrintf("\nFail to open Database!!"); return FALSE; }

  1981. if(pEnt->isKindOf(AcDbPolyline::desc())) {
  1982. ((AcDbPolyline*)pEnt)->setElevation(elevation);
  1983. pEnt->close(); // Finished with the pEnt header.
  1984. }
  1985. else if(pEnt->isKindOf(AcDb2dPolyline::desc())) {
  1986. ((AcDb2dPolyline*)pEnt)->setElevation(elevation);
  1987. pEnt->close(); // Finished with the pEnt header.
  1988. }
  1989. else if(pEnt->isKindOf(AcDbText::desc())) {
  1990. AcGePoint3d point3d;
  1991. point3d = ((AcDbText*)pEnt)->position();
  1992. point3d[2] = elevation;
  1993. ((AcDbText*)pEnt)->setPosition(point3d);
  1994. pEnt->close(); // Finished with the pEnt header.
  1995. }
  1996. else if(pEnt->isKindOf(AcDbMText::desc())) {
  1997. AcGePoint3d point3d;
  1998. point3d = ((AcDbMText*)pEnt)->location();
  1999. point3d[2] = elevation;
  2000. ((AcDbMText*)pEnt)->setLocation(point3d);
  2001. pEnt->close(); // Finished with the pEnt header.
  2002. }
  2003. else if(pEnt->isKindOf(AcDbBlockReference::desc())) {
  2004. AcGePoint3d point3d;
  2005. point3d = ((AcDbBlockReference*)pEnt)->position();
  2006. point3d[2] = elevation;
  2007. ((AcDbBlockReference*)pEnt)->setPosition(point3d);
  2008. pEnt->close(); // Finished with the pEnt header.
  2009. }
  2010. else if(pEnt->isKindOf(AcDbShape::desc())) {
  2011. AcGePoint3d point3d;
  2012. point3d = ((AcDbShape*)pEnt)->position();
  2013. point3d[2] = elevation;
  2014. ((AcDbShape*)pEnt)->setPosition(point3d);
  2015. pEnt->close(); // Finished with the pEnt header.
  2016. }
  2017. else if(pEnt->isKindOf(AcDbHatch::desc())) {
  2018. ((AcDbHatch*)pEnt)->setElevation(elevation);
  2019. pEnt->close(); // Finished with the pEnt header.
  2020. }
  2021. else { pEnt->close(); return FALSE; }
  2022. return TRUE;

  2023. 推荐使用方法二,基本上适用于AutoCAD所有版本(我可是跟踪分析注册表,费了好大一番劲才搞出来的哟)。"W-SCAS2006"为/p参数启动的配置名称。
  2024. 只要在退出消息里调用这个函数就行,如:在kPreQuitMsg消息里。
  2025. /*case AcRx::kPreQuitMsg:
  2026. REStoreDefPro();
  2027. break;*/

  2028. //////////////////////////////////////////////////////////////////////////
  2029. // 函数名: REStoreDefPro
  2030. // 输 入: /
  2031. // 输 出: /
  2032. // 功能描述: 恢复当前配置文件为AutoCAD默认配置
  2033. // 全局变量: /
  2034. // 调用模块:
  2035. // 作 者: Meng Huang
  2036. // 日 期: 2006.02.21
  2037. // 修 改: Meng Huang
  2038. // 日 期: 2006.02.23
  2039. // 版 本: 1.1
  2040. //////////////////////////////////////////////////////////////////////////
  2041. void REStoreDefPro()
  2042. {
  2043. // 方法一:
  2044. // AcApProfileNameArray arrNameList;
  2045. // int nProfiles = acProfileManagerPtr()->ProfileListNames(arrNameList);
  2046. // CString strProName;
  2047. // Acad::ErrorStatus es;
  2048. //
  2049. // for (int i = 0; i < nProfiles; i++)
  2050. // {
  2051. // strProName = arrNameList[i];
  2052. // if (strProName == _T("<<未命名配置>>") ||
  2053. // strProName == _T("<>"))
  2054. // {
  2055. // /*es = */acProfileManagerPtr()->ProfileSetCurrent(strProName);
  2056. // break;
  2057. // }
  2058. // else if (i == nProfiles - 1)
  2059. // {
  2060. // /*es = */acProfileManagerPtr()->ProfileCopy(_T("<<未命名配置>>"), _T("W-SCAS2006"), _T("默认配置名称"));
  2061. // /*es = */acProfileManagerPtr()->ProfileReset(_T("<<未命名配置>>"));
  2062. // /*es = */acProfileManagerPtr()->ProfileSetCurrent(_T("<<未命名配置>>"));
  2063. // }
  2064. // }

  2065. // 方法二:
  2066. HKEY hKey;
  2067. LONG lRetVal;
  2068. DWORD unSubKeyNum = 0;
  2069. DWORD sizeSubKey; // 子键大小
  2070. TCHAR subKeyName[255]; // 子键名

  2071. // 打开HKEY_USERS主键
  2072. lRetVal = RegOpenKeyEx(HKEY_USERS, _T(""), 0, KEY_READ, &hKey);
  2073. if (lRetVal != ERROR_SUCCESS)
  2074. return;

  2075. // 查得当前键下的子键项数
  2076. lRetVal = RegQueryInfoKey(hKey, NULL, NULL, NULL, &unSubKeyNum, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  2077. if (lRetVal != ERROR_SUCCESS)
  2078. {
  2079. RegCloseKey(hKey);
  2080. return;
  2081. }

  2082. // 取得含有S-1-5-21-***********,但不包含Classes字符串的子键名
  2083. CString strTmp;
  2084. CString strFullSubKey;
  2085. for (DWORD i = 0; i < unSubKeyNum; i++)
  2086. {
  2087. subKeyName[0] = '\0'; // 清空子键
  2088. sizeSubKey = 255; // 赋缓冲区大小
  2089. lRetVal = RegEnumKeyEx(hKey, i, subKeyName, &sizeSubKey, NULL, NULL, NULL, NULL);
  2090. if (lRetVal != ERROR_SUCCESS)
  2091. {
  2092. RegCloseKey(hKey);
  2093. return;
  2094. }

  2095. strTmp = subKeyName;
  2096. if (strTmp.Find(_T("S-1-5-21-")) != -1 &&
  2097. strTmp.Find(_T("Classes")) == -1)
  2098. {
  2099. strFullSubKey = strTmp;
  2100. break;
  2101. }
  2102. }
  2103. if (strFullSubKey.IsEmpty()) // 没有找到符合条件的子键
  2104. {
  2105. RegCloseKey(hKey);
  2106. return;
  2107. }

  2108. // 获取和配置文件相对应的的子键字符串
  2109. char *pchRegProfileKey;
  2110. acProfileManagerPtr()->ProfileRegistryKey(pchRegProfileKey, _T("W-SCAS2006"));
  2111. if (pchRegProfileKey == NULL)
  2112. {
  2113. RegCloseKey(hKey);
  2114. return;
  2115. }
  2116. else
  2117. {
  2118. strTmp = pchRegProfileKey;
  2119. acutDelString(pchRegProfileKey);
  2120. int nStart = strTmp.Find('\');
  2121. int nEnd = strTmp.ReverseFind('\');
  2122. strTmp = strTmp.Mid(nStart, nEnd - nStart);
  2123. RegCloseKey(hKey);
  2124. }

  2125. strFullSubKey += strTmp; // 要修改键值的完整子键字符串

  2126. // 修改对应的键值(把该键值置空,下次启动CAD如果没指定/p参数则使用<<未命名配置>>,
  2127. // 如果该配置不在,CAD会自动新一个,并使用它)
  2128. lRetVal = RegOpenKeyEx(HKEY_USERS, strFullSubKey, 0, KEY_WRITE, &hKey);
  2129. if (lRetVal != ERROR_SUCCESS)
  2130. {
  2131. RegCloseKey(hKey);
  2132. return;
  2133. }
  2134. BYTE uchProFile[1] = {'\0'};
  2135. RegSetValueEx(hKey, NULL, 0, REG_SZ, uchProFile, 1);
  2136. RegCloseKey(hKey);
  2137. }


  2138. //************************************************************************
  2139. //函数名称:selectEntityInLayer
  2140. //函数类型:Acad::ErrorStatus
  2141. //返回值:  正常:Acad::eOk
  2142. //功能描述:选择指定图层上的所有实体!
  2143. //函数作者:Darcy
  2144. //创建日期:200X-XX-XX
  2145. //参数列表:
  2146. //变量名:nLayerName     变量类型:const char*           变量说明:(输入)图层名
  2147. //变量名:nIDs           变量类型:AcDbObjectIdArray&    变量说明:(输出)图层中实体的对象标识符集合
  2148. //************************************************************************
  2149. Acad::ErrorStatus selectEntityInLayer(const char* nLayerName,AcDbObjectIdArray& nIDs)
  2150. {
  2151.   Acad::ErrorStatus es = Acad::eOk;

  2152.   ads_name ents;
  2153.   struct resbuf *rb;
  2154.   rb=acutNewRb(AcDb::kDxfLayerName);
  2155.   rb->restype=8;
  2156.   rb->resval.rstring=(char*)nLayerName;
  2157.   rb->rbnext=NULL;
  2158.   acedSSGet("X",NULL,NULL,rb,ents);
  2159.   long entNums=0;
  2160.   acedSSLength(ents,&entNums);
  2161.   if (entNums == 0)
  2162.     es = Acad::eInvalidInput;
  2163.   else
  2164.   {
  2165.     for (long a = 0; a < entNums ; a ++)
  2166.     {
  2167.       AcDbObjectId  objId;
  2168.       ads_name      ent;
  2169.       acedSSName(ents,a,ent);
  2170.       acdbGetObjectId(objId, ent);
  2171.       nIDs.append(objId);
  2172.     }
  2173.   }
  2174.   acedSSFree(ents);
  2175.   acutRelRb(rb);

  2176.   return es;
  2177. }


  2178. //创建字体类型
  2179. 代码:

  2180. void DrawAndEdit::createTextStyle(char *styleName, char *fontName, char *bigFontName, double textSize, double xScale, double obliqueAngle, double trPercent)
  2181. {
  2182.   AcDbTextStyleTable *pTextStyleTable;

  2183.     acdbHostApplicationServices()->workingDatabase()
  2184.         ->getSymbolTable(pTextStyleTable, AcDb::kForRead);
  2185.   AcDbTextStyleTableRecord *pRecord;
  2186.   pTextStyleTable->getAt(ACDB_MODEL_SPACE,pRecord,
  2187.     AcDb::kForWrite);

  2188.   if (!pTextStyleTable->has(styleName)){
  2189.     AcDbObjectId recId;
  2190.         pTextStyleTable->close();
  2191.     acdbHostApplicationServices()->workingDatabase()
  2192.       ->getSymbolTable(pTextStyleTable, AcDb::kForWrite);
  2193.     pRecord = new AcDbTextStyleTableRecord();
  2194.     pRecord->setName(styleName);
  2195.     pRecord->setFileName(fontName);
  2196.     pRecord->setBigFontFileName(bigFontName);
  2197.     pRecord->setTextSize(textSize);
  2198.     pRecord->setXScale(xScale);
  2199.     pRecord->setObliquingAngle(obliqueAngle);
  2200.     pRecord->setPriorSize(trPercent);
  2201.     pTextStyleTable->add(recId,pRecord);
  2202.     pRecord->close();
  2203.   }

  2204.   pTextStyleTable->close();

  2205.   return;
  2206. }


  2207. //生成新组(sGroupName)
  2208. //追加数组中所有实体到该组中
  2209. //组名字 ,   Id数组
  2210. int createGroup(CString sGroupName,
  2211.                    const AcDbObjectIdArray *idArr)
  2212. {
  2213.   AcDbGroup       *pGroup = new AcDbGroup((LPSTR)(LPCTSTR)sGroupName);
  2214.   AcDbObjectId     groupObjectId;
  2215.   AcDbDictionary  *pGroupDict = NULL;

  2216.   acdbHostApplicationServices()->workingDatabase()
  2217.         ->getGroupDictionary(pGroupDict, AcDb::kForWrite);
  2218.   pGroupDict->setAt(sGroupName, pGroup, groupObjectId);
  2219.   pGroupDict->close();
  2220.     pGroup->close();
  2221.     acdbOpenObject(pGroup, groupObjectId, AcDb::kForWrite);
  2222.   for (int i = 0; i < idArr->length(); i++)
  2223.   {
  2224.     groupObjectId = idArr->at(i);
  2225.     pGroup->append(groupObjectId);   
  2226.   }
  2227.   pGroup->close();
  2228.   return TRUE;
  2229. }


  2230. AcDbObjectId CreateNewTextStyle()
  2231. {
  2232.   AcDbTextStyleTable *pTextStyleTable;
  2233.   AcDbTextStyleTableRecord *pTextStyleTableRcd
  2234.   AcDbObjectId textId;
  2235.   acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTable,AcDb::kForWrite);

  2236.   if (!pTextStyleTable->has(StyleName)
  2237.   {
  2238.     AcDbTextStyleTableRecord *pTSTblRcd= new AcDbTextStyleTableRecord;
  2239.     pTSTblRcd->setName(StyleName);
  2240.     pTSTblRcd->setFileName(fontName);
  2241.     pTSTblRcd->setBigFontFileName(bigfontName);
  2242.     pTSTblRcd->setTextSize(textSize);
  2243.     pTSTblRcd->setXScale(xScale);
  2244.     pTSTblRcd->setObliquingAngle(obliqueAngle);
  2245.     pTSTblRcd->setPriorSize(trPercent);
  2246.     pTextStyleTable->add(textId,pTextStyleTableRcd);
  2247.     pTSTblRcd->close();
  2248.   }
  2249.     pTextStyleTable->close();
  2250.   return textId;

  2251. }

  2252. void CMyDatabase::rotationGroup(CString strGroupName ,CReiPoint ptRotation,double rotationAngle)
  2253. {
  2254.   AcGePoint3d pt;
  2255.   AcDbDictionary *pGroupDict;
  2256.   acdbCurDwg()->getGroupDictionary(pGroupDict,AcDb::kForWrite);
  2257.   AcDbObjectId groupId;
  2258.   AcDbGroup *pGroup;
  2259.   pt.x=ptRotation.x;
  2260.   pt.y=ptRotation.y;
  2261.   pt.z=ptRotation.z;
  2262.   if(pGroupDict->getAt(strGroupName,groupId)==Acad::eOk)
  2263.        acdbOpenObject(pGroup,groupId,AcDb::kForWrite);
  2264.   else
  2265.   {
  2266.     pGroupDict->close();
  2267.     return;
  2268.   }
  2269.   pGroupDict->close();
  2270.   AcDbGroupIterator* pIter=pGroup->newIterator();
  2271.   AcDbEntity* pEnt;
  2272.   AcDbObjectId objId;
  2273.     pIter=pGroup->newIterator();
  2274.   for(;!pIter->done();pIter->next())
  2275.   {
  2276.     objId=pIter->objectId();
  2277.     acdbOpenAcDbEntity(pEnt,objId,AcDb::kForWrite);
  2278.     rotationEntity(pEnt,pt,rotationAngle);
  2279.     pEnt->close();
  2280.   }
  2281.   delete pIter;
  2282.   pGroup->close();
  2283. }

  2284. //************************************************************************
  2285. //函数名称:selectEntityInLayer
  2286. //函数类型:Acad::ErrorStatus
  2287. //返回值:
  2288. //功能描述:选择指定层上的实体,得到其对象属性标识符!
  2289. //函数作者:Darcy
  2290. //创建日期:200X-XX-XX
  2291. //参数列表:
  2292. //变量名:nLayerName      变量类型:CString               变量说明:
  2293. //变量名:nIDs            变量类型:AcDbObjectIdArray&    变量说明:
  2294. //变量名:nModelSpace     变量类型:bool                  变量说明:
  2295. //************************************************************************
  2296. Acad::ErrorStatus    selectEntityInLayer(
  2297.                                          CString nLayerName,
  2298.                                          AcDbObjectIdArray& nIDs,
  2299.                                          bool nModelSpace
  2300.                                          )
  2301. {
  2302.     Acad::ErrorStatus es=Acad::eOk;

  2303.     AcDbBlockTable*        pBlockTable=NULL;
  2304.     AcDbBlockTableRecord*  pSpaceRecord=NULL;
  2305.     if (acdbHostApplicationServices()->workingDatabase()==NULL)
  2306.         return Acad::eNoDatabase;
  2307.     if ((es = acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead))==Acad::eOk)
  2308.     {
  2309.         char entryName[13];
  2310.         if (nModelSpace)
  2311.             strcpy(entryName,ACDB_MODEL_SPACE);
  2312.         else
  2313.             strcpy(entryName,ACDB_PAPER_SPACE);
  2314.         //Get the Model or Paper Space record and open it for read:  
  2315.         if ((es = pBlockTable->getAt((const char*)entryName, pSpaceRecord, AcDb::kForRead))==Acad::eOk)
  2316.         {
  2317.                 AcDbBlockTableRecordIterator* pIter;
  2318.                 if (pSpaceRecord->newIterator(pIter)==Acad::eOk)
  2319.                 {
  2320.                     for (pIter->start();!pIter->done();pIter->step())
  2321.                     {
  2322.                         char *name=NULL;
  2323.                         AcDbEntity* pEntity;
  2324.                         if (pIter->getEntity(pEntity,AcDb::kForRead)==Acad::eOk)
  2325.                         {
  2326.                             name=pEntity->layer();
  2327.                             if (nLayerName.CompareNoCase(name)==0)
  2328.                                 nIDs.append(pEntity->objectId());

  2329.                             pEntity->close();
  2330.                             acutDelString(name);
  2331.                         }
  2332.                     }
  2333.                     delete pIter;
  2334.                 }            
  2335.                 pSpaceRecord->close();
  2336.         }   
  2337.         pBlockTable->close();
  2338.     }

  2339.     return es;
  2340. }

  2341. 功能:   判断两点是否相同(误差在指定范围内)
  2342. 参数:   p1、p2为欲判定的两点
  2343. 返回值: 0 -- 不相同;1 -- 相同
  2344. //=========================================================
  2345. int Equal_Points (const ads_point p1, const ads_point p2)
  2346. {
  2347.     // 指定误差范围
  2348.     const ads_real Equality_Margin = (ads_real)0.00000001;
  2349.    
  2350.     int c ;
  2351.     for (c = X ; c <= Z ; c++) {
  2352.         if (fabs(p1[c] - p2[c]) > Equality_Margin) {
  2353.             return (0) ;
  2354.         }
  2355.     }
  2356.     return (1) ;
  2357. }
  2358. BOOL BaseHandle::PointIsInPolygon(AcGePoint3d pt, AcGePoint3dArray ptArr)
  2359. {
  2360.     int ptNum,i,interNum;
  2361.     AcGePoint3d ptA,ptB;
  2362.     ads_point pt0,pt1,pt2,ptIns,ptX;
  2363.    
  2364.     interNum = 0;
  2365.     pt0[X] = 0.0;
  2366.     pt0[Y] = 0.0;
  2367.     pt0[Z] = 0.0;
  2368.     ptX[X] = pt.x;
  2369.     ptX[Y] = pt.y;
  2370.     ptX[Z] = pt.z;
  2371.     ptNum = ptArr.length();
  2372.     for (i = 0;i < ptNum - 1;i++){
  2373.       ptA = ptArr.at(i);
  2374.       ptB = ptArr.at(i + 1);
  2375.       pt1[X] = ptA.x;
  2376.       pt1[Y] = ptA.y;
  2377.       pt1[Z] = 0.0;
  2378.       pt2[X] = ptB.x;
  2379.       pt2[Y] = ptB.y;
  2380.       pt2[Z] = 0.0;
  2381.       if (acdbInters(ptX,pt0,pt1,pt2,1,ptIns) == RTNORM){
  2382.         interNum++;
  2383.       }
  2384.     }
  2385. //上述交点没有判断是否与某一端点重合,如果有interNum-1,有
  2386. //兴趣的朋友可以修改完成。
  2387.     if (interNum % 2 == 0){
  2388.       return false;
  2389.     }else{
  2390.       return true;
  2391.     }
  2392. }


  2393. //==========================================================
  2394. 功能:新建一个图层
  2395. 参数:LayerName -- 图层名,LayerColor -- 颜色名
  2396. 返回:图层ID
  2397. //==========================================================
  2398. AcDbObjectId CreateNewLayer(CString LayerName, AcCmColor LayerColor)
  2399. {
  2400.   // 获得当前图形数据库的符号表
  2401.   AcDbLayerTable *pLayerTable;
  2402.   acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTable,
  2403.     AcDb::kForWrite);
  2404.   // 生成新的图层表记录
  2405.   AcDbLayerTableRecord *pLayerTableRecord = new AcDbLayerTableRecord;
  2406.   pLayerTableRecord->setName(LayerName);    // 设置图层名
  2407.   pLayerTableRecord->setColor(LayerColor);  // 设置图层颜色
  2408.   AcDbObjectId layerId;
  2409.   pLayerTable->add(layerId,pLayerTableRecord);
  2410.   // 关闭图层表和图层表记录
  2411.   pLayerTable->close();
  2412.   pLayerTableRecord->close();
  2413.   return layerId;
  2414. }

  2415. //==========================================================
  2416. 功能:在指定图层上新建一条直线
  2417. 参数:见注释
  2418. 返回:直线ID
  2419. //==========================================================
  2420. AcDbObjectId CreateLine( double x1,double y1,double z1,  // 起点坐标
  2421.                      double x2,double y2,double z2,  // 终点坐标
  2422.                      AcDbObjectId layer)                   // 直线所在图层
  2423. {
  2424.   AcGePoint3d StartPt(x1,y1,z1);  // 起点
  2425.   AcGePoint3d EndPt(x2,y2,z2);  // 终点
  2426.   AcDbLine *pLine = new AcDbLine(StartPt,EndPt);
  2427.   // 获得当前图形数据库的符号表
  2428.   AcDbBlockTable *pBlockTable;
  2429.   acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,
  2430.     AcDb::kForRead);
  2431.   // 获得符号表中的模型空间块表记录指针,用于添加对象
  2432.   AcDbBlockTableRecord *pBlockTableRecord;
  2433.   pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
  2434.   pBlockTable->close();
  2435.   // 将直线添加到模型空间块表记录中
  2436.   AcDbObjectId lineId;
  2437.   pLine->setLayer(layer,Adesk::kTrue);  // 设置直线所在图层
  2438.   pBlockTableRecord->appendAcDbEntity(lineId,pLine);
  2439.   // 关闭块表记录指针和直线指针
  2440.   pBlockTableRecord->close();
  2441.   pLine->close();
  2442.   // 返回直线ID号
  2443.   return lineId;
  2444. }



  2445. 已知一段弧的起点和终点以及其凸度,求其圆心

  2446. int getCenter(ads_point startPoint,ads_point endPoint,double bulge,ads_point& center)
  2447. {
  2448. if (bulge==0.0)
  2449. {
  2450. ads_point_set(startPoint,center);
  2451. return 0;
  2452. }
  2453. ads_point startPt,endPt;
  2454. if (bulge<0.0)
  2455. {
  2456. ads_point_set(endPoint,startPt);
  2457. ads_point_set(startPoint,endPt);
  2458. bulge=bulge*(-1.0);
  2459. }
  2460. else
  2461. {
  2462. ads_point_set(startPoint,startPt);
  2463. ads_point_set(endPoint,endPt);
  2464. }
  2465. double distStartToEnd,distX,distY,radius;
  2466. distStartToEnd=ads_distance(startPt,endPt);
  2467. distX=distStartToEnd/2.0;
  2468. distY=bulge*distX;
  2469. radius=((distX*distX)+(distY*distY))/(2.0*distY);

  2470. double tmpAng;
  2471. ads_point tmpPt;

  2472. tmpAng=ads_angle(startPt,endPt);
  2473. ads_polar(startPt,tmpAng,distX,tmpPt);
  2474. ads_polar(tmpPt,(tmpAng+(_PI/2.0)),(radius-distY),center);
  2475. return 1;

  2476. };

评分

参与人数 2D豆 +15 收起 理由
QiaoCheng + 5 多谢分享
XDSoft + 10 很给力!经验;技术要点;资料分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2014-9-23 10:20:07 | 显示全部楼层
好东西。有对应的头文件就更好了
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2014-12-26 16:13:41 | 显示全部楼层
好东西,就是有的函数开头没有说明,看起来很费劲。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2014-12-30 09:37:02 | 显示全部楼层
好好学习,减少了很多时间啊!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2015-1-12 10:04:49 | 显示全部楼层
多谢分享,非常感谢
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 28个

财富等级: 恭喜发财

发表于 2015-1-26 12:47:09 | 显示全部楼层
谢谢楼主总结分享
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2017-7-11 16:54:33 | 显示全部楼层
膜拜大神
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 22:55 , Processed in 0.310878 second(s), 47 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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