天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 69|回复: 0

ObjectArx自定义实体入门(C++)及注意事项 .

[复制链接]

该用户从未签到

主题

0

回帖

2912

积分

管理员

积分
2912
发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
[code]本文介绍了构造自定义实体的步骤、必须继承的函数和必须注意的事项

1.新建一个从AcDbEntity继承的类,如EntTest,必须添加的头文件: "stdarx.h","acadstrc.h", "geassign.h"。

2.在该类头文件的类声明中添加宏:ACRX_DECLARE_MEMBERS(EntTest);

3.在该类的cpp文件中,类的前面添加宏:

ACRX_DXF_DEFINE_MEMBERS(EntTest, AcDbEntity,AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent,0,平面实体,"autoCAD");//第5个参数为在CAD中该实体类的名称,
MAKE_ACDBOPENOBJECT_FUNCTION(EntTest);

4.从AcDbEntity类中重载几个必须的虚函数:

      virtual Adesk::Boolean worldDraw(AcGiWorldDraw* mode);
     virtual Acad::ErrorStatus dwgInFields(AcDbDwgFiler*);
    virtual Acad::ErrorStatus dwgOutFields(AcDbDwgFiler*) const;
virtual Acad::ErrorStatus getGripPoints(AcGePoint3dArray& gripPoints,AcDbIntArray&osnapModes,AcDbIntArray& geomIds) const;
    virtual Acad::ErrorStatus moveGripPointsAt(const AcDbIntArray& indices,const AcGeVector3d& offset);

5.实现第4步中几个虚函数必须注意:

  在dwgInFields函数的开始处必须有:

   assertWriteEnabled();
AcDbObject::dwgInFields(pFiler);
if(pFiler->filerType()==AcDb::kWblockCloneFiler)
{
  AcDbHardPointerId Id;
  pFiler->readItem(&Id);  
}

在dwgOutFields函数的开始处必须有:

     assertReadEnabled();
  AcDbObject::dwgOutFields(pFiler);
  if (pFiler->filerType()==AcDb::kWblockCloneFiler)
   pFiler->writeHardPointerId((AcDbHardPointerId)ownerId());

上叙两个函数是用来写入写出自定义实体的成员变量的,以保证在拖动夹点的过程中这些变量是实时变化的(可以在worldDraw或moveGripPointsAt中改变这些值)。成员变量不能为指针,在要用数组指针的场合可用Carray数组来实现。

6.这一步很重要,少了它就不是自定义实体了,呵呵。

在InitApplication()函数中添加:

  EntTest::rxInit();
  acrxBuildClassHierarchy();

下面是一个最简单的自定义实体的例子,一根线段,变量成员只有StartPoint,EndPoint,最重要的那几个重载的虚函数的作用和运行顺序得靠自己去慢慢摸索了。我想这个例子刚入门的你还是有点帮助的,注意:AddEntityToDbs()函数也比不可少

头文件:

#if !defined(AFX_ACDBENTITYTEST_H__E622CCB1_4E1D_4985_8813_0CEE3BF4ED20__INCLUDED_)
#define AFX_ACDBENTITYTEST_H__E622CCB1_4E1D_4985_8813_0CEE3BF4ED20__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "stdafx.h"
#include "stdarx.h"
#include "acadstrc.h"
#include "geassign.h"
#include "acedads.h"
#include "math.h"
#include <fstream.h>

class AcdbEntityTest:public AcDbEntity
{undefined
public:
ACRX_DECLARE_MEMBERS(AcdbEntityTest);
AcdbEntityTest();
virtual ~AcdbEntityTest();

virtual void Initialize();



//自定义实体必须重载的函数----------------------------------------------------
    virtual Adesk::Boolean worldDraw(AcGiWorldDraw* mode);
virtual Acad::ErrorStatus dwgInFields(AcDbDwgFiler*);
    virtual Acad::ErrorStatus dwgOutFields(AcDbDwgFiler*) const;
virtual Acad::ErrorStatus getGripPoints(AcGePoint3dArray& gripPoints,
  AcDbIntArray& osnapModes,
  AcDbIntArray& geomIds) const;

