ObjectArx自定义对象创建项目-自定义实体代码(2)加编译

  • A+
所属分类:CAD二次开发

前面已经说过如何创建一个完整的cad自定义实体项目,这里不重复说明,直接搬运代码,在dbx项目里面找到我们新建的huojibkcom.h类头文件,在里面粘贴下面代码,直接把原来的全部删除,

1、头文件  huojibkcom.h

// (C) Copyright 2002-2007 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
//-----------------------------------------------------------------------------
//----- huojibkcom.h : Declaration of the huojibkcom
//-----------------------------------------------------------------------------
#pragma once
#ifdef CUSTENTITYDBX_MODULE
#define DLLIMPEXP __declspec(dllexport)
#else
//----- Note: we don't use __declspec(dllimport) here, because of the
//----- "local vtable" problem with msvc. If you use __declspec(dllimport),
//----- then, when a client dll does a new on the class, the object's
//----- vtable pointer points to a vtable allocated in that client
//----- dll. If the client dll then passes the object to another dll,
//----- and the client dll is then unloaded, the vtable becomes invalid
//----- and any virtual calls on the object will access invalid memory.
//-----
//----- By not using __declspec(dllimport), we guarantee that the
//----- vtable is allocated in the server dll during the ctor and the
//----- client dll does not overwrite the vtable pointer after calling
//----- the ctor. And, since we expect the server dll to remain in
//----- memory indefinitely, there is no problem with vtables unexpectedly
//----- going away.
#define DLLIMPEXP
#endif
//-----------------------------------------------------------------------------
#include "dbmain.h"
//-----------------------------------------------------------------------------
class DLLIMPEXP huojibkcom : public AcDbEntity {
public:
ACRX_DECLARE_MEMBERS(huojibkcom) ;
protected:
static Adesk::UInt32 kCurrentVersionNumber;
public:
huojibkcom () ;
virtual ~huojibkcom () ;
//----- AcDbObject protocols
//- Dwg Filing protocol
virtual Acad::ErrorStatus dwgOutFields (AcDbDwgFiler *pFiler) const ;
virtual Acad::ErrorStatus dwgInFields (AcDbDwgFiler *pFiler) ;
//----- AcDbEntity protocols
//- Graphics protocol
protected:
virtual Adesk::Boolean subWorldDraw (AcGiWorldDraw *mode) ;
virtual Adesk::UInt32 subSetAttributes (AcGiDrawableTraits *traits) ;
//- Grip points protocol
virtual Acad::ErrorStatus subGetGripPoints (AcGePoint3dArray &gripPoints, AcDbIntArray &osnapModes, AcDbIntArray &geomIds) const ;
virtual Acad::ErrorStatus subMoveGripPointsAt (const AcDbIntArray &indices, const AcGeVector3d &offset) ;
//- Osnap points protocol
public:
virtual Acad::ErrorStatus subGetOsnapPoints (
AcDb::OsnapMode osnapMode,
int gsSelectionMark,
const AcGePoint3d &pickPoint,
const AcGePoint3d &lastPoint,
const AcGeMatrix3d &viewXform,
AcGePoint3dArray &snapPoints,
AcDbIntArray &geomIds) const ;
//- Grip points protocol
protected:
// PointA
AcGePoint3d m_PtA;
public:
AcGePoint3d get_m_PtA(void) const;
Acad::ErrorStatus put_m_PtA(AcGePoint3d newVal);
protected:
// PointB
AcGePoint3d m_PtB;
public:
AcGePoint3d get_m_PtB(void) const;
Acad::ErrorStatus put_m_PtB(AcGePoint3d newVal);
protected:
// PointAB
AcGePoint3d m_PtAB;
public:
AcGePoint3d get_m_PtAB(void) const;
Acad::ErrorStatus put_m_PtAB(AcGePoint3d newVal);
protected:
// PointBA
AcGePoint3d m_PtBA;
public:
AcGePoint3d get_m_PtBA(void) const;
Acad::ErrorStatus put_m_PtBA(AcGePoint3d newVal);
protected:
// Text
CString m_Text;
public:
CString get_m_Text(void) const;
Acad::ErrorStatus put_m_Text(CString newVal);
//Acad::ErrorStatus put_m_Text(const AcGePoint3d &ptInsert,const ACHAR *text,AcDbObjectId style=AcDbObjectId::kNull,double height=3.0,double rotation=0);
// -----------------------------------------------------------------------------
//virtual Acad::ErrorStatus transformBy(const AcGeMatrix3d & xform);
protected:
// -----------------------------------------------------------------------------
virtual Acad::ErrorStatus subTransformBy(const AcGeMatrix3d & xform);
//static AcDbObjectId CreateText(const AcGePoint3d &ptInsert,const ACHAR *text,AcDbObjectId style=AcDbObjectId::kNull,double height=3.0,double rotation=0);
//static AcDbObjectId CreateMText(const AcGePoint3d &ptInsert,const ACHAR *text,AcDbObjectId style=AcDbObjectId::kNull,double height=3.0,double width=20);
} ;
#ifdef CUSTENTITYDBX_MODULE
ACDB_REGISTER_OBJECT_ENTRY_AUTO(huojibkcom)
#endif

