找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2112|回复: 0

[曲线] 从 AcDbPolyline 派生遇到的问题

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-2-2 08:10:00 | 显示全部楼层 |阅读模式

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

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

×
AcDb: deriving from AcDbPolyline

问题:

Deriving from AcDbPolyline works correctly, however, it seems DWG files
containing instances of my custom entity derived from AcDbPolyline do not
contain proxy graphic information for them. Why?

解答:

AutoCAD does not save the proxy graphic information for classes derived from
AcDbPolyline. The reason for this is that the 'saveAs()' method is implemented
as follows:


  1. void AcDbPolyline::saveAs(AcGiWorldDraw* mode, AcDb::SaveType st) {
  2.     assertReadEnabled();

  3.     if ( st == AcDb::kR12Save )
  4.         // ... do something to convert into AcDb2dPolyline
  5. }


Whereas it should be as follows:

  1. void AcDbPolyline::saveAs(AcGiWorldDraw* mode, AcDb::SaveType st) {
  2.     assertReadEnabled();

  3.     if ( st == AcDb::kR12Save )
  4.         // ... do something to convert into AcDb2dPolyline
  5.     else
  6.         worldDraw (mode) ;
  7. }


As a result, proxy graphic data is never saved, which causes your derived
AcDbPolyline entity to be invisible if your DBX-object enabler application is
not loaded. Although this is not ideal, it does not affect any other behavior or
functionality of your custom entity.

A simple override of the 'saveAs()' method can solve this problem as follows:


  1. void AcDbMyPolyline::saveAs(AcGiWorldDraw* mode, AcDb::SaveType st) {
  2.     assertReadEnabled();

  3.     if ( st == AcDb::kR12Save )
  4.         AcDbPolyline::saveAs (mode, st) ;
  5.     else
  6.         worldDraw (mode) ;
  7. }


This would be the correct solution, however there is another known problem in
AutoCAD that prevents the 'saveAs()' method from being called for proxy graphic
generation. This second problem is more serious because it prevents 'saveAs()'
method being called. The reason for this is that AutoCAD makes an exception in
the case of an AcDbPolyline entity. Instead of asking 'are you an AcDbPolyline
class?', AutoCAD asks 'Are you a "kind of" AcDbPolyline?' This also includes any
of your derived classes from AcDbPolyline.

The only solution is to pretend your custom AcDbPolyline entity is not an
AcDbPolyline class, even if it really is. This will cause AutoCAD to properly
call the 'saveAs()' method and then your implementation of the 'worldDraw()'
method for your custom entity. In order to pretend that it is not an
AcDbPolyline entity, just implement the 'ACRX_DXF_DEFINE_MEMBERS' ARX macro as
follows:

  1. ACRX_DXF_DEFINE_MEMBERS(AcDbMyPolyline, AcDbCurve,
  2. AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent,
  3. AcDbProxyEntity::kNoOperation,
  4. ACDBMYPOLYLINE, ACDBMYAPP
  5. );


There are other requirements in the implemention of the 'worldDraw()' method:
you should not call AcDbPolyline::worldDraw() when called from the 'saveAs()'
method because this would cause the same problem that was outlined previously.
(AutoCAD filters out all AcDbPolyline calls in that context). An 'unhandled
exception error' will occur as a result. (rememeber we are pretending not to be
an AcDbPolyline entity). To solve the problem, you need to modify the 'saveAs()'
method like this:

  1. bool mbDrawWithExplode = false ;

  2. void AcDbMyPolyline::saveAs(AcGiWorldDraw* mode, AcDb::SaveType st) {
  3.     assertReadEnabled();

  4.     if ( st == AcDb::kR12Save ) {
  5.         AcDbPolyline::saveAs (mode, st) ;
  6.     } else {
  7.         mbDrawWithExplode = true ;
  8.         worldDraw (mode) ;
  9.         mbDrawWithExplode = false ;
  10.     }
  11. }
  12. Implement your 'worldDraw()' method like this:


  13. Adesk::Boolean AsdkOPLine::worldDraw(AcGiWorldDraw* mode)
  14. {
  15.     assertReadEnabled();

  16.     if ( !mbDrawWithExplode ) {
  17.         bPEdit =true ;
  18.         if ( AcDbPolyline::worldDraw (mode) != Adesk::kTrue ) {
  19.             bPEdit =false ;
  20.             return (Adesk::kFalse) ;
  21.         }
  22.         bPEdit =false ;
  23.     } else {
  24.         AcDbVoidPtrArray ents ;
  25.         AcDbPolyline::explode (ents) ;
  26.         for ( int j =ents.length () - 1 ; j >= 0 ; j-- ) {
  27.             ((AcDbEntity *)ents.at (j))->worldDraw (mode) ;
  28.             delete (AcDbEntity *)ents.at (j) ;
  29.         }
  30.         ents.setLogicalLength (0) ;
  31.     }
  32.     return (Adesk::kTrue) ;
  33. }


