/// <summary> /// create a horizontal nurbspline instance with specified z coordinate value /// </summary> public NurbSpline CreateNurbSpline(double z) { // create control points with same z value List <XYZ> ctrPoints = new List <XYZ>(); Autodesk.Revit.DB.XYZ xyz1 = new Autodesk.Revit.DB.XYZ(-41.887503610431267, -9.0290629129782189, z); Autodesk.Revit.DB.XYZ xyz2 = new Autodesk.Revit.DB.XYZ(-9.27600019217055, 0.32213521486563046, z); Autodesk.Revit.DB.XYZ xyz3 = new Autodesk.Revit.DB.XYZ(9.27600019217055, 0.32213521486563046, z); Autodesk.Revit.DB.XYZ xyz4 = new Autodesk.Revit.DB.XYZ(41.887503610431267, 9.0290629129782189, z); ctrPoints.Add(xyz1); ctrPoints.Add(xyz2); ctrPoints.Add(xyz3); ctrPoints.Add(xyz4); IList <double> weights = new List <double>(); double w1 = 1, w2 = 1, w3 = 1, w4 = 1; weights.Add(w1); weights.Add(w2); weights.Add(w3); weights.Add(w4); IList <double> knots = new List <double>(); double k0 = 0, k1 = 0, k2 = 0, k3 = 0, k4 = 34.425128, k5 = 34.425128, k6 = 34.425128, k7 = 34.425128; knots.Add(k0); knots.Add(k1); knots.Add(k2); knots.Add(k3); knots.Add(k4); knots.Add(k5); knots.Add(k6); knots.Add(k7); NurbSpline detailNurbSpline = NurbSpline.Create(ctrPoints, weights, knots, 3, false, true); m_revit.ActiveUIDocument.Document.Regenerate(); return(detailNurbSpline); }
public override FScheme.Value Evaluate(FSharpList <FScheme.Value> args) { if (SelectedIndex < 0) { throw new Exception("Please select a nurbsCurve node."); } var node_name = Items[SelectedIndex].Name; Point3DCollection controlVertices; List <double> weights, knots; int degree; bool closed, rational; DynamoMaya.Contract.IService s = MayaCommunication.openChannelToMaya(); s.receiveCurveFromMaya(node_name, out controlVertices, out weights, out knots, out degree, out closed, out rational); MayaCommunication.closeChannelToMaya(s); List <XYZ> controlPoints = new List <XYZ>(); foreach (Point3D cv in controlVertices) { controlPoints.Add(new XYZ(cv.X, cv.Y, cv.Z)); } NurbSpline ns = NurbSpline.Create(controlPoints, weights, knots, degree, closed, true); return(FScheme.Value.NewContainer(ns)); }
/***************************************************/ public static oM.Geometry.NurbsCurve FromRevit(this HermiteSpline curve) { if (curve == null) { return(null); } return(NurbSpline.Create(curve).FromRevit()); }
public void CreateInDynamoModifyInRevitToCauseFailure() { //Create a wall in Dynamo string dynFilePath = Path.Combine(workingDirectory, @".\ElementBinding\CreateWallInDynamo.dyn"); string testPath = Path.GetFullPath(dynFilePath); ViewModel.OpenCommand.Execute(testPath); RunCurrentModel(); //Modify the wall in Revit using (var trans = new Transaction(DocumentManager.Instance.CurrentUIDocument.Document, "ModifyInRevit")) { bool hasError = false; trans.Start(); try { IList <Element> rps = GetAllWallElements(false); Assert.AreEqual(1, rps.Count); Wall wall = rps.First() as Wall; List <XYZ> ctrlPnts = new List <XYZ>(); ctrlPnts.Add(new XYZ(0.0, 1.0, 0.0)); ctrlPnts.Add(new XYZ(1.0, 0.0, 0.0)); ctrlPnts.Add(new XYZ(2.0, 0.0, 0.0)); ctrlPnts.Add(new XYZ(3.0, 1.0, 0.0)); List <double> weights = new List <double>(); weights.Add(1.0); weights.Add(1.0); weights.Add(1.0); weights.Add(1.0); var spline = NurbSpline.Create(ctrlPnts, weights); var wallLocation = wall.Location as LocationCurve; wallLocation.Curve = spline; } catch (Exception e) { hasError = true; trans.RollBack(); } if (!hasError) { trans.Commit(); } } RunCurrentModel(); IList <Element> rps2 = GetAllWallElements(false); Assert.AreEqual(1, rps2.Count); }
/// <summary> /// Converts a List of Grevit Curves to Revit Curves /// </summary> /// <param name="document">Active Document</param> /// <param name="grevitCurves">List of Grevit Curves</param> /// <returns>List of Revit Curves</returns> public static List <Curve> GrevitCurvesToRevitCurves(Component component, CoordinatesOverride coords = null) { List <Curve> curvesOut = new List <Curve>(); if (component.GetType() == typeof(Grevit.Types.Line)) { Grevit.Types.Line line = (Grevit.Types.Line)component; curvesOut.Add(Autodesk.Revit.DB.Line.CreateBound(line.from.ToXYZ(coords), line.to.ToXYZ(coords))); } else if (component.GetType() == typeof(Grevit.Types.Arc)) { Grevit.Types.Arc arc = (Grevit.Types.Arc)component; curvesOut.Add(Autodesk.Revit.DB.Arc.Create(arc.center.ToXYZ(coords), arc.radius, arc.start, arc.end, XYZ.BasisX, XYZ.BasisY)); } else if (component.GetType() == typeof(Grevit.Types.Curve3Points)) { Grevit.Types.Curve3Points curve3points = (Grevit.Types.Curve3Points)component; curvesOut.Add(Autodesk.Revit.DB.Arc.Create(curve3points.a.ToXYZ(coords), curve3points.c.ToXYZ(coords), curve3points.b.ToXYZ(coords))); } else if (component.GetType() == typeof(Grevit.Types.PLine)) { Grevit.Types.PLine pline = (Grevit.Types.PLine)component; for (int i = 0; i < pline.points.Count - 1; i++) { curvesOut.Add(Autodesk.Revit.DB.Line.CreateBound(pline.points[i].ToXYZ(coords), pline.points[i + 1].ToXYZ(coords))); } } else if (component.GetType() == typeof(Spline)) { Spline spline = (Spline)component; IList <XYZ> points = new List <XYZ>(); foreach (Grevit.Types.Point point in spline.controlPoints) { points.Add(point.ToXYZ(coords)); } #if (Revit2015 || Revit2016 || Revit2017) NurbSpline ns = NurbSpline.Create(points, spline.weights); #else NurbSpline ns = (NurbSpline)NurbSpline.CreateCurve(points, spline.weights); #endif ns.isClosed = spline.isClosed; curvesOut.Add(ns); } return(curvesOut); }
static internal Rhino.Geometry.Curve ToRhino(this Autodesk.Revit.DB.Curve curve) { switch (curve) { case Autodesk.Revit.DB.Line line: { return(line.IsBound ? new Rhino.Geometry.LineCurve(line.GetEndPoint(0).ToRhino(), line.GetEndPoint(1).ToRhino()) : null); } case Autodesk.Revit.DB.Arc arc: { var plane = new Rhino.Geometry.Plane(arc.Center.ToRhino(), new Vector3d(arc.XDirection.ToRhino()), new Vector3d(arc.YDirection.ToRhino())); if (arc.IsBound) { var p0 = arc.GetEndPoint(0).ToRhino(); var p1 = arc.Evaluate(0.5, true).ToRhino(); var p2 = arc.GetEndPoint(1).ToRhino(); return(new Rhino.Geometry.ArcCurve(new Rhino.Geometry.Arc(p0, p1, p2))); } else { return(new Rhino.Geometry.ArcCurve(new Rhino.Geometry.Circle(plane, arc.Radius))); } } case Autodesk.Revit.DB.Ellipse ellipse: { var plane = new Rhino.Geometry.Plane(ellipse.Center.ToRhino(), new Vector3d(ellipse.XDirection.ToRhino()), new Vector3d(ellipse.YDirection.ToRhino())); var e = new Rhino.Geometry.Ellipse(plane, ellipse.RadiusX, ellipse.RadiusY); var n = e.ToNurbsCurve(); if (ellipse.IsBound) { var t0 = Math.IEEERemainder(ellipse.GetEndParameter(0), 2.0 * Math.PI); var t1 = Math.IEEERemainder(ellipse.GetEndParameter(1), 2.0 * Math.PI); return(n.Trim(t0, t1)); } return(n); } case Autodesk.Revit.DB.HermiteSpline hermite: { return(NurbSpline.Create(hermite).ToRhino()); } case Autodesk.Revit.DB.NurbSpline nurb: { var controlPoints = nurb.CtrlPoints; var n = new Rhino.Geometry.NurbsCurve(3, nurb.isRational, nurb.Degree + 1, controlPoints.Count); if (nurb.isRational) { using (var Weights = nurb.Weights) { var weights = Weights.OfType <double>().ToArray(); int index = 0; foreach (var pt in controlPoints) { var w = weights[index]; n.Points.SetPoint(index++, pt.X * w, pt.Y * w, pt.Z * w, w); } } } else { int index = 0; foreach (var pt in controlPoints) { n.Points.SetPoint(index++, pt.X, pt.Y, pt.Z); } } using (var Knots = nurb.Knots) { int index = 0; foreach (var w in Knots.OfType <double>().Skip(1).Take(n.Knots.Count)) { n.Knots[index++] = w; } } return(n); } case Autodesk.Revit.DB.CylindricalHelix helix: // TODO : default: return(new Rhino.Geometry.PolylineCurve(curve.Tessellate().ToRhino())); } }
//绘制模型线 private void CreateCurve(XYZ startPoint, XYZ endPoint, XYZ normal1, XYZ normal2) { XYZ StartToEnd = new XYZ((endPoint - startPoint).X, (endPoint - startPoint).Y, 0); XYZ p_normal1 = new XYZ(normal1.X, normal1.Y, 0); XYZ p_normal2 = new XYZ(normal2.X, normal2.Y, 0); p_normal1 = p_normal1 / (Math.Sqrt(p_normal1.X * p_normal1.X + p_normal1.Y * p_normal1.Y)); p_normal2 = p_normal2 / (Math.Sqrt(p_normal2.X * p_normal2.X + p_normal2.Y * p_normal2.Y)); XYZ XoYprj_start = new XYZ(startPoint.X, startPoint.Y, 0); XYZ XoYprj_end = new XYZ(endPoint.X, endPoint.Y, 0); //在起点、终点间插值,并在z=0平面上绘制NurbSpline曲线 double[] doubleArray = { 1, 1, 1, 1, 1, 1 }; IList <XYZ> controlPoints2 = new List <XYZ>(); controlPoints2.Add(XoYprj_start); controlPoints2.Add(XoYprj_start + p_normal1 * mmToFeet(2000)); controlPoints2.Add(XoYprj_start + p_normal1 * mmToFeet(4000)); controlPoints2.Add(XoYprj_end + p_normal2 * mmToFeet(4000)); controlPoints2.Add(XoYprj_end + p_normal2 * mmToFeet(2000)); controlPoints2.Add(XoYprj_end); Curve nbLine = NurbSpline.Create(controlPoints2, doubleArray); //提取曲线上的拟合点,并在z轴方向插值拟合原曲线 IList <XYZ> ptsOnCurve = nbLine.Tessellate(); int ptCount = ptsOnCurve.Count; ReferencePointArray ptArr = new ReferencePointArray(); for (int i = 0; i < ptCount; i++) { XYZ pt = ptsOnCurve[i]; if (i < (ptCount - 1) / 8) { ptArr.Append(m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, startPoint.Z))); } else if (i > 7 * (ptCount - 1) / 8) { ptArr.Append(m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, endPoint.Z))); } else { ptArr.Append(m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, startPoint.Z + (i - (ptCount - 1) / 8) * (endPoint.Z - startPoint.Z) / (0.75 * (ptCount - 1))))); } //ReferencePoint p = m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, startPoint.Z + i*(endPoint.Z - startPoint.Z)/ (ptCount - 1))); //ptArr.Append(p); } CurveByPoints curve = m_familyCreator.NewCurveByPoints(ptArr); curve.Visible = false; //创建放样平面并加入参照数组中 int step = 8;//取step个点进行拟合 ReferenceArrayArray refArr = new ReferenceArrayArray(); for (int i = 0; i <= step; i++) { int position = i * (ptCount - 1) / step; //取起点截面及第二个截面作为参照平面 if (i == 0) { refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), ptArr.get_Item((i + 1) * (ptCount - 1) / step), (curve.GeometryCurve as HermiteSpline).Tangents[position], (curve.GeometryCurve as HermiteSpline).Tangents[((i + 1) * (ptCount - 1) / step)])); } //取终点截面及倒数第二个截面作为参照平面 else if (i == step) { refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), ptArr.get_Item((i - 1) * (ptCount - 1) / step), (curve.GeometryCurve as HermiteSpline).Tangents[position], (curve.GeometryCurve as HermiteSpline).Tangents[((i - 1) * (ptCount - 1) / step)])); } else { refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), (curve.GeometryCurve as HermiteSpline).Tangents[position])); } } //创建放样实体 m_familyCreator.NewLoftForm(true, refArr); }
private void CreateCurve(XYZ startPoint, XYZ endPoint, XYZ normal1, XYZ normal2) { XYZ StartToEnd = new XYZ((endPoint - startPoint).X, (endPoint - startPoint).Y, 0); XYZ p_normal1 = new XYZ(normal1.X, normal1.Y, 0); XYZ p_normal2 = new XYZ(normal2.X, normal2.Y, 0); /** * double sita1 = StartToEnd.AngleTo(p_normal1); * double sita2 = StartToEnd.AngleTo(p_normal2); * XYZ e1 = Math.Abs(((0.25 * StartToEnd).GetLength() * Math.Cos(sita1) / p_normal1.GetLength())) * p_normal1; * XYZ e2 = Math.Abs(((0.25 * StartToEnd).GetLength() * Math.Cos(sita2) / p_normal2.GetLength())) * p_normal2; * * * XYZ prePt1 = new XYZ(startPoint.X + e1.X, startPoint.Y + e1.Y, 0); * XYZ prePt2 = new XYZ(endPoint.X + e2.X, endPoint.Y + e2.Y, 0); */ p_normal1 = p_normal1 / (Math.Sqrt(p_normal1.X * p_normal1.X + p_normal1.Y * p_normal1.Y)); p_normal2 = p_normal2 / (Math.Sqrt(p_normal2.X * p_normal2.X + p_normal2.Y * p_normal2.Y)); //在起点、终点间插值,绘制NurbSpline曲线 double[] doubleArray = { 1, 1, 1, 1, 1, 1 }; IList <XYZ> controlPoints2 = new List <XYZ>(); /** * controlPoints2.Insert(0, new XYZ(startPoint.X, startPoint.Y, 0)); * controlPoints2.Insert(1, prePt1); * //controlPoints2.Insert(2, new XYZ((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2, 0)); * controlPoints2.Insert(2, prePt2); * controlPoints2.Insert(3, new XYZ(endPoint.X, endPoint.Y, 0)); */ controlPoints2.Add(startPoint); controlPoints2.Add(startPoint + p_normal1 * mmToFeet(2000)); controlPoints2.Add(startPoint + p_normal1 * mmToFeet(4000)); controlPoints2.Add(endPoint + p_normal2 * mmToFeet(4000)); controlPoints2.Add(endPoint + p_normal2 * mmToFeet(2000)); controlPoints2.Add(endPoint); Curve nbLine = NurbSpline.Create(controlPoints2, doubleArray); //提取曲线上的拟合点 IList <XYZ> ptsOnCurve = nbLine.Tessellate(); int ptCount = ptsOnCurve.Count; ReferencePointArray ptArr = new ReferencePointArray(); for (int i = 0; i < ptCount; i++) { XYZ pt = ptsOnCurve[i]; ReferencePoint p = m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, startPoint.Z + i / (ptCount - 1) * (endPoint.Z - startPoint.Z))); ptArr.Append(p); } CurveByPoints curve = m_familyCreator.NewCurveByPoints(ptArr); curve.Visible = false; //创建放样平面并加入参照数组中 int step = 16;//取4分点进行拟合 ReferenceArrayArray refArr = new ReferenceArrayArray(); for (int i = 0; i <= step; i++) { int position = i * (ptCount - 1) / step; if (i == 0) { refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), normal1)); } else if (i == ptArr.Size - 1) { /** * { * Plane plane = new Plane(normal2, ptArr.get_Item(position).Position); * Plane planeEx = new Plane(plane.XVec.CrossProduct(normal2),plane.YVec.CrossProduct(normal2),plane.Origin); * } */ refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), -normal2)); } else { refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), (curve.GeometryCurve as HermiteSpline).Tangents[position])); } } //创建放样实体 m_familyCreator.NewLoftForm(true, refArr); }