设为首页收藏本站

中国膜结构网

 找回密码
 立即注册
膜结构车棚
膜结构车棚膜结构资质国产膜材 膜结构网中国膜结构协会
查看: 10|回复: 2

获取文字的长度

[复制链接]
  • TA的每日心情
    开心
    2021-3-7 10:01
  • 签到天数: 1440 天

    连续签到: 19 天

    [LV.10]以坛为家III

    发表于 2021-2-23 10:35 | 显示全部楼层 |阅读模式
    http://www.mjgw.org/ 专业从事膜结构设计、制作加工、施工安装的膜结构咨询服务,能够为客户提供专业的膜结构整体解决方案。做中国最好的膜结构综合服务平台。欢迎大家联系QQ:463017170.
    //getCurrentTextStyle是子函数,用于获取当前活动的文字样式及宽度因子
    double CBasic::getLength( CString str, double height )
    {
    ACHAR *txtStyle = NULL;
    double width = NULL;
    CBasic::getCurrentTextStyle( txtStyle, width );
    struct resbuf *txtlist;
    //下面的acutBuiltList内容,最好与下面的扩展内容结合来看,注意的是前面的数字,如1,40,41,7
    txtlist = acutBuildList( RTDXF0, _T("TEXT"),
                                              1, str,
                                              40, height,
                                              41, width,
                                              7, txtStyle,
                                              NULL);
    ads_point bottomLeft, upRight;
    acedTextBox( txtlist, bottomLeft, upRight );
    acutRelRb( txtlist );
    return fabs( bottomLeft[X] - upRight[X]);
    }



    acdbHostApplicationServices()->workingDatabase()->textstyle()   得到当前TEXTSTYLE变量定义的字体ID。




    objectARX获取当前图层所有文字样式
    复制代码
    void GetAllTextStyle(std::vector<CString> &textStyle)
    {
        textStyle.clear();
        AcDbTextStyleTable *pTextStyleTbl = NULL;
        acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTbl, AcDb::kForRead);
        AcDbTextStyleTableIterator *pIt = NULL;
        pTextStyleTbl->newIterator(pIt);
        for (;!pIt->done();pIt->step())
        {
            AcDbTextStyleTableRecord *pRcd = NULL;
            if (pIt->getRecord(pRcd, AcDb::kForRead) == Acad::eOk)
            {
                TCHAR *szName = NULL;
                pRcd->getName(szName);
                if (_tcslen(szName)>0)
                {
                    textStyle.push_back(szName);
                }
                acutDelString(szName);
                pRcd->close();
            }
        }
        delete pIt;
        pTextStyleTbl->close();
    }
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2021-3-7 10:01
  • 签到天数: 1440 天

    连续签到: 19 天

    [LV.10]以坛为家III

     楼主| 发表于 2021-2-23 10:37 | 显示全部楼层
    1. 由于使用AcDbMtext的extents()方法,只能得到MTEXT的外框,得不到字体的准确宽度,下面是用AcGiTextStyle的解决方法,得到准确的字体宽度。


    2. HOW TO GET A TEXT STRING'S ACTUAL WIDTH
    3. Product AUTOCAD Author LEE,HENRY
    4. Date 26-MAR-99 Document ID 29095
    5. Expiration date Attachments
    6. Keywords API_R14_OK; API_TAHOE_OK; ARX

    7. Developer Consulting Group technical solution. Autodesk confidential, for ADN members only. Please read the disclaimer

    8. Question


    9. We are trying to determine the actual width of the text in an AcDbMText
    10. entity. There is an extents() function which gives the width of the bounding
    11. box. However What I need is the length of the text itself and not its
    12. bounding box. Many times it so happens that the bounding box is much
    13. larger than the actual text itself. Is there a way of doing that?


    14. Answer


    15. Yes, there is a way but not directly from AcDbMText methods. What you said is
    16. true so we must look else where to find an solution. It is from AcGiTextStyle
    17. which has a method called extents(). It gives you the actual width in an
    18. AcGePoint2d.

    19. Here is a code snippet which demonstrates how to obtain the string width from
    20. a user input string.


    21. void getTextStrWidth()
    22. {
    23. char str[132];
    24. int rt = acedGetString(true, "/nEnter a string: ", str);
    25. if(rt != RTNORM)
    26. {
    27. acutPrintf("/nInvalid input, try again.");
    28. return;
    29. }

    30. AcGiTextStyle iStyle;
    31. AcDbTextStyleTable* pTable = NULL;
    32. AcDbTextStyleTableRecord* pRecord = NULL;
    33. try{
    34. ARXOK(curDoc()->database()->getTextStyleTable(pTable,
    35. AcDb::kForRead));
    36. const char styleName[] = "STANDARD";
    37. ARXOK(pTable->getAt(styleName, pRecord, AcDb::kForRead));
    38. ARXOK(fromAcDbTextStyle(iStyle, pRecord->objectId()));
    39. pRecord->close();
    40. pTable->close();
    41. }
    42. catch(const Acad::ErrorStatus es)
    43. {
    44. acutPrintf("/nError: %s", acadErrorStatusText(es));
    45. pRecord->close();
    46. pTable->close();
    47. }
    48. AcGePoint2d pt = iStyle.extents(str, Adesk::kFalse, _tcslen(str),
    49. Adesk::kTrue);
    50. // get the width
    51. acutPrintf("/nText string width is: /t.", pt.x);
    52.      // get the height too
    53.      acutPrintf("/nText string height is: /t.", pt.y);
    54. }

    55. //declare the following inline function in a header file
    56. inline void ARXOK(Acad::ErrorStatus what) throw(Acad::ErrorStatus)
    57. {
    58.     if (what!=Acad::eOk)
    59.      throw what;
    60. }


    61. Note: You have to strip some format characters off an AcDbMText text string
    62. which is some extra work. Otherwise, the width calculation above will not be
    63. accurate.

    64. To make the dimension meaningful for OUTLINETEXTMETRIC, you'll need to
    65. translate the point from WCS to the screen pixel size using
    66. acedCoordFromWorldToPixel().


    67. Disclaimer

    68. This information is derived from a response to a specific question sent to the Developer Consulting Group at Autodesk, and thus given its nature, is subject to change without notice. The information is provided to you on an "as is" basis, and you may use it only at your own risk. Autodesk is not responsible for its quality, nor shall it be liable for any damage or loss caused by your use of this information.

    69. Top
    复制代码
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-3-7 10:01
  • 签到天数: 1440 天

    连续签到: 19 天

    [LV.10]以坛为家III

     楼主| 发表于 2021-2-23 10:38 | 显示全部楼层
    1. void CDcText::getWH(AcDbObjectId objID , double txtScale = 1)
    2. {
    3.         AcDbText *pTxt = NULL;
    4.         acdbOpenObject(pTxt, objID);
    5.         ACHAR *txt;//文本内容
    6.         txt = pTxt->textString();
    7.         AcDbObjectId txtStyleID;//文本样式ID
    8.         txtStyleID = pTxt->textStyle();
    9.         AcDbTextStyleTableRecord *pTxtStyleRcd = new AcDbTextStyleTableRecord;
    10.         acdbOpenObject(pTxtStyleRcd, txtStyleID);
    11.         ACHAR *stylename;//文本样式名称
    12.         pTxtStyleRcd->getName(stylename);
    13.         AcGiTextStyle iStyle;
    14.         fromAcDbTextStyle(iStyle, txtStyleID);
    15.         pTxt->close();
    16.         pTxtStyleRcd->close();
    17.         AcGePoint2d ptWH = iStyle.extents(txt, Adesk::kFalse, _tcslen(txt), Adesk::kTrue);//宽高坐标点对
    18.         txtW = ptWH.x;//文本宽
    19.         txtH = ptWH.y;//文本高
    20.         pt_leftlower = AcGePoint2d(Position.x, Position.y);//左下角点
    21.         pt_rightlower = AcGePoint2d(Position.x +txtW, Position.y);//右下角点
    22.         pt_rightupper = AcGePoint2d(Position.x + txtW, Position.y + txtH);//右上角点
    23.         pt_leftupper = AcGePoint2d(Position.x , Position.y + txtH);//左上角点
    24.         pt_rightlower.rotateBy(Rotation, pt_leftlower);//考虑角度后的右下角点
    25.         pt_rightupper.rotateBy(Rotation ,pt_leftlower);//考虑角度后的右上角点
    26.         pt_leftupper.rotateBy(Rotation, pt_leftlower);//考虑角度后的左上角点
    27.         AcGePoint2d ptMid;//中点坐标
    28.         ptMid.x = (pt_leftlower.x + pt_rightupper.x) / 2;
    29.         ptMid.y = (pt_leftlower.y + pt_rightupper.y) / 2;
    30.         pt_leftlower.scaleBy(txtScale, ptMid);//考虑放缩后的左下点
    31.         pt_rightlower.scaleBy(txtScale, ptMid);//考虑放缩后的右下点
    32.         pt_rightupper.scaleBy(txtScale, ptMid);//考虑放缩后的右上点
    33.         pt_leftupper.scaleBy(txtScale, ptMid);//考虑放缩后的左上点
    34. }
    复制代码
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    关闭

    推荐膜材品牌上一条 /5 下一条

    进口膜材 国产膜材 pvdf膜材ptfe膜材ETFE膜材
    最好的膜结构公司 一级膜结构资质 膜结构一级资质
    膜结构设计-膜结构十大品牌-etfe设计-充气膜结构
    诺科膜结构
    遨都膜结构设计
    中国膜结构网
    中国空间膜结构协会

    QQ|申请友链|Archiver|手机版|中国膜结构论坛

    GMT+8, 2021-3-8 01:07 , Processed in 0.232154 second(s), 22 queries .

    Powered by 中国膜结构网->膜材采购->膜结构设计->膜结构安装->膜结构维修

    © 2014-2020 膜结构协会->进口膜材->国产膜材->PVDF膜材->PTFE膜材->ETFE膜材.

    快速回复 返回顶部 返回列表