批量输出dwg文件中的文本

  公司来了一批图纸,里面有一部分内容需要复制到excel中,几百张来图每一张都

手工复制,烦死了。编写一个CAD插件,自动导出文本,简单记录在下面。

想法是:

1.输入命令,选择所有dwg文件

2.挨个处理dwg文件,生成同名的txt文件保存文本

基本思路是用Database.ReadDwgFile 读取dwg文件,因为这样可以不用显示文档,可以提高速度;

        [CommandMethod("GetText")]
        public void GetTextCST()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "dwg files (*.dwg)|*.dwg|All files (*.*)|*.*";
            ofd.FilterIndex = 1;
            ofd.RestoreDirectory = true;
            ofd.Multiselect = true;

            if(ofd.ShowDialog()== DialogResult.OK)
                foreach (string fn in ofd.FileNames)
                    try
                    {
                        DoDwg2Csv(fn);
                    }
                    catch
                    {
                        File.AppendAllText("D:\\dwg2csv.txt", DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + fn + "\n");
                    }
        }

  

        private void DoDwg2Csv(string fn)
        {
            string csvname = fn + ".csv";
            using (Database db = new Database(false, true))
            {
                db.ReadDwgFile(fn, FileOpenMode.OpenForReadAndAllShare, false, "");
                db.CloseInput(true);

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    // 模型空间
                    BlockTable blkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(
                        blkTbl[BlockTableRecord.ModelSpace], 
                        OpenMode.ForRead)
                        as BlockTableRecord;

                    // 遍历模型空间,提取文字                    
                    List<DBText> txts = new List<DBText>();
                    foreach (ObjectId oid in modelSpace)
                    {
                        DBObject dbobj = tr.GetObject(oid, OpenMode.ForRead);
                        if (dbobj is Entity)
                        {
                            Entity entity = dbobj as Entity;
                            string enttype = entity.GetRXClass().Name;
                            if (enttype == "AcDbText")
                            {
                                DBText acText = entity as DBText;
                                if (acText.Position.X < 587 || acText.Position.Y < 115)//指定范围
                                    continue;                                
                                txts.Add( acText );
                            }
                        }//if (dbobj is Entity)
                    }//foreach (ObjectId oid in modelSpace)

                    txts.Sort((t1, t2) => t1.Position.X >= t2.Position.X ? 1 : -1);

                    for (int i = 0; i < txts.Count;i++)
                        File.AppendAllText(csvname, txts[i].TextString+"\r\n", Encoding.Default);
                }//using
            }//using           
        }

  

 

转载于:https://www.cnblogs.com/sinceret/p/10108866.html

在 WPF 使用 Teigha 插入图片可以通过以下步骤实现: 1. 首先,确保你已经安装了 Teigha.NET 控件,可以从 Teigha 官网下载并安装。 2. 在 WPF 项目添加对 Teigha.NET 控件的引用。在 Visual Studio 右键点击项目,选择“添加” -> “引用”,然后浏览到 Teigha.NET 控件的安装目录,选择相应的 DLL 文件进行引用。 3. 在 XAML 文件添加一个 Image 控件来显示图片,例如: ```xaml <Image x:Name="imageControl" /> ``` 4. 在代码使用 Teigha.NET 控件来加载和显示图片。首先,需要创建一个 Document 对象,并加载图片文件: ```csharp using Teigha.Runtime; using Teigha.DatabaseServices; using Teigha.GraphicsSystem; // ... private void LoadImage(string imagePath) { // 初始化 Teigha HostApplicationServices.Initialize(); // 创建 Document 对象 using (var doc = new Document(false)) { doc.Database.ReadDwgFile(imagePath, FileOpenMode.OpenForReadAndAllShare, false, null); // 获取 ModelSpace 块表记录 var modelSpace = (BlockTableRecord)TransactionHelper.GetModelSpace(doc.Database); // 获取第一个实体 var entity = modelSpace.Cast<ObjectId>().Select(id => id.GetObject(OpenMode.ForRead)).FirstOrDefault(); if (entity != null && entity is Entity) { // 使用 Teigha.GraphicsSystem 控件绘制实体 using (var image = new Image()) { var graphics = GraphicsSystemFactory.CreateGraphics(image, true); graphics.SetLogicalWindow(new Extents2d(0, 100, 0, 100)); graphics.SetViewParameters(Point2d.Origin, 1.0, 0); entity.Draw(graphics); // 将绘制的图像转换为 WPF 图像 var wpfImage = new System.Windows.Media.Imaging.BitmapImage(); wpfImage.BeginInit(); wpfImage.StreamSource = new MemoryStream(image.GetBitmap()); wpfImage.EndInit(); // 在 Image 控件显示图像 imageControl.Source = wpfImage; } } } } ``` 在上述代码,`imagePath` 是图片文件的路径。代码会加载图片文件,获取第一个实体并使用 Teigha.GraphicsSystem 控件绘制实体。然后,将绘制的图像转换为 WPF 图像,并在 Image 控件显示。 请注意,在使用 Teigha.NET 控件之前,需要初始化 Teigha,可以使用 `HostApplicationServices.Initialize()` 方法进行初始化。 希望这可以帮助到你!如果你有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值