插入块(数据库)、预览图 错误调试
下面是在新打开的文档中插入数据库或插入块参照(未解决)
using System.IO;
using System.Reflection;
namespace IfoxDemo;
public static class FZK
{//复制快到另一个图
[CommandMethod("xx")]
public static void FZKDemo()
{
//获取当前文档,复制当前文档模型空间所有实体到ents
//打开新文档,添加一个块。tr.BlockTable.Add(blockName, btr => btr.AddEntity(ents));
//插入块参照 tr.CurrentSpace.InsertBlock(point,objectid);
Application.SetSystemVariable("INSUNITSDEFTARGET", 4);//当 INSUNITS 设定为 0 时,设置目标图形单位值。(INSUNITS,4);//(INSUNITSDEFTARGET, 4);//("UNITS", 0); DIMALTU
Application.SetSystemVariable("INSUNITS", 4);//指定插入或附着到图形时,块、图像或外部参照进行自动缩放所使用的图形单位值。
DocumentCollection docs = Application.DocumentManager;
List<Entity> listEntities;
string blockName, ok;
// GetEntities(out docs, out listEntities, out blockName, out ok);
//sourceDoc.CloseAndDiscard();
//打开一个新文档
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\演示合并dwg\替换块\XK新块.dwg";
try
{
using (Database blockdb = new Database(false, true)) //创建临时数据库(两个参数:是否创建符号表,不与当前文档关联)
{
using (Transaction tr = blockdb.TransactionManager.StartTransaction())
{//当前数据库开启事务
blockdb.ReadDwgFile(blockPath, FileShare.Read, true, null);//临时数据库不打开dwg文档读取文件.只能读取dwg类型文件,不可读取dxf
tr.Commit();
// InsertDb(newdocPath, blockdb);//插入图形
//return (Database)blockdb.Clone() ;
}
// DocumentCollection docs = Application.DocumentManager;
//Document newdoc = docs.Open(newdocPath, false);//.Add("acad.dwt");//docs.Add("新");
Matrix3d mt = Matrix3d.Scaling(1, Point3d.Origin);//缩放一倍的矩阵
System.Windows.Forms.Application.DoEvents();
try
{
HostApplicationServices.WorkingDatabase.Insert(mt, blockdb, false);
using (DocumentLock acLckDoc = newdoc.LockDocument())
{
using (Transaction acTrans = newdoc.TransactionManager.StartTransaction())
{
// newdoc.Database.Insert(mt, blockdb, false);// 这句代码容易死机 当要在除当前文档外的其它文档的模型空间或图纸空间中添加图元时,需要先锁定其文档。用户可用要锁定的Doc对象的LockDocument方法进行锁定。在调用LockDocument方法后,将返回一个DocumentLock对象。
//插入数据只能是代码所在的当前文档,活动文档不能直接插入数据库
}
}
}
catch (System.Exception)
{
throw;
}
}
}
catch (System.Exception)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n出现错误......\n");
}
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n正在合并第个......\n");
//Application.DocumentManager.MdiActiveDocument.Editor.Regen();//刷新屏幕
System.Windows.Forms.Application.DoEvents();
//Document newdoc = docs.Open(newdocPath, false);//.Add("acad.dwt");//docs.Add("新");
//Application.SetSystemVariable("INSUNITSDEFTARGET", 4);//当 INSUNITS 设定为 0 时,设置目标图形单位值。(INSUNITS,4);//(INSUNITSDEFTARGET, 4);//("UNITS", 0); DIMALTU
//Application.SetSystemVariable("INSUNITS", 4);//指定插入或附着到图形时,
// // sourceDoc.CloseAborted += SourceDoc_CloseAborted;// (); //CloseAndSave("C:\\Users\\Administrator\\Desktop\\a.dwg");
// //using (DocumentLock lockDoc33 = sourceDoc.LockDocument())
// //{
// // sourceDoc.CloseAndDiscard();
// //}
// // docs.MdiActiveDocument = newdoc;//新文档设置为当前文档
//System.Windows.Forms.Application.DoEvents();
using (DocumentLock lockNewDoc = newdoc.LockDocument())
{
//using var newtr = new DBTrans(newdoc, true, true);//新文档的事务
// //ObjectId objidNewBlock = newtr.BlockTable.Add(blockName, btr => btr.AddEntity((listEntities.ToArray())));
//ObjectId objidNewBlock = newdoc.Database.AddBlock(blockName, listEntities);
newtr.ModelSpace.InsertBlock(Point3d.Origin, objidNewBlock);//为什么空图可以插入,有图像的图插入不成功?
//(newdoc.Database).InsertBlock(objidNewBlock, Point3d.Origin, 0.0, 1, 1, 1);
newdoc.SendStringToExecute("._zoom _e ", true, false, false);
//var line1 = new Line(new Point3d(0, 0, 0), new Point3d(10, 10, 0)); //
// //newtr.CurrentSpace.AddEntity(line1);
//newdoc.Database.AddEntityToModeSpace(line1);
}
newdoc.CloseAndDiscard();
//Env.Editor.ZoomExtents();
//(newdoc.Database).Zoom();
//Application.UpdateScreen();
newdoc.Editor.UpdateScreen();//错误
newdoc.Editor.WriteMessage(ok);
//Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ok);
//System.Windows.Forms.Application.DoEvents();
//MessageBox.Show("ok");
}
}
下面是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);
}