private void Member(Document doc, Dimension dimension, ref string hh, ref string type) { ReferenceArray referenceArray = dimension.References; List <string> list = new List <string>(); for (int i = 1; i < referenceArray.Size - 1; i++) { Reference reference = referenceArray.get_Item(i); Element element = doc.GetElement(reference); if (element.Category.Id.IntegerValue != (int)BuiltInCategory.OST_StructuralFraming) { ElementId elementId = element.GetTypeId(); Element eletype = doc.GetElement(elementId); Parameter sorting_order = eletype.LookupParameter("SORTING_ORDER"); string val = string.Empty; if (sorting_order != null) { if (sorting_order.AsInteger() == 405) { type = "REBAR"; val = element.LookupParameter("CONTROL_MARK").AsString(); } else { type = "EMBED"; val = eletype.LookupParameter("CONTROL_MARK").AsString(); } } else { type = "EMBED"; val = element.LookupParameter("CONTROL_MARK").AsString(); } list.Add(val); } } Removeduplicatestring(list); for (int i = 0; i < list.Count; i++) { if (i == 0) { hh = list[i]; } else { hh = hh + "," + list[i]; } } }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; // Select two walls and the dimension line point: Selection sel = uidoc.Selection; ReferenceArray refs = new ReferenceArray(); try { ISelectionFilter f = new JtElementsOfClassSelectionFilter <Wall>(); refs.Append(sel.PickObject( ObjectType.Element, f, "Please select first wall")); refs.Append(sel.PickObject( ObjectType.Element, f, "Please pick dimension line " + "point on second wall")); //rFace = sel.PickObject( ObjectType.Face, // "Please select face on second wall at dimension line point" ); // //rPoint = sel.PickObject( ObjectType.PointOnElement, // "Please select point on first wall" ); } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { message = "No two walls selected"; return(Result.Failed); } // Ensure the two selected walls are straight and // parallel; determine their mutual normal vector // and a point on each wall for distance // calculations: Wall[] walls = new Wall[2]; List <int> ids = new List <int>(2); XYZ[] pts = new XYZ[2]; Line[] lines = new Line[2]; IntersectionResult ir; XYZ normal = null; int i = 0; foreach (Reference r in refs) { // 'Autodesk.Revit.DB.Reference.Element' is // obsolete: Property will be removed. Use // Document.GetElement(Reference) instead. //Wall wall = r.Element as Wall; // 2011 Wall wall = doc.GetElement(r) as Wall; // 2012 walls[i] = wall; ids.Add(wall.Id.IntegerValue); // Obtain location curve and // check that it is straight: LocationCurve lc = wall.Location as LocationCurve; Curve curve = lc.Curve; lines[i] = curve as Line; if (null == lines[i]) { message = _prompt; return(Result.Failed); } // Obtain normal vectors // and ensure that they are equal, // i.e. walls are parallel: if (null == normal) { normal = Util.Normal(lines[i]); } else { if (!Util.IsParallel(normal, Util.Normal(lines[i]))) { message = _prompt; return(Result.Failed); } } // Obtain pick points and project // onto wall location lines: XYZ p = r.GlobalPoint; ir = lines[i].Project(p); if (null == ir) { message = string.Format( "Unable to project pick point {0} " + "onto wall location line.", i); return(Result.Failed); } pts[i] = ir.XYZPoint; Debug.Print( "Wall {0} id {1} at {2}, {3} --> point {4}", i, wall.Id.IntegerValue, Util.PointString(lines[i].GetEndPoint(0)), Util.PointString(lines[i].GetEndPoint(1)), Util.PointString(pts[i])); if (0 < i) { // Project dimension point selected on second wall // back onto first wall, and ensure that normal // points from second wall to first: ir = lines[0].Project(pts[1]); if (null == ir) { message = string.Format( "Unable to project selected dimension " + "line point {0} on second wall onto " + "first wall's location line.", Util.PointString(pts[1])); return(Result.Failed); } pts[0] = ir.XYZPoint; } ++i; } XYZ v = pts[0] - pts[1]; if (0 > v.DotProduct(normal)) { normal = -normal; } // Shoot a ray back from the second // picked wall towards first: Debug.Print( "Shooting ray from {0} in direction {1}", Util.PointString(pts[1]), Util.PointString(normal)); View3D view = Get3DView(doc); if (null == view) { message = "No 3D view named '{3D}' found; " + "run the View > 3D View command once " + "to generate it."; return(Result.Failed); } //refs = doc.FindReferencesByDirection( // pts[1], normal, view ); // 2011 //IList<ReferenceWithContext> refs2 // = doc.FindReferencesWithContextByDirection( // pts[1], normal, view ); // 2012 // In the Revit 2014 API, the call to // FindReferencesWithContextByDirection causes a // warning saying: // "FindReferencesWithContextByDirection is obsolete: // This method is deprecated in Revit 2014. // Use the ReferenceIntersector class instead." ReferenceIntersector ri = new ReferenceIntersector( walls[0].Id, FindReferenceTarget.Element, view); ReferenceWithContext ref2 = ri.FindNearest(pts[1], normal); if (null == ref2) { message = "ReferenceIntersector.FindNearest" + " returned null!"; return(Result.Failed); } #region Obsolete code to determine the closest reference #if NEED_TO_DETERMINE_CLOSEST_REFERENCE // Store the references to the wall surfaces: Reference[] surfrefs = new Reference[2] { null, null }; // Find the two closest intersection // points on each of the two walls: double[] minDistance = new double[2] { double.MaxValue, double.MaxValue }; //foreach( Reference r in refs ) foreach (ReferenceWithContext rc in refs2) { // 'Autodesk.Revit.DB.Reference.Element' is // obsolete: Property will be removed. Use // Document.GetElement(Reference) instead. //Element e = r.Element; // 2011 Reference r = rc.GetReference(); Element e = doc.GetElement(r); // 2012 if (e is Wall) { i = ids.IndexOf(e.Id.IntegerValue); if (-1 < i && ElementReferenceType.REFERENCE_TYPE_SURFACE == r.ElementReferenceType) { //GeometryObject g = r.GeometryObject; // 2011 GeometryObject g = e.GetGeometryObjectFromReference(r); // 2012 if (g is PlanarFace) { PlanarFace face = g as PlanarFace; Line line = (e.Location as LocationCurve) .Curve as Line; Debug.Print( "Wall {0} at {1}, {2} surface {3} " + "normal {4} proximity {5}", e.Id.IntegerValue, Util.PointString(line.GetEndPoint(0)), Util.PointString(line.GetEndPoint(1)), Util.PointString(face.Origin), Util.PointString(face.Normal), rc.Proximity); // First reference: assert it is a face on this wall // and the distance is half the wall thickness. // Second reference: the first reference on the other // wall; assert the distance between the two references // equals the distance between the wall location lines // minus half of the sum of the two wall thicknesses. if (rc.Proximity < minDistance[i]) { surfrefs[i] = r; minDistance[i] = rc.Proximity; } } } } } if (null == surfrefs[0]) { message = "No suitable face intersection " + "points found on first wall."; return(Result.Failed); } if (null == surfrefs[1]) { message = "No suitable face intersection " + "points found on second wall."; return(Result.Failed); } CmdDimensionWallsIterateFaces .CreateDimensionElement(doc.ActiveView, pts[0], surfrefs[0], pts[1], surfrefs[1]); #endif // NEED_TO_DETERMINE_CLOSEST_REFERENCE #endregion // Obsolete code to determine the closest reference CmdDimensionWallsIterateFaces .CreateDimensionElement(doc.ActiveView, pts[0], ref2.GetReference(), pts[1], refs.get_Item(1)); return(Result.Succeeded); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; // Select two walls and the dimension line point: Selection sel = uidoc.Selection; ReferenceArray refs = new ReferenceArray(); try { ISelectionFilter f = new JtElementsOfClassSelectionFilter<Wall>(); refs.Append( sel.PickObject( ObjectType.Element, f, "Please select first wall" ) ); refs.Append( sel.PickObject( ObjectType.Element, f, "Please pick dimension line " + "point on second wall" ) ); //rFace = sel.PickObject( ObjectType.Face, // "Please select face on second wall at dimension line point" ); // //rPoint = sel.PickObject( ObjectType.PointOnElement, // "Please select point on first wall" ); } catch( Autodesk.Revit.Exceptions.OperationCanceledException ) { message = "No two walls selected"; return Result.Failed; } // Ensure the two selected walls are straight and // parallel; determine their mutual normal vector // and a point on each wall for distance // calculations: Wall[] walls = new Wall[2]; List<int> ids = new List<int>( 2 ); XYZ[] pts = new XYZ[2]; Line[] lines = new Line[2]; IntersectionResult ir; XYZ normal = null; int i = 0; foreach( Reference r in refs ) { // 'Autodesk.Revit.DB.Reference.Element' is // obsolete: Property will be removed. Use // Document.GetElement(Reference) instead. //Wall wall = r.Element as Wall; // 2011 Wall wall = doc.GetElement( r ) as Wall; // 2012 walls[i] = wall; ids.Add( wall.Id.IntegerValue ); // Obtain location curve and // check that it is straight: LocationCurve lc = wall.Location as LocationCurve; Curve curve = lc.Curve; lines[i] = curve as Line; if( null == lines[i] ) { message = _prompt; return Result.Failed; } // Obtain normal vectors // and ensure that they are equal, // i.e. walls are parallel: if( null == normal ) { normal = Util.Normal( lines[i] ); } else { if( !Util.IsParallel( normal, Util.Normal( lines[i] ) ) ) { message = _prompt; return Result.Failed; } } // Obtain pick points and project // onto wall location lines: XYZ p = r.GlobalPoint; ir = lines[i].Project( p ); if( null == ir ) { message = string.Format( "Unable to project pick point {0} " + "onto wall location line.", i ); return Result.Failed; } pts[i] = ir.XYZPoint; Debug.Print( "Wall {0} id {1} at {2}, {3} --> point {4}", i, wall.Id.IntegerValue, Util.PointString( lines[i].GetEndPoint( 0 ) ), Util.PointString( lines[i].GetEndPoint( 1 ) ), Util.PointString( pts[i] ) ); if( 0 < i ) { // Project dimension point selected on second wall // back onto first wall, and ensure that normal // points from second wall to first: ir = lines[0].Project( pts[1] ); if( null == ir ) { message = string.Format( "Unable to project selected dimension " + "line point {0} on second wall onto " + "first wall's location line.", Util.PointString( pts[1] ) ); return Result.Failed; } pts[0] = ir.XYZPoint; } ++i; } XYZ v = pts[0] - pts[1]; if( 0 > v.DotProduct( normal ) ) { normal = -normal; } // Shoot a ray back from the second // picked wall towards first: Debug.Print( "Shooting ray from {0} in direction {1}", Util.PointString( pts[1] ), Util.PointString( normal ) ); View3D view = Get3DView( doc ); if( null == view ) { message = "No 3D view named '{3D}' found; " + "run the View > 3D View command once " + "to generate it."; return Result.Failed; } //refs = doc.FindReferencesByDirection( // pts[1], normal, view ); // 2011 //IList<ReferenceWithContext> refs2 // = doc.FindReferencesWithContextByDirection( // pts[1], normal, view ); // 2012 // In the Revit 2014 API, the call to // FindReferencesWithContextByDirection causes a // warning saying: // "FindReferencesWithContextByDirection is obsolete: // This method is deprecated in Revit 2014. // Use the ReferenceIntersector class instead." ReferenceIntersector ri = new ReferenceIntersector( walls[0].Id, FindReferenceTarget.Element, view ); ReferenceWithContext ref2 = ri.FindNearest( pts[1], normal ); if( null == ref2 ) { message = "ReferenceIntersector.FindNearest" + " returned null!"; return Result.Failed; } #region Obsolete code to determine the closest reference #if NEED_TO_DETERMINE_CLOSEST_REFERENCE // Store the references to the wall surfaces: Reference[] surfrefs = new Reference[2] { null, null }; // Find the two closest intersection // points on each of the two walls: double[] minDistance = new double[2] { double.MaxValue, double.MaxValue }; //foreach( Reference r in refs ) foreach( ReferenceWithContext rc in refs2 ) { // 'Autodesk.Revit.DB.Reference.Element' is // obsolete: Property will be removed. Use // Document.GetElement(Reference) instead. //Element e = r.Element; // 2011 Reference r = rc.GetReference(); Element e = doc.GetElement( r ); // 2012 if( e is Wall ) { i = ids.IndexOf( e.Id.IntegerValue ); if( -1 < i && ElementReferenceType.REFERENCE_TYPE_SURFACE == r.ElementReferenceType ) { //GeometryObject g = r.GeometryObject; // 2011 GeometryObject g = e.GetGeometryObjectFromReference( r ); // 2012 if( g is PlanarFace ) { PlanarFace face = g as PlanarFace; Line line = ( e.Location as LocationCurve ) .Curve as Line; Debug.Print( "Wall {0} at {1}, {2} surface {3} " + "normal {4} proximity {5}", e.Id.IntegerValue, Util.PointString( line.GetEndPoint( 0 ) ), Util.PointString( line.GetEndPoint( 1 ) ), Util.PointString( face.Origin ), Util.PointString( face.Normal ), rc.Proximity ); // First reference: assert it is a face on this wall // and the distance is half the wall thickness. // Second reference: the first reference on the other // wall; assert the distance between the two references // equals the distance between the wall location lines // minus half of the sum of the two wall thicknesses. if( rc.Proximity < minDistance[i] ) { surfrefs[i] = r; minDistance[i] = rc.Proximity; } } } } } if( null == surfrefs[0] ) { message = "No suitable face intersection " + "points found on first wall."; return Result.Failed; } if( null == surfrefs[1] ) { message = "No suitable face intersection " + "points found on second wall."; return Result.Failed; } CmdDimensionWallsIterateFaces .CreateDimensionElement( doc.ActiveView, pts[0], surfrefs[0], pts[1], surfrefs[1] ); #endif // NEED_TO_DETERMINE_CLOSEST_REFERENCE #endregion // Obsolete code to determine the closest reference CmdDimensionWallsIterateFaces .CreateDimensionElement( doc.ActiveView, pts[0], ref2.GetReference(), pts[1], refs.get_Item( 1 ) ); return Result.Succeeded; }
Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Level level = doc.ActiveView.GenLevel; View view = doc.ActiveView; FamilySymbol symbol = doc.GetElement(new ElementId(587194)) as FamilySymbol; ElementClassFilter dimfilter = new ElementClassFilter(typeof(Dimension)); ElementOwnerViewFilter viewfilter = new ElementOwnerViewFilter(view.Id); FilteredElementCollector collector = new FilteredElementCollector(doc); collector.WherePasses(dimfilter).WherePasses(viewfilter); List <Dimension> add = new List <Dimension>(); List <Dimension> add2 = new List <Dimension>(); List <DimensionSegmentArray> dimarrayarray = new List <DimensionSegmentArray>(); ReferenceArray referarray = new ReferenceArray(); ReferenceArray referarray2 = new ReferenceArray(); foreach (Element e in collector) { Dimension dim = e as Dimension; if (dim.NumberOfSegments > 1) { dimarrayarray.Add(dim.Segments); add2.Add(dim); } else { add.Add(dim); } } if (add2.Count != 0) //连续尺寸标注 { using (Transaction trans = new Transaction(doc)) { FailureHandlingOptions failureHandlingOptions = trans.GetFailureHandlingOptions(); FailureHandler failureHandler = new FailureHandler(); failureHandlingOptions.SetFailuresPreprocessor(failureHandler); // 这句话是关键 failureHandlingOptions.SetClearAfterRollback(true); trans.SetFailureHandlingOptions(failureHandlingOptions); trans.Start("Start"); ChangeDimensionTypeStyle(add2.First <Dimension>(), doc); for (int b = 0; b < add2.Count; b++) { referarray2 = add2.ElementAt <Dimension>(b).References; foreach (Reference refer in referarray2) { //string re = refer.ElementReferenceType.ToString(); var cu = add2.ElementAt <Dimension>(b).Curve; Dimension dim = add2.ElementAt <Dimension>(b); Line li = cu as Line; //var n = re.GetType().ToString(); Element el = doc.GetElement(refer); Curve curve = null; if (el.Location.GetType() != typeof(LocationCurve)) { if (el.GetType() == typeof(Grid)) { curve = (el as Grid).Curve; } else if ((el as FamilyInstance) is FamilyInstance) { FamilyInstance instance2 = el as FamilyInstance; Element hostelement = instance2.Host; curve = GetLineFromLcPoint(GetSolid(hostelement), instance2); } else if (el.GetType() == typeof(Floor)) { Floor floor = el as Floor; Face face = GetGeometryFace(GetSolid(el)); var test2 = GetCurveFromFloor(face, add2[b].Curve); if (test2.Size < 2) { break; } XYZ[] xYZsn = { test2.get_Item(0).GetEndPoint(0), test2.get_Item(1).GetEndPoint(0) }; Line l3 = Line.CreateBound(test2.get_Item(0).GetEndPoint(0), test2.get_Item(1).GetEndPoint(0)); curve = l3 as Curve; } } else { curve = (el.Location as LocationCurve).Curve; } if (curve == null) { break; } XYZ p0 = li.Project(curve.GetEndPoint(1)) .XYZPoint; if (!symbol.IsActive) { symbol.Activate(); } FamilyInstance instance = doc.Create.NewFamilyInstance(p0, symbol, level, Autodesk.Revit.DB.Structure.StructuralType.UnknownFraming); var l = GetGeometryLine(GetGeometryFace(GetSolid(instance))); double an = GetAngle(li, l); double rotateangle; double e = 1e-12; double a = Math.Round((an - Math.PI / 4), 13); double c = Math.Round(Math.Abs((an - Math.PI * 3 / 2)), 13); if (a != e || c != Math.Round(Math.PI * 3 / 2, 13)) //判断度数,相减做判断 { if (an > Math.PI * 0.5) { rotateangle = 0.75 * Math.PI - an; } else { rotateangle = an + 0.25 * Math.PI; } LocationPoint lp = instance.Location as LocationPoint; if (lp != null) { Line line3 = Line.CreateBound(new XYZ(lp.Point.X, lp.Point.Y, 0), new XYZ(lp.Point.X, lp.Point.Y, lp.Point.Z + 20)); lp.Rotate(line3, rotateangle); XYZ pNew = (instance.Location as LocationPoint).Point; if (p0.X != pNew.X || p0.Y != pNew.Y || p0.Z != pNew.Z) { lp.Point = p0; } //旋转基于平面与原点位置,对照两个位置将旋转后族放置到指定位置 } } } } trans.Commit(); } } //逐点标注 if (add.Count != 0) //普通尺寸标注 { using (Transaction trans = new Transaction(doc)) //将本身的Dimension样式修改未无对角线样式 { FailureHandlingOptions failureHandlingOptions = trans.GetFailureHandlingOptions(); FailureHandler failureHandler = new FailureHandler(); failureHandlingOptions.SetFailuresPreprocessor(failureHandler); // 这句话是关键 failureHandlingOptions.SetClearAfterRollback(true); trans.SetFailureHandlingOptions(failureHandlingOptions); trans.Start("Text"); ChangeDimensionTypeStyle(add.First <Dimension>(), doc); trans.Commit(); } for (int a = 0; a < add.Count; a++) { referarray = add.ElementAt <Dimension>(a).References; if (referarray.Size == 2) { foreach (Reference re in referarray) { var cu = add.ElementAt <Dimension>(a).Curve; Dimension dim = add.ElementAt <Dimension>(a); Line li = cu as Line; //var n = re.GetType().ToString(); Element el = doc.GetElement(re); Curve curve = null; using (Transaction test = new Transaction(doc, "change")) { FailureHandlingOptions failureHandlingOptions = test.GetFailureHandlingOptions(); FailureHandler failureHandler = new FailureHandler(); failureHandlingOptions.SetFailuresPreprocessor(failureHandler); // 这句话是关键 failureHandlingOptions.SetClearAfterRollback(true); test.SetFailureHandlingOptions(failureHandlingOptions); test.Start(); if (el.Location.GetType() != typeof(LocationCurve)) { if (el.GetType() == typeof(Grid)) { curve = (el as Grid).Curve; } else if ((el as FamilyInstance) is FamilyInstance) { FamilyInstance instance2 = el as FamilyInstance; Element hostelement = instance2.Host; curve = GetLineFromLcPoint(GetSolid(hostelement), instance2); } else if (el.GetType() == typeof(Floor)) { Floor floor = el as Floor; Face face = GetGeometryFace(GetSolid(el)); var test2 = GetCurveFromFloor(face, add[a].Curve); if (test2.Size < 2) { break; } XYZ[] xYZsn = { test2.get_Item(0).GetEndPoint(0), test2.get_Item(1).GetEndPoint(0) }; Line l = Line.CreateBound(test2.get_Item(0).GetEndPoint(0), test2.get_Item(1).GetEndPoint(0)); curve = l as Curve; } if (curve == null) { break; } XYZ[] xYZs = { curve.GetEndPoint(0), curve.GetEndPoint(1) }; foreach (XYZ xyz in xYZs) { XYZ p0 = li.Project(xyz).XYZPoint; if (!symbol.IsActive) { symbol.Activate(); } FamilyInstance instance = doc.Create.NewFamilyInstance(p0, symbol, level, Autodesk.Revit.DB.Structure.StructuralType.UnknownFraming); var l = GetGeometryLine(GetGeometryFace(GetSolid(instance))); double an = GetAngle(li, l); if (an != Math.PI / 4 || an != Math.PI * 3 / 2) { LocationPoint lp = instance.Location as LocationPoint; if (lp != null) { Line line3 = Line.CreateBound(new XYZ(lp.Point.X, lp.Point.Y, 0), new XYZ(lp.Point.X, lp.Point.Y, lp.Point.Z + 20)); lp.Rotate(line3, 0.25 * Math.PI + (Math.PI + an)); XYZ pNew = (instance.Location as LocationPoint).Point; if (p0.X != pNew.X || p0.Y != pNew.Y || p0.Z != pNew.Z) { lp.Point = p0; } //旋转基于平面与原点位置,对照两个位置将旋转后族放置到指定位置 } } } } else { curve = (el.Location as LocationCurve).Curve; if (doc.GetElement(referarray.get_Item(0)).Id == doc.GetElement(referarray.get_Item(1)).Id) { var cu4 = add.ElementAt <Dimension>(a).Curve; Dimension dim4 = add.ElementAt <Dimension>(a); Line li4 = cu as Line; //var n = re.GetType().ToString(); Element el4 = doc.GetElement(referarray.get_Item(0)); Curve curve4 = (el4.Location as LocationCurve).Curve; XYZ[] xyzs2 = { curve4.GetEndPoint(0), curve4.GetEndPoint(1) }; foreach (XYZ xyz in xyzs2) { XYZ p0 = li.Project(xyz).XYZPoint; if (!symbol.IsActive) { symbol.Activate(); } FamilyInstance instance = doc.Create.NewFamilyInstance(p0, symbol, level, Autodesk.Revit.DB.Structure.StructuralType.UnknownFraming); var l = GetGeometryLine(GetGeometryFace(GetSolid(instance))); double an = GetAngle(li, l); if (an != Math.PI / 4 || an != Math.PI * 3 / 2) { LocationPoint lp = instance.Location as LocationPoint; if (lp != null) { Line line3 = Line.CreateBound(new XYZ(lp.Point.X, lp.Point.Y, 0), new XYZ(lp.Point.X, lp.Point.Y, lp.Point.Z + 20)); lp.Rotate(line3, 0.25 * Math.PI + (Math.PI + an)); XYZ pNew = (instance.Location as LocationPoint).Point; if (p0.X != pNew.X || p0.Y != pNew.Y || p0.Z != pNew.Z) { lp.Point = p0; } //旋转基于平面与原点位置,对照两个位置将旋转后族放置到指定位置 } } } } else { foreach (Reference refer in referarray) { //string re = refer.ElementReferenceType.ToString(); var cu3 = add.ElementAt <Dimension>(a).Curve; Dimension dim3 = add.ElementAt <Dimension>(a); Line li3 = cu as Line; //var n = re.GetType().ToString(); Element el3 = doc.GetElement(refer); Curve curve3 = null; if (el3.Location.GetType() != typeof(LocationCurve)) { if (el3.GetType() == typeof(Grid)) { curve3 = (el3 as Grid).Curve; } else if ((el3 as FamilyInstance) is FamilyInstance) { FamilyInstance instance3 = el3 as FamilyInstance; Element hostelement = instance3.Host; curve3 = GetLineFromLcPoint(GetSolid(hostelement), instance3); } else if (el3.GetType() == typeof(Floor)) { Floor floor = el3 as Floor; Face face = GetGeometryFace(GetSolid(el3)); var test2 = GetCurveFromFloor(face, add2[a].Curve); if (test2.Size < 2) { break; } XYZ[] xYZsn = { test2.get_Item(0).GetEndPoint(0), test2.get_Item(1).GetEndPoint(0) }; Line l4 = Line.CreateBound(test2.get_Item(0).GetEndPoint(0), test2.get_Item(1).GetEndPoint(0)); curve3 = l4 as Curve; } } else { curve3 = (el3.Location as LocationCurve).Curve; } if (curve3 == null) { break; } XYZ p0 = li3.Project(curve.GetEndPoint(1)) .XYZPoint; if (!symbol.IsActive) { symbol.Activate(); } FamilyInstance instance = doc.Create.NewFamilyInstance(p0, symbol, level, Autodesk.Revit.DB.Structure.StructuralType.UnknownFraming); var l = GetGeometryLine(GetGeometryFace(GetSolid(instance))); double an = GetAngle(li, l); if (an != Math.PI / 4 || an != Math.PI * 3 / 2) { LocationPoint lp = instance.Location as LocationPoint; if (lp != null) { Line line3 = Line.CreateBound(new XYZ(lp.Point.X, lp.Point.Y, 0), new XYZ(lp.Point.X, lp.Point.Y, lp.Point.Z + 20)); lp.Rotate(line3, 0.25 * Math.PI + (Math.PI + an)); XYZ pNew = (instance.Location as LocationPoint).Point; if (p0.X != pNew.X || p0.Y != pNew.Y || p0.Z != pNew.Z) { lp.Point = p0; } //旋转基于平面与原点位置,对照两个位置将旋转后族放置到指定位置 } } } } } test.Commit(); } } } } } //连续标注 return(Result.Succeeded); }
/// <summary> /// Returns the percentage number of gridlines dimensioned in the drawing. If all gridlines are dimensioned the value should be 100%. /// </summary> /// <param name="sheets">sheets to check</param> /// <returns name="results">count of datums in each sheet</returns> /// <search> /// mace, structural, general, arrangement, drawing, datum, count, number /// </search> public static object PercentageOfDimensionedGridlines(Revit.Elements.Views.Sheet sheets) { Document doc = RevitServices.Persistence.DocumentManager.Instance.CurrentDBDocument; Autodesk.Revit.DB.Element unwrappedSheet = sheets.InternalElement; BuiltInCategory dimCat = BuiltInCategory.OST_Dimensions; BuiltInCategory gridCat = BuiltInCategory.OST_Grids; //Get views on sheet Autodesk.Revit.DB.ViewSheet viewSheet = unwrappedSheet as Autodesk.Revit.DB.ViewSheet; List <ElementId> viewIds = viewSheet.GetAllPlacedViews().ToList(); ElementId viewId = viewIds[0]; FilteredElementCollector dimElements = new FilteredElementCollector(doc, viewId).OfCategory(dimCat).WhereElementIsNotElementType(); FilteredElementCollector gridElements = new FilteredElementCollector(doc, viewId).OfCategory(gridCat).WhereElementIsNotElementType(); List <Autodesk.Revit.DB.Dimension> dimList = new List <Autodesk.Revit.DB.Dimension>(); foreach (Autodesk.Revit.DB.Dimension dim in dimElements) { dimList.Add(dim as Autodesk.Revit.DB.Dimension); } List <int> gridList = new List <int>(); foreach (Autodesk.Revit.DB.Element grid in gridElements) { gridList.Add(grid.Id.IntegerValue); } List <int> dimRefs = new List <int>(); foreach (Autodesk.Revit.DB.Dimension d in dimList) { ReferenceArray refArray = d.References; int size = refArray.Size; RevitServices.Transactions.TransactionManager.Instance.EnsureInTransaction(doc); for (int i = 0; i < size; i++) { Reference _ref = refArray.get_Item(i); ElementId id = _ref.ElementId; //Autodesk.Revit.DB.Element revitElement = doc.GetElement(id); dimRefs.Add(id.IntegerValue); } } List <int> uniqueIds = new HashSet <int>(dimRefs).ToList(); List <bool> bools = new List <bool>(); foreach (int id in gridList) { if (uniqueIds.Contains(id)) { bools.Add(true); } else { bools.Add(false); } } double countTrue = bools.Where(c => c).Count(); double countTotal = bools.Count(); double num = (countTrue / countTotal) * 100; decimal dec = System.Convert.ToDecimal(num); decimal round = System.Math.Round(dec, 1); string output = sheets.SheetNumber.ToString() + " - " + sheets.SheetName.ToString() + " = " + round.ToString() + "%"; return(output); }