- UID
- 361738
- 积分
- 439
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2005-12-5
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
我定义了一个自定义实体,如所贴代码中所示,我在输入命令生成一个实体后,再输相同的命令,但没想到出错,说是什么读无效!请高手指点!
#include "AzhtEntity.h"
class AzhtBreaker : public AzhtEntity
{
public:
ACRX_DECLARE_MEMBERS(AzhtBreaker);
AzhtBreaker();
AzhtBreaker(AcGePoint3d insertPt, int iDirection);
virtual ~AzhtBreaker();
//这四个重载基类的函数是用于将对象保存到文件中
virtual Acad::ErrorStatus dwgInFields (AcDbDwgFiler *pFiler);
virtual Acad::ErrorStatus dwgOutFields(AcDbDwgFiler *pFiler) const;
virtual Acad::ErrorStatus dxfInFields (AcDbDxfFiler *pFiler);
virtual Acad::ErrorStatus dxfOutFields(AcDbDxfFiler *pFiler) const;
//必须重载的基类函数,以支持一个断路器实体具有AutoCAD内置实体的一般功能
virtual Adesk::Boolean worldDraw(AcGiWorldDraw* pWD);
virtual void viewportDraw(AcGiViewportDraw* pVD);
virtual Acad::ErrorStatus getGeomExtents(AcDbExtents& extents);
virtual Acad::ErrorStatus transformBy(const AcGeMatrix3d& xform);
virtual Acad::ErrorStatus getTransformedCopy(const AcGeMatrix3d& xform, AcDbEntity*& pEnt);
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 m_otherPt; //断路器实体上相对于插入点的另一侧点
};
//AzhtBreaker.cpp
#include "stdafx.h"
#include <aced.h>
#include <rxregsvc.h>
#include "AzhtBreaker.h"
ACRX_DXF_DEFINE_MEMBERS(AzhtBreaker, AzhtEntity, AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent, 0, BREAKER, "Test")
AzhtBreaker::AzhtBreaker():AzhtEntity()
{}
AzhtBreaker::AzhtBreaker(AcGePoint3d insertPt, int iDirection) : AzhtEntity(insertPt, iDirection)
{
setName("断路器");
if(iDirection == 1)//向左
{
m_otherPt.set(insertPt[0]-8.0, insertPt[1], insertPt[2]);
setHeight(2.0);
setWidth(8.0);
}
if(iDirection == 2)//向右
{
m_otherPt.set(insertPt[0]+8.0, insertPt[1], insertPt[2]);
setHeight(2.0);
setWidth(8.0);
}
if(iDirection == 3)//向上
{
m_otherPt.set(insertPt[0], insertPt[1]+8.0, insertPt[2]);
setHeight(8.0);
setWidth(2.0);
}
if(iDirection == 4)//向下
{
m_otherPt.set(insertPt[0], insertPt[1]-8.0, insertPt[2]);
setHeight(8.0);
setWidth(2.0);
}
}
AzhtBreaker::~AzhtBreaker()
{}
Acad::ErrorStatus AzhtBreaker::dwgInFields(AcDbDwgFiler *pFiler)
{
assertWriteEnabled();
Acad::ErrorStatus es;
//派生类调用父类的同名函数来发出超级消息,发出超级消息后就可读写文件
es = AzhtEntity::dwgInFields(pFiler);
if(es != Acad::eOk)
return es;
//pFiler->readItem(&m_bUpOrDown);
pFiler->readPoint3d(&m_otherPt);
//pFiler->readItem(&m_dHeight);
//pFiler->readItem(&m_dWidth);
return pFiler->filerStatus();
}
Acad::ErrorStatus AzhtBreaker::dwgOutFields(AcDbDwgFiler *pFiler) const
{
assertReadEnabled();
Acad::ErrorStatus es;
//派生类调用父类的同名函数来发出超级消息,发出超级消息后就可读写文件
es = AzhtEntity::dwgOutFields(pFiler);
if(es != Acad::eOk)
return es;
//pFiler->writeItem(m_bUpOrDown);
pFiler->writePoint3d(m_otherPt);
//pFiler->writeItem(m_dHeight);
//pFiler->writeItem(m_dWidth);
return pFiler->filerStatus();
}
Acad::ErrorStatus AzhtBreaker::dxfInFields(AcDbDxfFiler *pFiler)
{
assertWriteEnabled();
struct resbuf rb;
Acad::ErrorStatus es;
es = AzhtEntity::dxfInFields(pFiler);
if((es != Acad::eOk) || !pFiler->atSubclassData("AzhtBreaker"))
return pFiler->filerStatus();
while((es == Acad::eOk) && ((es = pFiler->readResBuf(&rb)) == Acad::eOk))
{
switch(rb.restype)
{
//case AcDb::kDxfBool:
//m_bUpOrDown = rb.resval.rint;
//break;
case AcDb::kDxfXCoord:
m_otherPt = asPnt3d(rb.resval.rpoint);
//case AcDb::kDxfReal:
//m_dHeight = rb.resval.rreal;
//case AcDb::kDxfReal+1:
//m_dWidth = rb.resval.rreal;
default:
pFiler->pushBackItem();
es = Acad::eEndOfFile;
break;
}
}
if(es != Acad::eEndOfFile)
return Acad::eInvalidResBuf;
return pFiler->filerStatus();
}
Acad::ErrorStatus AzhtBreaker::dxfOutFields(AcDbDxfFiler *pFiler) const
{
assertReadEnabled();
Acad::ErrorStatus es;
//派生类调用父类的同名函数来发出超级消息,发出超级消息后就可读写文件
es = AzhtEntity::dxfOutFields(pFiler);
if(es != Acad::eOk)
return es;
//pFiler->writeItem(AcDb::kDxfSubclass, "AzhtBreaker");
//pFiler->writeItem(AcDb::kDxfBool, m_bUpOrDown);
pFiler->writePoint3d(AcDb::kDxfXCoord, m_otherPt);
//pFiler->writeItem(AcDb::kDxfReal, m_dHeight);
//pFiler->writeItem(AcDb::kDxfReal, m_dWidth);
return es;
}
//由于要在viewportDraw()中实现断路器实体的绘制功能,必须返回false
Adesk::Boolean AzhtBreaker::worldDraw(AcGiWorldDraw *pWD)
{
return Adesk::kFalse;
}
void AzhtBreaker::viewportDraw(AcGiViewportDraw *pVD)
{
AcGePoint3d line1PtArr[2], line2PtArr[2], polygonPtArr[4];
int iDirection = getDirection();
AcGePoint3d insertPt = getInsertPt();
if(iDirection == 1)//向左
{
//第一条线
line1PtArr[0] = insertPt;
line1PtArr[1].set(insertPt[0]-1.5, insertPt[1], insertPt[2]);
//中间的矩形
polygonPtArr[0].set(insertPt[0]-1.5, insertPt[1]-1, insertPt[2]);
polygonPtArr[1].set(insertPt[0]-1.5, insertPt[1]+1, insertPt[2]);
polygonPtArr[2].set(insertPt[0]-6.5, insertPt[1]+1, insertPt[2]);
polygonPtArr[3].set(insertPt[0]-6.5, insertPt[1]-1, insertPt[2]);
//第二条线
line2PtArr[0].set(insertPt[0]-6.5, insertPt[1], insertPt[2]);
line2PtArr[1].set(insertPt[0]-8.0, insertPt[1], insertPt[2]);
}
if(iDirection == 2)//向右
{
//第一条线
line1PtArr[0] = insertPt;
line1PtArr[1].set(insertPt[0]+1.5, insertPt[1], insertPt[2]);
//中间的矩形
polygonPtArr[0].set(insertPt[0]+1.5, insertPt[1]-1, insertPt[2]);
polygonPtArr[1].set(insertPt[0]+1.5, insertPt[1]+1, insertPt[2]);
polygonPtArr[2].set(insertPt[0]+6.5, insertPt[1]+1, insertPt[2]);
polygonPtArr[3].set(insertPt[0]+6.5, insertPt[1]-1, insertPt[2]);
//第二条线
line2PtArr[0].set(insertPt[0]+6.5, insertPt[1], insertPt[2]);
line2PtArr[1].set(insertPt[0]+8.0, insertPt[1], insertPt[2]);
}
if(iDirection == 3)//向上
{
//第一条线
line1PtArr[0] = insertPt;
line1PtArr[1].set(insertPt[0], insertPt[1]+1.5, insertPt[2]);
//中间的矩形
polygonPtArr[0].set(insertPt[0]-1, insertPt[1]+1.5, insertPt[2]);
polygonPtArr[1].set(insertPt[0]+1, insertPt[1]+1.5, insertPt[2]);
polygonPtArr[2].set(insertPt[0]+1, insertPt[1]+6.5, insertPt[2]);
polygonPtArr[3].set(insertPt[0]-1, insertPt[1]+6.5, insertPt[2]);
//第二条线
line2PtArr[0].set(insertPt[0], insertPt[1]+6.5, insertPt[2]);
line2PtArr[1].set(insertPt[0], insertPt[1]+8.0, insertPt[2]);
}
if(iDirection == 4)//向下
{
//第一条线
line1PtArr[0] = insertPt;
line1PtArr[1].set(insertPt[0], insertPt[1]-1.5, insertPt[2]);
//中间的矩形
polygonPtArr[0].set(insertPt[0]-1, insertPt[1]-1.5, insertPt[2]);
polygonPtArr[1].set(insertPt[0]+1, insertPt[1]-1.5, insertPt[2]);
polygonPtArr[2].set(insertPt[0]+1, insertPt[1]-6.5, insertPt[2]);
polygonPtArr[3].set(insertPt[0]-1, insertPt[1]-6.5, insertPt[2]);
//第二条线
line2PtArr[0].set(insertPt[0], insertPt[1]-6.5, insertPt[2]);
line2PtArr[1].set(insertPt[0], insertPt[1]-8.0, insertPt[2]);
}
pVD->geometry().polyline(2, line1PtArr);
pVD->geometry().polygon(4, polygonPtArr);
pVD->geometry().polyline(2, line2PtArr);
}
Acad::ErrorStatus AzhtBreaker::getGeomExtents(AcDbExtents& extents)
{
return Acad::eOk;
}
Acad::ErrorStatus AzhtBreaker::getGripPoints(AcGePoint3dArray& gripPoints, AcDbIntArray& osnapModes, AcDbIntArray& geomIds) const
{
assertReadEnabled();
int iDirection = ((AzhtEntity*)this)->getDirection();
AcGePoint3d insertPt = ((AzhtEntity*)this)->getInsertPt();
gripPoints.setLogicalLength(0);
gripPoints.append(m_otherPt);
gripPoints.append(insertPt);
/*if(iDirection == 3)
{
gripPoints.append(AcGePoint3d(insertPt[0], insertPt[1]+8.0, insertPt[2]));
}
if(iDirection == 4)
{
gripPoints.append(AcGePoint3d(insertPt[0], insertPt[1]-8.0, insertPt[2]));
}*/
//AzhtEntity::getGripPoints(gripPoints, osnapModes, geomIds);
return Acad::eOk;
}
Acad::ErrorStatus AzhtBreaker::moveGripPointsAt(const AcDbIntArray& indices, const AcGeVector3d& offset)
{
assertWriteEnabled();
//若没有选中点或偏移为0
if(indices.length()==0 || offset.isZeroLength())
return Acad::eOk;
return transformBy(AcGeMatrix3d::translation(offset));
//AzhtEntity::transformBy(AcGeMatrix3d::translation(offset));//整体移动
}
Acad::ErrorStatus AzhtBreaker::transformBy(const AcGeMatrix3d& xform)
{
assertWriteEnabled();
//AzhtEntity::transformBy(xform);
AcGePoint3d insertPt = getInsertPt();
insertPt.transformBy(xform);
setInsertPt(insertPt);
m_otherPt.transformBy(xform);
return Acad::eOk;
}
Acad::ErrorStatus AzhtBreaker::getTransformedCopy(const AcGeMatrix3d& xform, AcDbEntity*& pEnt)
{
assertReadEnabled();
Acad::ErrorStatus es = Acad::eOk;
return es;
}
#include "stdafx.h"
#include <aced.h>
#include <rxregsvc.h>
#include "AzhtBreaker.h"
Acad::ErrorStatus AddToMS(AcDbEntity* pEnt)
{
//ASSERT(m_pDb)
//ASSERT(pEnt)
Acad::ErrorStatus es;
//得到块表指针
AcDbBlockTable* pBT = NULL;
es = acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBT, AcDb::kForRead);
if(es != Acad::eOk)
{
acedAlert("得到块表指针失败!");
return es;
}
//得到模型空间块表记录指针
AcDbBlockTableRecord* pBTRec = NULL;
es = pBT->getAt(ACDB_MODEL_SPACE, pBTRec, AcDb::kForWrite);//注意是以写方式得到
pBT->close();
if(es != Acad::eOk)
{
acedAlert("得到模型空间块表记录指针失败!");
return es;
}
//添加实体到所要求模型空间对应的块表记录中
es = pBTRec->appendAcDbEntity(pEnt);
pBTRec->close();
if(es != Acad::eOk)
{
acedAlert("将实体添加到模型空间失败!");
return es;
}
return Acad::eOk;
}
void TestClass()
{
ads_point pt;
int i;
if(acedGetPoint(NULL, "请指定一个插入点!", pt) != RTNORM)
{
return;
}
if(acedGetInt("请输入一个整数来指定方向(1向左,2向右,3向上,4向下)!", &i) != RTNORM)
{
return;
}
AzhtBreaker* myBreaker = new AzhtBreaker(AcGePoint3d(pt[0], pt[1], 0.0), i);
AddToMS(myBreaker);
myBreaker->close();
acedCommand(RTSTR,"zoom",RTSTR,"E",0);//放大到图形范围内
} |
|