admin 发表于 2024-5-4 18:45:13

自创实体类从AcDbCurve派生,实现一些类方法并动态绘制

MyCurve.cpp:

#include "StdAfx.h"
#include "MyCurve.h"
#include "actrans.h"
#include "aced.h"

//-----------------------------------------------------------------------------
Adesk::UInt32 CMyCurve::kCurrentVersionNumber =1 ;

//-----------------------------------------------------------------------------
ACRX采用DXF采用DEFINE采用MEMBERS (
CMyCurve, AcDbCurve,
AcDb::kDHL采用CURRENT, AcDb::kMReleaseCurrent,
AcDbProxyEntity::kNoOperation, MYCURVE,
TESTCMYENTITY1APP
|Product Desc: A description for your object
|Company: Your company name
|WEB Address: Your company WEB site address
)

//-----------------------------------------------------------------------------
static double pi()
{
return atan(1.0)*4;
}
CMyCurve::CMyCurve () : AcDbCurve () {
}

CMyCurve::CMyCurve(const AcGePoint3d &ptC,const AcGePoint3d &ptE,int Layer/* =6 */,int num/* =25 */) : AcDbCurve () {
ptCenter=ptC;
ptEnd=ptE;
layers=Layer;
number=num;
AcGePoint2d pt1,pt2;
pt1.x=ptCenter.x;
pt1.y=ptCenter.y;
pt2.x=ptEnd.x;
pt2.y=ptEnd.y;
double radius=pt1.distanceTo(pt2);
AcGePoint3dArray pts;
AcGePoint3d pt;
for (int i=0;i<number*layers;i++)
{
pt.x=ptCenter.x+radius*cos(2*i*pi()/number);
pt.y=ptCenter.y+radius*sin(2*i*pi()/number);
pt.z=ptCenter.z+i*((ptEnd.z-ptCenter.z)/layers)/number;
pts.append(pt);
}
AcDb3dPolyline* p3dPline=new AcDb3dPolyline(k3dSimplePoly,pts);
pCurve=(CMyCurve*)p3dPline;
}

CMyCurve::~CMyCurve () {
delete pCurve;
}

//-----------------------------------------------------------------------------
//----- AcDbObject protocols
//- Dwg Filing protocol
void CMyCurve::reset(const AcGePoint3d &ptLast)
{
pCurve=new CMyCurve(ptCenter,ptLast);
}

AcGePoint3d CMyCurve::getCenterPoint()
{
return ptCenter;
}
void CMyCurve::setCenterPoint(const AcGePoint3d &ptcen)
{
pCurve=new CMyCurve(ptcen,ptEnd);
}

void CMyCurve::setEndPoint(const AcGePoint3d &pten)
{
pCurve=new CMyCurve(ptCenter,pten);
}
Acad::ErrorStatus CMyCurve::dwgOutFields (AcDbDwgFiler *pFiler) const {
assertReadEnabled () ;
//----- Save parent class information first.
Acad::ErrorStatus es =AcDbCurve::dwgOutFields (pFiler) ;
if ( es != Acad::eOk )
return (es) ;
//----- Object version number needs to be saved first
if ( (es =pFiler->writeUInt32 (CMyCurve::kCurrentVersionNumber)) != Acad::eOk )
return (es) ;
//----- Output params
pFiler->writeItem(ptCenter);
pFiler->writeItem(ptEnd);
pFiler->writeItem(layers);
pFiler->writeItem(number);

return (pFiler->filerStatus ()) ;
}

Acad::ErrorStatus CMyCurve::dwgInFields (AcDbDwgFiler *pFiler) {
assertWriteEnabled () ;
//----- Read parent class information first.
Acad::ErrorStatus es =AcDbCurve::dwgInFields (pFiler) ;
if ( es != Acad::eOk )
return (es) ;
//----- Object version number needs to be read first
Adesk::UInt32 version =0 ;
if ( (es =pFiler->readUInt32 (&version)) != Acad::eOk )
return (es) ;
if ( version > CMyCurve::kCurrentVersionNumber )
return (Acad::eMakeMeProxy) ;
//- Uncomment the 2 following lines if your current object implementation cannot
//- support previous version of that object.
//if ( version < CMyCurve::kCurrentVersionNumber )
// return (Acad::eMakeMeProxy) ;
//----- Read params
pFiler->readItem(&ptCenter);
pFiler->readItem(&ptEnd);
pFiler->readItem(&layers);
pFiler->readItem(&number);

return (pFiler->filerStatus ()) ;
}

