明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 927|回复: 0

批量替换cad文件里面的文字

[复制链接]
发表于 2019-3-7 16:53 | 显示全部楼层 |阅读模式
public void OandS()
        {
            string fdbname = "";
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult result = fbd.ShowDialog();
            if (result == DialogResult.OK)
            {
                fdbname = fbd.SelectedPath;
            }
            if (result == DialogResult.Cancel) { return; }
            DirectoryInfo fldinfo = new DirectoryInfo(fdbname);
            FileInfo[] files = fldinfo.GetFiles("*.dwg");
            foreach (var f in files)
            {
                //Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
                using (Database db = new Database(false, true))
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    string filename = f.FullName;
                    string filename2 = @"C:\Users\Administrator\Desktop\dwgout"+"\\"+f.Name;
                    if (System.IO.File.Exists(filename))
                    {
                        db.ReadDwgFile(filename, System.IO.FileShare.ReadWrite, true, null);
                        /*
                        LayerTable lt = (LayerTable) db.LayerTableId.GetObject(OpenMode.ForRead);
                        foreach (ObjectId id in lt )
                        {
                            LayerTableRecord ltr = (LayerTableRecord) id.GetObject(OpenMode.ForRead);
                            if(ltr != null) ed.WriteMessage("\n" + ltr.Name);
                        }
                        */
                        BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
                        BlockTableRecord btr =
                            (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        foreach (ObjectId id in btr)
                        {
                            Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
                            string enttype = ent.GetType().ToString();
                            if (ent.Layer.ToString() == "TK" && enttype == "Autodesk.AutoCAD.DatabaseServices.DBText")
                            {
                                DBText dbtext = ent as DBText;
                                if (dbtext.TextString.Contains("航测成图"))
                                {
                                    ent.Erase();
                                    //dbtext.TextString = "测试";
                                    DBText newtext = new DBText();
                                    newtext.Position = dbtext.Position;
                                    newtext.Height = dbtext.Height;
                                    newtext.TextString = dbtext.TextString.Replace("航测", "缩编");
                                    newtext.HorizontalMode = dbtext.HorizontalMode;
                                    newtext.VerticalMode = dbtext.VerticalMode;
                                    newtext.AlignmentPoint = dbtext.AlignmentPoint;
                                    newtext.TextStyleId = dbtext.TextStyleId;
                                    db.AddToModelSpace(newtext);

                                }
                            }
                        }

                        db.SaveAs(filename2, DwgVersion.AC1800);
                        trans.Commit();
                    }
                }
            }
            System.Windows.Forms.MessageBox.Show("文字替换转换完成!");
        }

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

本版积分规则

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

GMT+8, 2024-3-29 04:57 , Processed in 0.148087 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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