图层管理[实现图层的关闭打开、冻结解冻、锁定解锁、删除与图层相关联的实体对象]
/* 图层管理命令:LayerManagement[TT]* |-关闭或打开图层命令:LayerClose[TC]
* | |-[S]图选
* | |-[F]反选
* | |-[R]手输
* | |-[T]打开
* | |-[A]打开全部()
* |
* |-冻结或解冻图层命令:LayerFrozen[TF]
* | |-[S]图选
* | |-[F]反选
* | |-[R]手输
* | |-[T]打开
* | |-[A]解冻全部()
* |
* |-锁定或解锁图层命令:LayerLock[TS]
* | |-[S]图选
* | |-[F]反选
* | |-[R]手输
* | |-[T]打开
* | |-[A]解锁全部()
* |
* |-打开/解冻/解锁所有图层命令:LayerOpenAll[TA]
* | |-[A]打开解冻解锁所有图层
* | |-[C]打开
* | |-[F]解冻
* | |-[S]解锁
* |
* |-删除与图层关联的对象命令:LayerDelete[TD]
* | |-[S]图选
* | |-[F]反选
* | |-[R]手输
*/
狐哥,你终于出来,你的隐身真好,一年多了一点音讯都没,大伙还以为你出了什么事呢。差一点大伙一起要报警寻人了。 是什么语言啊 /// <summary>
/// 用户输入关键字数组
/// </summary>
private string[] array = new string[] { "S", "F", "R", "T", "A" };
private string strMessage = "\n请输入图层名称关键或全称";
/// <summary>
/// 关闭或打开图层
/// </summary>
public void CloseLayer()
{
string message = "\n图层关闭或打开:[图选(S)/反选(F)/手输(R)/打开(T)/打开全部(A)]<默认S>";
LayerCommandCalls(message, LayerStatus.CloseOrOpenLayer);
}
/// <summary>
/// 冻结或解冻图层
/// </summary>
public void FrozenLayer()
{
string message = "\n图层冻结或解冻:[图选(S)/反选(F)/手输(R)/解冻(T)/解冻全部(A)]<默认S>";
LayerCommandCalls(message, LayerStatus.FrozenOrUnFrozenLayer);
}
/// <summary>
/// 锁定或解锁图层
/// </summary>
public void LockLayer()
{
string message = "\n图层锁定或解锁:[图选(S)/反选(F)/手输(R)/解锁(T)/解锁全部(A)]<默认S>";
LayerCommandCalls(message, LayerStatus.LockOrUnLockLayer);
} /// <summary>
/// 打开/解冻/解锁所有图层
/// </summary>
public void OpenUnFrozenUnlockLayer()
{
bool IsOpen = false;
bool IsUnFzozen = false;
bool IsUnLock = false;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
string message = "\n[打开解冻解锁所有图层(A)/打开(C)/解冻(F)/解锁(S)]<默认A>";
string key = this.Keywords(message, new string[] { "A", "C", "F", "S" });
if (!string.IsNullOrEmpty(key))
{
switch (key)
{
case "C"://关闭或打开图层
IsOpen = true;
break;
case "F"://冻结或解冻图层
IsUnFzozen = true;
break;
case "S"://锁定或解锁图层
IsUnLock = true;
break;
case "A"://打开/解冻/解锁所有图层
IsOpen = true;
IsUnFzozen = true;
IsUnLock = true;
break;
default:
break;
}
foreach (string item in this.GetAllLayerName())
{
//关闭或打开图层
if (IsOpen)
this.LayerOperating(LayerStatus.CloseOrOpenLayer, item, false, false);
//锁定或解锁图层
if (IsUnLock)
this.LayerOperating(LayerStatus.LockOrUnLockLayer, item, false, false);
//冻结或解冻图层
if (IsUnFzozen)
this.LayerOperating(LayerStatus.FrozenOrUnFrozenLayer, item, false, false);
}
//解冻需要刷新视图
if (IsUnFzozen)
{
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("REGEN\r", true, false, false);
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage("图层打开/解冻/解锁发生错误:" + ex.Message);
}
}
/// <summary>
/// 删除与图层关联的对象
/// </summary>
public void DeleteLayer()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
this.strMessage = "\n请输入图层名全称";
string message = "\n删除图层关联的对象:[图选(S)/反选(F)/手输(R)]<默认S>";
string key = this.Keywords(message, new string[] { "S", "F", "R" });
string LayerName = string.Empty;
if (!string.IsNullOrEmpty(key))
{
switch (key)
{
case "S":
case "F":
LayerName = this.SelectEntityGetLayerName();
break;
case "R":
LayerName = this.ImportLayerName();
break;
default:
break;
}
Database db = HostApplicationServices.WorkingDatabase;
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
foreach (ObjectId btrid in bt) //遍历块表
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrid, OpenMode.ForWrite);
foreach (ObjectId eid in btr) //遍历块表记录
{
Entity ent = trans.GetObject(eid, OpenMode.ForWrite) as Entity;
if (ent != null)
{
if (key.ToUpper() == "F")
{
if (ent.Layer.ToUpper() != LayerName.ToUpper())
{
ent.Erase();
}
}
else
{
if (ent.Layer.ToUpper() == LayerName.ToUpper())
{
ent.Erase();
}
}
}
}
}
trans.Commit();
}
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage("图层打开/解冻/解锁发生错误:" + ex.Message);
}
} /// <summary>
/// 图层管理
/// </summary>
public void LayerManagement()
{
string message = "\n图层管理:[关闭(C)/冻结(F)/锁定(S)/删除(D)/打开解冻解锁所有图层(A)]<默认C>";
string key = this.Keywords(message, new string[] { "C", "F", "S", "D", "A" });
if (!string.IsNullOrEmpty(key))
{
switch (key)
{
case "C"://关闭或打开图层
this.CloseLayer();
break;
case "F"://冻结或解冻图层
this.FrozenLayer();
break;
case "S"://锁定或解锁图层
this.LockLayer();
break;
case "D"://删除与图层关联的对象
this.DeleteLayer();
break;
case "A"://打开/解冻/解锁所有图层
this.OpenUnFrozenUnlockLayer();
break;
default:
break;
}
}
}
/// <summary>
/// 图层命令调用
/// </summary>
/// <param name="message">提示字符串</param>
/// <param name="layerstatus">图层状态</param>
private void LayerCommandCalls(string message, LayerStatus layerstatus)
{
string key = this.Keywords(message, this.array);
if (!string.IsNullOrEmpty(key))
{
switch (key)
{
case "S":
LayerOperating(layerstatus, this.SelectEntityGetLayerName(), false, true);
break;
case "F":
LayerOperating(layerstatus, this.SelectEntityGetLayerName(), true, true);
break;
case "R":
LayerOperating(layerstatus, this.ImportLayerName(), false, true);
break;
case "T":
LayerOperating(layerstatus, this.ImportLayerName(), false, false);
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("REGEN\r", true, false, false);
break;
case "A":
foreach (string item in this.GetAllLayerName())
{
LayerOperating(layerstatus, item, false, false);
}
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("REGEN\r", true, false, false);
break;
default:
break;
}
}
}
/// <summary>
/// 获取所有图层的名称
/// </summary>
/// <returns></returns>
private List<string> GetAllLayerName()
{
List<string> LayerNames = new List<string>();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
foreach (ObjectId layerId in lt)
{
LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForRead);
LayerNames.Add(ltr.Name);
}
trans.Commit();
}
return LayerNames;
}
/// <summary>
/// 提示用户输入关键字
/// </summary>
/// <param name="message">提示字符串</param>
/// <param name="keywords">关键字数组</param>
/// <returns>用户输入的字符</returns>
private string Keywords(string message, string[] keywords)
{
try
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptKeywordOptions pko = new PromptKeywordOptions(message);
for (int i = 0; i < keywords.Length; i++)
{
pko.Keywords.Add(keywords, keywords, keywords, false, true);
}
pko.Keywords.Default = keywords;
//pko.AllowNone = true;
PromptResult k = ed.GetKeywords(pko);
if (k.Status == PromptStatus.OK)
{
return k.StringResult;
}
else
{
return string.Empty;
}
}
catch (System.Exception ex)
{
throw ex;
}
}
/// <summary>
/// 从图中选取实体对象,从中获得实体的图层名称
/// </summary>
/// <returns>图层名称</returns>
private string SelectEntityGetLayerName()
{
string strLayerName = string.Empty;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "\n请选择对象";
PromptSelectionResult psr = ed.GetSelection(pso);
if (psr.Status == PromptStatus.OK)
{
SelectionSet ss = psr.Value;
ObjectId[] objectIds = ss.GetObjectIds();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 遍历选择集.
foreach (ObjectId objectId in objectIds)
{
Entity entity = (Entity)trans.GetObject(objectId, OpenMode.ForRead);
strLayerName = entity.Layer;
}
trans.Commit();
}
}
}
}
catch (System.Exception ex)
{
throw ex;
}
return strLayerName;
}
/// <summary>
/// 获取输入图层名称关键或全称
/// </summary>
private string ImportLayerName()
{
string importLayerName = string.Empty;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
PromptStringOptions pso = new PromptStringOptions(this.strMessage);
PromptResult res = ed.GetString(pso);
if (res.Status == PromptStatus.OK || res.Status == PromptStatus.None)
{
importLayerName = res.StringResult;
}
}
}
catch (System.Exception ex)
{
throw ex;
}
return importLayerName;
} /// <summary>
/// 图层操作
/// </summary>
/// <param name="layerstatus">图层状态</param>
/// <param name="LayerName">图层名称或关键字</param>
/// <param name="IsInvertSelection">是否反向选取</param>
/// <param name="IsTureOFlase">是与否</param>
private void LayerOperating(LayerStatus layerstatus, string LayerName, bool IsInvertSelection, bool IsTureOFlase)
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
Database db = HostApplicationServices.WorkingDatabase;
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
foreach (ObjectId layerId in lt)
{
LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForWrite);
if (!string.IsNullOrEmpty(LayerName))
{
switch (layerstatus)
{
//关闭或打开图层
case LayerStatus.CloseOrOpenLayer:
CloseOrOpenLayer(LayerName, IsInvertSelection, IsTureOFlase, ltr);
break;
//锁定或解锁图层
case LayerStatus.LockOrUnLockLayer:
LockOrUnLockLayer(LayerName, IsInvertSelection, IsTureOFlase, ltr);
break;
//冻结或解冻图层
case LayerStatus.FrozenOrUnFrozenLayer:
FrozenOrUnFrozenLayer(LayerName, IsInvertSelection, IsTureOFlase, ltr,
(LayerTableRecord)trans.GetObject(db.Clayer, OpenMode.ForRead));
break;
default:
break;
}
}
}
trans.Commit();
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage("图层操作发生错误:" + ex.Message);
}
}
/// <summary>
/// 关闭或打开图层
/// </summary>
/// <param name="LayerName">图层名称或关键字</param>
/// <param name="IsInvertSelection">是否反向选取</param>
/// <param name="IsClose">是否关闭</param>
/// <param name="ltr">层表记录</param>
private void CloseOrOpenLayer(string LayerName, bool IsInvertSelection, bool IsClose, LayerTableRecord ltr)
{
//是否反向选取
if (IsInvertSelection)
{
if (!ltr.Name.ToUpper().Contains(LayerName.ToUpper()))
{
ltr.IsOff = IsClose;
}
}
else
{
if (ltr.Name.ToUpper().Contains(LayerName.ToUpper()))
{
ltr.IsOff = IsClose;
}
}
}
/// <summary>
/// 冻结或解冻图层
/// </summary>
/// <param name="LayerName">图层名称或关键字</param>
/// <param name="IsInvertSelection">是否反向选取</param>
/// <param name="IsFrozen">是否冻结</param>
/// <param name="ltr">层表记录</param>
/// <param name="curLtr">当前图层</param>
private void FrozenOrUnFrozenLayer(string LayerName, bool IsInvertSelection, bool IsFrozen, LayerTableRecord ltr, LayerTableRecord curLtr)
{
//是否反向选取
if (IsInvertSelection)
{
if (!ltr.Name.ToUpper().Contains(LayerName.ToUpper()))
{
//判断是否为当前图层,当前图层不能冻结
if (IsFrozen && ltr.Name != curLtr.Name)
{
ltr.IsFrozen = true;
}
else
{
if (ltr.IsFrozen == true)
ltr.IsFrozen = false;
}
}
}
else
{
if (ltr.Name.ToUpper().Contains(LayerName.ToUpper()))
{
//判断是否为当前图层,当前图层不能冻结
if (IsFrozen && ltr.Name != curLtr.Name)
{
ltr.IsFrozen = true;
}
else
{
if (ltr.IsFrozen == true)
ltr.IsFrozen = false;
}
}
}
}
/// <summary>
/// 锁定或解锁图层
/// </summary>
/// <param name="LayerName">图层名称或关键字</param>
/// <param name="IsInvertSelection">是否反向选取</param>
/// <param name="IsLock">是否关闭</param>
/// <param name="ltr">层表记录</param>
private void LockOrUnLockLayer(string LayerName, bool IsInvertSelection, bool IsLock, LayerTableRecord ltr)
{
//是否反向选取
if (IsInvertSelection)
{
if (!ltr.Name.ToUpper().Contains(LayerName.ToUpper()))
{
ltr.IsLocked = IsLock;
}
}
else
{
if (ltr.Name.ToUpper().Contains(LayerName.ToUpper()))
{
ltr.IsLocked = IsLock;
}
}
} 没地方加威望了 我已经Out了 呵呵 本帖最后由 chpmould 于 2012-6-28 07:09 编辑
支持一个,很好的贴子... 。。。 新新手弱弱的问,要怎样执行程序?
页:
[1]
2