//-----------------------------------------------------------------------------
//----- AcDbEntity protocols
Adesk::Boolean CMyCurve::subWorldDraw (AcGiWorldDraw *mode) {
assertReadEnabled () ;
acutPrintf(采用T("\nsubworldDraw"));
//------ Returning Adesk::kFalse here will force viewportDraw() call
pCurve->subWorldDraw(mode);
return (Adesk::kFalse) ;
}

void CMyCurve::subViewportDraw (AcGiViewportDraw *mode) {
assertReadEnabled () ;
AcDbCurve::subViewportDraw (mode) ;
}

Adesk::UInt32 CMyCurve::subViewportDrawLogicalFlags (AcGiViewportDraw *vd) {
assertReadEnabled () ;
return (AcDbCurve::subViewportDrawLogicalFlags (vd)) ;
}

Adesk::UInt32 CMyCurve::subSetAttributes (AcGiDrawableTraits *traits) {
assertReadEnabled () ;
return (AcDbCurve::subSetAttributes (traits)) ;
}

//- Osnap points protocol
Acad::ErrorStatus CMyCurve::subGetOsnapPoints (
AcDb::OsnapMode osnapMode,
int gsSelectionMark,
const AcGePoint3d &pickPoint,
const AcGePoint3d &lastPoint,
const AcGeMatrix3d &viewXform,
AcGePoint3dArray &snapPoints,
AcDbIntArray &geomIds) const
{
assertReadEnabled () ;
return (AcDbCurve::subGetOsnapPoints (osnapMode, gsSelectionMark, pickPoint, lastPoint, viewXform, snapPoints, geomIds)) ;
}

Acad::ErrorStatus CMyCurve::subGetOsnapPoints (
AcDb::OsnapMode osnapMode,
int gsSelectionMark,
const AcGePoint3d &pickPoint,
const AcGePoint3d &lastPoint,
const AcGeMatrix3d &viewXform,
AcGePoint3dArray &snapPoints,
AcDbIntArray &geomIds,
const AcGeMatrix3d &insertionMat) const
{
assertReadEnabled () ;
return (AcDbCurve::subGetOsnapPoints (osnapMode, gsSelectionMark, pickPoint, lastPoint, viewXform, snapPoints, geomIds, insertionMat)) ;
}

//- Grip points protocol
Acad::ErrorStatus CMyCurve::subGetGripPoints (
AcGePoint3dArray &gripPoints, AcDbIntArray &osnapModes, AcDbIntArray &geomIds
) const {
assertReadEnabled () ;
//----- This method is never called unless you return eNotImplemented
//----- from the new getGripPoints() method below (which is the default implementation)

return (AcDbCurve::subGetGripPoints (gripPoints, osnapModes, geomIds)) ;
}

Acad::ErrorStatus CMyCurve::subMoveGripPointsAt (const AcDbIntArray &indices, const AcGeVector3d &offset) {
assertWriteEnabled () ;
//----- This method is never called unless you return eNotImplemented
//----- from the new moveGripPointsAt() method below (which is the default implementation)

return (AcDbCurve::subMoveGripPointsAt (indices, offset)) ;
}

Acad::ErrorStatus CMyCurve::subGetGripPoints (
AcDbGripDataPtrArray &grips, const double curViewUnitSize, const int gripSize,
const AcGeVector3d &curViewDir, const int bitflags
) const {
assertReadEnabled () ;

//----- If you return eNotImplemented here, that will force AutoCAD to call
//----- the older getGripPoints() implementation. The call below may return
//----- eNotImplemented depending of your base class.
return (AcDbCurve::subGetGripPoints (grips, curViewUnitSize, gripSize, curViewDir, bitflags)) ;
}

Acad::ErrorStatus CMyCurve::subMoveGripPointsAt (
const AcDbVoidPtrArray &gripAppData, const AcGeVector3d &offset,
const int bitflags
) {
assertWriteEnabled () ;

//----- If you return eNotImplemented here, that will force AutoCAD to call
//----- the older getGripPoints() implementation. The call below may return
//----- eNotImplemented depending of your base class.
return (AcDbCurve::subMoveGripPointsAt (gripAppData, offset, bitflags)) ;
}

//-----------------------------------------------------------------------------
//----- AcDbCurve protocols
//- Curve property tests.
Adesk::Boolean CMyCurve::isClosed () const {
assertReadEnabled () ;
return (AcDbCurve::isClosed ()) ;
}

