Example #1
0
        public void ImportDxfDrawing(string dxfName, ProjectLoadingView loadingForm)
        {

            loadingForm.ShowProgress(0,"导入环境初始化...");
            //DbConnection.GetConnection().Open();
            MainService mainService = ServiceFactory.GetMainService();
            TmpCaddService caddService = ServiceFactory.GetTmpCaddService();
            TmpCadxService cadxService = ServiceFactory.GetTmpCadxService();
            TmpCadmService cadmService = ServiceFactory.GetTmpCadmService();
            TmpCadzjService cadzjService = ServiceFactory.GetTmpCadzjService();
            TmpCadxdataService cadxdataService = ServiceFactory.GetTmpCadxdataService();
            
            int srid = mainService.GetGeometryColumnSRID("tmpcadd", "geometry");
            mainService.ClearCADTemps();
           
            
            DataSource ds = Ogr.Open(dxfName, 0);
            Driver drv = ds.GetDriver();
            loadingForm.ShowProgress(0, "导入图形数据...");
            for (int i = 0; i < ds.GetLayerCount(); i++)
            {
                Layer pLayer = ds.GetLayerByIndex(i);
                Feature feat = pLayer.GetNextFeature();
                while (feat != null)
                {
                    string handle = feat.GetFieldAsString(4);
                    string typeName = feat.GetFieldAsString(1).Trim().Replace("AcDbEntity:","");
                    string geomtryStr = "";
                    Geometry geometry = feat.GetGeometryRef();
                    geometry.FlattenTo2D();
                    geometry.ExportToWkt(out geomtryStr);
                    switch (typeName)
                    {
                        case "AcDbPolyline":
                        case "AcDbLine":
                            TmpCadx cadx = new TmpCadx(handle, geomtryStr, "POLYLINE", dxfName);
                            cadxService.Create(cadx);
                            break;
                        case "AcDbBlockReference":
                            //TmpCadd cadd=new TmpCadd(handle, geomtryStr, "POINT");
                            //caddService.Create(cadd);
                            //因为GDAL直接读取了块图形,因此,块在下面的方法和属性一起读取
                            break;
                        case "AcDbText:AcDbText":
                            TmpCadzj cadzj = new TmpCadzj(handle, geomtryStr, "TEXT", dxfName);
                            cadzjService.Create(cadzj);
                            break;
                      
                    }
                  
                    feat.Dispose();
                    feat = pLayer.GetNextFeature();
                }
            }
            

            //开始使用netDxf读取数据
            DxfDocument doc=DxfDocument.Load(dxfName);
            //开始读取点数据
            loadingForm.ShowProgress(40, "导入块参照属性...");
            for (int i = 0; i < doc.Inserts.Count; i++)
            {
                Insert insert= doc.Inserts[i];
                TmpCadd cadd=new TmpCadd();
                cadd.Handle = insert.Handle;
                cadd.EntityType = "POINT";
                cadd.FileName = dxfName;
                cadd.Geometry =
                    DbGeometry.FromText(string.Format("POINT({0} {1})", insert.Position.X, insert.Position.Y));
                caddService.Create(cadd);

                TmpCadxdata cadxdata = new TmpCadxdata();
                cadxdata.Handle = insert.Handle;
                cadxdata.Tc = insert.Layer.Name;
                cadxdata.Fh = insert.Block.Name;
                cadxdata.Fhdx = insert.Scale.X;
                cadxdata.Xzjd = insert.Rotation;
                cadxdata.FileName = dxfName;
                cadxdata = ReadXData(cadxdata, insert.XData);
                cadxdataService.Create(cadxdata);
            }

            //开始读取点数据
            loadingForm.ShowProgress(50, "导入文本数据...");
            for (int i = 0; i < doc.Texts.Count; i++)
            {
                Text insert = doc.Texts[i];
                //TmpCadd cadd = new TmpCadd();
                //cadd.Handle = insert.Handle;
                //cadd.EntityType = "POINT";
                //cadd.Geometry =
                //    DbGeometry.FromText(string.Format("POINT({0} {1})", insert.Position.X, insert.Position.Y));
                //caddService.Create(cadd);

                TmpCadxdata cadxdata = new TmpCadxdata();
                cadxdata.Handle = insert.Handle;
                cadxdata.Tc = insert.Layer.Name;
                cadxdata.Fh = insert.Style.FontFamilyName;
                cadxdata.Fhdx = insert.Height;
                cadxdata.Xzjd = insert.Rotation;
                cadxdata.Wbnr = insert.Value;
                cadxdata.FileName = dxfName;
                cadxdata = ReadXData(cadxdata, insert.XData);
                cadxdataService.Create(cadxdata);
            }

            //开始读取线数据
            loadingForm.ShowProgress(60, "导入线数据...");
            for (int i = 0; i < doc.Lines.Count; i++)
            {
                Line insert = doc.Lines[i];
                //TmpCadd cadd = new TmpCadd();
                //cadd.Handle = insert.Handle;
                //cadd.EntityType = "POINT";
                //cadd.Geometry =
                //    DbGeometry.FromText(string.Format("POINT({0} {1})", insert.Position.X, insert.Position.Y));
                //caddService.Create(cadd);

                TmpCadxdata cadxdata = new TmpCadxdata();
                cadxdata.Handle = insert.Handle;
                cadxdata.Tc = insert.Layer.Name;
                cadxdata.Fh = insert.Linetype.Name;
                cadxdata.Fhdx = insert.LinetypeScale;
                cadxdata.Xzjd = 0.0;
                cadxdata.Wbnr = "";
                cadxdata.FileName = dxfName;
                cadxdata = ReadXData(cadxdata, insert.XData);
                cadxdataService.Create(cadxdata);
            }
            loadingForm.ShowProgress(80, "导入多边形数据...");
            for (int i = 0; i < doc.LwPolylines.Count; i++)
            {
                LwPolyline insert = doc.LwPolylines[i];
                if (insert.IsClosed)
                {
                    //插入多边形
                    TmpCadx cadx = cadxService.GetTmpCadx(insert.Handle);
                    if (cadx != null)
                    {
                        TmpCadm cadm=new TmpCadm() {Handle = cadx.Handle,EntityType="POLYGON",FileName=dxfName};
                        string wkt = cadx.Geometry.AsText();
                        Geometry geometry = Ogr.ForceToPolygon(Geometry.CreateFromWkt(wkt));
                        string geomStr = "";
                        geometry.ExportToWkt(out geomStr);
                        geomStr = geomStr.Replace("LINESTRING", "POLYGON (") + ")";
                        cadm.Geometry = DbGeometry.FromText(geomStr);
                        cadmService.Create(cadm);
                    }
                }
                //TmpCadd cadd = new TmpCadd();
                //cadd.Handle = insert.Handle;
                //cadd.EntityType = "POINT";
                //cadd.Geometry =
                //    DbGeometry.FromText(string.Format("POINT({0} {1})", insert.Position.X, insert.Position.Y));
                //caddService.Create(cadd);

                TmpCadxdata cadxdata = new TmpCadxdata();
                cadxdata.Handle = insert.Handle;
                cadxdata.Tc = insert.Layer.Name;
                cadxdata.Fh = insert.Linetype.Name;
                cadxdata.Fhdx = insert.LinetypeScale;
                cadxdata.Xzjd = 0.0;
                cadxdata.Wbnr = "";
                cadxdata.FileName = dxfName;
                cadxdata = ReadXData(cadxdata, insert.XData);
                cadxdataService.Create(cadxdata);
            }

            for (int i = 0; i < doc.Polylines.Count; i++)
            {
                Polyline insert = doc.Polylines[i];
                if (insert.IsClosed)
                {
                    //插入多边形
                    TmpCadx cadx = cadxService.GetTmpCadx(insert.Handle);
                    if (cadx != null)
                    {
                        TmpCadm cadm = new TmpCadm() { Handle = cadx.Handle, EntityType = "POLYGON" ,FileName = dxfName};
                        string wkt = cadx.Geometry.AsText();
                        Geometry geometry = Ogr.ForceToPolygon(Geometry.CreateFromWkt(wkt));
                        string geomStr = "";
                        geometry.ExportToWkt(out geomStr);
                        geomStr=geomStr.Replace("LINESTRING", "POLYGON (")+")";
                        cadm.Geometry = DbGeometry.FromText(geomStr);
                        cadmService.Create(cadm);
                    }
                }
                //TmpCadd cadd = new TmpCadd();
                //cadd.Handle = insert.Handle;
                //cadd.EntityType = "POINT";
                //cadd.Geometry =
                //    DbGeometry.FromText(string.Format("POINT({0} {1})", insert.Position.X, insert.Position.Y));
                //caddService.Create(cadd);

                TmpCadxdata cadxdata = new TmpCadxdata();
                cadxdata.Handle = insert.Handle;
                cadxdata.Tc = insert.Layer.Name;
                cadxdata.Fh = insert.Linetype.Name;
                cadxdata.Fhdx = insert.LinetypeScale;
                cadxdata.Xzjd = 0.0;
                cadxdata.Wbnr = "";
                cadxdata.FileName = dxfName;
                cadxdata = ReadXData(cadxdata, insert.XData);
                cadxdataService.Create(cadxdata);
            }
            loadingForm.ShowProgress(100, "导入完成...");
            //mainService.Close();
        }