|
/ (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.
//
//-----------------------------------------------------------------------------
//----- acrxEntryPoint.cpp
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include <AcDbAssocNetwork.h>
#include <AcDbAssocVariable.h>
#include <dbdimassoc.h>
#include "resource.h"
//-----------------------------------------------------------------------------
#define szRDS _RXST("")
//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CTestAssocApp : public AcRxArxApp {
public:
CTestAssocApp () : 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 void ListVarsInNetwork(const AcDbAssocNetwork *pNet)
{
AcDbObjectIdArray idsActions = pNet->getActions();
for (int i = 0; i < idsActions.length(); i++) {
AcDbObjectPointer<AcDbAssocAction> pAction(idsActions[i],AcDb::kForRead);
if (pAction.openStatus() == Acad::eOk) {
AcDbAssocNetwork *pNetSub = AcDbAssocNetwork::cast(pAction.object());
AcDbAssocVariable *pVar = AcDbAssocVariable::cast(pAction.object());
if (pNetSub) {
ListVarsInNetwork(pNetSub);
} else if (pVar) {
acutPrintf(_T("\nVariable Name=%s Value=%s"),
pVar->name().kACharPtr(), pVar->expression().kACharPtr());
}
}
}
}
static int GetVars(const AcDbAssocNetwork *pNet, AcStringArray &names, AcDbObjectIdArray &ids)
{
AcDbObjectIdArray idsActions = pNet->getActions();
for (int i = 0; i < idsActions.length(); i++) {
AcDbObjectPointer<AcDbAssocAction> pAction(idsActions[i],AcDb::kForRead);
if (pAction.openStatus() == Acad::eOk) {
AcDbAssocVariable *pVar = AcDbAssocVariable::cast(pAction.object());
if (pVar) {
names.append(pVar->name());
ids.append(pVar->objectId());
}
}
}
return ids.length();
}
//
// Command list all AssocVariable
//
static void TestAssocListAssocVars(void)
{
AcDbObjectId idNet = AcDbAssocNetwork::getInstanceFromDatabase(acdbCurDwg(),true);
AcDbObjectPointer<AcDbAssocNetwork> pNet(idNet,AcDb::kForRead);
if (pNet.openStatus() != Acad::eOk) return;
ListVarsInNetwork(pNet.object());
}
//
// Command Add new AssocVariable or change existent
//
static void TestAssocAddAssocVar(void)
{
// Add your code for command TestAssoc.AddAssocVar here
Acad::ErrorStatus es;
AcDbDatabase *pCurDb = acdbCurDwg();
AcDbObjectId idNet = AcDbAssocNetwork::getInstanceFromDatabase(pCurDb,true);
AcDbObjectPointer<AcDbAssocNetwork> pNetMain(idNet,AcDb::kForRead);
if (pNetMain.openStatus() != Acad::eOk) return;
AcDbObjectIdArray ids = pNetMain->getActions();
AcDbObjectId idNetSub;
if (ids.length() == 0) {
AcDbAssocNetwork *pNetSub = new AcDbAssocNetwork();
if (pNetMain->upgradeOpen() == Acad::eOk) {
if (pCurDb->addAcDbObject(idNetSub,pNetSub) == Acad::eOk) {
pNetSub->addAction(idNetSub,true);
pNetSub->close();
} else {
delete pNetSub;
}
pNetMain->downgradeOpen();
}
} else {
idNetSub = ids[0];
}
ACHAR name[512], val[512];
if (acedGetString(TRUE,_T("Variable name: "), name) != RTNORM ||
acedGetString(TRUE,_T("Variable expression: "), val) != RTNORM) {
return;
}
AcDbObjectPointer<AcDbAssocNetwork> pNet(idNetSub,AcDb::kForWrite);
if (pNet.openStatus() != Acad::eOk) return;
AcString nameStr = name, valStr = val, errorMsg;
AcStringArray sVarNames;
AcDbObjectIdArray idsVars;
GetVars(pNet.object(), sVarNames, idsVars);
int ivar = 0;
if (sVarNames.find(name, ivar)) {
// Variable exist - change it
AcDbObjectPointer<AcDbAssocVariable> pVar(idsVars[ivar], AcDb::kForWrite);
if (pVar.openStatus() == Acad::eOk) {
if (pVar->validateNameAndExpression(name,val,errorMsg) != Acad::eOk) {
acutPrintf(_T("\nError %s"), errorMsg.kACharPtr());
return;
}
if ((es = pVar->setExpression(valStr,_T(""),true,true,errorMsg,true)) != Acad::eOk) {
acutPrintf(_T("\npVar->setExpression(valStr))=%s error=%s"),\
acadErrorStatusText(es), errorMsg.kACharPtr());
return;
}
}
} else {
AcDbAssocVariable *ppVar = new AcDbAssocVariable();
AcDbObjectId idVar;
if (pCurDb->addAcDbObject(idVar,ppVar) == Acad::eOk) {
ppVar->close();
pNet->addAction(idVar,true);
} else {
delete ppVar; return;
}
AcDbObjectPointer<AcDbAssocVariable> pVar(idVar, AcDb::kForWrite);
if (pVar.openStatus() == Acad::eOk) {
if (pVar->validateNameAndExpression(name,val,errorMsg) != Acad::eOk) {
acutPrintf(_T("\npVar->validateNameAndExpression(name,val,errorMsg) = %s"), errorMsg.kACharPtr());
pVar->close();
pNet->removeAction(idVar,true);
return;
}
if ((es = pVar->setName(nameStr,true)) != Acad::eOk) {
acutPrintf(_T("\npVar->setName(nameStr,true)=%s error=%s"),\
acadErrorStatusText(es), errorMsg.kACharPtr());
pVar->close();
pNet->removeAction(idVar,true);
return;
}
if ((es = pVar->setExpression(valStr,_T(""),true,true,errorMsg,true)) != Acad::eOk) {
acutPrintf(_T("\npVar->setExpression(valStr))=%s error=%s"),\
acadErrorStatusText(es), errorMsg.kACharPtr());
pVar->close();
pNet->removeAction(idVar,true);
return;
}
}
}
}
// - TestAssoc.ChgBlockVar command (do not rename)
static void TestAssocChgBlockVar(void)
{
Acad::ErrorStatus es;
ads_name en; ads_point p;
AcString errorMsg;
AcStringArray sVarNames;
AcDbObjectIdArray idsVars;
if (acedEntSel(_T("\nSelect dynamic block: "), en, p) != RTNORM) return;
AcDbObjectId eidBref; acdbGetObjectId(eidBref, en);
ACHAR name[512], val[512];
if (acedGetString(TRUE,_T("Variable name: "), name) != RTNORM ||
acedGetString(TRUE,_T("Variable expression: "), val) != RTNORM) {
return;
}
AcDbObjectId idNet;
AcDbVoidPtrArray *reactors = NULL;
{
{
AcDbObjectPointer<AcDbBlockReference> pBref(eidBref, AcDb::kForRead);
if (pBref.openStatus() != Acad::eOk) return;
idNet = AcDbAssocNetwork::getInstanceFromObject(pBref.object()->blockTableRecord(),true);
reactors = pBref->reactors();
}
if (idNet.isNull()) {
acutPrintf(_T("\nNo variables")); return;
}
{
AcDbObjectPointer<AcDbAssocNetwork> pNetMain(idNet,AcDb::kForRead);
if (pNetMain.openStatus() != Acad::eOk) return;
AcDbObjectIdArray ids = pNetMain->getActions();
if (ids.length() == 0) return;
GetVars(pNetMain.object(), sVarNames, idsVars);
}
}
int ivar = 0;
if (sVarNames.find(name, ivar)) {
// Variable exist - change it
AcDbObjectPointer<AcDbAssocVariable> pVar(idsVars[ivar], AcDb::kForWrite);
if (pVar.openStatus() == Acad::eOk) {
if (pVar->validateNameAndExpression(name,val,errorMsg) != Acad::eOk) {
acutPrintf(_T("\nError %s"), errorMsg.kACharPtr());
return;
}
if ((es = pVar->setExpression(val,_T(""),true,true,errorMsg,true)) != Acad::eOk) {
acutPrintf(_T("\npVar->setExpression(valStr))=%s error=%s"),
acadErrorStatusText(es), errorMsg.kACharPtr());
return;
}
AcDbEvalVariant valvar;
if ((es = pVar->evaluateExpression(valvar)) != Acad::eOk) {
acutPrintf(_T("\npVar->evaluateExpression(valvar))=%s error=%s"),
acadErrorStatusText(es), errorMsg.kACharPtr());
return;
}
if ((es = pVar->setValue(valvar)) != Acad::eOk) {
acutPrintf(_T("\npVar->setValue(valvar))=%s error=%s"),
acadErrorStatusText(es), errorMsg.kACharPtr());
return;
}
pVar->evaluateDependencies();
}
{
AcDbObjectPointer<AcDbBlockReference> pBref(eidBref, AcDb::kForWrite);
if (pBref.openStatus() != Acad::eOk) return;
pBref->assertWriteEnabled(true);
}
void *pSomething = NULL;
if (reactors != NULL) {
for (int j = 0; j <= 1; j++) {
for (int i=0; i < reactors->length(); i++) {
pSomething = reactors->at(i);
AcDbObjectId idReact = acdbPersistentReactorObjectId(pSomething);
if (!idReact.isNull()) {
AcDbObjectPointer<AcDbDimAssoc> pDimAssoc(idReact,AcDb::kForWrite);
if (pDimAssoc.openStatus() != Acad::eOk) continue;
pDimAssoc->updateDimension(true,true);
AcDbObjectIdArray idsAss; pDimAssoc->getDimAssocGeomIds(idsAss);
if (idsAss.length() > 0) {
pDimAssoc->updateAssociativity(idsAss);
}
}
}
}
}
} else {
acutPrintf(_T("\nNot found variable %s"), name); return;
}
}
} ;
//-----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CTestAssocApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CTestAssocApp, TestAssoc, ListAssocVars, ListAssocVars, ACRX_CMD_TRANSPARENT, NULL)
ACED_ARXCOMMAND_ENTRY_AUTO(CTestAssocApp, TestAssoc, AddAssocVar, AddAssocVar, ACRX_CMD_TRANSPARENT, NULL)
ACED_ARXCOMMAND_ENTRY_AUTO(CTestAssocApp, TestAssoc, ChgBlockVar, ChgBlockVar, ACRX_CMD_TRANSPARENT, NULL) |
|