public bool ResizeElement() { List <Solid> solidList = GetSolid(); if (solidList.Count == 0) { return(false); } FaceArray faceArray = solidList[0].Faces; List <double> areaList = new List <double>(faceArray.Size); int top = 0; int bottom = 0; for (int i = 0; i < faceArray.Size; i++) { areaList.Add(faceArray.get_Item(i).Area); if (i > 0) { if (areaList[i] > areaList[top]) { top = i; } } } for (int i = 0; i < areaList.Count; i++) { if (i == top) { continue; } if (Math.Abs(areaList[i] - areaList[top]) < 0.001) { bottom = i; } } Face topFace = faceArray.get_Item(top); Face bottomFace = faceArray.get_Item(bottom); FamilyInstance instance = element as FamilyInstance; if (null != instance) { Transform transform = instance.GetTransform(); transform.ScaleBasis(2); } return(true); }
//在族实例上定位连接点 private XYZ LocatePointOnFamilyInstance(FamilyInstance fi, Direction dir) { //找到工井族文件中左边界或右边界的几何体,利用id定位 Options opt = new Options(); opt.IncludeNonVisibleObjects = true;//很重要,由于view为null需要将不可见的对象变为可见,否则提取空集 GeometryElement ge = null; switch (dir) { case (Direction.left): ge = welldoc.GetElement(new ElementId(leftId)).get_Geometry(opt); break; case (Direction.right): ge = welldoc.GetElement(new ElementId(rightId)).get_Geometry(opt); break; } //提取集合体上的面元素 Solid solid = null; foreach (GeometryObject obj in ge) { if (obj is Solid) { solid = obj as Solid; break; } } FaceArray fcArr = null; if (solid != null) { fcArr = solid.Faces; } //找到边界的面,并将面原点的族坐标转换为模型坐标 Transform trans = null; Options opt1 = new Options(); opt1.ComputeReferences = false; opt1.View = doc.ActiveView; GeometryElement geoElement = fi.get_Geometry(opt1); foreach (GeometryObject obj in geoElement) //利用GeometryInstrance的Transform提取坐标转换矩阵 { if (obj is GeometryInstance) { GeometryInstance inst = obj as GeometryInstance; trans = inst.Transform; break; } } EdgeArray edArr = solid.Edges; XYZ planeOrigin = (fcArr.get_Item(0) as PlanarFace).Origin; return(trans.OfPoint(new XYZ(planeOrigin.X, planeOrigin.Y + edArr.get_Item(0).ApproximateLength / 2, planeOrigin.Z - edArr.get_Item(1).ApproximateLength / 2))); }
bool GetBoundary(List <XYZ> ploygons, Solid solid) { PlanarFace heightest = null; FaceArray faceArrays = solid.Faces; heightest = faceArrays.get_Item(0) as PlanarFace; foreach (Face face in faceArrays) { //比较表面原点的Z轴确定最高点 PlanarFace pf = face as PlanarFace; if (null != pf && IsHorizontal(pf)) { if (null == heightest && pf.Origin.Z > heightest.Origin.Z) { heightest = pf; } } } if (null != heightest) { EdgeArrayArray loops = heightest.EdgeLoops; foreach (EdgeArray loop in loops) { foreach (Edge edge in loop) { IList <XYZ> points = edge.Tessellate(); foreach (var point in points) { bool isEqual = false; foreach (var item in ploygons) //去除相同的顶点 { isEqual = IsEqualXYZ(item, point); } if (!isEqual) { ploygons.Add(ResetPoint(point)); } } } } } return(null != heightest); }
public override Value Evaluate(FSharpList <Value> args) { var crv = (Curve)((Value.Container)args[0]).Item; Face face = null; Solid tempSolid = null; Plane thisPlane = null; if (((Value.Container)args[1]).Item is Face) { face = (Autodesk.Revit.DB.Face)((Value.Container)args[1]).Item; } else if (((Value.Container)args[1]).Item is Plane) { thisPlane = ((Value.Container)args[1]).Item as Plane; // tesselate curve and find uv envelope in projection to the plane IList <XYZ> tessCurve = crv.Tessellate(); var curvePointEnum = tessCurve.GetEnumerator(); XYZ corner1 = new XYZ(); XYZ corner2 = new XYZ(); bool cornersSet = false; for (; curvePointEnum.MoveNext();) { if (!cornersSet) { corner1 = curvePointEnum.Current; corner2 = curvePointEnum.Current; cornersSet = true; } else { for (int coord = 0; coord < 3; coord++) { if (corner1[coord] > curvePointEnum.Current[coord]) { corner1 = new XYZ(coord == 0 ? curvePointEnum.Current[coord] : corner1[coord], coord == 1 ? curvePointEnum.Current[coord] : corner1[coord], coord == 2 ? curvePointEnum.Current[coord] : corner1[coord]); } if (corner2[coord] < curvePointEnum.Current[coord]) { corner2 = new XYZ(coord == 0 ? curvePointEnum.Current[coord] : corner2[coord], coord == 1 ? curvePointEnum.Current[coord] : corner2[coord], coord == 2 ? curvePointEnum.Current[coord] : corner2[coord]); } } } } double dist1 = thisPlane.Origin.DistanceTo(corner1); double dist2 = thisPlane.Origin.DistanceTo(corner2); double sizeRect = 2.0 * (dist1 + dist2) + 100.0; CurveLoop cLoop = new CurveLoop(); for (int index = 0; index < 4; index++) { double coord0 = (index == 0 || index == 3) ? -sizeRect : sizeRect; double coord1 = (index < 2) ? -sizeRect : sizeRect; XYZ pnt0 = thisPlane.Origin + coord0 * thisPlane.XVec + coord1 * thisPlane.YVec; double coord3 = (index < 2) ? sizeRect : -sizeRect; double coord4 = (index == 0 || index == 3) ? -sizeRect : sizeRect; XYZ pnt1 = thisPlane.Origin + coord3 * thisPlane.XVec + coord4 * thisPlane.YVec; Line cLine = dynRevitSettings.Revit.Application.Create.NewLineBound(pnt0, pnt1); cLoop.Append(cLine); } List <CurveLoop> listCLoops = new List <CurveLoop> (); listCLoops.Add(cLoop); tempSolid = GeometryCreationUtilities.CreateExtrusionGeometry(listCLoops, thisPlane.Normal, 100.0); //find right face FaceArray facesOfExtrusion = tempSolid.Faces; for (int indexFace = 0; indexFace < facesOfExtrusion.Size; indexFace++) { Face faceAtIndex = facesOfExtrusion.get_Item(indexFace); if (faceAtIndex is PlanarFace) { PlanarFace pFace = faceAtIndex as PlanarFace; if (Math.Abs(thisPlane.Normal.DotProduct(pFace.Normal)) < 0.99) { continue; } if (Math.Abs(thisPlane.Normal.DotProduct(thisPlane.Origin - pFace.Origin)) > 0.1) { continue; } face = faceAtIndex; break; } } if (face == null) { throw new Exception("Curve Face Intersection could not process supplied Plane."); } } IntersectionResultArray xsects = new IntersectionResultArray(); SetComparisonResult result = face.Intersect(crv, out xsects); var xsect_results = FSharpList <Value> .Empty; var results = FSharpList <Value> .Empty; if (xsects != null) { foreach (IntersectionResult ir in xsects) { var xsect = FSharpList <Value> .Empty; try { xsect = FSharpList <Value> .Cons(Value.NewNumber(ir.EdgeParameter), xsect); } catch { xsect = FSharpList <Value> .Cons(Value.NewNumber(0), xsect); } xsect = FSharpList <Value> .Cons(Value.NewContainer(ir.EdgeObject), xsect); xsect = FSharpList <Value> .Cons(Value.NewNumber(ir.Parameter), xsect); if (thisPlane != null) { UV planeUV = new UV(thisPlane.XVec.DotProduct(ir.XYZPoint - thisPlane.Origin), thisPlane.YVec.DotProduct(ir.XYZPoint - thisPlane.Origin)); xsect = FSharpList <Value> .Cons(Value.NewContainer(planeUV), xsect); } else { xsect = FSharpList <Value> .Cons(Value.NewContainer(ir.UVPoint), xsect); } xsect = FSharpList <Value> .Cons(Value.NewContainer(ir.XYZPoint), xsect); xsect_results = FSharpList <Value> .Cons(Value.NewList(xsect), xsect_results); } } results = FSharpList <Value> .Cons(Value.NewList(xsect_results), results); results = FSharpList <Value> .Cons(Value.NewString(result.ToString()), results); return(Value.NewList(results)); }
//Create family for each building //http://thebuildingcoder.typepad.com/blog/2011/06/creating-and-inserting-an-extrusion-family.html private bool CreateFamilyFile(ProcessPolygon polygon, double height, string familyFileName, XYZ translation) { bool success = true; Document FamDoc = null; Autodesk.Revit.DB.Form form = null; using (Transaction CreateFamily = new Transaction(_doc)) { CreateFamily.Start("Create a new Family"); FamDoc = _doc.Application.NewFamilyDocument(this.FamilyTemplateAddress); CreateFamily.Commit(); } ReferenceArray refAr = polygon.Get_ReferenceArray(FamDoc, translation); using (Transaction CreateExtrusion = new Transaction(FamDoc)) { FailureHandlingOptions failOpt = CreateExtrusion.GetFailureHandlingOptions(); failOpt.SetFailuresPreprocessor(new WarningSwallower()); CreateExtrusion.SetFailureHandlingOptions(failOpt); CreateExtrusion.Start("Create Extrusion"); /*Mohammad took this out of try */ try { form = FamDoc.FamilyCreate.NewExtrusionForm(true, refAr, height * XYZ.BasisZ); } catch (Exception) { this.FailedAttemptsToCreateBuildings++; success = false; } CreateExtrusion.Commit(); } //Added by Mohammad using (Transaction AddParamTrans = new Transaction(FamDoc)) { AddParamTrans.Start("Add Parameter"); Autodesk.Revit.ApplicationServices.Application app = FamDoc.Application; View3D view3d = createView3d(); Dimension windowInsetDimension = null; FaceArray faces = new FaceArray(); if (form.IsSolid) { Options options = new Options(); options.ComputeReferences = true; //options.View = new Autodesk.Revit.DB.View(); //GeometryObjectArray geoArr = extrusion.get_Geometry(options).Objects; IEnumerator <GeometryObject> Objects = form.get_Geometry(options).GetEnumerator(); //foreach (GeometryObject geoObj in geoArr) while (Objects.MoveNext()) { GeometryObject geoObj = Objects.Current; if (geoObj is Solid) { Solid s = geoObj as Solid; foreach (Face fc in s.Faces) { //MessageBox.Show(fc.ComputeNormal(new UV(0, 0)).X.ToString() + "/n" + fc.ComputeNormal(new UV(0, 0)).Y.ToString() + "/n" + fc.ComputeNormal(new UV(0, 0)).Z.ToString()); if (Math.Round((fc.ComputeNormal(new UV(0, 0))).Z) == 1 || Math.Round((fc.ComputeNormal(new UV(0, 0))).Z) == -1) { faces.Append(fc); } } //************************************************************************************************************** //****************************Here is the Error ********************************************************************** //************************************************************************************************************************ //windowInsetDimension = AddDimension( FamDoc, view3d, faces.get_Item( 0 ), faces.get_Item( 1 ) ); View viewElevation = new FilteredElementCollector(FamDoc).OfClass(typeof(View)).Cast <View>().Where <View>(v => ViewType.Elevation == v.ViewType).FirstOrDefault <View>(); windowInsetDimension = AddDimension(FamDoc, viewElevation, faces.get_Item(0), faces.get_Item(1)); } } } //Test for creating dimension #region two lines creating dimenstion //// first create two lines //XYZ pt1 = new XYZ(5, 0, 5); //XYZ pt2 = new XYZ(5, 0, 10); //Line line = Line.CreateBound(pt1, pt2); //Plane plane = app.Create.NewPlane(pt1.CrossProduct(pt2), pt2); //SketchPlane skplane = SketchPlane.Create(FamDoc, plane); //ModelCurve modelcurve1 = FamDoc.FamilyCreate.NewModelCurve(line, skplane); //pt1 = new XYZ(10, 0, 5); //pt2 = new XYZ(10, 0, 10); //line = Line.CreateBound(pt1, pt2); //plane = app.Create.NewPlane(pt1.CrossProduct(pt2), pt2); //skplane = SketchPlane.Create(FamDoc, plane); //ModelCurve modelcurve2 = FamDoc.FamilyCreate.NewModelCurve(line, skplane); //// now create a linear dimension between them //ReferenceArray ra = new ReferenceArray(); //ra.Append(modelcurve1.GeometryCurve.Reference); //ra.Append(modelcurve2.GeometryCurve.Reference); //pt1 = new XYZ(5, 0, 10); //pt2 = new XYZ(10, 0, 10); //line = Line.CreateBound(pt1, pt2); //Dimension dim = FamDoc.FamilyCreate.NewLinearDimension(view3d, line, ra); #endregion //creates a prameter named index for each family. BuiltInParameterGroup paramGroup = (BuiltInParameterGroup)Enum.Parse(typeof(BuiltInParameterGroup), "PG_GENERAL"); ParameterType paramType = (ParameterType)Enum.Parse(typeof(ParameterType), "Length"); FamilyManager m_manager = FamDoc.FamilyManager; FamilyParameter famParam = m_manager.AddParameter("Height", paramGroup, paramType, true); //Set the value for the parameter if (m_manager.Types.Size == 0) { m_manager.NewType("Type 1"); } m_manager.Set(famParam, height); //connects dimension to lable called with //windowInsetDimension.FamilyLabel = famParam; AddParamTrans.Commit(); } SaveAsOptions opt = new SaveAsOptions(); opt.OverwriteExistingFile = true; FamDoc.SaveAs(familyFileName, opt); FamDoc.Close(false); return(success); }
public override Value Evaluate(FSharpList <Value> args) { if (_reference.ElementReferenceType != ElementReferenceType.REFERENCE_TYPE_SURFACE && _reference.ElementReferenceType != ElementReferenceType.REFERENCE_TYPE_LINEAR) { ElementId refElementId = _reference.ElementId; Element refElement = dynRevitSettings.Doc.Document.GetElement(refElementId); if (refElement is ReferencePoint) { ReferencePoint rp = refElement as ReferencePoint; XYZ rpXYZ = rp.Position; return(Value.NewContainer(rpXYZ)); } GeometryObject thisObjectPoint = SelectedElement.GetGeometryObjectFromReference(_reference); if (!(thisObjectPoint is Autodesk.Revit.DB.Point)) { throw new Exception("Could not use face or edge which is not part of the model"); } var thisPoint = thisObjectPoint as Autodesk.Revit.DB.Point; XYZ pointXYZ = thisPoint.Coord; return(Value.NewContainer(pointXYZ)); } GeometryObject thisObject = SelectedElement.GetGeometryObjectFromReference(_reference); Autodesk.Revit.DB.Transform thisTrf = null; if (_init && (old_refXyz == null || !_reference.Equals(old_refXyz))) { _init = false; } { GeometryObject geomObj = SelectedElement.get_Geometry(new Options()); var geomElement = geomObj as GeometryElement; // ugly code to detect if transform for geometry object is needed or not // filed request to provide this info via API, but meanwhile ... foreach (GeometryObject geob in geomElement) { if (!(geob is GeometryInstance)) { continue; } var ginsta = geob as GeometryInstance; GeometryElement gSymbolElement = ginsta.GetSymbolGeometry(); var geometryElements = new List <GeometryElement> { gSymbolElement }; bool found = false; for (; geometryElements.Count > 0 && !found;) { GeometryElement thisGeometryElement = geometryElements[0]; geometryElements.Remove(thisGeometryElement); foreach (GeometryObject geobSym in thisGeometryElement) { if (geobSym is GeometryElement) { geometryElements.Add(geobSym as GeometryElement); continue; } if ((thisObject is Curve) && (geobSym is Curve) && (thisObject == geobSym)) { found = true; break; } if (thisObject is Curve) { continue; } if ((thisObject is Autodesk.Revit.DB.Face) && (geobSym is Autodesk.Revit.DB.Face) && (thisObject == geobSym)) { found = true; break; } if ((thisObject is Edge) && (geobSym is Autodesk.Revit.DB.Face)) { var edge = thisObject as Edge; //use GetFace after r2013 support is dropped if (geobSym == edge.get_Face(0) || geobSym == edge.get_Face(1)) { found = true; break; } } if (!(geobSym is Solid)) { continue; } FaceArray solidFaces = ((Solid)geobSym).Faces; int numFaces = solidFaces.Size; for (int index = 0; index < numFaces && !found; index++) { Autodesk.Revit.DB.Face faceAt = solidFaces.get_Item(index); if ((thisObject is Autodesk.Revit.DB.Face) && (thisObject == faceAt)) { found = true; break; } if (thisObject is Edge) { var edge = thisObject as Edge; //use GetFace after r2013 support is dropped if (faceAt == edge.get_Face(0) || faceAt == edge.get_Face(1)) { found = true; break; } } } } } if (found) { thisTrf = ginsta.Transform; break; } } if (thisObject == null) { throw new Exception("could not resolve reference for XYZ on Element"); } } XYZ thisXYZ; if (_reference.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE && thisObject is Autodesk.Revit.DB.Face) { var face = thisObject as Autodesk.Revit.DB.Face; if (!_init) { _param0 = _reference.UVPoint[0]; _param1 = _reference.UVPoint[1]; _init = true; } var uv = new UV(_param0, _param1); thisXYZ = face.Evaluate(uv); if (thisTrf != null) { thisXYZ = thisTrf.OfPoint(thisXYZ); } } else if (_reference.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_LINEAR) { Curve curve; if (thisObject is Edge) { var edge = (Edge)SelectedElement.GetGeometryObjectFromReference(_reference); curve = edge.AsCurve(); } else { curve = (Curve)SelectedElement.GetGeometryObjectFromReference(_reference); } if (curve != null) { if (_init) { thisXYZ = curve.Evaluate(_param0, true); } else { XYZ curPoint = _reference.GlobalPoint; if (thisTrf != null) { Autodesk.Revit.DB.Transform inverseTrf = thisTrf.Inverse; curPoint = inverseTrf.OfPoint(_reference.GlobalPoint); } IntersectionResult thisResult = curve.Project(curPoint); _param0 = curve.ComputeNormalizedParameter(thisResult.Parameter); _init = true; } thisXYZ = curve.Evaluate(_param0, true); _param1 = -1.0; } else { throw new Exception("could not evaluate point on face or edge of the element"); } if (thisTrf != null) { thisXYZ = thisTrf.OfPoint(thisXYZ); } } else { throw new Exception("could not evaluate point on face or edge of the element"); } old_refXyz = _reference; return(Value.NewContainer(thisXYZ)); }
public static Face buildFaceOnPlaneByCurveExtensions(Curve crv, Autodesk.Revit.DB.Plane thisPlane) { Face face = null; // tesselate curve and find uv envelope in projection to the plane IList <XYZ> tessCurve = crv.Tessellate(); var curvePointEnum = tessCurve.GetEnumerator(); var corner1 = new XYZ(); var corner2 = new XYZ(); bool cornersSet = false; for (; curvePointEnum.MoveNext();) { if (!cornersSet) { corner1 = curvePointEnum.Current; corner2 = curvePointEnum.Current; cornersSet = true; } else { for (int coord = 0; coord < 3; coord++) { if (corner1[coord] > curvePointEnum.Current[coord]) { corner1 = new XYZ(coord == 0 ? curvePointEnum.Current[coord] : corner1[coord], coord == 1 ? curvePointEnum.Current[coord] : corner1[coord], coord == 2 ? curvePointEnum.Current[coord] : corner1[coord]); } if (corner2[coord] < curvePointEnum.Current[coord]) { corner2 = new XYZ(coord == 0 ? curvePointEnum.Current[coord] : corner2[coord], coord == 1 ? curvePointEnum.Current[coord] : corner2[coord], coord == 2 ? curvePointEnum.Current[coord] : corner2[coord]); } } } } double dist1 = thisPlane.Origin.DistanceTo(corner1); double dist2 = thisPlane.Origin.DistanceTo(corner2); double sizeRect = 2.0 * (dist1 + dist2) + 100.0; var cLoop = new Autodesk.Revit.DB.CurveLoop(); for (int index = 0; index < 4; index++) { double coord0 = (index == 0 || index == 3) ? -sizeRect : sizeRect; double coord1 = (index < 2) ? -sizeRect : sizeRect; XYZ pnt0 = thisPlane.Origin + coord0 * thisPlane.XVec + coord1 * thisPlane.YVec; double coord3 = (index < 2) ? sizeRect : -sizeRect; double coord4 = (index == 0 || index == 3) ? -sizeRect : sizeRect; XYZ pnt1 = thisPlane.Origin + coord3 * thisPlane.XVec + coord4 * thisPlane.YVec; Line cLine = dynRevitSettings.Revit.Application.Create.NewLineBound(pnt0, pnt1); cLoop.Append(cLine); } var listCLoops = new List <Autodesk.Revit.DB.CurveLoop> { cLoop }; Solid tempSolid = GeometryCreationUtilities.CreateExtrusionGeometry(listCLoops, thisPlane.Normal, 100.0); //find right face FaceArray facesOfExtrusion = tempSolid.Faces; for (int indexFace = 0; indexFace < facesOfExtrusion.Size; indexFace++) { Face faceAtIndex = facesOfExtrusion.get_Item(indexFace); if (faceAtIndex is PlanarFace) { var pFace = faceAtIndex as PlanarFace; if (Math.Abs(thisPlane.Normal.DotProduct(pFace.Normal)) < 0.99) { continue; } if (Math.Abs(thisPlane.Normal.DotProduct(thisPlane.Origin - pFace.Origin)) > 0.1) { continue; } face = faceAtIndex; break; } } if (face == null) { throw new Exception("Curve Face Intersection could not process supplied Plane."); } return(face); }