复制

2、源文件 huojibkcom.cpp

// (C) Copyright 2002-2007 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

//-----------------------------------------------------------------------------
//----- huojibkcom.cpp : Implementation of huojibkcom
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "huojibkcom.h"

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

//-----------------------------------------------------------------------------
ACRX_DXF_DEFINE_MEMBERS (
huojibkcom, AcDbEntity,
AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent,
AcDbProxyEntity::kNoOperation, huojibkcom,
CUSTENTITYDBXAPP
|Product Desc: A description for your object
|Company: Your company name
|WEB Address: Your company WEB site address
)

//-----------------------------------------------------------------------------
huojibkcom::huojibkcom () : AcDbEntity () {
}

huojibkcom::~huojibkcom () {
}

//-----------------------------------------------------------------------------
//----- AcDbObject protocols
//- Dwg Filing protocol
Acad::ErrorStatus huojibkcom::dwgOutFields (AcDbDwgFiler *pFiler) const {
assertReadEnabled () ;
//----- Save parent class information first.
Acad::ErrorStatus es =AcDbEntity::dwgOutFields (pFiler) ;
if ( es != Acad::eOk )
return (es) ;
//----- Object version number needs to be saved first
if ( (es =pFiler->writeUInt32 (huojibkcom::kCurrentVersionNumber)) != Acad::eOk )
return (es) ;
//----- Output params
//.....

pFiler->writeItem (m_PtA) ;
pFiler->writeItem (m_PtB) ;
pFiler->writeItem (m_PtAB) ;
pFiler->writeItem (m_PtBA) ;
//pFiler->writeItem (m_Text) ;//用下面两句代替,不支持CString类型
AcString str(m_Text);
pFiler->writeString(str);

return (pFiler->filerStatus ()) ;
}

Acad::ErrorStatus huojibkcom::dwgInFields (AcDbDwgFiler *pFiler) {
assertWriteEnabled () ;
//----- Read parent class information first.
Acad::ErrorStatus es =AcDbEntity::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 > huojibkcom::kCurrentVersionNumber )
return (Acad::eMakeMeProxy) ;
//- Uncomment the 2 following lines if your current object implementation cannot
//- support previous version of that object.
//if ( version < huojibkcom::kCurrentVersionNumber )
// return (Acad::eMakeMeProxy) ;
//----- Read params
//.....

if ( version >= kCurrentVersionNumber /*&& version <= endVersion*/ ) pFiler->readItem (&m_PtA) ;
if ( version >= kCurrentVersionNumber /*&& version <= endVersion*/ ) pFiler->readItem (&m_PtB) ;
if ( version >= kCurrentVersionNumber /*&& version <= endVersion*/ ) pFiler->readItem (&m_PtAB) ;
if ( version >= kCurrentVersionNumber /*&& version <= endVersion*/ ) pFiler->readItem (&m_PtBA) ;
//if ( version >= NaN /*&& version <= endVersion*/ ) pFiler->readChar(&m_Text) ;//用下面四句代替
CString m_strTextContent;
AcString strTextContent;
if ( version >= kCurrentVersionNumber /*&& version <= endVersion*/ ) pFiler->readString(strTextContent);
m_Text.Format(_T("%s"), strTextContent.kACharPtr());

return (pFiler->filerStatus ()) ;
}

