明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2627|回复: 4

统一文字样式、字体高度及宽高比

[复制链接]
发表于 2015-4-4 16:11 | 显示全部楼层 |阅读模式
本帖最后由 j15tty 于 2015-4-5 17:31 编辑
  1. [CommandMethod("WZXG")]
  2.         public void WZG()
  3.         {
  4.             Database db = HostApplicationServices.WorkingDatabase;
  5.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  6.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  7.             PromptSelectionOptions options = new PromptSelectionOptions();
  8.             SelectionFilter fil = new SelectionFilter(new TypedValue[] { new TypedValue(0, "*text,mtext") });
  9.             options.MessageForAdding = "\n选择文字:";
  10.             OpenTextStyle.ss1  = ed.GetSelection(options, fil);
  11.             OpenTextStyle opentextstyle = new OpenTextStyle();
  12.             Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(opentextstyle);
  13.         }

  14.     public partial class OpenTextStyle : Form
  15.     {
  16.         public static PromptSelectionResult ss1;
  17.         public OpenTextStyle()
  18.         {
  19.             InitializeComponent();
  20.         }

  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             this.Hide();
  24.             Database db = HostApplicationServices.WorkingDatabase;
  25.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  26.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  27.             using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
  28.             {
  29.                 SelectionSet ss = ss1.Value;
  30.                 if (ss1.Status != PromptStatus.OK) return;
  31.                 if (ss1.Status == PromptStatus.Cancel) return;
  32.                 ObjectIdCollection objectId1 = new ObjectIdCollection();
  33.                 ObjectIdCollection objectId2 = new ObjectIdCollection();
  34.                 using (Transaction tr1 = doc.TransactionManager.StartTransaction())
  35.                 {
  36.                     foreach (ObjectId id in ss.GetObjectIds())
  37.                     {
  38.                         Entity entity = tr.GetObject(id, OpenMode.ForRead) as Entity;
  39.                         if (entity.GetType().Name == "DBText")
  40.                         {
  41.                             objectId1.Add(id);
  42.                         }
  43.                         else if (entity.GetType().Name == "MText")
  44.                         {
  45.                             objectId2.Add(id);
  46.                         }
  47.                     }
  48.                     tr1.Commit();
  49.                 }
  50.                 using (Transaction tr2 = doc.TransactionManager.StartTransaction())
  51.                 {
  52.                     foreach (ObjectId textId in objectId1)
  53.                     {
  54.                         DBText dText = tr2.GetObject(textId, OpenMode.ForWrite) as DBText;
  55.                             using (Transaction tr3 = db.TransactionManager.StartTransaction())
  56.                             {
  57.                                 TextStyleTable st = (TextStyleTable)db.TextStyleTableId.GetObject(OpenMode.ForRead);
  58.                                 foreach (ObjectId td in st)
  59.                                 {
  60.                                     TextStyleTableRecord textR = tr.GetObject(td, OpenMode.ForRead) as TextStyleTableRecord;
  61.                                     if (textR.Name == this.comboBox1.SelectedItem.ToString())
  62.                                     {
  63.                                         dText.TextStyleId = td;
  64.                                     }
  65.                                 }
  66.                                 tr3.Commit();
  67.                             }
  68.                             dText.Height = Convert.ToDouble(this.textBox1.Text.ToString());
  69.                             dText.WidthFactor = Convert.ToDouble(this.textBox2.Text.ToString());
  70.                             dText.DowngradeOpen();
  71.                     }

  72.                     foreach (ObjectId textId1 in objectId2)
  73.                     {
  74.                         MText Mtext = tr2.GetObject(textId1, OpenMode.ForWrite) as MText;
  75.                             using (Transaction tr4 = db.TransactionManager.StartTransaction())
  76.                             {
  77.                                 TextStyleTable st = (TextStyleTable)db.TextStyleTableId.GetObject(OpenMode.ForRead);
  78.                                 foreach (ObjectId td in st)
  79.                                 {
  80.                                     TextStyleTableRecord textR = tr.GetObject(td, OpenMode.ForWrite) as TextStyleTableRecord;
  81.                                     if (textR.Name == this.comboBox1.SelectedItem.ToString())
  82.                                     {

  83.                                         textR.XScale = Convert.ToDouble(this.textBox2.Text.ToString());
  84.                                         Mtext.TextStyleId = td;
  85.                                         textR.DowngradeOpen();
  86.                                     }
  87.                                 }
  88.                                 tr4.Commit();
  89.                             }
  90.                             Mtext.TextHeight = Convert.ToDouble(this.textBox1.Text.ToString());
  91.                             Mtext.DowngradeOpen();
  92.                     }
  93.                     tr2.Commit();
  94.                 }
  95.                 tr.Commit();
  96.             }
  97.             this.Close();
  98.         }

  99.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  100.         {

  101.         }

  102.         private void OpenTextStyle_Load(object sender, EventArgs e)
  103.         {
  104.             Database db = HostApplicationServices.WorkingDatabase;
  105.             List<string> comString = new List<string>();
  106.             using (Transaction tr = db.TransactionManager.StartTransaction())
  107.             {
  108.                 TextStyleTable st = (TextStyleTable)db.TextStyleTableId.GetObject(OpenMode.ForRead);
  109.                 foreach (ObjectId tId in st)
  110.                 {
  111.                     TextStyleTableRecord textR = tr.GetObject(tId, OpenMode.ForRead) as TextStyleTableRecord;
  112.                     comString.Add(textR.Name);
  113.                 }
  114.                 this.comboBox1.DataSource = comString;
  115.                 tr.Commit();
  116.             }
  117.         }

  118.         private void button2_Click(object sender, EventArgs e)
  119.         {
  120.             this.Close();
  121.         }
  122.     }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2015-4-5 10:46 | 显示全部楼层
代码函数错误
 楼主| 发表于 2015-4-5 17:29 | 显示全部楼层
wyl605 发表于 2015-4-5 10:46
代码函数错误

这个我测试可以用啊
发表于 2015-4-5 23:12 | 显示全部楼层
本帖最后由 雪山飞狐_lzh 于 2015-4-5 23:14 编辑

代码写的太啰嗦了,,,
这里可以这样,只需要一次事务的就不要多开,否则效率很低
而且其他的实体也可以只用这个事务打开

  1.             private void button1_Click(object sender, EventArgs e)
  2.             {
  3.                 this.Hide();
  4.                 Database db = HostApplicationServices.WorkingDatabase;
  5.                 Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  6.                 Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  7.                 SelectionSet ss = ss1.Value;
  8.                 if (ss1.Status != PromptStatus.OK) return;

  9.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  10.                 {

  11.                     foreach (ObjectId id in ss.GetObjectIds())
  12.                     {
  13.                         Entity entity = tr.GetObject(id, OpenMode.ForRead) as Entity;
  14.                         if (entity is DBText)
  15.                         {
  16.                             DBText txt = entity as DBText;
  17.                             //...
  18.                         }
  19.                         else
  20.                         {
  21.                             MText txt = entity as MText;
  22.                             //...
  23.                         }

  24.                     }


  25.                     tr.Commit();
  26.                 }
  27.                 //this.Close();
  28.             }
 楼主| 发表于 2015-4-6 21:23 | 显示全部楼层
雪山飞狐_lzh 发表于 2015-4-5 23:12
代码写的太啰嗦了,,,
这里可以这样,只需要一次事务的就不要多开,否则效率很低
而且其他的实体也可以 ...

恩,我也觉得,看人家用lisP做过,所以就用C#写了个,不太熟悉,谢谢修改
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-26 11:11 , Processed in 0.462457 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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