private double getDimValueAngular() { UIDocument UiDoc = this.ActiveUIDocument; Document doc = UiDoc.Document; Reference myRef = UiDoc.Selection.PickObject(ObjectType.Element, new AngularDimFilter(), "Pick An Angular Dimension..."); AngularDimension angleDim = doc.GetElement(myRef) as AngularDimension; // Get infor from InputBox return(Math.Round((double)(angleDim.Value * 180 / Math.PI), 4)); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; Autodesk.Revit.DB.View view = doc.ActiveView; ViewType vt = view.ViewType; if (vt == ViewType.FloorPlan || vt == ViewType.Elevation) { #region /* * Reference eRef = uidoc.Selection.PickObject(ObjectType.Element, "Please pick a curve based element like wall."); * Element element = doc.GetElement(eRef); * if (eRef != null && element != null) * { * XYZ dirVec = new XYZ(); * //视图投影位置 * XYZ viewNormal = view.ViewDirection; * //元素的曲线位置 * LocationCurve locCurve = element.Location as LocationCurve; * if (locCurve == null || locCurve.Curve == null) * { * TaskDialog.Show("Prompt", "Selected element isn’t curve based!"); * // return Result.Cancelled; * } * //曲线开始坐标减去终点坐标并标准化 * XYZ dirCur = locCurve.Curve.GetEndPoint(0).Subtract(locCurve.Curve.GetEndPoint(1)).Normalize(); * //点积 * double d = dirCur.DotProduct(viewNormal); * //点积是否为0 即这两个向量是否垂直 * if (d > -0.000000001 && d < 0.000000001) * { * //交叉乘积 * dirVec = dirCur.CrossProduct(viewNormal); * XYZ p0 = locCurve.Curve.GetEndPoint(0); * XYZ p2 = locCurve.Curve.GetEndPoint(1); * * XYZ p1 = new XYZ(p0.X, p2.Y, 0); * XYZ dirLine = XYZ.Zero.Add(p0); * double distancep2 = p2.DistanceTo(p1); * double distancep0 = p0.DistanceTo(p1); * double slope = distancep2 / distancep0; * dirLine = dirLine.Subtract(p2); * //这几行主要设置标注的位置 * XYZ newVec = XYZ.Zero.Add(dirVec); * newVec = newVec.Normalize().Multiply(10); * p1 = p1.Add(newVec); * p2 = p2.Add(newVec); * // TaskDialog.Show("Prompt", p0.ToString()+ p2.ToString()+ p1.ToString()); * //end * Line newLine = Line.CreateBound(p1, p2); * ReferenceArray arrRefs = new ReferenceArray(); * * Options options = app.Create.NewGeometryOptions(); * options.ComputeReferences = true; * options.DetailLevel = ViewDetailLevel.Fine; * GeometryElement gelement = element.get_Geometry(options); * foreach (var geoObject in gelement) * { * Solid solid = geoObject as Solid; * if (solid == null) * continue; * * FaceArrayIterator fIt = solid.Faces.ForwardIterator(); * while (fIt.MoveNext()) * { * PlanarFace p = fIt.Current as PlanarFace; * if (p == null) * continue; * * p2 = p.FaceNormal.CrossProduct(dirLine); * if (p2.IsZeroLength()) * { * arrRefs.Append(p.Reference); * } * if (2 == arrRefs.Size) * { * break; * } * } * if (2 == arrRefs.Size) * { * break; * } * } * if (arrRefs.Size != 2) * { * TaskDialog.Show("Prompt", "Couldn’t find enough reference for creating dimension"); * //return Result.Cancelled; * } * * Transaction trans = new Transaction(doc, "create dimension"); * trans.Start(); * doc.Create.NewDimension(doc.ActiveView, newLine, arrRefs); * TextNote text = AddNewTextNote(uidoc,p0,0.2,slope.ToString()); * trans.Commit(); * } * else * { * TaskDialog.Show("Prompt", "Selected element isn’t curve based!"); * // return Result.Cancelled; * } * } * } * else * { * TaskDialog.Show("Prompt", "Only support Plan View or Elevation View"); * }*/ #endregion XYZ pt1 = XYZ.Zero; XYZ pt2 = new XYZ(10, 20, 0); XYZ pt3 = new XYZ(30, 10, 0); Line line1 = Line.CreateBound(pt1, pt2); Line line2 = Line.CreateBound(pt2, pt3); Transaction trans1 = new Transaction(doc, "creaerwqte dimension"); trans1.Start(); //辅助线 Line dummyLine = Line.CreateBound(XYZ.Zero, XYZ.BasisY); //辅助点 XYZ pt0 = new XYZ(pt3.X, pt1.Y, 0); Line newLine = Line.CreateBound(pt0, pt3); Line newLine2 = Line.CreateBound(pt0, pt1); DetailLine dummy = doc.Create.NewDetailCurve(view, dummyLine) as DetailLine; DetailLine detailLine1 = doc.Create.NewDetailCurve(view, line1) as DetailLine; DetailLine detailLine2 = doc.Create.NewDetailCurve(view, line2) as DetailLine; ReferenceArray arrRefs = new ReferenceArray(); arrRefs.Append(dummy.GeometryCurve.Reference); arrRefs.Append(detailLine1.GeometryCurve.GetEndPointReference(0)); arrRefs.Append(detailLine2.GeometryCurve.GetEndPointReference(1)); Dimension newdimension = doc.Create.NewDimension(view, newLine, arrRefs); Dimension newdimension2 = doc.Create.NewDimension(view, newLine2, arrRefs); List <Dimension> linearDimensions = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Dimensions) .Cast <Dimension>().Where(q => q.DimensionShape == DimensionShape.Linear).ToList(); newdimension2 = linearDimensions[0]; Arc arc = Arc.Create(pt1, pt3, pt2); IList <Reference> refe = new List <Reference>(); refe.Add(detailLine1.GeometryCurve.GetEndPointReference(0)); refe.Add(detailLine2.GeometryCurve.GetEndPointReference(1)); FilteredElementCollector DimesionTypeCollector = new FilteredElementCollector(doc); DimesionTypeCollector.OfClass(typeof(DimensionType)); DimensionType dimesionType = DimesionTypeCollector.Cast <DimensionType>().ToList().FirstOrDefault(); // DimensionType newdimesionType = dimesionType.Duplicate("new dimesionType") as DimensionType; // newdimesionType.LookupParameter("Color").Set(125); AngularDimension dd = AngularDimension.Create(doc, view, arc, refe, dimesionType); //删除辅助线 doc.Delete(dummy.Id); trans1.Commit(); } return(Result.Succeeded); }
internal static GeometryBase CreateGeometryHelper(IntPtr pGeometry, object parent, int subobjectIndex) { if (IntPtr.Zero == pGeometry) { return(null); } var type = UnsafeNativeMethods.ON_Geometry_GetGeometryType(pGeometry); if (type < 0) { return(null); } GeometryBase rc = null; switch (type) { case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Curve: //1 rc = new Curve(pGeometry, parent, subobjectIndex); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_NurbsCurve: //2 rc = new NurbsCurve(pGeometry, parent, subobjectIndex); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_PolyCurve: // 3 rc = new PolyCurve(pGeometry, parent, subobjectIndex); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_PolylineCurve: //4 rc = new PolylineCurve(pGeometry, parent, subobjectIndex); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_ArcCurve: //5 rc = new ArcCurve(pGeometry, parent, subobjectIndex); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_LineCurve: //6 rc = new LineCurve(pGeometry, parent, subobjectIndex); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Mesh: //7 rc = new Mesh(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Point: //8 rc = new Point(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_TextDot: //9 rc = new TextDot(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Surface: //10 rc = new Surface(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Brep: //11 rc = new Brep(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_NurbsSurface: //12 rc = new NurbsSurface(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_RevSurface: //13 rc = new RevSurface(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_PlaneSurface: //14 rc = new PlaneSurface(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_ClippingPlaneSurface: //15 rc = new ClippingPlaneSurface(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Hatch: // 17 rc = new Hatch(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_SumSurface: //19 rc = new SumSurface(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_BrepFace: //20 { int faceindex = -1; IntPtr ptr_brep = UnsafeNativeMethods.ON_BrepSubItem_Brep(pGeometry, ref faceindex); if (ptr_brep != IntPtr.Zero && faceindex >= 0) { Brep b = new Brep(ptr_brep, parent); rc = b.Faces[faceindex]; } } break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_BrepEdge: // 21 { int edgeindex = -1; IntPtr ptr_brep = UnsafeNativeMethods.ON_BrepSubItem_Brep(pGeometry, ref edgeindex); if (ptr_brep != IntPtr.Zero && edgeindex >= 0) { Brep b = new Brep(ptr_brep, parent); rc = b.Edges[edgeindex]; } } break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_InstanceReference: // 23 rc = new InstanceReferenceGeometry(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Extrusion: //24 rc = new Extrusion(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_PointCloud: // 26 rc = new PointCloud(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_DetailView: // 27 rc = new DetailView(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Light: //32 rc = new Light(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_PointGrid: //33 rc = new Point3dGrid(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_MorphControl: //34 rc = new MorphControl(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_BrepLoop: //35 { int loopindex = -1; IntPtr ptr_brep = UnsafeNativeMethods.ON_BrepSubItem_Brep(pGeometry, ref loopindex); if (ptr_brep != IntPtr.Zero && loopindex >= 0) { Brep b = new Brep(ptr_brep, parent); rc = b.Loops[loopindex]; } } break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_BrepTrim: // 36 { int trimindex = -1; IntPtr ptr_brep = UnsafeNativeMethods.ON_BrepSubItem_Brep(pGeometry, ref trimindex); if (ptr_brep != IntPtr.Zero && trimindex >= 0) { Brep b = new Brep(ptr_brep, parent); rc = b.Trims[trimindex]; } } break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Leader: // 38 rc = new Leader(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_SubD: // 39 rc = new SubD(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_DimLinear: //40 rc = new LinearDimension(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_DimAngular: //41 rc = new AngularDimension(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_DimRadial: //42 rc = new RadialDimension(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_DimOrdinate: //43 rc = new OrdinateDimension(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Centermark: //44 rc = new Centermark(pGeometry, parent); break; case UnsafeNativeMethods.OnGeometryTypeConsts.ON_Text: //45 rc = new TextEntity(pGeometry, parent); break; default: rc = new UnknownGeometry(pGeometry, parent, subobjectIndex); break; } return(rc); }