Adesk::Boolean CMyCurve::isPeriodic () const {
assertReadEnabled () ;
return (AcDbCurve::isPeriodic ()) ;
}

//- Get planar and start/end geometric properties.
Acad::ErrorStatus CMyCurve::getStartParam (double &param) const {
assertReadEnabled () ;
return (AcDbCurve::getStartParam (param)) ;
}

Acad::ErrorStatus CMyCurve::getEndParam (double &param) const {
assertReadEnabled () ;
return (AcDbCurve::getEndParam (param)) ;
}

Acad::ErrorStatus CMyCurve::getStartPoint (AcGePoint3d &point) const {
assertReadEnabled () ;
return (AcDbCurve::getStartPoint (point)) ;
}

Acad::ErrorStatus CMyCurve::getEndPoint (AcGePoint3d &point) const {
assertReadEnabled () ;
return (AcDbCurve::getEndPoint (point)) ;
}

//- Conversions to/from parametric/world/distance.
Acad::ErrorStatus CMyCurve::getPointAtParam (double param, AcGePoint3d &point) const {
assertReadEnabled () ;
return (AcDbCurve::getPointAtParam (param, point)) ;
}

Acad::ErrorStatus CMyCurve::getParamAtPoint (const AcGePoint3d &point, double &param) const {
assertReadEnabled () ;
return (AcDbCurve::getParamAtPoint (point, param)) ;
}

Acad::ErrorStatus CMyCurve::getDistAtParam (double param, double &dist) const {
assertReadEnabled () ;
return (AcDbCurve::getDistAtParam (param, dist)) ;
}

Acad::ErrorStatus CMyCurve::getParamAtDist (double dist, double &param) const {
assertReadEnabled () ;
return (AcDbCurve::getParamAtDist (dist, param)) ;
}

Acad::ErrorStatus CMyCurve::getDistAtPoint (const AcGePoint3d &point , double &dist) const {
assertReadEnabled () ;
return (AcDbCurve::getDistAtPoint (point, dist)) ;
}

Acad::ErrorStatus CMyCurve::getPointAtDist (double dist, AcGePoint3d &point) const {
assertReadEnabled () ;
return (AcDbCurve::getPointAtDist (dist, point)) ;
}

//- Derivative information.
Acad::ErrorStatus CMyCurve::getFirstDeriv (double param, AcGeVector3d &firstDeriv) const {
assertReadEnabled () ;
return (AcDbCurve::getFirstDeriv (param, firstDeriv)) ;
}

Acad::ErrorStatus CMyCurve::getFirstDeriv (const AcGePoint3d &point, AcGeVector3d &firstDeriv) const {
assertReadEnabled () ;
return (AcDbCurve::getFirstDeriv (point, firstDeriv)) ;
}

Acad::ErrorStatus CMyCurve::getSecondDeriv (double param, AcGeVector3d &secDeriv) const {
assertReadEnabled () ;
return (AcDbCurve::getSecondDeriv (param, secDeriv)) ;
}

Acad::ErrorStatus CMyCurve::getSecondDeriv (const AcGePoint3d &point, AcGeVector3d &secDeriv) const {
assertReadEnabled () ;
return (AcDbCurve::getSecondDeriv (point, secDeriv)) ;
}

//- Closest point on curve.
Acad::ErrorStatus CMyCurve::getClosestPointTo (const AcGePoint3d &givenPnt, AcGePoint3d &pointOnCurve, Adesk::Boolean extend /*=Adesk::kFalse*/) const {
assertReadEnabled () ;
return (AcDbCurve::getClosestPointTo (givenPnt, pointOnCurve, extend)) ;
}

Acad::ErrorStatus CMyCurve::getClosestPointTo (const AcGePoint3d &givenPnt, const AcGeVector3d &direction, AcGePoint3d &pointOnCurve, Adesk::Boolean extend /*=Adesk::kFalse*/) const {
assertReadEnabled () ;
return (AcDbCurve::getClosestPointTo (givenPnt, direction, pointOnCurve, extend)) ;
}

//- Get a projected copy of the curve.
Acad::ErrorStatus CMyCurve::getOrthoProjectedCurve (const AcGePlane &plane, AcDbCurve *&projCrv) const {
assertReadEnabled () ;
return (AcDbCurve::getOrthoProjectedCurve (plane, projCrv)) ;
}

