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
{
/// <summary>
/// 转shp
/// </summary>
/// <param name="filename">打开文件的文件名openFileDialog.FileName</param>
/// <param name="openindex">打开文件的类型openFileDialog.FilterIndex</param>
/// <param name="strVectorFile">输出文件目录"E:\\Test.shp"</param>
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<string> bb = new List<string>();
#region
//为了支持中文路径,请添加下面这句代码
OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
// 为了使属性表字段支持中文,请添加下面这句
OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
// string strVectorFile =
cad图层dwg,dxf转shp
最新推荐文章于 2024-07-28 19:02:59 发布
本文档介绍了一个C#程序,用于将CAD的DWG和DXF文件转换为ESRI SHP格式。程序使用Teigha库解析CAD图层,并通过OGR库将几何对象转换为SHP文件中的点、线和多边形。
摘要由CSDN通过智能技术生成