【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(&

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

}

时间: 2024-10-08 11:02:02

【CAD】自定义实体的步骤(转)的相关文章

用ARX自定义实体

本文介绍了构造自定义实体的步骤.必须继承的函数和必须注意的事项 1.新建一个从AcDbEntity继承的类,如EntTest,必须添加的头文件: "stdarx.h","acadstrc.h", "geassign.h". 2.在该类头文件的类声明中添加宏:ACRX_DECLARE_MEMBERS(EntTest); 3.在该类的cpp文件中,类的前面添加宏: ACRX_DXF_DEFINE_MEMBERS(EntTest, AcDbEntity

CAD控件:COM接口实现自定义实体的方法

1. 实现步骤: 3 1. 实现步骤: 参考例子 :Src\MxDraw5.2\samples\ie\iedemoTest.htm 1) 增加自定义实体对象 调用DrawCustomEntity函数,绘制一个自定义实体对象 函数说明如下: JS例子,下面代码绘制一个自定义实体,设置了两个属性,属性名分别" startpoint"," endpoint"的两个点坐标, // 插入自定义实体函数 function InsertCustomEntity() { var g

CAD绘制自定义实体(com接口)

在cad使用过程中,用户可以绘制自定义实体.点击此处下载演示实例. 调用DrawCustomEntity函数,绘制一个自定义实体对象. 下面代码绘制一个自定义实体,C#代码实现如下: private void DrawMlineCommand() { MxDrawUiPrPoint getPt = new MxDrawUiPrPoint(); getPt.message = "点取第一点"; if (getPt.go() != MCAD_McUiPrStatus.mcOk) { ret

CAD控件使用教程 自定义实体的实现

自定义实体的实现 1 .       自定义实体... 3 1.1      说明... 3 1.2      类的类型信息... 3 1.3      worldDraw.. 4 1.4      getGripPoints 4 1.5      moveGripPointsAt 5 1.6      getGeomExtents 6 1.7      getOsnapPoints 6 1.8      explode. 7 1.9      dwgInFields 8 1.10    dwg

梦想CAD控件自定义实体实现

一.增加自定义实体对象 调用DrawCustomEntity函数,绘制一个自定义实体对象. 下面代码绘制一个自定义实体,C#代码实现如下: private void DrawMlineCommand() {     MxDrawUiPrPoint getPt = new MxDrawUiPrPoint();     getPt.message = "点取第一点";     if (getPt.go() != MCAD_McUiPrStatus.mcOk)     {         r

CAD实现自定义实体夹点移动(com接口VB语言)

主要用到函数说明: MxDrawXCustomEvent::MxDrawXCustomEntity::moveGripPointsAt 自定义实体事件,自定义实体夹点被移动,详细说明如下: 参数 说明 LONGLONG lEntity 自定义实体id IN LONG lGridIndex 夹点索引号 IN McGePoint3d vec 夹点移动的向量 VB代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

CAD由一个自定义实体事件中的id得到自定义实体对象(com接口VB语言)

由一个自定义实体事件中的id得到自定义实体对象.该函数只能在自定义实体事件中调用. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 If sEventName = "MxDrawXCustomEntity::explode" Then             Dim param66 As M

解决双击dwg文件ARX自定义实体提示代理的问题

双击dwg文件的时候,如果没有通过注册表设置会提示代理实体. 注册表自动加载arx 注册表参考路径 R18.1 是cad版本 ACAD-9001:409 是cad的地区语言,409是英文 ,804是中文 HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\ACAD-9001:409\Applications 新建项 如果是包含自定义实体的arx或dbx,这个项的名字不能乱写,必须和注册自定义实体类的注册宏的app名称相同,否则打开文件的时候不能自动

自定义实体类在三层架构之间传递数据

自定义实体类是专门承载业务实体数据的类,一般将方法分离出来,只包含属性和字段的定义.每个实体类对应数据库中的一个表,每个属性对应表中的一个相应的字段.使用实体类属于面向对象编程的思想,将每个表封装成对应的类. 1. 为什么使用自定义实体类 (1)使程序更加灵活.如果数据库发生了更改,只需要修改数据实体类的代码,其他层的代码修改量很小. (2)使用面向对象思想将数据库中的表抽象为类,消除了关系数据与类的差别,使程序维护和扩展更方便. (3)更有利于分层架构设计. 2. 使用自定义实体类作为三层架构