明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1078|回复: 3

PromptSelectionResult 查询总是error

[复制链接]
发表于 2017-12-20 17:07 | 显示全部楼层 |阅读模式
本帖最后由 蓝色 于 2017-12-21 09:12 编辑

public class Class1
    {
        [CommandMethod("GetLayerPro")]
        public static void GetLayerPro()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //新建一个数据库对象以读取Dwg文件
            Database db = new Database(false, true);
            string fileName = "C:/Users/admin/Desktop/1.dwg";
            //如果指定文件名的文件存在
            if (System.IO.File.Exists(fileName))
            {
                //把文件读入到数据库中
                db.ReadDwgFile(fileName, System.IO.FileShare.Read, true, null);
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    //获取数据库的图层表对象
                    LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
                    //循环遍历每个图层
                    foreach (ObjectId layerId in lt)
                    {
                        LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForRead);
                        if (ltr != null)
                        {

                           List<ObjectId> entitys = GetLayerEntity(ltr.Name);
                           foreach (ObjectId id in entitys)
                                {
                                    Entity hatchobj = trans.GetObject(id, OpenMode.ForWrite) as Entity;
                                if (hatchobj != null)
                                {
                                    Debug.WriteLine("===============");

                                    TypedValue[] typedValue = hatchobj.XData.AsArray();
                                    if (typedValue[0].Value.Equals("PIPELINE") || typedValue[0].Value.Equals("PIPE"))
                                    {
                                        foreach (TypedValue obj in hatchobj.XData)
                                        {
                                            Debug.WriteLine(obj.Value);
                                        }
                                    }
                                }
                            }
                        }
                           
                            Autodesk.AutoCAD.Colors.Color layerColor = ltr.Color;
                            ed.WriteMessage("\n图层名称为:" + ltr.Name);
                            ed.WriteMessage("\n图层颜色为:" + layerColor.ToString());
                        }
                    trans.Commit();
                }
            }
        }
        
         //根据图层名获取该图层下的实体id
        public static List<ObjectId> GetLayerEntity(String layerName)
        {
            List<ObjectId> entitys = new List<ObjectId>();
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            TypedValue[] values = { new TypedValue((int)DxfCode.LayerName, layerName) };
            SelectionFilter filters = new SelectionFilter(values);

            PromptSelectionResult result = ed.SelectAll(filters );
            PromptSelectionResult result = ed.SelectAll();


            if (result.Status == PromptStatus.OK)
            {
                ObjectId[] ids = result.Value.GetObjectIds();
                foreach (ObjectId id in ids)
                {
                    entitys.Add(id);
                }
            }
            else
            {
                return entitys;
            }
            return entitys;
        }
    }
}

红色部分为什么获取的都是error   ,  求大神来解释一下

发表于 2017-12-20 18:27 来自手机 | 显示全部楼层
你在当前文档过滤打开的文档的对象?没看懂你要做什么 如果是读入的db只能遍历 不能用选择集的
 楼主| 发表于 2017-12-21 08:59 | 显示全部楼层
本帖最后由 蓝色 于 2017-12-21 09:04 编辑
雪山飞狐_lzh 发表于 2017-12-20 18:27
你在当前文档过滤打开的文档的对象?没看懂你要做什么 如果是读入的db只能遍历 不能用选择集的

如果要用选择集该怎么写,我这里是要分层读出每层的实体
发表于 2017-12-21 09:58 来自手机 | 显示全部楼层
用db.readdwg读入的文件是无法用选择集的 只能遍历
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-26 16:09 , Processed in 0.291933 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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