Reference 是Revit对象的引用 也是二次开发必须了解和熟练掌握的技能之一,对象引用包括 图元基础引用 可以根据此引用 实现图元转化 参数信息过滤和设置,此外对于标注来说更重要的是 Face-面引用 Curve-线引用 Point-点引用。
那么根据Revit版本的不同对象的引用获取 也会有所不同 需要查找API做相应的匹配
其实我们可以获得族实例的参照引用也就是族样板里的那两条中心参照线如下图
那么怎么获得呢?大家应该知道 其实图元引用是可以转化为字符串的 reference.ConvertToStableRepresentation(doc) 在使用是可以转化回来Reference.ParseFromStableRepresentation(doc, string),那么和我们说的问题有什么关系呢,答案是肯定的 有!根据Autodesk的指引 ,我们分别获得实例的本身引用和实例的几何面或边的引用 转化为字符串后看看有什么不同:如下
实例本身引用
a77b1edc-61fc-4855-8179-b72c50bdd617-00072e8b
实例几何面引用
a77b1edc-61fc-4855-8179-b72c50bdd617-00072e8b:0:INSTANCE:a77b1edc-61fc-4855-8179-b72c50bdd617-00072cc7:9:SURFACE
我们可以看到 几何内部的引用 信息要比实例本身多的多,而且利用“:”号分隔,都是什么意思呢
第一段,很显然和实例本身的引用相同 也就是族实例的UniqueId
第二段,应该是 实例的集合索引 这里应该会保持为零 因为只有一个实例被选中 索引为零
第三段,表明这个引用是Instance内部的 也就是来自于实例的几何图形的引用
第四段,就是最重要的一段 它标示得到的引用是族内部的那个参照 如 前,后, 左, 右,上,下 等共有九个
第五段,指的是引用是几何图形的面或边或点的引用
所以我们只要改变第四段的索引值就可以获得相对应的是参照引用了 就可以标注尺寸了
这里不详细叙述了 参照的选项设为枚举类型:
public enum SpecialReferenceType
{
Left = 0,
CenterLR = 1,
Right = 2,
Front = 3,
CenterFB = 4,
Back = 5,
Bottom = 6,
CenterElevation = 7,
Top = 8,
NoRefer = 12,
StrongRefer = 13,
LowRefer = 14,
}
private Reference GetSpecialFamilyReference(Document doc, FamilyInstance instance, SpecialReferenceType ReferenceType, int num = 0)
{
Reference indexReference = null;
int index = (int)ReferenceType;
Options geomOptions = new Options();
geomOptions.ComputeReferences = true;
geomOptions.DetailLevel = ViewDetailLevel.Medium;
geomOptions.IncludeNonVisibleObjects = true;
GeometryElement geoElement = instance.get_Geometry(geomOptions);
foreach (GeometryObject obj in geoElement)
{
if (obj is GeometryInstance)
{
GeometryInstance geoInstance = obj as GeometryInstance;
String sampleStableRef = null;
if (geoInstance != null)
{
GeometryElement geoSymbol = geoInstance.GetSymbolGeometry();
if (geoSymbol != null)
{
foreach (GeometryObject geomObj in geoSymbol)
{
if (geomObj is Solid)
{
Solid solid = geomObj as Solid;
if (solid.Faces.Size > 0)
{
Face face = solid.Faces.get_Item(0);
sampleStableRef = face.Reference.ConvertToStableRepresentation(doc);
break;
}
}
}
}
if (sampleStableRef != null)
{
String[] refTokens = sampleStableRef.Split(new char[] { ':' });
String customStableRef = refTokens[0] + ":" + num + ":" + refTokens[2] + ":" + refTokens[3] + ":" + index.ToString();
indexReference = Reference.ParseFromStableRepresentation(doc, customStableRef);
}
break;
}
else
{
}
}
}
return indexReference;
}
修改了一个版本,枚举改为了索引,可以取任意值:
public static Reference GetSpecialFamilyReference(this Element instance, Document doc, int index)
{
Reference indexReference = null;
Options geomOptions = new Options();
geomOptions.ComputeReferences = true;
geomOptions.DetailLevel = ViewDetailLevel.Medium;
geomOptions.IncludeNonVisibleObjects = true;
GeometryElement geoElement = instance.get_Geometry(geomOptions);
foreach (GeometryObject obj in geoElement)
{
String sampleStableRef = null;
if (obj is GeometryInstance)
{
GeometryInstance geoInstance = obj as GeometryInstance;
GeometryElement geoSymbol = geoInstance.GetSymbolGeometry();
if (geoSymbol != null)
{
foreach (GeometryObject geomObj in geoSymbol)
{
if (geomObj is Solid)
{
Solid solid = geomObj as Solid;
if (solid.Faces.Size > 0)
{
Face face = solid.Faces.get_Item(0);
sampleStableRef = face.Reference.ConvertToStableRepresentation(doc);
break;
}
}
}
}
}
else
{
Solid solid2 = obj as Solid;
if (solid2 != null)
{
if (solid2.Faces.Size > 0)
{
Face face = solid2.Faces.get_Item(0);
sampleStableRef = face.Reference.ConvertToStableRepresentation(doc);
}
}
}
if (sampleStableRef != null)
{
String[] refTokens = sampleStableRef.Split(new char[] { ':' });
String customStableRef = refTokens[0] + ":" + refTokens[1] + ":" + refTokens[2] + ":" + refTokens[3] + ":" + index.ToString();
indexReference = Reference.ParseFromStableRepresentation(doc, customStableRef);
break;
}
}
return indexReference;
}
通过传入不同面的枚举类型值就可以获取到对应的参照面
然后主程序只需要调用就行了
dbHelp.createreference(doc, shy, SpecialReferenceType.Bottom)
原文地址:https://blog.csdn.net/weixin_45884724/article/details/123535461