通过Teigha 实现Arcgis格式转换CAD

文章目录

    • 概要
    • 技术细节
    • 小结

概要

通过Teigha 实现Arcgis格式转换CAD  可实现转换arcgis mxd文档 简单填充、单一符号转换,也可以根据要素类直接转换点线面格式,按模板更新线型、字体、填充、块。

技术细节

Teigha使用的.net 4.0版本,具体部署不再描述,网上能够下载,ARCGIS 10.2.2版本

首先建立一个CADCommonHelper

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Runtime;
using System.Drawing.Design;
using ESRI.ArcGIS.Geometry;
using Teigha.Colors;
using Teigha.GraphicsInterface;
using ESRI.ArcGIS.Display;
using System.Drawing;namespace CADCommonHelper
{public class DatabaseHelper{public DatabaseHelper(){}#region 打开一个已知dwg文件public static Database DbOpen(string dwgpath){// 打开主 CAD 文件if (File.Exists(dwgpath)){Database db = new Database(false, true);db.ReadDwgFile(dwgpath, FileOpenMode.OpenTryForReadShare, true, "");return db;}else{Database db = new Database();db.SaveAs(dwgpath, DwgVersion.vAC18);db.Dispose();Database pdb = new Database(false, true);pdb.ReadDwgFile(dwgpath, FileOpenMode.OpenTryForReadShare, true, "");return pdb;}}public static Database DbOpentempSaveas(string dwgpath){if (File.Exists(dwgpath)){File.Delete(dwgpath);}string temppath = Application.StartupPath + "\\style\\Template.dwt";// 打开主 CAD 文件if (File.Exists(temppath)){Database db = new Database(false, true);db.ReadDwgFile(temppath, FileOpenMode.OpenTryForReadShare, true, "");db.SaveAs(dwgpath, DwgVersion.vAC18);return db;}else{MessageBox.Show("模板文件不存在!");return null;}}#endregion#region 添加一个图层/// /// 添加图层或者查询图层 返回ObjectId/// /// /// /// ObjectIdpublic static Dictionary CreateCADLayer(Database db, HashSet layername){Dictionary layerdict = new Dictionary();// 加入图层:using (LayerTable pLayers = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForWrite)){foreach (object layerobj in layername){//图层string layer = layerobj.ToString();if (pLayers.Has(layer)){layerdict.Add(layer, pLayers[layer]);}else{using (LayerTableRecord pLayer = new LayerTableRecord()){pLayer.Name = layer;//图层名必须有//pLayer.Color = Teigha.Colors.Color.FromColorIndex(Teigha.Colors.ColorMethod.ByAci, 1);//颜色if(layer =="水域"){pLayer.Color = Teigha.Colors.Color.FromColorIndex(Teigha.Colors.ColorMethod.ByLayer, 4);//颜色}else if (layer =="行政区注记"){pLayer.Color = Teigha.Colors.Color.FromColorIndex(Teigha.Colors.ColorMethod.ByLayer, 5);//颜色}else{pLayer.Color = Teigha.Colors.Color.FromColorIndex(Teigha.Colors.ColorMethod.ByLayer, 7);//颜色}图层线型获取线型表//LinetypeTable linetypeTable = (LinetypeTable)db.LinetypeTableId.GetObject(OpenMode.ForRead);//using (LinetypeTable pLinetypes = (LinetypeTable)db.LinetypeTableId.GetObject(OpenMode.ForWrite))//{//    ObjectId linetypeId = pLinetypes[Teigha.GraphicsInterface.linetype];//    pLayer.LinetypeObjectId = linetypeId;//}layerdict.Add(layer, pLayers.Add(pLayer));//ObjectID}}}return layerdict;}}public static  ObjectId CreateCADLayer(Database db, string layername){// 加入图层:using (LayerTable pLayers = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForWrite)){//图层if (pLayers.Has(layername)){return pLayers[layername];}else{using (LayerTableRecord pLayer = new LayerTableRecord()){pLayer.Name = layername;//图层名必须有//pLayer.Color = Teigha.Colors.Color.FromColorIndex(Teigha.Colors.ColorMethod.ByAci, 1);//颜色pLayer.Color = Teigha.Colors.Color.FromColorIndex(Teigha.Colors.ColorMethod.ByLayer, 7);//颜色图层线型获取线型表//LinetypeTable linetypeTable = (LinetypeTable)db.LinetypeTableId.GetObject(OpenMode.ForRead);//using (LinetypeTable pLinetypes = (LinetypeTable)db.LinetypeTableId.GetObject(OpenMode.ForWrite))//{//    ObjectId linetypeId = pLinetypes[Teigha.GraphicsInterface.linetype];//    pLayer.LinetypeObjectId = linetypeId;//}return pLayers.Add(pLayer);//ObjectID}}}}#endregion#region 调整图层或图元绘制顺序/// /// 调整绘制顺序/// /// /// /// /// public static void DrawOrderSet(Database pDb, BlockTableRecord bBTR, ObjectId objid, bool top){using (DrawOrderTable pSET = (DrawOrderTable)bBTR.DrawOrderTableId.GetObject(OpenMode.ForWrite)){ObjectIdCollection id = new ObjectIdCollection();//id.Add(greenHatchId);//pSET.MoveBelow(id, redHatchId);id.Add(objid);if (top){pSET.MoveToTop(id);}else{pSET.MoveToBottom(id);}}}#endregion#region 加载快映射表/// /// 加载快映射表/// /// public static Dictionary LoadBlockMaping(Database db){Dictionary blockdict = new Dictionary();Dictionary mapkdict = new Dictionary();string filename = Application.StartupPath + "\\style\\blockmap.dat";if (!File.Exists(filename)){MessageBox.Show("没找到块文件映射关系");return null;}using (BlockTable pTable = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForWrite)){foreach (ObjectId id in pTable){BlockTableRecord pBlock = (BlockTableRecord)id.GetObject(OpenMode.ForWrite);                        


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章