    virtual Acad::ErrorStatus moveGripPointsAt(const AcDbIntArray& indices,
  const AcGeVector3d& offset);

virtual Acad::ErrorStatus getOsnapPoints(
  AcDb::OsnapMode   osnapMode,
  int               gsSelectionMark,
  const AcGePoint3d&    pickPoint,
  const AcGePoint3d&    lastPoint,
  const AcGeMatrix3d&   viewXform,
  AcGePoint3dArray& snapPoints,
  AcDbIntArray&     geomIds) const ;

private:
AcGePoint3d StartPoint;
AcGePoint3d EndPoint;
};
#endif // !defined(AFX_ACDBENTITYTEST_H__E622CCB1_4E1D_4985_8813_0CEE3BF4ED20__INCLUDED_)

cpp:

#include "stdafx.h"
#include "stdarx.h"
#include "Resource.h"
#include "acadstrc.h"
#include "geassign.h"
#include "acedads.h"
#include "math.h"
#include <fstream.h>
#include "AcdbEntityTest.h"
#include "global.h"

ACRX_DXF_DEFINE_MEMBERS(AcdbEntityTest, AcDbEntity,AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent,0,EntityDIM,"autoCAD");
MAKE_ACDBOPENOBJECT_FUNCTION(AcdbEntityTest);

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

AcdbEntityTest::AcdbEntityTest()
{undefined
abarx=new AboutArx();
}

AcdbEntityTest::~AcdbEntityTest()
{undefined

}
Acad::ErrorStatus AcdbEntityTest::dwgInFields(AcDbDwgFiler* pFiler)
{undefined
assertWriteEnabled();
AcDbObject::dwgInFields(pFiler);
if(pFiler->filerType()==AcDb::kWblockCloneFiler)
{undefined
  AcDbHardPointerId Id;
  pFiler->readItem(&Id);  
}

pFiler->readItem(&StartPoint);
pFiler->readItem(&EndPoint);

return pFiler->filerStatus();
}
Acad::ErrorStatus AcdbEntityTest::dwgOutFields(AcDbDwgFiler* pFiler) const
{undefined
     assertReadEnabled();
  AcDbObject::dwgOutFields(pFiler);

  if (pFiler->filerType()==AcDb::kWblockCloneFiler)
  {undefined
   pFiler->writeHardPointerId((AcDbHardPointerId)ownerId());
  }

  pFiler->writeItem(StartPoint);
  pFiler->writeItem(EndPoint);

return pFiler->filerStatus();
}

Adesk::Boolean  AcdbEntityTest::worldDraw(AcGiWorldDraw* pWd)
{

AcDbObjectId originalLineTypeID;
originalLineTypeID=pWd->subEntityTraits().lineTypeId();
Adesk::UInt16 originalColor;
originalColor=pWd->subEntityTraits().color();

//获取虚线线型ID

Acad::ErrorStatus err;
AcDbObjectId dashId;
AcDbLinetypeTable*pLinetypeTable;

acdbHostApplicationServices()->workingDatabase()->loadLineTypeFile("ACAD_ISO02W100","acadiso.lin");
acdbHostApplicationServices()->workingDatabase()->getLinetypeTable(pLinetypeTable,AcDb::kForRead);
err=pLinetypeTable->getAt("ACAD_ISO02W100",dashId,Adesk::kTrue);
pLinetypeTable->close();

pWd->subEntityTraits().setLineType(originalLineTypeID);  
pWd->subEntityTraits().setColor(1);

AcGePoint3d Verts[2];
Verts[0]=StartPoint;
Verts[1]=EndPoint;
pWd->geometry().polyline(2,Verts);
return AcDbEntity::worldDraw(pWd);
}
Acad::ErrorStatus AcdbEntityTest::getGripPoints(AcGePoint3dArray& gripPoints,
           AcDbIntArray& osnapModes,
           AcDbIntArray& geomIds)const
{undefined
assertReadEnabled();
gripPoints.append(StartPoint);
gripPoints.append(EndPoint);
return Acad::eOk;
}
Acad::ErrorStatus AcdbEntityTest::moveGripPointsAt(const AcDbIntArray& indices,const AcGeVector3d& offset)
{undefined
assertReadEnabled();
Acad::ErrorStatus es=Acad::eOk;
if(indices.length()==0)
{undefined
  return Acad::eOk;
}

int num=indices.length();
for(int i=0;i<num;i++)
{undefined
  int k=indices[i];
  if (k==0)
  {undefined
   StartPoint.x+=offset.x;
   StartPoint.y+=offset.y;
  }
  else
  {undefined
   EndPoint.x+=offset.x;
   EndPoint.y+=offset.y;
  }


}


return Acad::eOk;
}