Acad::ErrorStatus CMyCurve::getProjectedCurve (const AcGePlane &plane, const AcGeVector3d &projDir, AcDbCurve *&projCrv) const {
assertReadEnabled () ;
return (AcDbCurve::getProjectedCurve (plane, projDir, projCrv)) ;
}

//- Get offset, spline and split copies of the curve.
Acad::ErrorStatus CMyCurve::getOffsetCurves (double offsetDist, AcDbVoidPtrArray &offsetCurves) const {
assertReadEnabled () ;
return (AcDbCurve::getOffsetCurves (offsetDist, offsetCurves)) ;
}

Acad::ErrorStatus CMyCurve::getOffsetCurvesGivenPlaneNormal (const AcGeVector3d &normal, double offsetDist, AcDbVoidPtrArray &offsetCurves) const {
assertReadEnabled () ;
return (AcDbCurve::getOffsetCurvesGivenPlaneNormal (normal, offsetDist, offsetCurves)) ;
}

Acad::ErrorStatus CMyCurve::getSpline (AcDbSpline *&spline) const {
assertReadEnabled () ;
return (AcDbCurve::getSpline (spline)) ;
}

Acad::ErrorStatus CMyCurve::getSplitCurves (const AcGeDoubleArray &params, AcDbVoidPtrArray &curveSegments) const {
assertReadEnabled () ;
return (AcDbCurve::getSplitCurves (params, curveSegments)) ;
}

Acad::ErrorStatus CMyCurve::getSplitCurves (const AcGePoint3dArray &points, AcDbVoidPtrArray &curveSegments) const {
assertReadEnabled () ;
return (AcDbCurve::getSplitCurves (points, curveSegments)) ;
}

//- Extend the curve.
Acad::ErrorStatus CMyCurve::extend (double newParam) {
assertReadEnabled () ;
return (AcDbCurve::extend (newParam)) ;
}

Acad::ErrorStatus CMyCurve::extend (Adesk::Boolean extendStart, const AcGePoint3d &toPoint) {
assertReadEnabled () ;
return (AcDbCurve::extend (extendStart, toPoint)) ;
}

//- Area calculation.
Acad::ErrorStatus CMyCurve::getArea (double &area) const {
assertReadEnabled () ;
return (AcDbCurve::getArea (area)) ;
}

MyJig.cpp:

#include "StdAfx.h"
#include "MyJig.h"
#include <math.h>
#include "..\..\cmyentity1\cmyentity1\entity.h"

//-----------------------------------------------------------------------------
CMyJig::CMyJig () : AcEdJig ()
{
m采用pEnt=NULL;
}

CMyJig::~CMyJig () {
}

//-----------------------------------------------------------------------------
static AcDbObjectId AddToModelSpace(AcDbEntity *pEnt)
{
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase() ->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord *pBlockTableRecord;

pBlockTable->getAt(ACDB采用MODEL采用SPACE, pBlockTableRecord,AcDb::kForWrite);
AcDbObjectId entId;
pBlockTableRecord->appendAcDbEntity(entId, pEnt);
pEnt->close();
pBlockTable->close();
pBlockTableRecord->close();
return entId;
}
static AcDbObjectId AddToCurrentSpace(AcDbEntity *pEnt)
{
AcDbBlockTable *pBlockTable;
AcDbDatabase *pDb=acdbHostApplicationServices()->workingDatabase();
pDb->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbObjectId currentSpaceId=pDb->currentSpaceId();
AcDbBlockTableRecord *pBlockTableRecord;
if (acdbOpenObject(pBlockTableRecord,currentSpaceId,AcDb::kForWrite)!=Acad::eOk)
{
return AcDbObjectId::kNull;
}
AcDbObjectId entId;
pBlockTableRecord->appendAcDbEntity(entId, pEnt);
pBlockTable->close();
pBlockTableRecord->close();
return entId;
}

static double pi()
{
return atan(1.0)*4;
}
bool CMyJig::doIt(const AcGePoint3d &ptCenter)
{
m采用ptCenter=ptCenter;
m采用pEnt=new CMyCurve(m采用ptCenter,m采用ptCenter,6,25);
setDispPrompt(采用T("指定下一点:"));
AcEdJig::DragStatus stat=drag();

if (stat==kNormal)
{
AddToModelSpace(m采用pEnt);
m采用pEnt->close();
return true;
}
else
{
delete m采用pEnt;
return false;
}
}