//-----------------------------------------------------------------------------
//----- AcDbEntity protocols
Adesk::Boolean huojibkcom::subWorldDraw (AcGiWorldDraw *mode) {
//assertReadEnabled () ;
//return (AcDbEntity::subWorldDraw (mode)) ;
assertReadEnabled();
// Bounding Polyline
AcGePoint3d pts[4];
pts[0] = m_PtA;
pts[1] = m_PtAB;
pts[2] = m_PtB;
pts[3] = m_PtBA;
mode->subEntityTraits().setSelectionMarker(1); // Mark 1
mode->subEntityTraits().setColor(1); // Red
mode->geometry().polygon(4,pts);
// Entity's Text
mode->subEntityTraits().setSelectionMarker(2); // Mark 2
mode->subEntityTraits().setColor(256); // ByLayer
AcGiTextStyle style;
style.setFileName(_T("txt.shx"));
style.setBigFontFileName(_T(""));
style.setTextSize(25);
style.loadStyleRec();
AcGePoint3d txtPt((m_PtB.x+m_PtA.x)/2.0,(m_PtB.y+m_PtA.y)/2.0,
m_PtA.z);
mode->geometry().text(txtPt, AcGeVector3d::kZAxis,
(m_PtAB-m_PtA),m_Text,m_Text.GetLength(),Adesk::kFalse, style);
return Adesk::kTrue;

}

Adesk::UInt32 huojibkcom::subSetAttributes (AcGiDrawableTraits *traits) {
assertReadEnabled () ;
return (AcDbEntity::subSetAttributes (traits)) ;
}

//- Osnap points protocol
Acad::ErrorStatus huojibkcom::subGetOsnapPoints (
AcDb::OsnapMode osnapMode,
int gsSelectionMark,
const AcGePoint3d &pickPoint,
const AcGePoint3d &lastPoint,
const AcGeMatrix3d &viewXform,
AcGePoint3dArray &snapPoints,
AcDbIntArray &geomIds) const
{
//assertReadEnabled () ;
//return (AcDbEntity::subGetOsnapPoints (osnapMode, gsSelectionMark, pickPoint, lastPoint, viewXform, snapPoints, geomIds)) ;
assertReadEnabled();
switch (osnapMode) {
case AcDb::kOsModeEnd:
snapPoints.append(m_PtA);
snapPoints.append(m_PtAB);
snapPoints.append(m_PtB);
snapPoints.append(m_PtBA);
break;
case AcDb::kOsModeMid:
snapPoints.append(m_PtA+((m_PtAB-m_PtA).length()/2.0)*((m_PtAB-m_PtA).normalize()));
snapPoints.append(m_PtAB+((m_PtB-m_PtAB).length()/2.0)*((m_PtB-m_PtAB).normalize()));
snapPoints.append(m_PtB+((m_PtBA-m_PtB).length()/2.0)*((m_PtBA-m_PtB).normalize()));
snapPoints.append(m_PtBA+((m_PtA-m_PtBA).length()/2.0)*((m_PtA-m_PtBA).normalize()));
break;
case AcDb::kOsModeCen:
snapPoints.append(AcGePoint3d((m_PtB.x+m_PtA.x)/2.0,
(m_PtB.y+m_PtA.y)/2.0, m_PtA.z));
break;
}
return (Acad::eOk);

}

//- Grip points protocol
Acad::ErrorStatus huojibkcom::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)
gripPoints.append(m_PtA);
gripPoints.append(m_PtAB);
gripPoints.append(m_PtB);
gripPoints.append(m_PtBA);
gripPoints.append(AcGePoint3d((m_PtB.x+m_PtA.x)/2.0,(m_PtB.y+m_PtA.y)/2.0,m_PtA.z));
//return (AcDbEntity::subGetGripPoints (gripPoints, osnapModes, geomIds)) ;
return (Acad::eOk);

}

Acad::ErrorStatus huojibkcom::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)
assertWriteEnabled();
for(int i=0; i<indices.length(); i++) {
int idx = indices.at(i);
// For A and center point
if (idx==0 && idx==4) m_PtA += offset;
// For AB and center point
if (idx==1 && idx==4) m_PtAB += offset;
// For B and center point
if (idx==2 && idx==4) m_PtB += offset;
// For BA and center point
if (idx==3 && idx==4) m_PtBA += offset;
}
return (Acad::eOk);

//return (AcDbEntity::subMoveGripPointsAt (indices, offset)) ;
}

AcGePoint3d huojibkcom::get_m_PtA(void) const
{
assertReadEnabled () ;
return (m_PtA) ;
}

Acad::ErrorStatus huojibkcom::put_m_PtA(AcGePoint3d newVal)
{
assertWriteEnabled () ;
m_PtA =newVal ;
return (Acad::eOk) ;
}

AcGePoint3d huojibkcom::get_m_PtB(void) const
{
assertReadEnabled () ;
return (m_PtB) ;
}

Acad::ErrorStatus huojibkcom::put_m_PtB(AcGePoint3d newVal)
{
assertWriteEnabled () ;
m_PtB =newVal ;
return (Acad::eOk) ;
}

AcGePoint3d huojibkcom::get_m_PtAB(void) const
{
assertReadEnabled () ;
return (m_PtAB) ;
}

