图形复制问题
本帖最后由 dpec1982 于 2015-7-9 15:40 编辑各位大侠:
环境(vs2010+CAD)
目的:有一张DWG文件,用框选的形式把其中一部分图形框选(这部分图形中有文字、块参照、直线等各种图元文件)然后复制。在打开另外一张DWG文件,把这部分框选的图形,按指定坐标粘贴到这个新打开DWG文件中。我是初学者,请各位大侠帮忙,不要见笑。小妹再次先谢谢啦!!!
这段代码,是把一张DWG文件中的图元框选复制
static ObjectIdCollection acObjIdColl; //这个是全局静态变量
public void copy()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
db = doc.Database;
Editor ed = doc.Editor;
//选择矩形区域,获取矩形的两个对角点
PromptPointResult pPResult = ed.GetPoint("选择起点");
ptStart = pPResult.Value;
PromptPointResult pPResult2 = ed.GetCorner("\n选择对角点", ptStart);
ptEnd = pPResult2.Value;
//选择包含在框内部的所有实体
acObjIdColl = InW(ptStart, ptEnd, doc);
}
public ObjectIdCollection InW(Point3d pt1, Point3d pt2, Document doc)
{
Database db = doc.Database;
Editor ed = doc.Editor;
Entity entity = null;
ObjectIdCollection c = new ObjectIdCollection();
PromptSelectionResult ents = ed.SelectWindow(pt1, pt2);
if (ents.Status == PromptStatus.OK)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
SelectionSet ss = ents.Value;
foreach (ObjectId id in ss.GetObjectIds())
{
entity = trans.GetObject(id, OpenMode.ForWrite, true, true) as Entity;
if (entity != null)
{ c.Add(entity.ObjectId); }
}
trans.Commit();
}
}
return c;
}
这段代码,我是想实现把这些框选好的图元文件粘贴到另外一张DWG文件中。但就是实现不了
public void pasteRec()
{
Document acNewDoc = Application.DocumentManager.MdiActiveDocument;
PromptPointResult pPResult3 = acNewDoc.Editor.GetPoint("\n选择目标点");
Point3d targetPt = pPResult3.Value;
Database acDbNewDoc = acNewDoc.Database;
using (DocumentLock acLckDoc = acNewDoc.LockDocument())
{
// 在新数据库中启动事务 Start a transaction in the new database
using (Transaction acTrans = acDbNewDoc.TransactionManager.StartTransaction())
{
BlockTableacBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc,
OpenMode.ForRead) as BlockTableRecord;
// 复制对象到新的数据库中 Clone the objects to the new database
IdMapping acIdMap = new IdMapping();
db.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId, acIdMap, DuplicateRecordCloning.Ignore, false);
//db.Wblock(acObjIdColl, targetPt); //其中这种写法也不对,都提示db是wrong
acTrans.Commit();
}
}
}
ctrl+shift+c 复制,ctrl+v 粘贴。。
建议你先把基础的教程之类的东西看完,再用它工作,磨刀不误砍柴工。。 ㄘ丶转裑ㄧ灬 发表于 2015-7-9 09:14 static/image/common/back.gif
ctrl+shift+c 复制,ctrl+v 粘贴。。
建议你先把基础的教程之类的东西看完,再用它工作,磨刀不误砍柴工。 ...
大侠,我是想问用程序怎么写 各位大侠,给小妹一些建议呗,谢谢 一般复制目标用后台数据库就可以了 打开的文档我没试过 雪山飞狐_lzh 发表于 2015-7-9 17:52 static/image/common/back.gif
一般复制目标用后台数据库就可以了 打开的文档我没试过
飞狐大侠:
那框选的目标用后台数据库怎么写程序??我这么写对吗
public void copy()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
db = doc.Database;
Editor ed = doc.Editor;
//选择矩形区域,获取矩形的两个对角点
PromptPointResult pPResult = ed.GetPoint("选择起点");
ptStart = pPResult.Value;
PromptPointResult pPResult2 = ed.GetCorner("\n选择对角点", ptStart);
ptEnd = pPResult2.Value;
//选择包含在框内部的所有实体
acObjIdColl = InW(ptStart, ptEnd, doc);
}
public ObjectIdCollection InW(Point3d pt1, Point3d pt2, Document doc)
{
Database db = doc.Database;
Editor ed = doc.Editor;
Entity entity = null;
ObjectIdCollection c = new ObjectIdCollection();
PromptSelectionResult ents = ed.SelectWindow(pt1, pt2);
if (ents.Status == PromptStatus.OK)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
SelectionSet ss = ents.Value;
foreach (ObjectId id in ss.GetObjectIds())
{
entity = trans.GetObject(id, OpenMode.ForWrite, true, true) as Entity;
if (entity != null)
{ c.Add(entity.ObjectId); }
}
trans.Commit();
}
}
return c;
} 雪山飞狐_lzh 发表于 2015-7-9 17:52 static/image/common/back.gif
一般复制目标用后台数据库就可以了 打开的文档我没试过
飞狐大侠:
这是复制代码
public static void CopyObjectsBetweenDatabases()
{
//ObjectIdCollection acObjIdColl = new ObjectIdCollection();
Document acDoc = Application.DocumentManager.MdiActiveDocument;
acCurDb = acDoc.Database;
using (DocumentLock acLckDocCur = acDoc.LockDocument())
{
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl,OpenMode.ForWrite) as BlockTableRecord;
Circle acCirc1 = new Circle();
acCirc1.SetDatabaseDefaults();
acCirc1.Center = new Point3d(0, 0, 0);
acCirc1.Radius = 5;
acBlkTblRec.AppendEntity(acCirc1);
acTrans.AddNewlyCreatedDBObject(acCirc1, true);
Circle acCirc2 = new Circle();
acCirc2.SetDatabaseDefaults();
acCirc2.Center = new Point3d(0, 0, 0);
acCirc2.Radius = 7;
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);
// 保存新对象到数据库中
acTrans.Commit();
}
}
}
这是所谓的粘贴代码
public static void PasteObjectsBetweenDatabases()
{
string sLocalRoot = Application.GetSystemVariable("LOCALROOTPREFIX") as string;
string sTemplatePath = sLocalRoot + "Template\\acad.dwt";
DocumentCollection acDocMgr = Application.DocumentManager;
//Document acNewDoc = acDocMgr.Add(sTemplatePath);//新添加的DWG就好使
Document acNewDoc = ucMain.newDoc;//而打开原有的文档,就提示eInvalidObjectId问题,怎么回事啊,大侠
Database acDbNewDoc = acNewDoc.Database;
Application.ShowAlertDialog(acNewDoc.Name);
// 锁定新文档 Lock the new document
using (DocumentLock acLckDoc = acNewDoc.LockDocument())
{
using (Transaction acTrans = acDbNewDoc.TransactionManager.StartTransaction())
{
BlockTable acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc, OpenMode.ForRead) as BlockTableRecord;
// 复制对象到新的数据库中 Clone the objects to the new database
IdMapping acIdMap = new IdMapping();
acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId, acIdMap, DuplicateRecordCloning.Replace, true);
// 保存复制的对象到数据库中 Save the copied objects to the database
acTrans.Commit();
}
}
acDocMgr.MdiActiveDocument = acNewDoc;
}
我的功能是新建2个圆作为复制的原文件,然后粘贴到新打开的文档中,提示eInvalidObjectId错误,而添加新文档就没有错误,为什么 各位大侠,帖子别沉啊,给小妹看看呗,困扰我2天了 各位大侠,帮帮小妹啊,别沉呀 各位大侠,帮帮小妹啊,别沉呀
页:
[1]
2