If you pretend it is not an AcDbPolyline, the AutoCAD "_PEDIT" command will not
work for your custom entity anymore. If you do not intend to support "_PEDIT",
then it is not a problem. However, if you intend to support "_PEDIT" then you
need to pretend that it is an AcDbPolyline only for the "_PEDIT" command. To
support "_PEDIT" for a special class requires a considerable amount of work.
First, you need to rewrite the 'ACRX_DXF_DEFINE_MEMBERS' ARX macro like this:

  1. bool bPEdit =false ;

  2. void set_bPEdit (bool flag) {
  3.     bPEdit =flag ;
  4. }

  5. //------------------------------------------
  6. #define MY_ACRX_DEFINE_MEMBERS(CLASS_NAME) \
  7. AcRxClass* CLASS_NAME::desc() \
  8. { \
  9.    return (AcRxClass*)((AcRxDictionary*)acrxSysRegistry() \
  10.         ->at(ACRX_CLASS_DICTIONARY))->at(bPEdit ? "AcDbPolyline" :
  11. "AcDbMyPolyline"); \
  12. } \
  13. AcRxClass* CLASS_NAME::isA() const \
  14. { \
  15.    return (AcRxClass*)((AcRxDictionary*)acrxSysRegistry() \
  16.         ->at(ACRX_CLASS_DICTIONARY))->at(bPEdit ? "AcDbPolyline" :
  17. "AcDbMyPolyline"); \
  18. } \
  19. AcRxClass* CLASS_NAME::gpDesc = NULL

  20. //----------------------------------------------------------------------
  21. #define MY_ACRX_DXF_DEFINE_MEMBERS(CLASS_NAME,PARENT_CLASS,DWG_VERSION,\
  22.           MAINTENANCE_VERSION,PROXY_FLAGS,DXF_NAME,APP) \
  23. MY_ACRX_DEFINE_MEMBERS(CLASS_NAME); \
  24. static AcRxObject * make##CLASS_NAME() { return new CLASS_NAME(); } \
  25. void CLASS_NAME::rxInit() { \
  26.    if (CLASS_NAME::gpDesc != NULL) \
  27.     return; \
  28.    CLASS_NAME::gpDesc = newAcRxClass(#CLASS_NAME, #PARENT_CLASS, \
  29.     DWG_VERSION,MAINTENANCE_VERSION,PROXY_FLAGS, \
  30.     &make##CLASS_NAME, #DXF_NAME, #APP); \
  31. }
  32. Replace the 'ACRX_DXF_DEFINE_MEMBERS' ARX macro with the following definition:


  33. MY_ACRX_DXF_DEFINE_MEMBERS(AcDbMyPolyline, AcDbCurve,
  34.             AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent,
  35.             AcDbProxyEntity::kNoOperation,
  36.             ACDBMYPOLYLINE, ACDBMYAPP
  37. );
  38. The last item you need to implement is an AcEditorReactor to detect when the
  39. "_PEDIT" command is executed and set the pPEdit flag to True as follows:


  40. void AsdkEdReactor::commandWillStart(const TCHAR* cmdStr)
  41. {
  42. if ( _tcsicmp (cmdStr, _T("PEDIT")) == 0 )
  43.   set_bPEdit (true) ;
  44. }
  45. void AsdkEdReactor::commandEnded(const TCHAR* cmdStr)
  46. {
  47. if ( _tcsicmp (cmdStr, _T("PEDIT")) == 0 )
  48.   set_bPEdit (false) ;
  49. }
  50. void AsdkEdReactor::commandCancelled(const TCHAR* cmdStr)
  51. {
  52. if ( _tcsicmp (cmdStr, _T("PEDIT")) == 0 )
  53.   set_bPEdit (false) ;
  54. }
  55. void AsdkEdReactor::commandFailed(const TCHAR* cmdStr)
  56. {
  57. if ( _tcsicmp (cmdStr, _T("PEDIT")) == 0 )
  58.   set_bPEdit (false) ;
  59. }


When the "_PEDIT" command starts, the AsdkEdReactor::commandWillStart() reactor
callback is called and the bPEdit Boolean is set to True. Then AutoCAD verifies
that the entity selected is "kind of" an AcDbPolyline. To do this AutoCAD calls
the AcRxObject::isKindOf() method, this in turn calls the class descriptor of
the entity (AcDbMyPloyline::desc()), which returns the AutoCAD AcDbPolyline
class descriptor. In all other situations, it returns a real custom entity class
descriptor (child of AcDbCurve).

Forward Compatibility Issues
The two problems will be fixed in a later release of AutoCAD, it should be asked
if the custom entity derived from AcDbPolyline will continue to work. Although a
custom entity derived from AcDbPolyline will continue to work, you will also be
allowed to remove the workarounds in this solution without any problems,
including reloading files from one implementation to another.

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

本版积分规则

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

GMT+8, 2024-12-14 17:38 , Processed in 0.176883 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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