AcEdJig::DragStatus CMyJig::sampler () {
setUserInputControls((UserInputControls)(AcEdJig::kAccept3dCoordinates|AcEdJig::kNoNegativeResponseAccepted|AcEdJig::kNullResponseAccepted));
static AcGePoint3d pt;
DragStatus stat=acquirePoint(m采用ptCur);
if (pt!=m采用ptCur)
{
pt=m采用ptCur;
}
else if (stat==AcEdJig::kNormal)
{
return AcEdJig::kNoChange;
}
return stat;
}

//-----------------------------------------------------------------------------
//- Jigged entity update
Adesk::Boolean CMyJig::update () {
// AcGeVector2d ver(m采用ptCur.x-m采用ptCenter.x,m采用ptCur.y-m采用ptCenter.y);
// for (int i=1;i<4;i++)
// {
// AcGePoint3d pt;
// ver.rotateBy(2*pi()/3);
// pt.x=m采用ptCenter.x+ver.x;
// pt.y=m采用ptCenter.y+ver.y;
// pt.z=m采用ptCenter.z;
// m采用pEnt->setVertexAt(i-1,pt);
// }
m采用pEnt->reset(m采用ptCur);
return Adesk::kTrue;
}

//-----------------------------------------------------------------------------
//- Jigged entity pointer return
AcDbEntity *CMyJig::entity () const {
return m采用pEnt;
}



acrxEntryPoint.cpp:

#include "StdAfx.h"
#include "resource.h"
#include "..\CMyEntity1\Entity.h"
#include "acgi.h"
#include "myjig.h"
#include "..\..\cmyentity1\cmyentity1\mycurve.h"


//-----------------------------------------------------------------------------
#define szRDS 采用RXST("zff")

//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CObjectARXApp : public AcRxArxApp {

public:
CObjectARXApp () : AcRxArxApp () {}

virtual AcRx::AppRetCode On采用kInitAppMsg (void *pkt) {
// TODO: Load dependencies here

// You *must* call On采用kInitAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On采用kInitAppMsg (pkt) ;

// TODO: Add your initialization code here

return (retCode) ;
}

virtual AcRx::AppRetCode On采用kUnloadAppMsg (void *pkt) {
// TODO: Add your code here

// You *must* call On采用kUnloadAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On采用kUnloadAppMsg (pkt) ;

// TODO: Unload dependencies here

return (retCode) ;
}

virtual void RegisterServerComponents () {
}
static double pi()
{
return atan(1.0)*4;
}
static AcDbObjectId AddToModelSpace(AcDbEntity *pEnt)
{
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase() ->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord *pBlockTableRecord;

pBlockTable->getAt(ACDB采用MODEL采用SPACE, pBlockTableRecord,AcDb::kForWrite);
AcDbObjectId entId;
pBlockTableRecord->appendAcDbEntity(entId, pEnt);
pBlockTable->close();
pBlockTableRecord->close();
return entId;
}
static AcDbObjectId AddToCurrentSpace(AcDbEntity *pEnt)
{
AcDbBlockTable *pBlockTable;
AcDbDatabase *pDb=acdbHostApplicationServices()->workingDatabase();
pDb->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbObjectId currentSpaceId=pDb->currentSpaceId();
AcDbBlockTableRecord *pBlockTableRecord;
if (acdbOpenObject(pBlockTableRecord,currentSpaceId,AcDb::kForWrite)!=Acad::eOk)
{
return AcDbObjectId::kNull;
}
AcDbObjectId entId;
pBlockTableRecord->appendAcDbEntity(entId, pEnt);
pBlockTable->close();
pBlockTableRecord->close();
return entId;
}

static void zffObjectARX采用MyCommand1(void)
{
ads采用point pt;
acedGetPoint(NULL,采用T("指定中心点:"),pt);
CMyJig jig;
jig.doIt(asPnt3d(pt));
}
} ;

//-----------------------------------------------------------------------------
IMPLEMENT采用ARX采用ENTRYPOINT(CObjectARXApp)

ACED采用ARXCOMMAND采用ENTRY采用AUTO(CObjectARXApp, zffObjectARX, 采用MyCommand1, MyCommand1, ACRX采用CMD采用TRANSPARENT, NULL)


https://www.cnblogs.com/NewAutoMan/p/6373661.html
页: [1]
查看完整版本: 自创实体类从AcDbCurve派生,实现一些类方法并动态绘制