- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
[CommandMethod("CopyLayerFromObject")]
public void testCopyLayer()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptStringOptions sopts = new PromptStringOptions("\nEnter new layer name :");
sopts.AllowSpaces = true;
PromptResult stres = ed.GetString(sopts);
if (stres.Status != PromptStatus.OK) return;
// get new layer name
string layerName = stres.StringResult;
PromptSelectionOptions opts = new PromptSelectionOptions();
opts.MessageForRemoval = "Wrong selection, try again.";
opts.MessageForAdding = "Select single object to copy layer from: ";
opts.SingleOnly = true;
opts.SinglePickInSpace = true;
// select single object
PromptSelectionResult selRes = ed.GetSelection(opts);
if (selRes.Status != PromptStatus.OK) return;
ObjectId entId = selRes.Value.GetObjectIds()[0];
// open transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable ltb = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
// get entity
Entity ent = (Entity)tr.GetObject(entId, OpenMode.ForRead);
// get entity layer
string oldname = ent.Layer;
// get LayerTableRecord by name
LayerTableRecord oldLtr = (LayerTableRecord)tr.GetObject(ltb[oldname], OpenMode.ForRead);
// check if layer exists
if (!ltb.Has(layerName))
{
ltb.UpgradeOpen();
//create new layer
LayerTableRecord ltr = new LayerTableRecord();
// copy properties from entity layer
ltr.CopyFrom(oldLtr);
// add new layer name
ltr.Name = layerName;
// here you can to add other properties to a layer: linetype, color etc
//
// add layer to LayerTable
ltb.Add(ltr);
// confirm transaction
tr.AddNewlyCreatedDBObject(ltr, true);
}
// end command if layer exist
else
{
return;
}
tr.Commit();
}//end using transaction
}
|
|