Migration Advice - AcString Type

The original file is at http://private.peterlink.ru/poleshchuk/cad/2006/M2007-AcStringe.htm

A.Rivilis. Migration Advice - AcString Type

 

This is a continuation of the theme: migration of ARX applications to AutoCAD 2007.
In ObjectARX there is a special class (data type) AcString declared in AcString.h of versions 2006 and 2007 (in ObjectARX 2005 or earlier there is no such a type). Text string declaration when using the class looks like the following one:

AcString str;

This data type is very much alike CString type (from the MFC library) and std::string (from the standard library, STL) but is different because it allows to operate both on strings containing char symbols and on string containing wchar_t symbols. The AcString class has constructors creating strings from char type and wchar_t type, e.g.:

AcString sa("String of char");
AcString sw(_T("String of wchar_t"));

With the help of a corresponding constructor one can create an empty string or a string initialized with a symbol of type char or of type wchar_t.
AcString type assists in conversion between char and wchar_t types, e.g.:

const char *pStrA1 = sa.kszPtr();

or

const char *pStrA2 = sa; // implicitely (const char *)sa; is called
const wchar_t *pStrW1 = sa.kwszPtr();
const wchar_t *pStrW2 = sa; // implicitely (const wchar_t *)sa; is called

Moreover the class has operators/functions for string concatenation, substring search and selection, getting string length, formatting, conversion to number and so on. Using AcString type allows to write the code that in overwhelming majority of cases will work in AutoCAD 2006 as well as in AutoCAD 2007 and next versions.
As an example, the following expression will work in both versions of AutoCAD:

acedPrompt (AcString("/nПечатаем строку"));

You must be careful only while calling the function with a variable number of arguments (e.g. acutPrintf when number of arguments is greater than one, as well as acedCommand, acutBuildList) because compilator cannot understand what type is to be passed. In these cases it is more preferable to use methods AcString::kszPtr() and AcString::kwszPtr() for explicit clarification of data type. On basis of this class it is convenient to make macros for passing required type to functions, e.g.:

#ifndef INC_UNI_H
    #include <AcString.h>
#define INC_UNI_H
  #if defined(ACADR17)
 // Conversion from UNICODE to ANSI
    #define W2C(x) ((const char    *)AcString(x))
 // Conversion from ANSI to UNICODE
    #define C2W(x) ((const wchar_t *)AcString(x))
  #else
    #define W2C(x) (x)
    #define C2W(x) (x)
  #endif
#endif

If for preprocessor in ObjectARX 2006 project the ACADR16 identifier is set or in ObjectARX 2007 project the ACADR17 identifier is set then the lines

char *str1 = "/nPrinting a string <%s>";
char *str2 = "---my string---";
acutPrintf(C2W(str1), C2W(str2));

will be compiled rightly and will work in both versions of AutoCAD.
Similarly the following expression will be compiled without errors and will be normally executed:

acedCommand (RTSTR, C2W("_.LINE"), RTSTR, C2W("0,0"), RTSTR, C2W("10,10"), RTNONE);

(Translation: N.Poleshchuk)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值