跨数据库复制块参照,目前无法缩放和颜色不对
public static class BlockEX
{
[CommandMethod("xx")]
// [CommandMethod("CopyObjectsBetweenDatabases", CommandFlags.Session)]
public static void CopyObjectsBetweenDatabases()
{
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 锁定当前图形 Lock the current document
using (DocumentLock acLckDocCur = acDoc.LockDocument())
{
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只读方式打开块表记录 Open the Block table record for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 创建一个圆心为(0,0,0),半径为 5 的圆 Create a circle that is at (0,0,0) with a radius of 5
//Circle acCirc1 = new Circle();
//acCirc1.SetDatabaseDefaults();
//acCirc1.Center = new Point3d(0, 0, 0);
//acCirc1.Radius = 5;
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
//acBlkTblRec.AppendEntity(acCirc1);
// acTrans.AddNewlyCreatedDBObject(acCirc1, true);
// 创建一个圆心为(0,0,0),半径为 7 的圆 Create a circle that is at (0,0,0) with a radius of 7
//Circle acCirc2 = new Circle();
//acCirc2.SetDatabaseDefaults();
//acCirc2.Center = new Point3d(0, 0, 0);
//acCirc2.Radius = 7;
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
//acBlkTblRec.AppendEntity(acCirc2);
//acTrans.AddNewlyCreatedDBObject(acCirc2, true);
// 添加所有要复制的对象到新的文档中 Add all the objects to copy to the new document
//acObjIdColl = new ObjectIdCollection();
//acObjIdColl.Add(acCirc1.ObjectId);
//acObjIdColl.Add(acCirc2.ObjectId);
foreach (var item in acBlkTblRec)
{
acObjIdColl.Add(item);
}
// 保存新对象到数据库中 Save the new objects to the database
acTrans.Commit();
}
// 解锁文档 Unlock the document
}
// 修改文件和路径以匹配你工作站中的图形模板 Change the file and path to match a drawing template on your workstation
string sLocalRoot = Application.GetSystemVariable("LOCALROOTPREFIX") as string;
string sTemplatePath = sLocalRoot + "Template\\acad.dwt";
// 创建新的图形用于复制对象 Create a new drawing to copy the objects to
DocumentCollection acDocMgr = Application.DocumentManager;
Document acNewDoc = acDocMgr.Add(sTemplatePath);
Database acDbNewDoc = acNewDoc.Database;
// 锁定新文档 Lock the new document
using (DocumentLock acLckDoc = acNewDoc.LockDocument())
{
// 在新数据库中启动事务 Start a transaction in the new database
using (Transaction acTrans = acDbNewDoc.TransactionManager.StartTransaction())
{
// 以只读方式打开块表 Open the Block table for read
BlockTable acBlkTblNewDoc;
acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以只读方式打开模型空间的块表记录 Open the Block table record Model space for read
BlockTableRecord acBlkTblRecNewDoc;
acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
// 复制对象到新的数据库中 Clone the objects to the new database
IdMapping acIdMap = new IdMapping();
acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId, acIdMap,
DuplicateRecordCloning.Ignore, false);
// 保存复制的对象到数据库中 Save the copied objects to the database
acTrans.Commit();
}
// 解锁文档 Unlock the document
}
// 设置新文档为当前文档 Set the new document current
acDocMgr.MdiActiveDocument = acNewDoc;
//System.Windows.Forms.Application.DoEvents();//目前设置当前图形和缩放失败,且存在颜色不对的情况
// acNewDoc.Editor.UpdateScreen();
acNewDoc.Editor.ZoomExtents();
// acNewDoc.Editor.Redraw();
}
public static void Test_InsertBlockWithDoubleDatabase()
{
using var tr = new DBTrans(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.dwg"));
using var trans = new DBTrans();
Database db = Env.Database;
//BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
List<Entity> list = new List<Entity>();
var btr = trans.ModelSpace;
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
foreach (var item in btr)
{
Entity ent = item.GetObject(OpenMode.ForRead) as Entity;
//ent.GetType().Print();
acObjIdColl.Add(item);
//Entity ent1 = ent.DeepClone(ent, maping, true) as Entity;
Entity ent1 = ent.Clone() as Entity;
// return ent.DeepClone(ent, maping, true) as Entity;
list.Add(ent1);
}
IdMapping acIdMap = new IdMapping();
tr.Database.WblockCloneObjects(acObjIdColl, trans.ModelSpace.ObjectId, acIdMap, DuplicateRecordCloning.Ignore, false);
// tr.BlockTable.Add("zz1",list);
//tr.BlockTable.Add("test4562",
// btr =>
// {
// btr.Origin = new(0, 0, 0);
// },
// () =>
// {
// var line = new Line(new(0, 0, 0), new(2000, 1000, 0));
// var actext = DBTextEx.CreateDBText(Point3d.Origin, "555", 2.5, database: tr.Database);
// return new List<Entity> { line, actext };
// });
// tr.CurrentSpace.InsertBlock(Point3d.Origin,"zz1");//, "test4561");
tr.Database.SaveDwgFile();
"ok\n".Print();
btr.Name.Print();
}
public static void Test_BlockDef()
{
using DBTrans tr = new();
// var line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0));
tr.BlockTable.Add("test",
btr =>
{
btr.Origin = new Point3d(0, 0, 0);
},
() => // 图元
new List<Entity> { new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0)) },
() => // 属性定义
{
var id1 = new AttributeDefinition() { Position = new Point3d(0, 0, 0),TextString = "ad", Tag = "start", Height = 0.2 };
var id2 = new AttributeDefinition() { Position = new Point3d(1, 1, 0), Tag = "end", Height = 0.2 };
return new List<AttributeDefinition> { id1, id2 };
}
);
// ObjectId objectId = tr.BlockTable.Add("a");// 新建块
// objectId.GetObject<BlockTableRecord>().AddEntity();// 测试添加空实体
tr.BlockTable.Add("test1",
btr =>
{
btr.Origin = new Point3d(0, 0, 0);
},
() =>
{
var line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0));
var acText = DBTextEx.CreateDBText(Point3d.Origin, "123", 2.5);
return new List<Entity> { line, acText };
});
}
}
namespace IfoxDemo;
public static class BlockEX
{
[CommandMethod("xx")]
public static void Test_InsertBlockWithDoubleDatabase()
{
using var tr = new DBTrans(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.dwg"));
using var trans = new DBTrans();
Database db = HostApplicationServices.WorkingDatabase;
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
List<Entity> list = new List<Entity>();
foreach (var item in btr)
{
Entity ent = item.GetObject(OpenMode.ForRead) as Entity;
//ent.GetType().Print();
var maping = new IdMapping();
//Entity ent1 = ent.DeepClone(ent, maping, true) as Entity;
Entity ent1 = ent.Clone() as Entity;
// return ent.DeepClone(ent, maping, true) as Entity;
list.Add(ent1);
}
tr.BlockTable.Add("zz",list);
//tr.BlockTable.Add("test4562",
// btr =>
// {
// btr.Origin = new(0, 0, 0);
// },
// () =>
// {
// var line = new Line(new(0, 0, 0), new(2000, 1000, 0));
// var actext = DBTextEx.CreateDBText(Point3d.Origin, "555", 2.5, database: tr.Database);
// return new List<Entity> { line, actext };
// });
tr.CurrentSpace.InsertBlock(Point3d.Origin,"zz");//, "test4561");
tr.Database.SaveDwgFile();
"ok\n".Print();
btr.Name.Print();
}
public static void Test_BlockDef()
{
using DBTrans tr = new();
// var line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0));
tr.BlockTable.Add("test",
btr =>
{
btr.Origin = new Point3d(0, 0, 0);
},
() => // 图元
new List<Entity> { new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0)) },
() => // 属性定义
{
var id1 = new AttributeDefinition() { Position = new Point3d(0, 0, 0),TextString = "ad", Tag = "start", Height = 0.2 };
var id2 = new AttributeDefinition() { Position = new Point3d(1, 1, 0), Tag = "end", Height = 0.2 };
return new List<AttributeDefinition> { id1, id2 };
}
);
// ObjectId objectId = tr.BlockTable.Add("a");// 新建块
// objectId.GetObject<BlockTableRecord>().AddEntity();// 测试添加空实体
tr.BlockTable.Add("test1",
btr =>
{
btr.Origin = new Point3d(0, 0, 0);
},
() =>
{
var line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0));
var acText = DBTextEx.CreateDBText(Point3d.Origin, "123", 2.5);
return new List<Entity> { line, acText };
});
}
}
global using System;
global using IfoxDemo;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;
global using Autodesk.AutoCAD.ApplicationServices;
global using Autodesk.AutoCAD.EditorInput;
global using Autodesk.AutoCAD.Runtime;
global using Autodesk.AutoCAD.Geometry;
global using Autodesk.AutoCAD.DatabaseServices;
global using IFoxCAD.Cad;
global using Application = Autodesk.AutoCAD.ApplicationServices.Application;
global using Polyline = Autodesk.AutoCAD.DatabaseServices.Polyline;
global using Autodesk.AutoCAD.Features.PointCloud.PointCloudColorMapping;
global using Autodesk.AutoCAD.GraphicsInterface;
global using IFoxCAD.Basal;
global using System.Diagnostics;
global using System.Reflection;
global using System.Runtime.Remoting.Channels;
global using System.Windows;
global using System.Windows.Shapes;
global using Autodesk.AutoCAD.MacroRecorder;
global using System.Net.NetworkInformation;
global using System.Windows.Controls;
global using MessageBox = System.Windows.MessageBox;
global using Autodesk.AutoCAD.Colors;
global using Ellipse = Autodesk.AutoCAD.DatabaseServices.Ellipse;
global using Line = Autodesk.AutoCAD.DatabaseServices.Line;
global using Exception = Autodesk.AutoCAD.Runtime.Exception;
global using System.IO;
global using Path = System.IO.Path;
namespace IfoxDemo
{
public class GlobalUsing
{
}
public static partial class AddEntityTools
{
public static bool GetPoint(this Editor ed, out Point3d point, string message)
{
PromptPointResult pr;
PromptPointOptions ps = new PromptPointOptions(message);
pr = ed.GetPoint(ps);
if (pr.Status == PromptStatus.OK)
{
point = pr.Value;
return true;
}
else
{
ed.WriteMessage("\n用户取消了点的选择.");
point = pr.Value;
return false;
}
}
public static string AddLayer(this Database db, string layname, short colorindex)
{
if ((colorindex < 0) || (colorindex > 256))
{
colorindex = 0;
};
var pt = Point3d.Origin;
Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(out pt, "asdf");
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
//Database db = acDoc.Database;
// 启动一个事务 Start a transaction
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
// 以只读方式打开图层表 Open the Layer table for read
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(db.LayerTableId,
OpenMode.ForRead) as LayerTable;
if (acLyrTbl.Has(layname) == false)
{
LayerTableRecord acLyrTblRec = new LayerTableRecord();
// Assign the layer the ACI color 1 and a name
acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, colorindex);
acLyrTblRec.Name = layname;
// Upgrade the Layer table for write
acLyrTbl.UpgradeOpen();
// Append the new layer to the Layer table and the transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
// 以只读方式打开块表 Open the Block table for read
BlockTable acBlkTbl;
acBlkT