group命令的深度定制和应用
public string GroupJudge(){
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityResult per = doc.Editor.GetEntity("\nspecify an object in a group");
if (per.Status == PromptStatus.OK)
{
string groupname = GroupJudge(per.ObjectId);
if (groupname != "")
{
return groupname;
}
}
ed.WriteMessage("\nno group will be edit");
return "";//方法定义有输出结果,需指定默认返回值
}
public string GroupJudge(ObjectId oid)
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Entity ent = (Entity)trans.GetObject(oid, OpenMode.ForWrite, false);
DBDictionary GroDic = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
Group gro;
string groname = "";
int i = 0;
Stack mystack = new Stack();
foreach (DBDictionaryEntry groRec in GroDic)//DBDictionary由DBDictionaryEntry组成
{
gro = (Group)trans.GetObject(groRec.Value, OpenMode.ForWrite);//Group继承自DBDictionaryEntry,并且DBDictionaryEntry正好就是Group
if (gro.Has(ent))
{
groname = gro.Name;
mystack.Push(groname);
i++;
}
}
if (i == 1)
{
gro = (Group)trans.GetObject(GroDic.GetAt((string)mystack.Pop()), OpenMode.ForWrite);
gro.SetHighlight(true);
ed.WriteMessage(string.Format("\n{0},{1}", groname, Convert.ToString(i)));
trans.Commit();
return groname;
}
while (mystack.Count != 0)
{
gro = (Group)trans.GetObject(GroDic.GetAt((string)mystack.Pop()), OpenMode.ForWrite);
gro.SetHighlight(true);
PromptKeywordOptions pko = new PromptKeywordOptions(string.Format("\n{0},{1}", gro.Name, Convert.ToString(i)));
pko.Keywords.Add("This");
pko.Keywords.Add("Next");
pko.Keywords.Default = "This";
PromptResult pkr = doc.Editor.GetKeywords(pko);
switch (pkr.StringResult)
{
case "This":
trans.Commit();
return gro.Name;
case "Next":
gro.SetHighlight(false);
break;
default:
goto case "Next";
}
}
trans.Commit();
}
return "";//方法定义有输出结果,需指定默认返回值
}
//把所有组炸开
public void GroupAllExplode()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary GroDic = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
foreach (DBDictionaryEntry groRec in GroDic)
{
Group gro = (Group)trans.GetObject(groRec.Value, OpenMode.ForWrite);
gro.Erase();
}
ed.WriteMessage("\nall group was explode");
trans.Commit();
}
}
//所有组组选
public void GroupAllSelectable()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary GroDic = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
foreach (DBDictionaryEntry groRec in GroDic)
{
Group gro = (Group)trans.GetObject(groRec.Value, OpenMode.ForWrite);
gro.Selectable = true;
gro.SetHighlight(false);
}
object oGroupSelect = Application.GetSystemVariable("PICKSTYLE");
if (Convert.ToInt16(oGroupSelect) != 1)
{
Application.SetSystemVariable("PICKSTYLE", 1);
}
ed.WriteMessage("\nall group is selectable");
doc.Database.BeginSave -= saveEvent;//先取消订阅,后订阅事件,防止重复订阅
doc.Database.BeginSave += saveEvent;//订阅beginsave事件,事件引发包含ge命令DE方法saveEvent
trans.Commit();
}
}
public void saveEvent(object sender, DatabaseIOEventArgs a)
{
GroupEntitySelectable();
}
//所有组可单选,所有空组炸开
public void GroupEntitySelectable()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary GroDic = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
foreach (DBDictionaryEntry groRec in GroDic)
{
Group gro = (Group)trans.GetObject(groRec.Value, OpenMode.ForWrite);
gro.Selectable = false;
gro.SetHighlight(false);
if (gro.NumEntities == 0)
{
gro.Erase();
}
}
ed.WriteMessage("\nall group is not seclectable");
doc.Database.BeginSave -= saveEvent;
trans.Commit();
}
}
//所有组高亮
public void AllGroupHighlight()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary GroDic = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
foreach (DBDictionaryEntry groRec in GroDic)
{
Group gro = (Group)trans.GetObject(groRec.Value, OpenMode.ForWrite);
gro.SetHighlight(true);
}
PromptResult pr = doc.Editor.GetString("\nall group highlight");
foreach (DBDictionaryEntry groRec in GroDic)
{
Group gro = (Group)trans.GetObject(groRec.Value, OpenMode.ForWrite);
gro.SetHighlight(false );
}
trans.Commit();
}
}
public void groupcreat()
{
GroupCreat();//用commandmethod绑定方法到命令时,返回值必须是void
}
public string GroupCreat()//建无名组
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary GroDic = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
Group gro = new Group("x", false);
ed.WriteMessage("\nplease select entity or group to creat a new group");
PromptSelectionResult psr = doc.Editor.SelectImplied();
if (psr.Status != PromptStatus.OK)
{
psr = doc.Editor.GetSelection();
}
if (psr.Status == PromptStatus.OK)
{
SelectionSet sset = psr.Value;
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
LayerTableRecord ltr;
Entity ent;
foreach (ObjectId oid in sset.GetObjectIds())
{
ent = (Entity)trans.GetObject(oid, OpenMode.ForRead);
ltr = (LayerTableRecord)trans.GetObject(lt, OpenMode.ForRead);
if (!ltr.IsLocked)
{
gro.Append(oid);
}
}
}
else
{ return ""; }
GroDic.SetAt("x", gro);
trans.AddNewlyCreatedDBObject(gro, true);
gro.SetAnonymous();
gro.Selectable = false;
trans.Commit();
return gro.Name;
}
}
public void GroupOperate()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
DBDictionary GroDic = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
string group = GroupJudge();//判断是否为组
Group currentGro;
if (group!="")
{
currentGro = (Group)trans.GetObject(GroDic.GetAt(group), OpenMode.ForWrite);
}
else { return; }
PromptKeywordOptions pko = new PromptKeywordOptions("\n");
pko.Keywords.Add("Add");
pko.Keywords.Add("Remove");
pko.Keywords.Add("Select");
pko.Keywords.Add("EDit");
pko.Keywords.Add("eXplode");
pko.Keywords.Default = "Select";
PromptResult pkr = doc.Editor.GetKeywords(pko);
currentGro.SetHighlight(false);//对应GroupJudge代码段里的setHighlight(ture)
PromptSelectionResult pssr;
SelectionSet sset;
Entity ent;
switch (pkr.StringResult)
{
case "Add":
ed.WriteMessage("\nplease select entity which will be add in the group");
pssr = doc.Editor.GetSelection();
if (pssr.Status == PromptStatus.OK)
{
sset = pssr.Value;
foreach (ObjectId oid in sset.GetObjectIds())
{
ent = (Entity)trans.GetObject(oid, OpenMode.ForRead);
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(lt, OpenMode.ForRead);
if (!ltr.IsLocked)
{
if (!currentGro.Has(ent))
{ currentGro.Append(oid); }
}
}
}
break;
case "Remove":
bool select = currentGro.Selectable;
currentGro.Selectable = false;//确保组中对象可单选,方便剔除
ed.WriteMessage("\nplease select entity which will be remove in the group");
pssr = doc.Editor.GetSelection();
if (pssr.Status == PromptStatus.OK)
{
sset = pssr.Value;
foreach (ObjectId oid in sset.GetObjectIds())
{
ent = (Entity)trans.GetObject(oid, OpenMode.ForRead);
if (currentGro.Has(ent))
{ currentGro.Remove(oid); }
}
}
currentGro.Selectable = select;
break;
case "Select":
object oGroupSelect = Application.GetSystemVariable("PICKSTYLE");
if (Convert.ToInt16(oGroupSelect) != 1)
{
Application.SetSystemVariable("PICKSTYLE", 1);
}
currentGro.Selectable = true;
doc.Database.BeginSave -= saveEvent;//先取消订阅,后订阅事件,防止重复订阅
doc.Database.BeginSave += saveEvent;//订阅beginsave事件,事件引发包含ge命令DE方法saveEvent
break;
case "EDit":
currentGro.Selectable = false;
break;
case "eXplode":
currentGro.Erase();
break;
default:
goto case "EDit";//switch配合PromptKeywordOptions,可以用switch-default表示pko为cancel下的条件语句
}
trans.Commit();
}
}
g 创建无名组
gg 关于组的添加,删除,可选,不可选,炸组,检查三视组的操作。可选操作订阅beginsave事件,事件引发包含ge命令的方法saveEvent
gs 所有组组选,删除所有空组,同时激活保存时GE命令。订阅beginsave事件,事件引发包含ge命令的方法saveEvent
ge 所有组可单选,删除所有空组,取消订阅beginsave事件
gh 所有组高亮
gx 删除所有组定义
以上几个命令的代码见下方
沙发自己做。我在这几个组命令的基础上又开发了几个命令,实现了三视图关联移动的功能,即 移动俯视图,主视图和左视图跟着做关联移动,不过仅限移动命令
页:
[1]