下面是在新打开的文档中插入数据库或插入块参照(未解决)
using System.IO;
using System.Reflection;
namespace IfoxDemo;
public static class FZK
{//复制快到另一个图
[CommandMethod("xx")]
public static void FZKDemo()
{//新打开一个指定文档,把另一个文档当成快插入到新文档,缩放新文档视图
var doca = Env.Document;
using var tr1= new DBTrans(doca,true,true);
DocumentCollection docs = Application.DocumentManager;
string newdocPath = "C:\\Users\\Administrator\\Desktop\\演示合并dwg\\33.dwg";
Document newdoc = docs.Open(newdocPath, false);//.Add("acad.dwt");//docs.Add("新");
docs.MdiActiveDocument = newdoc;//新文档设置为当前文档
System.Windows.Forms.Application.DoEvents();
string blockPath = @"C:\Users\Administrator\Desktop\XK新块.dwg";
using var tr = new DBTrans(newdoc,true,true);
var btrid = tr.BlockTable.GetBlockFrom(blockPath, false);
tr.ModelSpace.Database.InsertBlock( btrid,Point3d.Origin);
System.Windows.Forms.Application.DoEvents();
//var ed = tr.Editor;
//ed.Redraw();
//ed.ZoomExtents();//目前缩放失败,关闭时需保存,否则插入不进去块
//Env.Editor.ZoomExtents();//第一个图可以缩放和重画,打开的新图缩放失败
//Env.Editor.Redraw();
}
}
批量插入失败:
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace IfoxDemo;
public static class FZK
{//复制快到另一个图
[CommandMethod("xx")]
public static void FZKDemo()
{//新打开一个指定文档,把另一个文档当成快插入到新文档,缩放新文档视图
var doca = Env.Document;
using var tr1= new DBTrans(doca,true,true);
DocumentCollection docs = Application.DocumentManager;
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择dwg文件所在文件夹:"; // 设置对话框的标题
DialogResult dlgResult = dialog.ShowDialog();
if (dlgResult == DialogResult.OK)
{
string dialogPathName = dialog.SelectedPath;
string[] fileNames = Directory.GetFiles(dialogPathName, "*.dwg", SearchOption.AllDirectories);//此行代码为只合并dwg
foreach (string fileName in fileNames)
{
// string newdocPath = "C:\\Users\\Administrator\\Desktop\\演示合并dwg\\33.dwg";
Document newdoc = docs.Open(fileName, false);//.Add("acad.dwt");//docs.Add("新");
docs.MdiActiveDocument = newdoc;//新文档设置为当前文档
System.Windows.Forms.Application.DoEvents();
string blockPath = @"C:\Users\Administrator\Desktop\XK新块.dwg";
using var tr = new DBTrans(newdoc, true, true);
var btrid = tr.BlockTable.GetBlockFrom(blockPath, false);
tr.ModelSpace.Database.InsertBlock(btrid, Point3d.Origin);//目前批量插入还未能成功
System.Windows.Forms.Application.DoEvents();
// newdoc.CloseAndSave(newdoc.Database.Filename );
//var ed = tr.Editor;
//ed.Redraw();
//ed.ZoomExtents();//目前缩放失败,关闭时需保存,否则插入不进去块
//Env.Editor.ZoomExtents();//第一个图可以缩放和重画,打开的新图缩放失败
//Env.Editor.Redraw();
tr.Commit();
}
}
}
}
下面是dxf预览图问题(未解决)
public void DwgToDxf1()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
//Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
DateTime beforDT = System.DateTime.Now;
// Database currentdb = HostApplicationServices.WorkingDatabase;
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择dwg文件所在文件夹:"; // 设置对话框的标题
DialogResult dlgResult = dialog.ShowDialog();
if (dlgResult == DialogResult.OK)
{
int counter = 0;
string dialogPathName = dialog.SelectedPath;
// 新文件夹的名称 和 新文件夹路径的名称
string newFolderName = "DXF_"+DateTime.Now.ToString("yyyyMMdd_HHmm");
string newFolderPath = Path.Combine(dialogPathName, newFolderName);
// 检查新文件夹是否已存在
if (!Directory.Exists(newFolderPath))
{ // 如果不存在,则创建它
Directory.CreateDirectory(newFolderPath);
}
string[] fileNames = Directory.GetFiles(dialogPathName, "*.dwg", SearchOption.AllDirectories);//此行代码为只合并dwg
foreach (string fileName in fileNames)
{
string fullfilename = Path.Combine(newFolderPath, Path.GetFileNameWithoutExtension(fileName)+".dxf");//getfilename不是全路径,getfile是全路径
Document opendoc = Application.DocumentManager.Open(fileName,false);
Bitmap thumb = null;
using (DocumentLock acLckDoc = opendoc.LockDocument())
{
Application.DocumentManager.MdiActiveDocument = opendoc;
System.Windows.Forms.Application.DoEvents();
Editor opened = opendoc.Editor;
try
{
var db = opendoc.Database;
db.UpdateExt(true);
doc.CloseAndDiscard();
using (ViewTableRecord view = opened.GetCurrentView())
{
//opendoc.Editor.GetCurrentView();new ViewTableRecord();//
double xxx = (db.Extmin.X + db.Extmax.X) / 2;
double yyy = (db.Extmin.Y + db.Extmax.Y) / 2;
view.CenterPoint = new Point2d(xxx, yyy);
view.Height = db.Extmax.Y - db.Extmin.Y;
view.Width = db.Extmax.X - db.Extmin.X;
opened.SetCurrentView(view);
opendoc.CloseAndDiscard();
}
thumb = opendoc.CapturePreviewImage(200, 300);
}//生成略缩图预览//opendoc.CapturePreviewImage(200, 300);//生成略缩图预览
catch (System.Exception)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n出现错误......\n");
opendoc.CloseAndDiscard();
return;
}
}
using (Database tempdb = new Database(false, true)) //创建临时数据库(两个参数:是否创建符号表,不与当前文档关联)
{
tempdb.ThumbnailBitmap = thumb;
tempdb.ReadDwgFile(fileName, FileShare.Read, true, null);//临时数据库不打开dwg文档读取文件.只能读取dwg类型文件,不可读取dxf
tempdb.DxfOut(fullfilename,10,true);
}
counter = counter + 1;
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n正在转换第{counter}个......{fullfilename}\n");
}
//Application.DocumentManager.MdiActiveDocument.Editor.Regen();//刷新屏幕
System.Windows.Forms.Application.DoEvents();
//Application.DocumentManager.MdiActiveDocument.SendStringToExecute("._zoom _e ", true, false, false);
}
DateTime afterDT = System.DateTime.Now;
string totaltime = afterDT.Subtract(beforDT).TotalSeconds.ToString("F3", CultureInfo.InvariantCulture);
MessageBox.Show($"\n已转换counter个dwg文件(包含路径下dwg文件和所有子文件夹内的dwg文件)!\n共{totaltime}秒!\nCAD插件定制qq:443440204\n", "版权所有:山水 qq:443440204", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n已转换counter个dwg文件(包含路径下dwg文件和所有子文件夹内的dwg文件)!\n共{totaltime}秒!\nCAD插件定制qq:443440204\n", "版权所有:山水 qq:443440204", MessageBoxButtons.OK, MessageBoxIcon.Information);
}