CAD二次开发基础之图层管理
本人初学CAD二次开发,已有半载,感觉中途还是走了很多弯路,在此共享些基础东西。和前辈们探讨。本文主要是图层管理,如常见的新建、关闭、删除等。基本上都是基础东西,只列出了关键代码,没写注释。
[*]新建图层
/// <summary>
/// create new layer
/// </summary>
/// <param name="layerName"></param>
public void CreateLayer(String layerName)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction trs = db.TransactionManager.StartTransaction();
using (trs)
{
LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
if (!lt.Has(layerName))
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = layerName;
lt.Add(ltr);
trs.AddNewlyCreatedDBObject(ltr, true);
}
trs.Commit();
}
}
[*]判断图层是否存在
/// <summary>
/// judge if exist layer
/// </summary>
/// <param name="layerName"></param>
/// <returns></returns>
public bool IsExistLayer(String layerName)
{
bool retval = false;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction trs = db.TransactionManager.StartTransaction();
using (trs)
{
LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
if (lt.Has(layerName))
{
retval = true;
}
trs.Commit();
}
return retval;
}
[*]锁定图层
/// <summary>
/// lock layer
/// </summary>
/// <param name="layerName"></param>
public void LockLayer(String layerName)
{
if (!IsExistLayer(layerName))
{
return;
}
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction trs = db.TransactionManager.StartTransaction();
using (trs)
{
LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
LayerTableRecord ltr = trs.GetObject(lt, OpenMode.ForWrite) as LayerTableRecord;
if (!ltr.IsLocked)
{
ltr.IsLocked = true;
}
trs.Commit();
}
}
[*]删除图层
/// <summary>
/// erase layer
/// </summary>
/// <param name="layerName"></param>
/// <returns></returns>
public bool EraseLayer(String layerName)
{
bool retval = false;
if (!IsExistLayer(layerName))
{
return retval;
}
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction trs = db.TransactionManager.StartTransaction();
using (trs)
{
LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
LayerTableRecord ltr = trs.GetObject(lt, OpenMode.ForWrite) as LayerTableRecord;
lt.GenerateUsageData();
if ((db.Clayer == lt) || ltr.IsUsed
|| layerName == "0" || layerName == "Defpoints")
{
trs.Commit();
return retval;
}
ltr.Erase(true);
retval = true;
trs.Commit();
}
return retval;
}
[*]设置当前图层
/// <summary>
/// set layer to current layer
/// </summary>
/// <param name="layerName"></param>
public void SetLayerCurrent(String layerName)
{
if (!IsExistLayer(layerName))
{
return;
}
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction trs = db.TransactionManager.StartTransaction();
using (trs)
{
LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
db.Clayer = lt;
trs.Commit();
}
}
[*]获取所有图层名称
/// <summary>
/// get all layers names
/// </summary>
/// <returns></returns>
public List<String> GetAllLayersName()
{
List<String> layers = new List<String>();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction trs = db.TransactionManager.StartTransaction();
using (trs)
{
LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (ObjectId id in lt)
{
LayerTableRecord ltr = trs.GetObject(id, OpenMode.ForRead) as LayerTableRecord;
layers.Add(ltr.Name);
}
trs.Commit();
}
return layers;
}
[*]获取图层上的实体id
/// <summary>
/// get layer entity id
/// </summary>
/// <param name="entitys"></param>
public void GetLayerEntity(String layerName, ref List<ObjectId> entitys)
{
if (!IsExistLayer(layerName))
{
return;
}
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
TypedValue []values = { new TypedValue((int)DxfCode.LayerName, layerName)};
SelectionFilter filters = new SelectionFilter(values);
PromptSelectionResult result = ed.SelectAll();
if (result.Status == PromptStatus.OK)
{
ObjectId []ids = result.Value.GetObjectIds();
foreach (ObjectId id in ids)
{
entitys.Add(id);
}
}
else
{
return;
}
}
本帖最后由 cdinten 于 2014-4-3 22:04 编辑
嗯,做的不错。不过还是有两个地方先交流一下。
获取指定图层上的实体id的方法,可以考虑使用如下函数签名:
ObjectIdCollection GetObjectsOnLayer(string layerName);
这样和ObjectARX的使用风格保持统一,还是一个问题就是:
PromptSelectionResult result = ed.SelectAll();
应该需要加上过滤吧?如下:
PromptSelectionResult result = ed.SelectAll(filters);
不然感觉没有过滤?不知道我说的对不对,你可以检验一下。 cdinten 发表于 2014-4-3 22:00 static/image/common/back.gif
嗯,做的不错。不过还是有两个地方先交流一下。
获取指定图层上的实体id的方法,可以考虑使用如下函数签名 ...
嗯嗯,好,是的,那里的确是有一处错误,谢谢指正 本帖最后由 hjki_i 于 2016-10-8 16:45 编辑
楼主,求分享
1246462177@qq.com 好东西,刚好要用到 好东西,学习了 好东西!先留名以后会用的着 好东西,新手学习了。求教一下怎么将根据选择的图元移动到指定图层?
谢谢分享,先点赞
页:
[1]