ObjectARX

ObjectARX

Reply
Active Contributor
zhangxinhua789
Posts: 32
Registered: ‎01-29-2013
Message 1 of 14 (585 Views)
Accepted Solution

how to change the value of user variable of autocad2010

585 Views, 13 Replies
01-29-2013 06:55 PM

    HEllo, I want to ask a question of Parametric Drawing using objectarx 2010

 

    I define a user Variables in autocad2010(such as variableaa=10),I want to use the variable to drive the dimension of a line。

   Now I want to change the value of  variableaa  through objectarx2010,can you tell me how to change the value of  variableaa  through objectarx2010 ? can you tell me the code?

 

thank you very much!

// (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 "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;
}
}
}
}
} ;
//-----------------------------------------------------------------------------
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)

 

Also a great sample: http://adndevblog.typepad.com/adnassocconstraintapi.zip

Mentor
diagodose2009
Posts: 263
Registered: ‎12-04-2009
Message 2 of 14 (573 Views)

Re: how to change the value of user variable of autocad2010

01-29-2013 11:03 PM in reply to: zhangxinhua789

Hi...

You can use ads_getsym and ads_putsym

Regards.

[source=c++arx]

#define tchar ACHAR *

int
lspfn_setqE(const tchar variabile,ads_name *puiubrat)
{ int rr;
  static struct resbuf * a1;
  rr=RTREJ;
  a1=nil;
  if (variabile==nil)   //euaici: aici seteaza variabila din lisp
       return(rr);
  a1 = ads_buildlist(RTENAME,puiubrat, NULL);
  rr = ads_putsym(variabile, a1);        
  ads_relrb(a1);
  a1 = nil;
  return(rr);
};
inline int
/*FCN*/lspfn_setqA(const tchar variabile,int vashort)
{ int rr;
  static struct resbuf * a1;
  rr=RTREJ;
  a1=nil;
  if (variabile==nil)   //euaici: aici seteaza variabila din lisp
       return(rr);
  a1 = ads_buildlist(RTSHORT, vashort, NULL);
  rr = ads_putsym(variabile, a1);        
  ads_relrb(a1);
  a1 = nil;
  return(rr);
};

[/source=c++arx]

Active Contributor
zhangxinhua789
Posts: 32
Registered: ‎01-29-2013
Message 3 of 14 (566 Views)

Re: how to change the value of user variable of autocad2010

01-29-2013 11:37 PM in reply to: zhangxinhua789

  Firstly,Thank you very much!

  but I can't understand your code ,I am a beginner,I can not find the function "ads_buildlist" in objectarx2010 Help。

  I think the class "AcDbAssocVariables 、AcDbAssoc2dConstraintGroup、AcExplicitConstraint"  is helpful,but I don't know how to use them。

  Thanks again!

Moderator
Alexander.Rivilis
Posts: 1,452
Registered: ‎04-09-2008
Message 4 of 14 (547 Views)

Re: how to change the value of user variable of autocad2010

01-30-2013 07:50 AM in reply to: zhangxinhua789
// (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 "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;
                }
            }

        }
    }
} ;

//-----------------------------------------------------------------------------
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)

 

Also a great sample: http://adndevblog.typepad.com/adnassocconstraintapi.zip


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
zhangxinhua789
Posts: 32
Registered: ‎01-29-2013
Message 5 of 14 (536 Views)

Re: how to change the value of user variable of autocad2010

01-30-2013 06:12 PM in reply to: zhangxinhua789

firstly thank you very much!
Then I'm so sorry,I can't understand your code,your code is very complex.
In order to achieve my aim,I think there is three steps;
1 open the database of the DWG file(but I don't know the function );
2 search the user parameter of autocad2010;(I don't know the function );
3 change the value of the user parameter;(I don't know the function );
I think the class "AcDbAssocVariables 、AcDbAssoc2dConstraintGroup、AcExplicitConstraint"
is helpful,but I don't know how to use them。
Can you tell me the code?
thank you very much!

Moderator
Alexander.Rivilis
Posts: 1,452
Registered: ‎04-09-2008
Message 6 of 14 (528 Views)

Re: how to change the value of user variable of autocad2010

01-30-2013 10:27 PM in reply to: zhangxinhua789

1. Have you tried to build my code?

2. Have you tried to run commands ListAssocVars and AddAssocVar?

3. This is exactly the variables of which you speak? There are also AutoCAD system variables and lisp-variables.

It is very difficult to understand what you want without the dwg-file and a detailed description of what the drawing contains and how this must be replaced.

Without answers to these questions I will not try to help you.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
zhangxinhua789
Posts: 32
Registered: ‎01-29-2013
Message 7 of 14 (484 Views)

Re: how to change the value of user variable of autocad2010

02-20-2013 10:22 PM in reply to: Alexander.Rivilis

aa.jpg  Thank you very much! It's very kind of you.

 this is a screenshot of the dwg-file。I want to change the value of user1  through objectarx2010(user1 is a User Variables),my purpose is to change the length of the line.could you tell me the code ? Thank you very much!

Moderator
Alexander.Rivilis
Posts: 1,452
Registered: ‎04-09-2008
Message 8 of 14 (479 Views)

Re: how to change the value of user variable of autocad2010

02-20-2013 10:40 PM in reply to: zhangxinhua789

Hi,zhangxinhua789!

Please attach this dwg-file to your post in order to I can test my code with it.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
zhangxinhua789
Posts: 32
Registered: ‎01-29-2013
Message 9 of 14 (466 Views)

ok

02-21-2013 02:04 AM in reply to: Alexander.Rivilis
Thank you very much!
Active Contributor
zhangxinhua789
Posts: 32
Registered: ‎01-29-2013
Message 10 of 14 (463 Views)

Re: how to change the value of user variable of autocad2010

02-21-2013 02:09 AM in reply to: zhangxinhua789
 
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.