- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
//using System.Windows.Forms;
static public void createCopyDoc(string fileName) // This method can have any name
{
DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
Document doc = dm.MdiActiveDocument;
try
{
using (DocumentLock docloc = doc.LockDocument())
{
if (!File.Exists(fileName))
{
Document newdoc = dm.Add("");
Database newdb=newdoc.Database;
newdb.SaveAs(fileName, DwgVersion.Current);
newdoc.CloseAndDiscard();
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
[CommandMethod("CSset", CommandFlags.Session)]
static public void CopySelectedText() // This method can have any name
{
string fileName = "c:\\test\\CopySet.dwg"; // add target drawing full name here
DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
Document doc = dm.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
try
{
using (DocumentLock docloc = doc.LockDocument())
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
// build filter to select texts and mtexts in the model space
TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "*text"), new TypedValue(410, "Model") };
SelectionFilter filt = new SelectionFilter(tv);
//perform selection on screen
PromptSelectionResult res = ed.GetSelection(filt);
SelectionSet sset = res.Value;
ObjectId[] idcoll = sset.GetObjectIds();
ed.WriteMessage("\nSelected: {0}", idcoll.Length);
ObjectIdCollection ids = new ObjectIdCollection();
foreach (ObjectId id in idcoll)
{
ids.Add(id);
} // may use ids.CopyTo(idcoll,idcoll.Length-1) instead
// create drawing if does not exists
if (!File.Exists(fileName))
{
createCopyDoc(fileName);
}
using (Database newdb = new Database(false, true))
{
newdb.ReadDwgFile(fileName, System.IO.FileShare.ReadWrite, true, "");
using (Transaction newtr = newdb.TransactionManager.StartTransaction())
{
// Get Block table of target database
BlockTable newbt = (BlockTable)newtr.GetObject(newdb.BlockTableId, OpenMode.ForRead);
// Get model space of target database
BlockTableRecord newbtr = (BlockTableRecord)newtr.GetObject(newbt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
if (ids.Count != 0)
{
IdMapping iMap = new IdMapping();
db.WblockCloneObjects(ids, newbtr.ObjectId, iMap, DuplicateRecordCloning.Ignore, false);
// Save target drawing
newdb.SaveAs(fileName, DwgVersion.Current);// [A2010] this syntax ought be different for other Acad release
}
}
}
tr.Commit();
}//end using transaction
}
}
catch (System.Exception ex)
{
// informative message
ed.WriteMessage("\n{0}\n", ex.ToString());
MessageBox.Show(ex.ToString());
}
} |
|