Acad::ErrorStatus AcdbEntityTest::getOsnapPoints(
             AcDb::OsnapMode   osnapMode,
             int   gsSelectionMark,
             const AcGePoint3d&    pickPoint,
             const AcGePoint3d&    lastPoint,
             const AcGeMatrix3d&   viewXform,
             AcGePoint3dArray& snapPoints,
             AcDbIntArray&     geomIds) const
{   //设置捕捉点
assertReadEnabled();
Acad::ErrorStatus es=Acad::eOk;
if(osnapMode!=AcDb::kOsModeEnd)
{undefined
  return Acad::eOk;
}
return Acad::eOk;
}


void AcdbEntityTest::Initialize()
{undefined
AcGePoint3d inputPt;
if(ads_getpoint(NULL,"/n ==NOTE== 请输入起点: ",asDblArray(inputPt))!=RTNORM)
{undefined
  ads_printf("/n ==NOTE== 错误");
}
  StartPoint=inputPt;
AcDbObjectId entityID=AddEntityToDbs(this);
int track,type;
track=1;
struct resbuf entdata;
while (track>0)
{undefined
  if(ads_grread (track, &type, &entdata)!=RTNORM)
  {undefined
   ads_printf("/n !=RTNORM");
   break;
  }
  AcdbEntityTest * pmyEntity=this;
  acdbOpenObject(pmyEntity,entityID, AcDb::kForWrite);
  this->assertWriteEnabled();
  if(type==5)
  {undefined
   AcGePoint3d movePt;    //鼠标当前位置
   movePt[X]=entdata.resval.rpoint[X];
   movePt[Y]=entdata.resval.rpoint[Y];
   EndPoint=movePt;
   this->close();
  }
  
  if(type==3)
  {undefined
   track=1;
   AcGePoint3d movePt;    //鼠标当前位置
   movePt[X]=entdata.resval.rpoint[X];
   movePt[Y]=entdata.resval.rpoint[Y];
   EndPoint=movePt;
   this->close();
   break;
  }
  if(type==13)
  {undefined
   //鼠标右键退出
   track=1;
   AcGePoint3d movePt;  //鼠标当前位置
   movePt[X]=entdata.resval.rpoint[X];
   movePt[Y]=entdata.resval.rpoint[Y];
   EndPoint=movePt;
   this->close();
   break;
  }
}
}

AcDbObjectId AboutArx::AddEntityToDbs(AcDbEntity*pEnt)//将生成的自定义实体对象加入CAD数据库
{undefined
AcDbBlockTable *bt;
acDocManager->lockDocument(acDocManager->curDocument(),AcAp::kWrite);
AcDbObjectId entityId;
acdbCurDwg()->getBlockTable(bt,AcDb::kForRead);
AcDbBlockTableRecord *btr;
bt->getAt(ACDB_MODEL_SPACE,btr,AcDb::kForWrite);
bt->close();
btr->appendAcDbEntity(entityId,pEnt);
pEnt->close();
btr->close();
return entityId;
}
[/code]

 

 

 

 

ObjectArx自定义实体入门(C++)及注意事项 .
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池|中国膜结构网_中国空间膜结构协会

GMT+8, 2024-11-1 09:32 , Processed in 0.133287 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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