using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;
using Teigha.Export_Import;
using Teigha.GraphicsInterface;
using Teigha.GraphicsSystem;
using Teigha.Runtime;
using OSGeo.OGR;
using System.IO;
namespace ToShp
{
public class ToShp
{
///
/// 转shp
///
/// 打开文件的文件名openFileDialog.FileName
/// 打开文件的类型openFileDialog.FilterIndex
/// 输出文件目录"E:\\Test.shp"
public string Convert(string filename, int openindex, string strVectorFile)
{
Database pDb;
Transaction trans;
//点
OSGeo.OGR.Geometry point = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPoint);
//线
OSGeo.OGR.Geometry linepoint = new OSGeo.OGR.Geometry(wkbGeometryType.wkbMultiLineString);
//圆
OSGeo.OGR.Geometry circlepoint = new OSGeo.OGR.Geometry(wkbGeometryType.wkbLinearRing);
//椭圆
OSGeo.OGR.Geometry arcpoint = new OSGeo.OGR.Geometry(wkbGeometryType.wkbMultiPoint);
//圆弧
// 为了支持中文路径,请添加下面这句代码
OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
// 为了使属性表字段支持中文,请添加下面这句
OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
// 注册所有的驱动
Ogr.RegisterAll();
using (Services ser = new Services())
{
using (pDb = new Database(false, false))//不加参数会出错
{
if (openindex == 1)
{ pDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndReadShare, false, ""); }
if (openindex == 2)
{ pDb.ReadDwgFile(filename, FileShare.Read, true, ""); }
using (trans = pDb.TransactionManager.StartTransaction())
{
BlockTableRecord btab = (BlockTableRecord)pDb.CurrentSpaceId.GetObject(OpenMode.ForRead);
List bb = new List();
#region
//为了支持中文路径,请添加下面这句代码
OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
// 为了使属性表字段支持中文,请添加下面这句
OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
// string strVectorFile = "E:\\Test.shp";
// 注册所有的驱动
Ogr.RegisterAll();
//创建数据,这里以创建ESRI的shp文件为例
string strDriverName = "ESRI Shapefile";
int count = Ogr.GetDriverCount();
Driver oDriver = Ogr.GetDriverByName(strDriverName);
if (oDriver == null)
{
return "驱动不可用";
}
// 创建数据源
DataSource oDS = oDriver.CreateDataSource(strVectorFile, null);
if (oDS == null)
{
return "创建矢量文件【%s】失败!";
}
// 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定
Layer oLayer = oDS.CreateLayer("TestPolygon", null, wkbGeometryType.wkbPolygon, null);
Layer pLayer = oDS.CreateLayer("TestPoint", null, wkbGeometryType.wkbPoint, null);
if (oLayer == null)
{
return "图层创建失败";
}
// 下面创建属性表
// 先创建一个叫FieldID的整型属性
FieldDefn oFieldID = new FieldDefn("FieldID", FieldType.OFTInteger);
oLayer.CreateField(oFieldID, 1);
// 再创建一个叫FeatureName的字符型属性,字符长度为50
FieldDefn oFieldName = new FieldDefn("FieldName", FieldType.OFTString);
oFieldName.SetWidth(100);
oLayer.CreateField(oFieldName, 1);
FeatureDefn oDefn = oLayer.GetLayerDefn();
#endregion
foreach (ObjectId btr in btab)
{
DBObject obj;
try
{
obj = trans.GetObject(btr, OpenMode.ForWrite);
}
catch
{ return "图层被锁定";};
#region 判断obj是否在选定图层上
////获取数据库的图层表对象
//LayerTable lt = (LayerTable)trans.GetObject(pDb.LayerTableId,OpenMode.ForRead);
//foreach (ObjectId layerId in lt)
//{
// LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForRead);
// if (layer.SelectedItems.Contains(ltr.Name)&&)
// {
// }
//}
#endregion
string a = obj.GetType().Name;
if (!bb.Contains(a))
{
bb.Add(a);
}
#region switch
switch (obj.GetType().Name)
{
case "DBPoint":
DBPoint dbpoint = (DBPoint)obj;
#region
//ToMethod.deploy();
//// 创建图层,
//Layer oLayer = oDS.CreateLayer("TestPoint", null, wkbGeometryType.wkbPoint, null);
//if (oLayer == null)
//{
// Console.WriteLine("图层创建失败!\n");
// return;
//}
//// 下面创建属性表
//// 先创建一个叫FieldID的整型属性
//FieldDefn oFieldID = new FieldDefn("FieldID", FieldType.OFTInteger);
//oLayer.CreateField(oFieldID, 1);
//// 再创建一个叫FeatureName的字符型属性,字符长度为50
//FieldDefn oFieldName = new FieldDefn("FieldName", FieldType.OFTString);
//oFieldName.SetWidth(100);
//oLayer.CreateField(oFieldName, 1);
//FeatureDefn oDefn = oLayer.GetLayerDefn();
//// 创建点要素
//Feature oFeatureTriangle = new Feature(oDefn);
//oFeatureTriangle.SetField(0, 0);
//oFeatureTriangle.SetField(1, "点");
//OSGeo.OGR.Geometry point = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPoint);
////添加点
//point.AddPoint(dbpoint.Position.X,dbpoint.Position.Y,dbpoint.Position.Z);
//oFeatureTriangle.SetGeometry(point);
//oLayer.CreateFeature(oFeatureTriangle);
#endregion
point.AddPoint(dbpoint.Position.X, dbpoint.Position.Y, dbpoint.Position.Z);
//创建点
Feature oFeaturePoint = new Feature(oDefn);
oFeaturePoint.SetField(0, 1);
oFeaturePoint.SetField(1, "点");
oFeaturePoint.SetGeometry(point);
pLayer.CreateFeature(oFeaturePoint);
break;
case "Line":
Line line = (Line)obj;
OSGeo.OGR.Geometry pointt = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPoint);
pointt.AddPoint(line.StartPoint.X, line.StartPoint.Y, line.StartPoint.Z);
pointt.AddPoint(line.EndPoint.X, line.EndPoint.Y, line.EndPoint.Z);
linepoint.AddGeometryDirectly(pointt);
//创建线
Feature oFeatureLine = new Feature(oDefn);
oFeatureLine.SetField(0, 1);
oFeatureLine.SetField(1, "线");
oFeatureLine.SetGeometry(linepoint);
oLayer.CreateFeature(oFeatureLine);
break;
case "Ellipse":
Ellipse ellipse = (Ellipse)obj;
circlepoint.AddPoint(ellipse.Center.X+ellipse.MajorRadius, ellipse.Center.Y, 0);
circlepoint.AddPoint(ellipse.Center.X-ellipse.MajorRadius, ellipse.Center.Y, 0);
circlepoint.AddPoint(ellipse.Center.X, ellipse.Center.Y + ellipse.MinorRadius, 0);
circlepoint.AddPoint(ellipse.Center.X, ellipse.Center.Y - ellipse.MinorRadius, 0);
// 创建椭圆
Feature oFeatureHole = new Feature(oDefn);
oFeatureHole.SetField(0, 1);
oFeatureHole.SetField(1, "椭圆");
OSGeo.OGR.Geometry geo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
geo.AddGeometryDirectly(circlepoint);
oFeatureHole.SetGeometry(geo);
oLayer.CreateFeature(oFeatureHole);
break;
#region
//case"Arc":
// Arc arc = (Arc)obj;
// ToMethod.ToArc(arc, oDefn, oLayer);
// break;
#endregion
case "Circle":
Circle circle = (Circle)obj;
circlepoint.AddPoint(circle.Center.X,circle.Center.Y-circle.Radius,0);
circlepoint.AddPoint(circle.Center.X, circle.Center.Y + circle.Radius, 0);
circlepoint.AddPoint(circle.Center.X - circle.Radius, circle.Center.Y, 0);
circlepoint.AddPoint(circle.Center.X + circle.Radius, circle.Center.Y, 0);
// 创建圆
Feature cFeatureHole = new Feature(oDefn);
cFeatureHole.SetField(0, 1);
cFeatureHole.SetField(1, "圆");
OSGeo.OGR.Geometry cgeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
cgeo.AddGeometryDirectly(circlepoint);
cFeatureHole.SetGeometry(cgeo);
oLayer.CreateFeature(cFeatureHole);
break;
case "Polyline":
//Line polyline = (Line)obj;
Teigha.DatabaseServices.Polyline polyline = (Teigha.DatabaseServices.Polyline)obj;
OSGeo.OGR.Geometry pointline = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPoint);
pointline.AddPoint(polyline.StartPoint.X, polyline.StartPoint.Y, polyline.StartPoint.Z);
pointline.AddPoint(polyline.EndPoint.X, polyline.EndPoint.Y, polyline.EndPoint.Z);
linepoint.AddGeometryDirectly(pointline);
//创建线
Feature pFeatureLine = new Feature(oDefn);
pFeatureLine.SetField(0, 1);
pFeatureLine.SetField(1, "线");
pFeatureLine.SetGeometry(linepoint);
oLayer.CreateFeature(pFeatureLine);
break;
}
#endregion
}
trans.Commit();
}
return "转化完成";
}
}
}
}
}