统一文字样式、字体高度及宽高比
本帖最后由 j15tty 于 2015-4-5 17:31 编辑public void WZG()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionOptions options = new PromptSelectionOptions();
SelectionFilter fil = new SelectionFilter(new TypedValue[] { new TypedValue(0, "*text,mtext") });
options.MessageForAdding = "\n选择文字:";
OpenTextStyle.ss1= ed.GetSelection(options, fil);
OpenTextStyle opentextstyle = new OpenTextStyle();
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(opentextstyle);
}
public partial class OpenTextStyle : Form
{
public static PromptSelectionResult ss1;
public OpenTextStyle()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{
SelectionSet ss = ss1.Value;
if (ss1.Status != PromptStatus.OK) return;
if (ss1.Status == PromptStatus.Cancel) return;
ObjectIdCollection objectId1 = new ObjectIdCollection();
ObjectIdCollection objectId2 = new ObjectIdCollection();
using (Transaction tr1 = doc.TransactionManager.StartTransaction())
{
foreach (ObjectId id in ss.GetObjectIds())
{
Entity entity = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (entity.GetType().Name == "DBText")
{
objectId1.Add(id);
}
else if (entity.GetType().Name == "MText")
{
objectId2.Add(id);
}
}
tr1.Commit();
}
using (Transaction tr2 = doc.TransactionManager.StartTransaction())
{
foreach (ObjectId textId in objectId1)
{
DBText dText = tr2.GetObject(textId, OpenMode.ForWrite) as DBText;
using (Transaction tr3 = db.TransactionManager.StartTransaction())
{
TextStyleTable st = (TextStyleTable)db.TextStyleTableId.GetObject(OpenMode.ForRead);
foreach (ObjectId td in st)
{
TextStyleTableRecord textR = tr.GetObject(td, OpenMode.ForRead) as TextStyleTableRecord;
if (textR.Name == this.comboBox1.SelectedItem.ToString())
{
dText.TextStyleId = td;
}
}
tr3.Commit();
}
dText.Height = Convert.ToDouble(this.textBox1.Text.ToString());
dText.WidthFactor = Convert.ToDouble(this.textBox2.Text.ToString());
dText.DowngradeOpen();
}
foreach (ObjectId textId1 in objectId2)
{
MText Mtext = tr2.GetObject(textId1, OpenMode.ForWrite) as MText;
using (Transaction tr4 = db.TransactionManager.StartTransaction())
{
TextStyleTable st = (TextStyleTable)db.TextStyleTableId.GetObject(OpenMode.ForRead);
foreach (ObjectId td in st)
{
TextStyleTableRecord textR = tr.GetObject(td, OpenMode.ForWrite) as TextStyleTableRecord;
if (textR.Name == this.comboBox1.SelectedItem.ToString())
{
textR.XScale = Convert.ToDouble(this.textBox2.Text.ToString());
Mtext.TextStyleId = td;
textR.DowngradeOpen();
}
}
tr4.Commit();
}
Mtext.TextHeight = Convert.ToDouble(this.textBox1.Text.ToString());
Mtext.DowngradeOpen();
}
tr2.Commit();
}
tr.Commit();
}
this.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void OpenTextStyle_Load(object sender, EventArgs e)
{
Database db = HostApplicationServices.WorkingDatabase;
List<string> comString = new List<string>();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TextStyleTable st = (TextStyleTable)db.TextStyleTableId.GetObject(OpenMode.ForRead);
foreach (ObjectId tId in st)
{
TextStyleTableRecord textR = tr.GetObject(tId, OpenMode.ForRead) as TextStyleTableRecord;
comString.Add(textR.Name);
}
this.comboBox1.DataSource = comString;
tr.Commit();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
} 代码函数错误 wyl605 发表于 2015-4-5 10:46 static/image/common/back.gif
代码函数错误
这个我测试可以用啊 本帖最后由 雪山飞狐_lzh 于 2015-4-5 23:14 编辑
代码写的太啰嗦了,,,
这里可以这样,只需要一次事务的就不要多开,否则效率很低
而且其他的实体也可以只用这个事务打开
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
SelectionSet ss = ss1.Value;
if (ss1.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in ss.GetObjectIds())
{
Entity entity = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (entity is DBText)
{
DBText txt = entity as DBText;
//...
}
else
{
MText txt = entity as MText;
//...
}
}
tr.Commit();
}
//this.Close();
} 雪山飞狐_lzh 发表于 2015-4-5 23:12 static/image/common/back.gif
代码写的太啰嗦了,,,
这里可以这样,只需要一次事务的就不要多开,否则效率很低
而且其他的实体也可以 ...
恩,我也觉得,看人家用lisP做过,所以就用C#写了个,不太熟悉,谢谢修改 要是能加个点选源对象,再框选目标对象就更好了
页:
[1]