anywhere

导航

【CAD】自定义实体的步骤(转)

本文介绍了构造自定义实体的步骤、必须继承的函数和必须注意的事项

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

{

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()

{

abarx=new AboutArx();

}

AcdbEntityTest::~AcdbEntityTest()

{

 

}

Acad::ErrorStatus AcdbEntityTest::dwgInFields(AcDbDwgFiler* pFiler)

{

assertWriteEnabled();

AcDbObject::dwgInFields(pFiler);

if(pFiler->filerType()==AcDb::kWblockCloneFiler)

{

AcDbHardPointerId Id;

pFiler->readItem(&Id);

}

pFiler->readItem(&StartPoint);

pFiler->readItem(&EndPoint);

return pFiler->filerStatus();

}

Acad::ErrorStatus AcdbEntityTest::dwgOutFields(AcDbDwgFiler* pFiler) const {

assertReadEnabled();

AcDbObject::dwgOutFields(pFiler);

if (pFiler->filerType()==AcDb::kWblockCloneFiler)

{

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();

ErrorStatuserr;AcDbObjectIddashId;AcDbLinetypeTable*pLinetypeTable;acdbHostApplicationServices()->workingDatabase()->loadLineTypeFile(&

ObjectARX自定义实体的步骤

 

//获取虚线线型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

{

assertReadEnabled();

gripPoints.append(StartPoint);

gripPoints.append(EndPoint);

return Acad::eOk;

}

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

{

assertReadEnabled();

Acad::ErrorStatus es=Acad::eOk;

if(indices.length()==0)

{

return Acad::eOk;

}

int num=indices.length();

for(int i=0;i<num;i++)

{

int k=indices[i];

if (k==0)

{

StartPoint += offset;

}

else

{

EndPoint += offset

} }

 

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)

{

return Acad::eOk;

}

return Acad::eOk;

}

 

void AcdbEntityTest::Initialize()

{

AcGePoint3d inputPt;

if(ads_getpoint(NULL,"\n ==NOTE== 请输入起点: ",asDblArray(inputPt))!=RTNORM) {

ads_printf("\n ==NOTE== 错误");

}

StartPoint=inputPt;

AcDbObjectId entityID=AddEntityToDbs(this);

int track,type;

track=1;

struct resbuf entdata;

while (track>0)

{

if(ads_grread (track, &type, &entdata)!=RTNORM)

{

ads_printf("\n !=RTNORM");

break;

}

AcdbEntityTest * pmyEntity=this;

acdbOpenObject(pmyEntity,entityID, AcDb::kForWrite);

this->assertWriteEnabled();

if(type==5)

{

AcGePoint3d movePt; //鼠标当前位置

movePt[X]=entdata.resval.rpoint[X];

movePt[Y]=entdata.resval.rpoint[Y];

EndPoint=movePt;

this->close();

}

 

if(type==3)

{

track=1;

AcGePoint3d movePt; //鼠标当前位置

movePt[X]=entdata.resval.rpoint[X];

movePt[Y]=entdata.resval.rpoint[Y];

EndPoint=movePt;

this->close();

break;

}

if(type==13)

{

//鼠标右键退出

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数据库

{

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;

}

posted on 2015-01-31 13:18  anywhere  阅读(3597)  评论(0编辑  收藏  举报