本篇文章是利用teigha这个库来执行cad的dwg文件相关操作。利用该库读取了dwg数据库,然后对数据库复制块,设置块的单位为无。开发这样功能工具的缘由,来源于dwg文件是由arcgis转换过来的,其中带有投影信息,在进行坐标转换的时候出现由于投影信息的缘故,使文件数据库dwg文件的单位为英寸,导致了坐标转换出现了很大的偏差。而我们有相当数量这样的文件,为此急需开发这样的小工具。
而如果是使用AutoCAD制作无单位的块,其方式如下图所示。
我们使用c#开发AutoCAD的方式,在winform窗体中添加数据表格控件datagridview用于展示某目录下所有的dwg、DWG后缀的文件,将数据的路径和文件名呈现在该控件中。最后利用teigha库读取表格中的路径,再做相应的转块制作,结果保存在读取目录下新创建的“结果”文件。开发的工具界面如下图所示。
其中源代码如下所示。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.Runtime;
namespace DeleteUnitWin
{
public partial class Form1 : Form
{
/*路径链表*/
private IList<PathInfo> IPathInfo = new List<PathInfo>();
/*BindingList的路径链表*/
private BindingList<PathInfo> BPathInfo;
/*默认路径*/
string defaultPath = "";
public Form1()
{
InitializeComponent();
}
private void btn_Dir_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择一个文件夹";
dialog.ShowNewFolderButton = false;
if (defaultPath != "")
{
//设置此次默认目录为上一次选中目录
dialog.SelectedPath = defaultPath;
} //按下确定选择的按钮
if (dialog.ShowDialog() == DialogResult.OK)
{
//记录选中的目录
defaultPath = dialog.SelectedPath;
}
List<FileInfo> listFiles = GetAllFilesInDirectory(defaultPath);
int cnt = listFiles.Count;
/*清空之前的数据*/
IPathInfo.Clear();
int id = 1;
for (int i = 0; i < cnt; i++)
{
FileInfo fileInfo = listFiles[i];
string fullName = fileInfo.FullName;
string fileName = fileInfo.Name;
PathInfo pathInfo = new PathInfo();
string prex = fullName.Substring(fullName.LastIndexOf(".")+1);
/*如果后缀为DWG或dwg*/
if (prex.Equals("DWG"))
{
pathInfo.PathID = Convert.ToString(id);
pathInfo.PathName = fullName;
pathInfo.FileName = fileName;
IPathInfo.Add(pathInfo);
id++;
}else if( prex.Equals("dwg")){
pathInfo.PathID = Convert.ToString(id);
pathInfo.PathName = fullName;
pathInfo.FileName = fileName;
IPathInfo.Add(pathInfo);
id++;
}
}
BPathInfo = new BindingList<PathInfo>(IPathInfo);
this.dgv_dwgs.DataSource = BPathInfo;
}
/// <summary>
/// 返回指定目录下所有文件信息
/// </summary>
/// <param name="strDirectory">目录字符串</param>
/// <returns></returns>
public List<FileInfo> GetAllFilesInDirectory(string strDirectory)
{
List<FileInfo> listFiles = new List<FileInfo>(); //保存所有的文件信息
DirectoryInfo directory = new DirectoryInfo(strDirectory);
DirectoryInfo[] directoryArray = directory.GetDirectories();
FileInfo[] fileInfoArray = directory.GetFiles();
if (fileInfoArray.Length > 0) listFiles.AddRange(fileInfoArray);
foreach (DirectoryInfo _directoryInfo in directoryArray)
{
DirectoryInfo directoryA = new DirectoryInfo(_directoryInfo.FullName);
DirectoryInfo[] directoryArrayA = directoryA.GetDirectories();
FileInfo[] fileInfoArrayA = directoryA.GetFiles();
if (fileInfoArrayA.Length > 0) listFiles.AddRange(fileInfoArrayA);
GetAllFilesInDirectory(_directoryInfo.FullName);//递归遍历
}
return listFiles;
}
private void btn_Convert_Click(object sender, EventArgs e)
{
/*保存路径*/
string savepath = "";
if (string.IsNullOrEmpty(defaultPath) || defaultPath == "")
{
System.Windows.Forms.MessageBox.Show("选择文件存放位置");
return;
}
else {
savepath = defaultPath + "/结果文件/";
if (false == System.IO.Directory.Exists(savepath))
{
/*创建保存的文件夹*/
System.IO.Directory.CreateDirectory(savepath);
}/*如果存在则删除该目录下所有的文件*/
else if (true== System.IO.Directory.Exists(savepath))
{
string[] strFileName = Directory.GetFiles(savepath);
foreach (var item in strFileName)
{
File.Delete(item);
}
}
}
int cnt = IPathInfo.Count;
int cntSuccess = 0;
foreach(PathInfo pathInfo in BPathInfo)
{
using (Services svc = new Services())
{
Database db = new Database(false, false);
if (!File.Exists(pathInfo.PathName))
{
return;
}
db.ReadDwgFile(pathInfo.PathName, FileOpenMode.OpenForReadAndReadShare, false, "");
/*制作块*/
Database wbdb = db.Wblock();
/*设置无单位块*/
wbdb.Insunits = UnitsValue.Undefined;
/*如果文件不存在,则保存*/
if (!File.Exists(savepath + pathInfo.FileName))
{
wbdb.SaveAs(savepath + pathInfo.FileName, DwgVersion.Current);
db.Dispose();
cntSuccess++;
//BPathInfo.Remove(pathInfo);
}
/*释放资源*/
db.Dispose();
}
}
System.Windows.Forms.MessageBox.Show("成功转换了"+Convert.ToString(cntSuccess)+"个dwg文件");
}
}
}
更多内容,微信扫二维码关注公众号