Acad::ErrorStatus huojibkcom::put_m_PtAB(AcGePoint3d newVal)
{
assertWriteEnabled () ;
m_PtAB =newVal ;
return (Acad::eOk) ;
}

AcGePoint3d huojibkcom::get_m_PtBA(void) const
{
assertReadEnabled () ;
return (m_PtBA) ;
}

Acad::ErrorStatus huojibkcom::put_m_PtBA(AcGePoint3d newVal)
{
assertWriteEnabled () ;
m_PtBA =newVal ;
return (Acad::eOk) ;
}

CString huojibkcom::get_m_Text(void) const
{
assertReadEnabled () ;
return (m_Text) ;
}

Acad::ErrorStatus huojibkcom::put_m_Text(CString newVal)
{
assertWriteEnabled () ;
m_Text =newVal ;
return (Acad::eOk) ;
}

// -----------------------------------------------------------------------------
/*Acad::ErrorStatus huojibkcom::transformBy(const AcGeMatrix3d & xform)
{
//Acad::ErrorStatus retCode =AcDbEntity::transformBy (xform) ;
//return (retCode) ;
assertWriteEnabled();
m_PtA.transformBy(xform);
m_PtAB.transformBy(xform);
m_PtB.transformBy(xform);
m_PtBA.transformBy(xform);
return (Acad::eOk);

}*/

// -----------------------------------------------------------------------------
Acad::ErrorStatus huojibkcom::subTransformBy(const AcGeMatrix3d & xform)
{
//Acad::ErrorStatus retCode =AcDbEntity::subTransformBy (xform) ;
//return (retCode) ;
assertWriteEnabled();
m_PtA.transformBy(xform);
m_PtAB.transformBy(xform);
m_PtB.transformBy(xform);
m_PtBA.transformBy(xform);
return (Acad::eOk);
}

复制

 

3.arx项目里面找到 acrxEntryPoint.cpp文件后再里面的-----MyGroupMyCommand里面添加下面代码,我新建项目时候是HJB,那我这里就是HJBMyGroupMyCommand,如下代码

//在顶部添加  ,自定义的类

#include "StdAfx.h"
#include "resource.h"

#include "..\hjbkdbx\huojibkcom.h"  // 自定义的类

static void HJBMyGroupMyCommand () {
// Put your command code here

ads_point pt1,pt2;
if (acedGetPoint(NULL,_T("第一点:\n"),pt1) != RTNORM)
return;
if (acedGetCorner(pt1,_T("设置第二点:\n"),pt2) != RTNORM)
return;
ACHAR buffer[512];
if (acedGetString(0,_T("输入文本:\n"),buffer) != RTNORM)
return;

// 设置实体

huojibkcom *pEnt = new huojibkcom();

pEnt->put_m_PtA(asPnt3d(pt1));
pEnt->put_m_PtAB(AcGePoint3d(pt2[X],pt1[Y],pt1[Z]));
pEnt->put_m_PtB(asPnt3d(pt2));
pEnt->put_m_PtBA(AcGePoint3d(pt1[X],pt2[Y],pt2[Z]));
pEnt->put_m_Text(buffer);
// 数据库
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,AcDb::kForRead);
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
pBlockTable->close();
AcDbObjectId retId = AcDbObjectId::kNull;
pBlockTableRecord->appendAcDbEntity(retId, pEnt);
pBlockTableRecord->close();
pEnt->close();

}

复制

改变快捷命令代码,系统的太长了,不方便输入,改简单一点

找到

ACED_ARXCOMMAND_ENTRY_AUTO(ChjbkarxApp, hjbMyGroup, MyCommand, MyCommandLocal, ACRX_CMD_MODAL, NULL)

修改说明 ACED_ARXCOMMAND_ENTRY_AUTO(ChjbkarxApp, hjbMyGroup, MyCommand, (此处自定义命令), ACRX_CMD_MODAL, NULL)

代码搬运完后再编译一次,成功就对了,要是失败,如果提示:未定义标识符"CString",是不是新建项目的时候没选MFC选项,dbx和arx都要选择这MFC选项。在第一节新建项目的时候里面有,大概在图6

,如果编译提示

错误 3 error LNK2001: 无法解析的外部符号 "public: enum Acad::ErrorStatus..........
请看下面设置,
ObjectArx自定义对象创建项目-自定义实体代码(2)加编译
ObjectArx自定义对象创建项目-自定义实体代码(2)加编译
如果还是不行,请多试几次,试的时候,一次先设置好在编译,我也试了好几次,才成功
创建自定义实体项目  http://huojibk.com/2021/06/847.html

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: