c#读DWG文件中Model_Space的数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013779141/article/details/52687519

一,读取DWG文件中的对象坐标信息,比如‘等高线’,遇到代理实体则炸开处理

资料下载:点击这里

二、一言不合就直接上代码

using [email protected];
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Runtime;

namespace Skyline.app.DWG2SKModel
{
    class DWGReader
    {
        Database mDB = null;
        Dictionary<string, List<ObjectId>> mLyrObjDic;
        Dictionary<ObjectId, Entity> mEntityDic;

        //间隔阈值,arc另外考虑一下
        double LineParamValue = 5;//对矢量线按照一定间隔打断,这是作者自己的需求,读者按照自己需求进行获取坐标信息

        Teigha.Runtime.Services mRServices;
        public DWGReader()
        {
            mRServices = new Teigha.Runtime.Services();
        }

        /// <summary>
        /// 图层以及图层中的对象在EntityDic中的key
        /// </summary>
        public Dictionary<string, List<ObjectId>> LyrObjDic
        {
            get
            {
                return mLyrObjDic;
            }
        }

        public Dictionary<ObjectId, Entity> EntityDic
        {
            get
            {
                return mEntityDic;
            }
        }

        public void Open(string pDWGFilePath)
        {
            if (pDWGFilePath == string.Empty) return;
            if (!File.Exists(pDWGFilePath)) return;
            mDB = new Database(false, false);
            mDB.ReadDwgFile(pDWGFilePath, FileOpenMode.OpenForReadAndAllShare, false, "");
            OpenLayer();
            OpenModel();
        }

        private void OpenLayer()
        {
            mLyrObjDic = new Dictionary<string, List<ObjectId>>();
            TransactionManager pTMgr = mDB.TransactionManager;
            using (Transaction pTransac = pTMgr.StartTransaction())
            {
                LayerTable pLTable = pTMgr.GetObject(mDB.LayerTableId, OpenMode.ForRead) as LayerTable;
                IEnumerator pIterator = pLTable.GetEnumerator();
                while (pIterator.MoveNext())
                {
                    ObjectId objId = (ObjectId)pIterator.Current;
                    LayerTableRecord pLTRecord = pTMgr.GetObject(objId, OpenMode.ForRead) as LayerTableRecord;
                    mLyrObjDic.Add(pLTRecord.Name, new List<ObjectId>());
                }
                pTransac.Commit();
            }
        }

        private void OpenModel()
        {
            mEntityDic = new Dictionary<ObjectId, Entity>();
            TransactionManager pTMgr = mDB.TransactionManager;
            using (Transaction pTransac = pTMgr.StartTransaction())
            {
                BlockTable pBTable = pTMgr.GetObject(mDB.BlockTableId, OpenMode.ForRead) as BlockTable;
                ObjectId pModelSpaceId = pBTable[BlockTableRecord.ModelSpace];
                //pModelSpaceId.GetObject(OpenMode.ForRead);
                BlockTableRecord pBTRecord = pTMgr.GetObject(pModelSpaceId, OpenMode.ForRead) as BlockTableRecord;
                IEnumerator pIterator = pBTRecord.GetEnumerator();

                while (pIterator.MoveNext())
                {
                    ObjectId objId = (ObjectId)pIterator.Current;
                    Entity pEntity = pTMgr.GetObject(objId, OpenMode.ForRead) as Entity;
                    //if (!mLyrObjDic.ContainsKey(pEntity.Layer)) continue;
                    mEntityDic.Add(objId, pEntity);
                    mLyrObjDic[pEntity.Layer].Add(objId);//按照图层名进行组织
                }
                pTransac.Commit();
            }
        }

        public List<string> GetLayersName()
        {
            return mLyrObjDic.Keys.ToList();
        }

        

        /// <summary>
        /// 各种类型的间隔取值阈值需要指定
        /// </summary>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public Point3DFList GetObjCoords(ObjectId ObjId)
        {
            Entity pEntity = mEntityDic[ObjId];
            Point3DFList result = new Point3DFList();//该数据结构是作者自己设计的数据结构,读者可以换做其他数据结构
            Point3DFList pList;
            DBObjectCollection dbObjColl = new DBObjectCollection();
            switch (pEntity.GetRXClass().Name.ToLower())
            {
                //弧线与直线、样条曲线的阈值计算不同,单独考虑一下
                case "acdbarc":
                case "acdbcircle":
                case "acdbline":
                case "acdbspline":
                    pList = GetArcCoords(pEntity as Curve, LineParamValue);
                    result.AddRange(pList);
                    break;

                #region Need Explode Entity                    
                case "acdbpolyline":
                    break;
                case "acdbhatch":
                    break;
                case "acdbblockreference":
                    break;
                case "acdbrotateddimension":
                    break;
                case "acdbproxyentity":
                    pList = GetProxyEntityCoords(pEntity as ProxyEntity);
                    result.AddRange(pList);
                    break;
                    #endregion
            }
            return result;
        }

        private Point3DFList GetProxyEntityCoords(ProxyEntity pProxyEntity)
        {
            Point3DFList result = new Point3DFList();

            DBObjectCollection dbObjColl = new DBObjectCollection();
            try
            {
                pProxyEntity.Explode(dbObjColl);//炸开代理实体
            }
            catch (System.Exception)
            {
                return result;
            }
            //pProxyEntity.BlockId as BlockTableRecord;

            foreach (DBObject dbObj in dbObjColl)
            {
                switch (dbObj.GetRXClass().Name.ToLower())
                {

                    //炸开之后还有acdbpolyline类型
                    case "acdbcircle":
                    case "acdbline":
                    case "acdbspline":
                    case "acdbarc":
                        result.AddRange(GetArcCoords(dbObj as Curve, LineParamValue));
                        break;
                    case "acdbpolyline":
                        result.AddRange(GetPolylineCoords(dbObj as Entity, LineParamValue));
                        break;
                    case "acdbproxyentity":
                        result.AddRange(GetProxyEntityCoords(dbObj as ProxyEntity));
                        break;
                }
            }
            return result;
        }

        #region 获取简单实体的坐标
        private Point3DFList GetArcCoords(Curve curve, double ParamSpace)
        {
            Point3DFList result = new Point3DFList();
            if (curve.StartParam == curve.EndParam) return result;
            Point3DF pPt3DF = new Point3DF();
            double splineStartParam = curve.StartParam;
            do
            {
                Point3d pt3d = curve.GetPointAtParameter(splineStartParam);
                result.Add(new Point3DF(pt3d.Y, pt3d.X, pt3d.Z));
                splineStartParam += ParamSpace;
            } while (splineStartParam <= curve.EndParam);
            return result;
        }

        #endregion
        private Point3DFList GetPolylineCoords(Entity pEntity, double ParamSpace)
        {
            bool isc = mEntityDic.Keys.Contains(pEntity.Id);
            Teigha.DatabaseServices.Polyline poly = pEntity as Teigha.DatabaseServices.Polyline;
            Point3DFList result = new Point3DFList();
            DBObjectCollection dbObjColl = new DBObjectCollection();
            try
            {
                poly.Explode(dbObjColl);//炸开代理实体
            }
            catch (System.Exception)
            {
                return result;
            }

            foreach (DBObject dbObj in dbObjColl)
            {
                bool iscon = mEntityDic.Keys.Contains(dbObj.Id);
                //查看炸开后的实体是否还属于原图层
                switch (dbObj.GetRXClass().Name.ToLower())
                {
                    //炸开之后还有acdbpolyline类型
                    case "acdbcircle":
                    case "acdbline":
                    case "acdbspline":
                    case "acdbarc":
                        result.AddRange(GetArcCoords(dbObj as Curve, LineParamValue));
                        break;
                    case "acdbpolyline":
                        result.AddRange(GetPolylineCoords(dbObj as Entity, ParamSpace));
                        break;
                }
            }
            return result;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u013779141/article/details/52687519
今日推荐