如何在dwg文件

时间:2016-07-27 04:20:17

标签: c# autocad autocad-plugin

这是我的c#代码,用于标识autocad中包含属性的块。在我的dwg文件中有重复的no.of属性我想只显示一次所选属性并避免重复值。

[CommandMethod("NLTAG")]
public void ListAttributes()
{
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Editor ed=Application.DocumentManager.MdiActiveDocument.Editor;
    Database db = HostApplicationServices.WorkingDatabase;
    Transaction tr = db.TransactionManager.StartTransaction();

    try
    {
        TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "INSERT") };
        SelectionFilter filter = new SelectionFilter(filList);
        // Do nothing if selection is unsuccessful
        if (res.Status != PromptStatus.OK)
            return;

        SelectionSet selSet = res.Value;
        ObjectId[] idArray = selSet.GetObjectIds();

        foreach (ObjectId blkId in idArray)
        {
            BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead);
            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
            ////ed.WriteMessage(
            ////  "\nBlock: " + btr.Name
            ////);
            btr.Dispose();

            AttributeCollection attCol = blkRef.AttributeCollection;
            AttributeCollection parts = blkRef.AttributeCollection;
            foreach (ObjectId attId in attCol)
            {
                AttributeReference attRef(AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
                string str = ("\n " + attRef.TextString);//here i get duplicate value
                ed.WriteMessage(str);
            }
        }
        tr.Commit();
    }
    catch (Autodesk.AutoCAD.Runtime.Exception ex)
    {
        ed.WriteMessage(("Exception: " + ex.Message));
    }
    finally
    {
        tr.Dispose();
    }
}

1 个答案:

答案 0 :(得分:0)

属性太多了吗?您可以将它们放入列表中然后获取不同的值吗?

public class MyHomeController
{
    private readonly IListCrud _listcrud;

    public MyHomeController(IListCrud listcrud)
    {
        _listcrud = listcrud;
    }

    public ActionResult Index()
    {
        return View(_listcrud.GetAllLists());
    }
}

public interface IListCrud
{
    List<ListModel> GetAllLists();
}

public class ListCrud : IListCrud
{
    public List<ListModel> GetAllLists()
    {
        using (MySqlConnection connection = new MySqlConnection(this.connectionString))
        {
            return connection.Query<ListModel>("SELECT * FROM projectx.lists").AsList();
        }
    }
}

public class ListCrudCache : IListCrud
{
    private readonly ICacheService _cache;
    private readonly IListCrud _inner;

    public ListCrudCache(ICacheService cache, IListCrud inner)
    {
        _cache = cache;
        _inner = inner;
    }

    public List<ListModel> GetAllLists()
    {
        return _cache.Get("lists", _inner.GetAllLists);
    }
}

当然,这还没有得到优化,但重点是你需要&#34;预处理&#34;输出。