//把当前视图名称与比例标注在最低的轴网下面
[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, -1, 0);
//得到视图名称,得到视图比例
string strText = "视图名称:" + GetViewName(doc) + " 视图比例:" + GetViewScale(doc);
//
XYZ baseVec = new XYZ(0, 0, 0);
XYZ upVec = new XYZ(0, 0, 0);
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;
}
}
[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, -1, 0);
//得到视图名称,得到视图比例
string strText = "视图名称:" + GetViewName(doc) + " 视图比例:" + GetViewScale(doc);
//
XYZ baseVec = new XYZ(0, 0, 0);
XYZ upVec = new XYZ(0, 0, 0);
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