明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1838|回复: 8

[图元] 求助,如何实现从屏幕选中一个DBText,并且读取出单行文本的内容?

[复制链接]
发表于 2015-12-10 15:16 | 显示全部楼层 |阅读模式
本帖最后由 你在仰望天空 于 2015-12-10 15:19 编辑

各位大大,如何实现从屏幕选中一个DBText,并且读取出单行文本的内容?

我试过以下方法没有输出内容。

请教如何才能得到单行文本的内容呢?

      public void Zgbz()
        {

            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //获得文本内容
            string xffl1;

            using (Transaction trans = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())

            {

                 PromptEntityOptions optEnt=new PromptEntityOptions("\n请选取文本");
                 PromptEntityResult resEnt=ed.GetEntity(optEnt);

                 if (resEnt.Status==PromptStatus.OK)
                   {
                     Entity ent = (Entity)trans.GetObject(resEnt.ObjectId, OpenMode.ForRead);
                     xffl1=ent.ToString ();
                    }
                 
                 ed.WriteMessage("\n读取的文本内容为:"+ xffl1);
            }

}







发表于 2015-12-10 20:27 | 显示全部楼层
DBtext ent = (DBtext)trans.GetObject(resEnt.ObjectId, OpenMode.ForRead);
string X= ent.TextSring
 楼主| 发表于 2015-12-10 21:19 | 显示全部楼层
谢谢,这样可以了。之前不知道怎么回事一直没有成功。
 楼主| 发表于 2015-12-10 21:20 | 显示全部楼层
Real_King 发表于 2015-12-10 20:27
DBtext ent = (DBtext)trans.GetObject(resEnt.ObjectId, OpenMode.ForRead);
string X= ent.TextSring

非常感谢,现在可以了。
发表于 2015-12-11 08:21 | 显示全部楼层
最好加一句对象类型判断:
if (Ent is DBText) {
}
保险点!
发表于 2015-12-12 08:39 | 显示全部楼层
本帖最后由 ivde 于 2015-12-12 08:41 编辑

public void ShowTextString ()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
             //获得文本内容
             using (Transaction trans = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
             {
                 PromptEntityOptions optEnt=new PromptEntityOptions("\n请选取文本");
                 optEnt.SetRejectMessage("\n***Only Text***");
                 optEnt.AddAllowedClass(typeof(DBText),true);

                 PromptEntityResult resEnt=ed.GetEntity(optEnt);
                  if (resEnt.Status==PromptStatus.OK)
                  {
                      DBText ent = (DBText)trans.GetObject(resEnt.ObjectId, OpenMode.ForRead);
                      var xffl1 = ent.TextString;
                      ed.WriteMessage("\n读取的文本内容为:"+ xffl1);
                  }
                  trans.Commit();
             }
        }
发表于 2015-12-12 16:45 | 显示全部楼层
这样也行,做好判断即可
发表于 2015-12-14 15:42 | 显示全部楼层
做好判断防止意外中止。楼上果然厉害
发表于 2018-3-3 10:26 | 显示全部楼层
学习了 谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-29 03:58 , Processed in 0.399893 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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