示例#1
0
        public void CaptureText()
        {
            Document doc    = Application.DocumentManager.MdiActiveDocument;
            Database db     = doc.Database;
            Editor   editor = doc.Editor;

            List <TextCapture> list = new List <TextCapture>();

            using (DocumentLock m_DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    // 获取PickFirst选择集
                    PromptSelectionResult psr = editor.SelectImplied();

                    // 如果提示状态OK,说明启动命令前选择了对象;
                    if (psr.Status != PromptStatus.OK)
                    {
                        WriteMessage($"{psr.Status}没有选中对象\n");
                        return;
                    }
                    SelectionSet sset = psr.Value;
                    foreach (var oid in sset.GetObjectIds())
                    {
                        if (oid.ObjectClass.DxfName == "TEXT")
                        {
                            DBText text = (DBText)oid.GetObject(OpenMode.ForWrite);
                            try
                            {
                                Bitmap      img = EntityToImage(doc, tr, text);
                                TextCapture tc  = new TextCapture(text.TextString, img);
                                list.Add(tc);
                            }
                            catch (System.Exception)
                            {
                                WriteMessage(text.TextString);
                                break;
                            }
                        }
                        else if (oid.ObjectClass.DxfName == "MTEXT")
                        {
                            MText text = (MText)oid.GetObject(OpenMode.ForWrite);
                            // WriteMessage(text.Text + "\n");
                            Bitmap      img = EntityToImage(doc, tr, text);
                            TextCapture tc  = new TextCapture(text.Text, img);
                            list.Add(tc);
                        }
                    }

                    tr.Commit();
                }
            }
            SaveImages(list);
        }
示例#2
0
        private List <TextCapture> EnumBlockTableRecord(Document doc, Transaction tr, BlockTableRecord btr)
        {
            List <TextCapture> list = new List <TextCapture>();

            // 替换模型里所有文本的TextStyle
            foreach (ObjectId oid in btr)
            {
                if (oid.ObjectClass.DxfName == "TEXT")
                {
                    DBText text = (DBText)oid.GetObject(OpenMode.ForWrite);
                    if (string.IsNullOrEmpty(text.TextString))
                    {
                        continue;
                    }
                    try
                    {
                        Bitmap      img = EntityToImage(doc, tr, text);
                        TextCapture tc  = new TextCapture(text.TextString, img);
                        list.Add(tc);
                    }
                    catch (System.Exception e)
                    {
                        WriteMessage($"TEXT ERROR: {text.TextString} , 错误信息 {e}\n");
                        break;
                    }
                }
                else if (oid.ObjectClass.DxfName == "MTEXT")
                {
                    MText text = (MText)oid.GetObject(OpenMode.ForWrite);
                    if (string.IsNullOrEmpty(text.Text))
                    {
                        continue;
                    }
                    try
                    {
                        Bitmap      img = EntityToImage(doc, tr, text);
                        TextCapture tc  = new TextCapture(text.Text, img);
                        list.Add(tc);
                    }
                    catch (System.Exception e)
                    {
                        WriteMessage($"MTEXT ERROR: {text.Text} , 错误信息 {e}\n");
                        break;
                    }
                }
                else if (oid.ObjectClass.DxfName == "INSERT")
                {
                    Entity entity = (Entity)oid.GetObject(OpenMode.ForRead);
                    if (entity is BlockReference)
                    {
                        BlockReference br = entity as BlockReference;
                        // 处理未命名块(命名块在其他地方处理)
                        if (br.Name.StartsWith("*"))
                        {
                            list.AddRange(EnumBlockTableRecord(doc, tr, br.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord));
                        }
                    }
                }
            }
            return(list);
        }