Revit Family API 添加参数与尺寸标注
API 参数 添加 标注 尺寸 revit Family
2023-09-11 14:21:42 时间
使用FamilyManager其他的与普通添加参数与标注没区别。url:http://greatverve.cnblogs.com/p/revit-family-api-param.html
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdAddParam : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Transaction ts = new Transaction(doc, "NewDimension");
ts.Start();
//添加参数
FamilyManager m_familyMgr = doc.FamilyManager;
bool isInstance = false;
FamilyParameter paramTw = m_familyMgr.AddParameter("Tw", BuiltInParameterGroup.PG_GEOMETRY, ParameterType.Length, isInstance);
double tw = Util.mmToFeet(150.0);
m_familyMgr.Set(paramTw, tw);
m_familyMgr.SetFormula(paramTw, "Width / 4.0");//公式
//添加尺寸标注
View pViewPlan = Util.findElement(doc, typeof(ViewPlan), "Lower Ref. Level") as View;
ReferencePlane ref1 = Util.findElement(doc, typeof(ReferencePlane), "Left") as ReferencePlane;
ReferencePlane ref2 = Util.findElement(doc, typeof(ReferencePlane), "offsetV") as ReferencePlane;
ReferenceArray pRefArray = new ReferenceArray();
pRefArray.Append(ref1.Reference);
pRefArray.Append(ref2.Reference);
XYZ p0 = ref1.FreeEnd;
XYZ p1 = ref2.FreeEnd;
Line pLine = app.Application.Create.NewLineBound(p0, p1);
Dimension pDimTw = doc.FamilyCreate.NewDimension(pViewPlan, pLine, pRefArray);
pDimTw.Label = m_familyMgr.get_Parameter("Tw");
ts.Commit();
return Result.Succeeded;
}
}
public class Util
{
//Revit内部单位feet转化为mm即毫米
public static double mmToFeet(double val) { return val / 304.8; }
public static double feetToMm(double val) { return val * 304.8; }
//通过类型与名称找Element
public static Element findElement(Document _rvtDoc, Type targetType, string targetName)
{
// get the elements of the given type
//
FilteredElementCollector collector = new FilteredElementCollector(_rvtDoc);
collector.WherePasses(new ElementClassFilter(targetType));
// parse the collection for the given name
// using LINQ query here.
//
var targetElems = from element in collector where element.Name.Equals(targetName) select element;
List<Element> elems = targetElems.ToList<Element>();
if (elems.Count > 0)
{ // we should have only one with the given name.
return elems[0];
}
// cannot find it.
return null;
}
#region Formatting and message handlers
public const string Caption = "Revit Family API Labs";
/// <summary>
/// MessageBox wrapper for informational message.
/// </summary>
public static void InfoMsg(string msg)
{
System.Diagnostics.Debug.WriteLine(msg);
WinForm.MessageBox.Show(msg, Caption, WinForm.MessageBoxButtons.OK, WinForm.MessageBoxIcon.Information);
}
/// <summary>
/// MessageBox wrapper for error message.
/// </summary>
public static void ErrorMsg(string msg)
{
WinForm.MessageBox.Show(msg, Caption, WinForm.MessageBoxButtons.OK, WinForm.MessageBoxIcon.Error);
}
#endregion // Formatting and message handlers
}
public class cmdAddParam : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Transaction ts = new Transaction(doc, "NewDimension");
ts.Start();
//添加参数
FamilyManager m_familyMgr = doc.FamilyManager;
bool isInstance = false;
FamilyParameter paramTw = m_familyMgr.AddParameter("Tw", BuiltInParameterGroup.PG_GEOMETRY, ParameterType.Length, isInstance);
double tw = Util.mmToFeet(150.0);
m_familyMgr.Set(paramTw, tw);
m_familyMgr.SetFormula(paramTw, "Width / 4.0");//公式
//添加尺寸标注
View pViewPlan = Util.findElement(doc, typeof(ViewPlan), "Lower Ref. Level") as View;
ReferencePlane ref1 = Util.findElement(doc, typeof(ReferencePlane), "Left") as ReferencePlane;
ReferencePlane ref2 = Util.findElement(doc, typeof(ReferencePlane), "offsetV") as ReferencePlane;
ReferenceArray pRefArray = new ReferenceArray();
pRefArray.Append(ref1.Reference);
pRefArray.Append(ref2.Reference);
XYZ p0 = ref1.FreeEnd;
XYZ p1 = ref2.FreeEnd;
Line pLine = app.Application.Create.NewLineBound(p0, p1);
Dimension pDimTw = doc.FamilyCreate.NewDimension(pViewPlan, pLine, pRefArray);
pDimTw.Label = m_familyMgr.get_Parameter("Tw");
ts.Commit();
return Result.Succeeded;
}
}
public class Util
{
//Revit内部单位feet转化为mm即毫米
public static double mmToFeet(double val) { return val / 304.8; }
public static double feetToMm(double val) { return val * 304.8; }
//通过类型与名称找Element
public static Element findElement(Document _rvtDoc, Type targetType, string targetName)
{
// get the elements of the given type
//
FilteredElementCollector collector = new FilteredElementCollector(_rvtDoc);
collector.WherePasses(new ElementClassFilter(targetType));
// parse the collection for the given name
// using LINQ query here.
//
var targetElems = from element in collector where element.Name.Equals(targetName) select element;
List<Element> elems = targetElems.ToList<Element>();
if (elems.Count > 0)
{ // we should have only one with the given name.
return elems[0];
}
// cannot find it.
return null;
}
#region Formatting and message handlers
public const string Caption = "Revit Family API Labs";
/// <summary>
/// MessageBox wrapper for informational message.
/// </summary>
public static void InfoMsg(string msg)
{
System.Diagnostics.Debug.WriteLine(msg);
WinForm.MessageBox.Show(msg, Caption, WinForm.MessageBoxButtons.OK, WinForm.MessageBoxIcon.Information);
}
/// <summary>
/// MessageBox wrapper for error message.
/// </summary>
public static void ErrorMsg(string msg)
{
WinForm.MessageBox.Show(msg, Caption, WinForm.MessageBoxButtons.OK, WinForm.MessageBoxIcon.Error);
}
#endregion // Formatting and message handlers
}
相关文章
- Java 8 新特性-菜鸟教程 (8) -Java 8 日期时间 API
- Web APi之捕获请求原始内容的实现方法以及接受POST请求多个参数多种解决方案(十四)
- js api 实现钉钉免登
- Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群
- kubernetes API 查询各种骚操作 | label selector、field-selector查询
- HFSS-API入门第一弹:画个Box
- FORBIDDEN/12/index read-only / allow delete (api)
- linux C-文件API介绍与使用
- C# 获得窗体句柄并发送消息(利用windows API可在不同进程中获取)
- 请求与上传文件,Session简介,Restful API,Nodemon
- client-go调用Kubernetes Api
- 《HTML5游戏编程核心技术与实战》——2.3 图像API
- 微信小程序API~用户信息
- 分布式事务(二)Java事务API(JTA)规范
- arcgis api 3.x for js 入门开发系列二十二地图模态层(附源码下载)
- Tensorflow2.0 入门与实战学习笔记(四)-函数式Api
- EBuild-API常见问题汇总
- 使用flink Table &Sql api来构建批量和流式应用(2)Table API概述
- 关于Revit API修改元素参数的问题?
- Revit API单位转换类
- Revit API根据参数类型取得参数的值