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

原创 2007年10月11日 12:35:00

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

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();
 
 //获取虚线线型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.x+=offset.x;
   StartPoint.y+=offset.y;
  }
  else
  {
   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)
 {
  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;
}

 

CAD控件:自定义实体的实现

1 、 自定义实体 1.1 说明 控件支持自定义实体功能,可以从McDbEntity继承自己的实体,在自定体实体类中,实现相关虚函数来实现实体的自定义功能。   1.2 类的类型信息 使用M...
  • u013725001
  • u013725001
  • 2015-06-18 14:24:37
  • 1338

auto CAD .NET二次开发之 添加,删除实体(entity)

个人感觉添加删除实体这个方法比较水。只要是操作过CAD的.NET程序员都是小菜一碟。不过在这里还是赘述一下。呵呵 添加实体的核心代码只有两句:                           ...
  • luckysiesteven
  • luckysiesteven
  • 2013-09-22 13:30:56
  • 3837

ObjectARX学习笔记(一)----中如何根据组名将组里的所有实体删除,以及清除组

AcDbDictionary *pGroupDict = NULL; Acad::ErrorStatus es; acdbHostApplicationServices()->workingDatab...
  • jangdong
  • jangdong
  • 2013-09-17 18:21:42
  • 1527

使用AcDbEntity的Explode方法后分解对象的删除

如果使用explode方法炸开对象,但是又不需要炸开的对象放到块表中时,应调用delete方法将对象删除,代码如下: AcDbVoidPtrArray arr_ExplodeEnt; en...
  • TXH0001
  • TXH0001
  • 2017-12-21 22:35:28
  • 84

ObjectArx自定义实体

1.arx文档中规定的必须重写的几个函数 //AcDbObject: virtual Acad::ErrorStatus dwgInFields(AcDbDwgFiler* fi...
  • panzhijiepanzhiyang
  • panzhijiepanzhiyang
  • 2015-01-15 15:25:14
  • 1734

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

http://blog.csdn.net/zhujiang1001/article/details/1819938   本文介绍了构造自定义实体的步骤、必须继承的函数和必须注意的事项 1.新...
  • silyvin
  • silyvin
  • 2013-03-06 16:34:28
  • 4107

3.1.4、ObjectARX程序的初始化

你必须初始化你的应用程序里面的自定义用户类,
  • mj511099781
  • mj511099781
  • 2014-08-05 21:42:51
  • 552

ObjectArx创建自定义实体

ObjectArx创建自定义实体 一。目的 在ObjectArx中已经有了许多实体,如AcDbLine,AcDbCircle,AcDbArc等,但在用户使用Cad时,会有一些对他们来讲常用...
  • u012158162
  • u012158162
  • 2017-03-28 20:39:41
  • 783

ObjectArx 自定义实体

1.arx文档中规定的必须重写的几个函数 //AcDbObject: virtual Acad::ErrorStatus dwgInFields(AcDbDwgFiler* fi...
  • Alexander_Frank
  • Alexander_Frank
  • 2016-10-08 09:25:38
  • 1389

自定义一个实体类

import java.util.*; public class StudentTest{ public static void main (String[] args){ ...
  • wh_java01
  • wh_java01
  • 2017-01-03 13:56:07
  • 1216
收藏助手
不良信息举报
您举报文章:ObjectArx自定义实体入门(C++)及注意事项
举报原因:
原因补充:

(最多只允许输入30个字)