net模块总帖子比较少,正在练习,分享一下
-
- //查找图块并删除
- [CommandMethod("sctk")]
- public void Mydeltk()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter your name: ");
- pStrOpts.AllowSpaces = true;
- PromptResult pStrRes = ed.GetString(pStrOpts);
- if (pStrRes.Status != PromptStatus.OK) return;
- Application.ShowAlertDialog("The name entered was: " +
- pStrRes.StringResult);
- String Blockname = pStrRes.StringResult;
- //定义过滤器
- TypedValue[] filList = new TypedValue[1];
- filList[0] = new TypedValue(0, "Insert");
- //filList[1] = new TypedValue(2, br.Name);
- SelectionFilter filter = new SelectionFilter(filList);
- //选择对象
- PromptSelectionResult res = ed.SelectAll(filter);
- if(res.Status != PromptStatus.OK) return;
- Transaction trans = db.TransactionManager.StartTransaction();
- using (trans)
- {
- try
- {
- //
- //遍历选择集
- foreach (ObjectId id in res.Value.GetObjectIds())
- {
- BlockReference Selent = (BlockReference)trans.GetObject(id, OpenMode.ForWrite);
- // BlockReference br = ent as BlockReference;
- // Selent.Color = Color.FromColorIndex(ColorMethod.ByLayer, 256);
- DelBlockSubEntity(ed, trans, Selent, Blockname);
- }
- //ed.WriteMessage("\n一共查找了" + res.Value.Count );
- trans.Commit();
- }
- catch (Autodesk..Runtime.Exception e)
- {
- // Something went wrong
- ed.WriteMessage(e.ToString());
- }
- }
- }
- [hide] ///
- /// 删除图块内部指定名称的图块
- ///
- ///
- /// 块参照
- /// 图块名称
- private static void DelBlockSubEntity(Editor ed, Transaction trans, BlockReference blockObject, String Blockname)
- {
- //获取图块的块表记录
- BlockTableRecord btrBlock = trans.GetObject(blockObject.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
- //遍历图块里的所有实体对象
- foreach (ObjectId BlcokId in btrBlock)
- {
- Entity BlockEntity = trans.GetObject(BlcokId, OpenMode.ForRead) as Entity;
- if (BlockEntity is BlockReference)
- {
- BlockReference ReEntity = (BlockReference)BlockEntity;
- //获取图块中的所有实体对象
- if (ReEntity.Name == Blockname)
- {
- Entity BlockEntity2 = trans.GetObject(BlcokId, OpenMode.ForWrite) as Entity;
- BlockEntity2.Erase();
- Application.ShowAlertDialog("The name entered was: " + Blockname + "删除了哦!");
- }
- DelBlockSubEntity(ed, trans, (BlockReference)BlockEntity, (String)Blockname);
- }
- }
- [/hide]}
|