Example #1
0
        public Entity DynToStaticEntity(Entity entity)
        {
            MText text = entity as MText;

            if (text != null)
            {
                if (text.HasFields)
                {
                    text.ConvertFieldToText();
                }
                text.Contents = text.Text;
            }
            else
            {
                DBText dbText = entity as DBText;
                if (dbText != null)
                {
                    if (dbText.HasFields)
                    {
                        dbText.ConvertFieldToText();
                    }
                }
            }
            return(entity);
        }
Example #2
0
        //这是转换操作函数
        private void convertToText(PromptSelectionResult SelResult)
        {
            //初始化转换计数
            int count = 0;

            //获取选择集所有实体的ID
            ObjectId[] ObIDs = SelResult.Value.GetObjectIds();
            //启动事务进行实体操作
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //加一个try进行异常处理
                try
                {
                    //遍历选择集中的所有实体
                    foreach (ObjectId ObID in ObIDs)
                    {
                        //依据实体ID获取实体对象
                        //为加快系统处理速度,OpenMode使用ForRead,轮到需要的实体时再用UpgradeOpen()改为ForWrite
                        DBObject ent = trans.GetObject(ObID, OpenMode.ForRead) as DBObject;
                        //属性块的情况麻烦些,单独拎出来
                        if (ent != null)
                        {
                            if (ent.GetType().Name == "BlockReference")
                            {
                                BlockReference blk = ent as BlockReference;
                                //判断是否是动态块
                                if (blk.IsDynamicBlock)
                                {
                                    AttributeCollection ac = blk.AttributeCollection;
                                    foreach (ObjectId arID in ac)
                                    {
                                        AttributeReference ar = arID.GetObject(OpenMode.ForWrite) as AttributeReference;
                                        if (ar.HasFields)
                                        {
                                            ar.ConvertFieldToText();
                                            count++;
                                        }
                                    }
                                }
                            }
                            if (ent.HasFields)
                            {
                                //获取实体的类型进行判断
                                switch (ent.GetType().Name)
                                {
                                //单行文字
                                case "DBText":
                                {
                                    DBText Dt = (DBText)ent;
                                    //改为ForWrite
                                    Dt.UpgradeOpen();
                                    //利用DBText的ConvertFieldToText()方法转换
                                    //注:该方法用在不含有字段的文字时会报错
                                    Dt.ConvertFieldToText();
                                    count++;
                                    break;
                                }

                                //多行文字
                                case "MText":
                                {
                                    MText Dt = (MText)ent;
                                    Dt.UpgradeOpen();
                                    Dt.ConvertFieldToText();
                                    count++;
                                    break;
                                }
                                }
                            }
                        }
                    }
                    //提交事务保存
                    trans.Commit();
                }

                catch (Autodesk.AutoCAD.Runtime.Exception Ex)
                {
                    ed.WriteMessage("\n出错了!" + Ex.ToString());
                }
                finally
                {
                    //关闭事务
                    trans.Dispose();
                }
            }
            ed.WriteMessage("\n完成转换" + count + "个字段");
        }