cmdCreateViewTag

start
//把当前视图名称与比例标注在最低的轴网下面
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class cmdCreateViewTag : IExternalCommand
{
    /// <summary>
    
/// 得到最下面的轴网
    
/// </summary>
    
/// <param name="doc"></param>
    
/// <returns></returns>
    public Grid GetLowestGrid(Document doc)
    {
        FilteredElementCollector collector = new FilteredElementCollector(doc);
        collector.OfClass(typeof(Grid)).OfCategory(BuiltInCategory.OST_Grids);//
        Grid grid = null;
        foreach (Element el in collector)
        {
            if (grid == null)
            {
                grid = el as Grid;
            }
            else
            {
                Grid gridTmp = el as Grid;
                if (gridTmp.Curve.get_EndPoint(0).Y < grid.Curve.get_EndPoint(0).Y)
                {
                    grid = gridTmp;
                }
            }
        }
        return grid;
    }
    /// <summary>
    
/// 得到当前视图名称
    
/// </summary>
    
/// <param name="doc"></param>
    
/// <returns></returns>
    public string GetViewName(Document doc)
    {
        return doc.ActiveView.Name;
    }
    /// <summary>
    
/// 得到视图比例
    
/// </summary>
    
/// <param name="doc"></param>
    
/// <returns></returns>
    public string GetViewScale(Document doc)
    {
        return "1 : " + doc.ActiveView.get_Parameter(BuiltInParameter.VIEW_SCALE).AsInteger().ToString();
    }
    public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elements)
    {
        Document doc = cmdData.Application.ActiveUIDocument.Document;

        Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
        ts.Start();
        //得到最低的轴网,得到原点
        Grid grid = GetLowestGrid(doc);
        if (grid == null)
        {
            ts.RollBack();
            return Result.Cancelled;
        }
        XYZ xyzOrigin = grid.Curve.get_EndPoint(0) + new XYZ(0, -10);
        //得到视图名称,得到视图比例
        string strText = "视图名称:" + GetViewName(doc) + "\n视图比例:" + GetViewScale(doc);
        //
        XYZ baseVec = new XYZ(000);
        XYZ upVec = new XYZ(000);
        double dWidth = 0.16;
        TextAlignFlags textAlign = TextAlignFlags.TEF_ALIGN_LEFT | TextAlignFlags.TEF_ALIGN_TOP;
        TextNote textNote = doc.Create.NewTextNote(doc.ActiveView, xyzOrigin, baseVec, upVec, dWidth, textAlign, strText);

        ts.Commit();

        return Result.Succeeded;
    }
}


end
posted @ 2014-04-26 22:13  大气象  阅读(2383)  评论(1编辑  收藏  举报
http://www.tianqiweiqi.com