- 积分
- 949
- 明经币
- 个
- 注册时间
- 2014-12-1
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 dpec1982 于 2015-7-9 15:40 编辑
各位大侠:
环境(vs2010+CAD)
目的:有一张DWG文件,用框选的形式把其中一部分图形框选(这部分图形中有文字、块参照、直线等各种图元文件)然后复制。在打开另外一张DWG文件,把这部分框选的图形,按指定坐标粘贴到这个新打开DWG文件中。我是初学者,请各位大侠帮忙,不要见笑。小妹再次先谢谢啦!!!
这段代码,是把一张DWG文件中的图元框选复制
static ObjectIdCollection acObjIdColl; //这个是全局静态变量
[CommandMethod("copyRec")]
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文件中。但就是实现不了
[CommandMethod("pasteRec")]
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())
{
BlockTable acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc[BlockTableRecord.ModelSpace],
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();
}
}
}
|
|