📄 addxdata.cpp
字号:
// addXdata.cpp
// date: 03st.Dec.2003
#include <aced.h>
#include <rxregsvc.h>
#include <adsdef.h>
#include <adscodes.h>
void initApp();
void unloadApp();
AcDbObject* selectObject(AcDb::OpenMode openMode);
void printList(struct resbuf* pRb);
void addXdata();
void initApp()
{
// register a command with the AutoCAD mechanism.
acedRegCmds->addCommand("ASDK_AEXP_COMMANDS",
"ADDXDATA",
"ADDXDATA",
ACRX_CMD_TRANSPARENT,
addXdata);
}
void unloadApp()
{
acedRegCmds->removeGroup("ASDK_AEXP_COMMANDS");
}
void addXdata()
{
AcDbObject* pObj = selectObject(AcDb::kForRead);
if (!pObj) {
acutPrintf("Error selecting object\n");
return;
}
// Get the applicatoin name and string to be added to xdata.
//
char appName[132], resString[200];
appName[0] = resString[0] = '\0';
acedGetString(NULL, "\nEnter application name: ", appName);
acedGetString(NULL, "\nEnter string to be added: ", resString);
struct resbuf *pRb, *pTemp;
pRb = pObj->xData(appName);
if (pRb != NULL) {
// If xdata is present, then walk to the end of the list.
//
for (pTemp = pRb; pTemp->rbnext != NULL; pTemp = pTemp->rbnext)
{ ; }
}
else {
// If xdata is not present, register the application and add appName to
// the first resbuf in the list.
// Notice that there is no -3 group as there is in AutoLISP.
// This is ONLY the xdata so the -3 xdata-start marker isn't needed.
//
acdbRegApp(appName);
pRb = acutNewRb(AcDb::kDxfRegAppName);
pTemp = pRb;
pTemp->resval.rstring = (char*) malloc(strlen(appName) + 1);
strcpy(pTemp->resval.rstring, appName);
}
// Add user-specified string to the xdata.
//
pTemp->rbnext = acutNewRb(AcDb::kDxfXdAsciiString);
pTemp = pTemp->rbnext;
pTemp->resval.rstring = (char*) malloc(strlen(resString) + 1);
strcpy(pTemp->resval.rstring, resString);
// The following code shows the use of upgradeOpen()
// to change the entity from read to write.
//
pObj->upgradeOpen();
pObj->setXData(pRb);
pObj->close();
acutRelRb(pRb);
}
// This function prompts the user to select an entity or
// enter an object's handle. It then proceeds to open the
// object/entity and return a pointer to it.
//
AcDbObject* selectObject(AcDb::OpenMode openMode)
{
// Allow user to either pick an entity, or type in the
// object handle.
//
int ss;
ads_name en;
ads_point pt;
acedInitGet(RSG_OTHER, "Handle _Handle");
ss = acedEntSel("\nSelect an Entity or either"
" 'H' to enter its handle: ", en, pt);
char handleStr[132];
AcDbObjectId eId;
switch (ss)
{
case RTNORM: // got it!
break;
case RTKWORD:
if ((acedGetString(Adesk::kFalse, "Enter Valid Object Handle: ", handleStr) == RTNORM)
&& (acdbHandEnt(handleStr, en) == RTNORM))
{
break;
}
// Fall-through intentional
//
default:
acutPrintf("Nothing Selected, Return Code == %d\n", ss);
return NULL;
}
// Now, exchange the ads_name for the object Id...
//
Acad::ErrorStatus retStat;
retStat = acdbGetObjectId(eId, en);
if (retStat != Acad::eOk) {
acutPrintf("\nacdbGetObjectId failed");
acutPrintf("\nen==(%lx,%lx), retStat==%d\n",
en[0], en[1], eId);
return NULL;
}
AcDbObject* obj;
if ((retStat = acdbOpenObject(obj, eId, openMode)) != Acad::eOk)
{
acutPrintf("acdbOpenEntity failed: ename:(%lx,%lx),"
" mode:%d retStat:%d", en[0], en[1], openMode, retStat);
return NULL;
}
return obj;
}
// This function accepts a linked list of resbufs as it's
// argument and runs through the list printing out the
// restype and resval values one set per line.
//
void printList(struct resbuf* pRb)
{
int rt, i;
char buf[133];
for (i = 0; pRb != NULL; i++,pRb = pRb->rbnext){
if (pRb->restype < 1010) {
rt = RTSTR;
} else if (pRb->restype < 1040) {
rt = RT3DPOINT;
} else if (pRb->restype < 1060) {
rt = RTREAL;
} else if (pRb->restype < 1071) {
rt = RTSHORT;
} else if (pRb->restype == 1071) {
rt = RTLONG;
} else {// restype is already RTSHORT, RTSTR,...
rt = pRb->restype; // or else it is unknown.
}
switch (rt)
{
case RTSHORT:
if (pRb->restype == RTSHORT) {
acutPrintf("RTSHORT : %d\n", pRb->resval.rint);
} else {
acutPrintf("(%d . %d)\n", pRb->restype, pRb->resval.rint);
};
break;
case RTREAL:
if (pRb->restype == RTSTR) {
acutPrintf("RTSTR : %s\n", pRb->resval.rstring);
} else {
acutPrintf("(%d . \"%s\")\n", pRb->restype, pRb->resval.rstring);
};
break;
case RTSTR:
if (pRb->restype == RTSTR) {
acutPrintf("RTSTR : %s\n",
pRb->resval.rstring);
} else {
acutPrintf("(%d . \"%s\")\n", pRb->restype, pRb->resval.rstring);
};
break;
case RT3DPOINT:
if (pRb->restype == RT3DPOINT) {
acutPrintf("RT3DPOINT : %0.3f, %0.3f, %0.3f\n",
pRb->resval.rpoint[X],
pRb->resval.rpoint[Y],
pRb->resval.rpoint[Z]);
} else {
acutPrintf("(%d %0.3f %0.3f %0.3f)\n",
pRb->restype,
pRb->resval.rpoint[X],
pRb->resval.rpoint[Y],
pRb->resval.rpoint[Z]);
}
break;
case RTLONG:
acutPrintf("RTLONG : %dl\n", pRb->resval.rlong);
break;
}
if ((i == 23) && (pRb->rbnext != NULL)) {
i = 0;
acedGetString(0, "Press <ENTER> to continue...", buf);
}
}// for
}
// End of function 'selectObject.
// Dll Entry Point.
extern "C" AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void *pkt)
{
switch (msg)
{
case AcRx::kInitAppMsg:
acrxDynamicLinker->unlockApplication(pkt);
acrxDynamicLinker->registerAppMDIAware(pkt);
initApp();
break;
case AcRx::kUnloadAppMsg:
unloadApp();
break;
default:
break;
}
return AcRx::kRetOK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -