Files
RookieStation/RookieStation/Drawing/ExecuteCmd/CreateViewPlanAnnotation.cs
2022-01-11 16:45:48 +08:00

770 lines
34 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI;
using RookieStation.Utils;
using System.Windows.Controls;
using System;
using System.Text;
using System.Windows.Media.Media3D;
using Autodesk.Revit.DB.Architecture;
namespace RookieStation.Drawing.ExecuteCmd
{
[Obsolete]
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
internal class CreateViewPlanAnnotation : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
Autodesk.Revit.DB.View v = doc.ActiveView;
if (v is ViewPlan)
{
//var wallrefer = uidoc.Selection.PickObject(ObjectType.Element, "请选择元素");
//var wall = doc.GetElement(wallrefer) as Wall;
//var joinids = JoinGeometryUtils.GetJoinedElements(doc, wall);//接触关系过滤不出来,连接命令连接的才能过滤
//uidoc.Selection.SetElementIds(joinids);
doc.InvokeGroup(tg =>
{
doc.Invoke(ts =>
{
JoinWallsAndColumns(doc);
JointStructureCom(doc, BuiltInCategory.OST_Walls, BuiltInCategory.OST_StructuralFraming);
JointStructureCom(doc, BuiltInCategory.OST_StructuralFraming, BuiltInCategory.OST_StructuralColumns);
}, "连接墙柱梁");
CreateWallDimension(doc);
CreateColumnsDimension(doc);
CreateBeamsDimension(doc);
var visableCol = doc.QueryInstancesInView(v).ToElements();
doc.Invoke(ts =>
{
CreateDoorWinTags(doc, visableCol);
CreateLightAnnotations(doc, visableCol);
CreateShelvesAnnotations(doc, visableCol);
}, "创建标注");
});
//var cutids = SolidSolidCutUtils.GetCuttingSolids(wall);
//uidoc.Selection.SetElementIds(insertids);
//uidoc.Selection.SetElementIds(cutids);
//Outline outline = new Outline(wall.get_BoundingBox(doc.ActiveView).Min, wall.get_BoundingBox(doc.ActiveView).Max);
//BoundingBoxIntersectsFilter intersectsFilter = new BoundingBoxIntersectsFilter(outline);
//ElementIntersectsElementFilter intersectsElementFilter = new ElementIntersectsElementFilter(wall, false);
//var col = doc.QueryByType<FamilyInstance>().WherePasses(intersectsElementFilter).ToElementIds();
//uidoc.Selection.SetElementIds(col);
}
else
{
message = "需在平面视图使用此命令";
//TaskDialog.Show("温馨提示", "需在平面视图使用此命令");
return Result.Failed;
}
return Result.Succeeded;
}
public void CreateColumnsDimension(Document doc)
{
var col = new FilteredElementCollector(doc, doc.ActiveView.Id);
ElementCategoryFilter categoryFilter1 = new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns);
ElementCategoryFilter categoryFilter2 = new ElementCategoryFilter(BuiltInCategory.OST_Columns);
LogicalOrFilter andFilter = new LogicalOrFilter(categoryFilter1, categoryFilter2);
col.WherePasses(andFilter);
var columns = col.GroupBy(g => g.Name).Select(s => s.FirstOrDefault());
doc.Invoke(ts =>
{
foreach (FamilyInstance familyInstance in columns)
{
var loc = RsRevitUtils.GetLocationPointByElement(familyInstance);
var faces = RsRevitUtils.GetSideFacesByElement(familyInstance);
if (faces.Count == 0)//找不到实例的几何元素时,需从类型集合查找
{
Options options = new Options
{
ComputeReferences = true
};
faces = RsRevitUtils.GetFacesByFamilyInstance(familyInstance, options);
for (int i = faces.Count - 1; i >= 0; i--)
{
PlanarFace pf = faces[i] as PlanarFace;
if (pf.FaceNormal.CrossProduct(XYZ.BasisZ).IsAlmostEqualTo(XYZ.Zero))
{
faces.Remove(faces[i]);//移除顶面底面
}
}
}
var referenceArray = new ReferenceArray();
var referenceArrayArray = new ReferenceArrayArray();
GetParallFacesReferenceArray(faces, faces.FirstOrDefault(), referenceArray, referenceArrayArray);
foreach (ReferenceArray array in referenceArrayArray)
{
if (array.Size == 0)
{
continue;
}
var face = familyInstance.GetGeometryObjectFromReference(array.get_Item(0)) as PlanarFace;
var line = Line.CreateUnbound(loc, face.FaceNormal).CreateOffset(600 / 304.8, XYZ.BasisZ) as Line;
doc.Create.NewDimension(doc.ActiveView, line, array);
}
}
}, "创建柱子标注");
}
public void CreateBeamsDimension(Document doc)
{
var col = new FilteredElementCollector(doc, doc.ActiveView.Id);
ElementCategoryFilter categoryFilter1 = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
col.WherePasses(categoryFilter1);
var beams = col.GroupBy(g => g.Name).Select(s => s.FirstOrDefault());
doc.Invoke(ts =>
{
foreach (FamilyInstance familyInstance in beams)
{
var loc = RsRevitUtils.GetLocationPointByElement(familyInstance);
var faces = RsRevitUtils.GetSideFacesByElement(familyInstance);
if (faces.Count == 0)//找不到实例的几何元素时,需从类型集合查找
{
Options options = new Options
{
ComputeReferences = true
};
faces = RsRevitUtils.GetFacesByFamilyInstance(familyInstance, options);
for (int i = faces.Count - 1; i >= 0; i--)
{
PlanarFace pf = faces[i] as PlanarFace;
if (pf.FaceNormal.CrossProduct(XYZ.BasisZ).IsAlmostEqualTo(XYZ.Zero))
{
faces.Remove(faces[i]);//移除顶面底面
}
}
}
var referenceArray = new ReferenceArray();
var referenceArrayArray = new ReferenceArrayArray();
GetParallFacesReferenceArray(faces, faces.FirstOrDefault(), referenceArray, referenceArrayArray);
foreach (ReferenceArray array in referenceArrayArray)
{
var face = familyInstance.GetGeometryObjectFromReference(array.get_Item(0)) as PlanarFace;
var line = Line.CreateUnbound(loc, face.FaceNormal).CreateOffset(600 / 304.8, XYZ.BasisZ) as Line;
doc.Create.NewDimension(doc.ActiveView, line, array);
}
}
}, "创建柱子标注");
}
/// <summary>
/// 递归找到所有面法向相等的面集合
/// </summary>
/// <param name="faces"></param>
/// <param name="firstFace"></param>
/// <param name="referenceArray"></param>
/// <param name="referenceArrayArray"></param>
public void GetParallFacesReferenceArray(List<Face> faces, Face firstFace, ReferenceArray referenceArray, ReferenceArrayArray referenceArrayArray)
{
if (referenceArrayArray.Size == 0)
{
referenceArrayArray.Append(referenceArray);
}
for (int i = 0; i < faces.Count; i++)
{
PlanarFace tempFace = faces[i] as PlanarFace;
var pf = firstFace as PlanarFace;
var facenormal = pf.FaceNormal;
//把初始的第一个添加进集合
if (tempFace.FaceNormal.CrossProduct(facenormal).IsAlmostEqualTo(XYZ.Zero))
{
referenceArray.Append(tempFace.Reference);
faces.Remove(tempFace);
GetParallFacesReferenceArray(faces, tempFace, referenceArray, referenceArrayArray);
}
if (i == faces.Count() - 1)
{
referenceArray = new ReferenceArray();
GetParallFacesReferenceArray(faces, faces.FirstOrDefault(), referenceArray, referenceArrayArray);
if (faces.Count() == 0)
{
referenceArrayArray.Append(referenceArray);
}
}
}
}
private static void CreateShelvesAnnotations(Document doc, IList<Element> visableCol)
{
var shelves = new List<Element>();
foreach (var ele in visableCol)
{
var instance = ele as FamilyInstance;
if (instance != null && instance.Symbol.FamilyName.Contains("货架"))
{
shelves.Add(instance);
}
}
CreateShelvesXDimensions(doc, shelves);
CreateShelvesYDimensions(doc, shelves);
}
private static bool CreateShelvesYDimensions(Document doc, List<Element> shelves)
{
var shelfDistincts = shelves
.OrderBy(o => RsRevitUtils.GetLocationPointByElement(o).Y)
.ThenBy(o => RsRevitUtils.GetLocationPointByElement(o).X)
.GroupBy(l => Math.Round(RsRevitUtils.GetLocationPointByElement(l).Y, 4))
.Select(g => g.First() as FamilyInstance)
.ToList();
Func<FamilyInstance, object> p = g =>
{
if (g.Room != null)
{
return g.Room;
}
else
{
return g.Name;
}
};
var group = shelfDistincts.GroupBy(p);
List<ReferenceArray> referenceArrays = new List<ReferenceArray>();
List<Line> lines = new List<Line>();
foreach (var g in group)
{
ReferenceArray referenceArray = new ReferenceArray();
Line line = Line.CreateUnbound(RsRevitUtils.GetLocationPointByElement(g.FirstOrDefault()), XYZ.BasisY);
for (int i = 0; i < g.Count(); i++)
{
FamilyInstance ele = g.ElementAt(i);
if (ele == null)
{
continue;
}
if (ele.FacingOrientation.CrossProduct(XYZ.BasisX).IsAlmostEqualTo(XYZ.Zero))
{
//referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Left).FirstOrDefault());
referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Right).FirstOrDefault());
}
else
{
//if (i < g.Count() - 1)//通道标注
//{
// if (ele.Symbol.GetParameters("长度").FirstOrDefault().AsDouble() < RsRevitUtils.GetLocationPointByElement(g.ElementAt(i + 1)).DistanceTo(RsRevitUtils.GetLocationPointByElement(g.ElementAt(i))))
// {
// referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Back).FirstOrDefault());
// }
//}
referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Front).FirstOrDefault());
//referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Back).FirstOrDefault());
}
}
lines.Add(line);
referenceArrays.Add(referenceArray);
}
for (int i = 0; i < referenceArrays.Count; i++)
{
ReferenceArray referenceArray = referenceArrays[i];
Line line = lines[i];
try
{
doc.Create.NewDimension(doc.ActiveView, line, referenceArray);
}
catch (Exception)
{
continue;
}
}
return true;
}
private static bool CreateShelvesXDimensions(Document doc, List<Element> shelves)
{
var shelfDistincts = shelves
.OrderBy(o => RsRevitUtils.GetLocationPointByElement(o).X)
.ThenBy(o => RsRevitUtils.GetLocationPointByElement(o).Y)
.GroupBy(l => Math.Round(RsRevitUtils.GetLocationPointByElement(l).X, 4))
.Select(g => g.First() as FamilyInstance)
.ToList();
Func<FamilyInstance, object> p = g =>
{
if (g.Room != null)
{
return g.Room;
}
else
{
return g.Name;
}
};
var group = shelfDistincts.GroupBy(p);
List<ReferenceArray> referenceArrays = new List<ReferenceArray>();
List<Line> lines = new List<Line>();
foreach (var g in group)
{
ReferenceArray referenceArray = new ReferenceArray();
Line line = Line.CreateUnbound(RsRevitUtils.GetLocationPointByElement(g.FirstOrDefault()), XYZ.BasisX);
for (int i = 0; i < g.Count(); i++)
{
FamilyInstance ele = g.ElementAt(i);
if (ele == null)
{
continue;
}
if (ele.FacingOrientation.CrossProduct(XYZ.BasisX).IsAlmostEqualTo(XYZ.Zero))
{
//if (i > 0)//通道标注
//{
// if (ele.Symbol.GetParameters("长度").FirstOrDefault().AsDouble() < RsRevitUtils.GetLocationPointByElement(g.ElementAt(i - 1)).DistanceTo(RsRevitUtils.GetLocationPointByElement(g.ElementAt(i))))
// {
// referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Back).FirstOrDefault());
// }
//}
//referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Back).FirstOrDefault());
referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Front).FirstOrDefault());
}
else
{
//referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Left).FirstOrDefault());
referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.Right).FirstOrDefault());
}
}
lines.Add(line);
referenceArrays.Add(referenceArray);
}
for (int i = 0; i < referenceArrays.Count; i++)
{
ReferenceArray referenceArray = referenceArrays[i];
Line line = lines[i];
try
{
doc.Create.NewDimension(doc.ActiveView, line, referenceArray);
}
catch (Exception)
{
continue;
}
}
return true;
}
private bool CreateLightTags(Document doc, IList<Element> visableCol, out List<Element> lights)
{
lights = new List<Element>();
foreach (var light in visableCol)
{
var lightcategory = light.Category;
var loc = RsRevitUtils.GetLocationPointByElement(light);
if (loc != null)
{
loc -= new XYZ(-200 / 304.8, 200 / 304.8, 0);
}
if (loc != null && lightcategory != null && lightcategory.Id.IntegerValue == -2001120)
{
IndependentTag.Create(doc, doc.ActiveView.Id, new Reference(light), false, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, loc);
lights.Add(light);
}
}
return true;
}
private void CreateLightAnnotations(Document doc, IList<Element> visableCol)
{
List<Element> lights;
CreateLightTags(doc, visableCol, out lights);
CreateLightXDimensions(doc, lights);
CreateLightYDimensions(doc, lights);
}
private void CreateLightXDimensions(Document doc, List<Element> lights)
{
var lightDistincts = lights
.OrderBy(o => RsRevitUtils.GetLocationPointByElement(o).X)
.ThenBy(o => RsRevitUtils.GetLocationPointByElement(o).Y)
.GroupBy(l => Math.Round(RsRevitUtils.GetLocationPointByElement(l).X, 4))
.Select(g => g.First())
.ToList();
var group = lightDistincts.GroupBy(g => g.Name);//按族类型名称分组
List<ReferenceArray> referenceArrays = new List<ReferenceArray>();
List<Line> lines = new List<Line>();
foreach (var g in group)
{
ReferenceArray referenceArray = new ReferenceArray();
Line line = Line.CreateUnbound(RsRevitUtils.GetLocationPointByElement(g.FirstOrDefault()), XYZ.BasisX);
foreach (FamilyInstance ele in g)
{
if (ele != null)
{
referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.CenterLeftRight).FirstOrDefault());
}
}
lines.Add(line);
referenceArrays.Add(referenceArray);
}
for (int i = 0; i < referenceArrays.Count; i++)
{
ReferenceArray referenceArray = referenceArrays[i];
Line line = lines[i];
try
{
doc.Create.NewDimension(doc.ActiveView, line, referenceArray);
}
catch (Exception)
{
continue;
}
}
}
private void CreateLightYDimensions(Document doc, List<Element> lights)
{
var lightDistincts = lights
.OrderBy(o => RsRevitUtils.GetLocationPointByElement(o).Y)
.ThenBy(o => RsRevitUtils.GetLocationPointByElement(o).X)
.GroupBy(l => Math.Round(RsRevitUtils.GetLocationPointByElement(l).Y, 4))
.Select(g => g.First())
.ToList();
var group = lightDistincts.GroupBy(g => g.Name);
List<ReferenceArray> referenceArrays = new List<ReferenceArray>();
List<Line> lines = new List<Line>();
foreach (var g in group)
{
ReferenceArray referenceArray = new ReferenceArray();
Line line = Line.CreateUnbound(RsRevitUtils.GetLocationPointByElement(g.FirstOrDefault()), XYZ.BasisY);
foreach (FamilyInstance ele in g)
{
if (ele != null)
{
referenceArray.Append(ele.GetReferences(FamilyInstanceReferenceType.CenterFrontBack).FirstOrDefault());
}
}
lines.Add(line);
referenceArrays.Add(referenceArray);
}
for (int i = 0; i < referenceArrays.Count; i++)
{
ReferenceArray referenceArray = referenceArrays[i];
Line line = lines[i];
try
{
doc.Create.NewDimension(doc.ActiveView, line, referenceArray);
}
catch (Exception)
{
continue;
}
}
}
private void CreateDoorWinTags(Document doc, IList<Element> visableCol)
{
foreach (Element ele in visableCol)
{
var cate = ele.Category;
var loc = RsRevitUtils.GetLocationPointByElement(ele);
var d = ele as FamilyInstance;
if (loc != null && cate != null)
{
if (cate.Id.IntegerValue == -2000023 || cate.Id.IntegerValue == -2000014)
{
IndependentTag independentTag = IndependentTag.Create(doc, doc.ActiveView.Id, new Reference(ele), false, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, loc);
//ElementTransformUtils.RotateElement(doc, independentTag.Id, rotateaxis, d.HandOrientation.AngleTo(XYZ.BasisY));
double substract = d.HandOrientation.AngleTo(XYZ.BasisX) - Math.PI / 2;
if (substract > -0.001 && substract < 0.001)
{
independentTag.TagOrientation = TagOrientation.Vertical;
}
}
}
}
}
private void JointStructureCom(Document doc, BuiltInCategory builtInCategory1, BuiltInCategory builtInCategory2)
{
var collector1 = doc.QueryInstancesByCategory(builtInCategory1).WhereElementIsNotElementType();
var collector2 = doc.QueryInstancesByCategory(builtInCategory2).WhereElementIsNotElementType();
foreach (var fi1 in collector1)
{
ElementIntersectsElementFilter intersectsElementFilter = new ElementIntersectsElementFilter(fi1, false);
collector2.WherePasses(intersectsElementFilter);
foreach (var fi2 in collector2)
{
try
{
JoinGeometryUtils.JoinGeometry(doc, fi2, fi1);
}
catch (Exception)
{
continue;
}
}
}
}
private void JoinWallsAndColumns(Document doc)
{
var wallcol = doc.QueryInstancesByTypeInView<Wall>(doc.ActiveView);
StringBuilder sb = new StringBuilder();
foreach (var wall in wallcol)
{
var columncol = doc.QueryInstancesByTypeAndCategoryInView<FamilyInstance>(doc.ActiveView, BuiltInCategory.OST_StructuralColumns);
BoundingBoxXYZ boundingBox = wall.get_BoundingBox(doc.ActiveView);
Outline outline = new Outline(boundingBox.Min, boundingBox.Max);
BoundingBoxIntersectsFilter intersectsFilter = new BoundingBoxIntersectsFilter(outline);
BoundingBoxIsInsideFilter isInsideFilter = new BoundingBoxIsInsideFilter(outline);
LogicalOrFilter orFilter = new LogicalOrFilter(isInsideFilter, intersectsFilter);
columncol.WherePasses(orFilter);
foreach (FamilyInstance column in columncol)
{
if (!JoinGeometryUtils.AreElementsJoined(doc, wall, column))
{
try
{
JoinGeometryUtils.JoinGeometry(doc, wall, column);
}
catch (Exception)
{
sb.Append($"{column.Name}:{column.Id.IntegerValue}/{wall.Name}:{wall.Id.IntegerValue}" + "\n\r");
}
}
}
}
if (sb.Length > 0)
{
TaskDialog.Show("连接墙柱存在错误", sb.ToString());
}
}
private static List<ElementId> GetWallConnectEles(Document doc, Wall wall)
{
GeometryElement geometryElement = wall.get_Geometry(new Options());
List<ElementId> joinedElements = new List<ElementId>();
foreach (GeometryObject geometryObject in geometryElement)
{
if (geometryObject is Solid)
{
Solid solid = geometryObject as Solid;
foreach (Face face in solid.Faces)
{
// 查找生成该几何面的元素ID
ICollection<ElementId> generatingElementIds = wall.GetGeneratingElementIds(face);
generatingElementIds.Remove(wall.Id); // 移除墙体本身
foreach (ElementId id in generatingElementIds)
{
if (!joinedElements.Contains(id))
joinedElements.Add(id);
}
}
}
}
var loc = wall.Location as LocationCurve;
Curve curve = loc.Curve;
FilteredElementCollector col = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType();
IntersectionResultArray intersection;
//StringBuilder sb = new StringBuilder();
foreach (Wall w in col)
{
if (w != null)
{
var lc = w.Location as LocationCurve;
var c = lc.Curve;
var result = curve.Intersect(c, out intersection);
if (result == SetComparisonResult.Overlap && !joinedElements.Contains(w.Id))
{
joinedElements.Add(w.Id);
}
//sb.Append(w.Id.ToString() + result + "\r\n");
}
}
var ids = JoinGeometryUtils.GetJoinedElements(doc, wall);
foreach (var id in ids)
{
if (!joinedElements.Contains(id))
{
joinedElements.Add(id);
}
}
return joinedElements;
}
/// <summary>
/// 获取当前视图中与当前墙体连接和相交的墙体
/// </summary>
/// <param name="doc"></param>
/// <param name="w"></param>
/// <returns></returns>
private static List<ElementId> FindWallIdsConnected(Document doc, Wall w)
{
List<ElementId> joinedElements = new List<ElementId>();
var loc = w.Location as LocationCurve;
Curve curve = loc.Curve;
FilteredElementCollector eles = doc.QueryInstancesByTypeInView<Wall>(doc.ActiveView).WhereElementIsNotElementType();
IntersectionResultArray intersection;
StringBuilder sb = new StringBuilder();
foreach (Element e in eles)
{
Wall wall = e as Wall;
if (wall != null)
{
var lc = wall.Location as LocationCurve;
var c = lc.Curve;
var result = curve.Intersect(c, out intersection);
if (result == SetComparisonResult.Overlap && !e.Name.Contains("踢脚线") && !e.Name.Contains("墙饰面"))
{
joinedElements.Add(wall.Id);
}
sb.Append(wall.Id.ToString() + result + "\r\n");
}
}
return joinedElements;
}
private static void CreateWallDimension(Document doc)
{
var wallList = doc.QueryInstancesByTypeInView<Wall>(doc.ActiveView).Cast<Wall>().Where(w =>
{
if (w.Name.Contains("墙饰面") || w.Name.Contains("踢脚线") || w.Width < 50 / 304.8)
{
return false;
}
return true;
});
doc.Invoke(ts =>
{
foreach (Wall wall in wallList)
{
//if (wall.WallType.Kind != WallKind.Basic)
//{
// continue;
//}
ReferenceArray wallThickReferenceArray = new ReferenceArray();
ReferenceArray wallLengthRefernceArray = new ReferenceArray();
List<Face> faces = RsRevitUtils.GetSideFacesByElement(wall);
List<ElementId> ids = FindWallIdsConnected(doc, wall);
foreach (var id in ids)
{
Element ele = doc.GetElement(id);
faces.AddRange(RsRevitUtils.GetSideFacesByElement(ele));
}
foreach (Face face in faces)
{
PlanarFace pf = face as PlanarFace;
if (pf == null || face.Reference == null || pf.OrientationMatchesSurfaceOrientation == false)
{
continue;
}
if (pf.FaceNormal.CrossProduct(wall.Orientation).IsAlmostEqualTo(XYZ.Zero))
{
if (doc.GetElement(face.Reference).Id == wall.Id)
{
wallThickReferenceArray.Append(face.Reference);//墙厚标注
}
}
else if (pf.FaceNormal.DotProduct(wall.Orientation) < 0.001)
{
wallLengthRefernceArray.Append(face.Reference);
}
else
{
}
}
try
{
var scale = doc.ActiveView.Scale;
var tranform = Transform.CreateTranslation(wall.Orientation * scale * 15 / 304.8);
var wallcurve = RsRevitUtils.GetLocationCurveByElement(wall) as Line;
Line lengthLine = wallcurve.CreateTransformed(tranform) as Line;
var lengthdim = doc.Create.NewDimension(doc.ActiveView, lengthLine, wallLengthRefernceArray);
doc.Regenerate();
ReferenceArray finallengthreferenceArray = RemoveZeroSegements(wall, wallLengthRefernceArray, lengthdim);
doc.Delete(lengthdim.Id);
doc.Create.NewDimension(doc.ActiveView, lengthLine, finallengthreferenceArray);
Line thickLine = Line.CreateUnbound(wallcurve.Evaluate(0.2, true), wall.Orientation);
var thickdim = doc.Create.NewDimension(doc.ActiveView, thickLine, wallThickReferenceArray);
if (lengthdim.Segments.Size > 2)
{
ReferenceArray referenceArray = new ReferenceArray();
var refer1 = lengthdim.References.get_Item(0);
var refer2 = lengthdim.References.get_Item(lengthdim.References.Size - 1);
referenceArray.Append(refer1);
referenceArray.Append(refer2);
var tranform1 = Transform.CreateTranslation(wall.Orientation * scale * 25 / 304.8);
Line lengthLine1 = wallcurve.CreateTransformed(tranform1) as Line;
doc.Create.NewDimension(doc.ActiveView, lengthLine1, referenceArray);
}
}
catch (Exception)
{
continue;
}
}
});
}
private static ReferenceArray RemoveZeroSegements(Wall wall, ReferenceArray wallLengthRefernceArray, Dimension lengthdim)
{
List<Reference> referencesdelete = new List<Reference>();
var finallengthreferenceArray = new ReferenceArray();
for (int i = 0; i < lengthdim.NumberOfSegments; i++)
{
if (lengthdim.Segments.get_Item(i).ValueString == "0")
{
if (lengthdim.References.get_Item(i).ElementId == wall.Id)
{
referencesdelete.Add(lengthdim.References.get_Item(i));
}
if (lengthdim.References.get_Item(i + 1).ElementId == wall.Id)
{
referencesdelete.Add(lengthdim.References.get_Item(i + 1));
}
}
}
for (int i = 0; i < wallLengthRefernceArray.Size; i++)
{
bool isContain = false;
var reference = wallLengthRefernceArray.get_Item(i);
foreach (var referdelete in referencesdelete)
{
if (reference.EqualTo(referdelete))
{
isContain = true;
}
}
if (!isContain)
{
finallengthreferenceArray.Append(reference);
}
}
return finallengthreferenceArray;
}
}
}