/// <summary> /// Computes the Area properties for a collection of geometric objects. /// At present only Breps, Surfaces, Meshes and Planar Closed Curves are supported. /// </summary> /// <param name="geometry">Objects to include in the area computation.</param> /// <returns>The Area properties for the entire collection or null on failure.</returns> public static AreaMassProperties Compute(IEnumerable <GeometryBase> geometry) { const double relativeTolerance = 1.0e-6; const double absoluteTolerance = 1.0e-6; Rhino.Runtime.InteropWrappers.SimpleArrayGeometryPointer array = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(geometry); IntPtr pConstGeometryArray = array.ConstPointer(); IntPtr rc = UnsafeNativeMethods.ON_Geometry_AreaMassProperties(pConstGeometryArray, relativeTolerance, absoluteTolerance); return(IntPtr.Zero == rc ? null : new AreaMassProperties(rc, false)); }
/// <summary> /// Computes the VolumeMassProperties for a collection of geometric objects. /// At present only Breps, Surfaces, Meshes and Planar Closed Curves are supported. /// </summary> /// <param name="geometry">Objects to include in the area computation.</param> /// <param name="volume">true to calculate volume.</param> /// <param name="firstMoments">true to calculate volume first moments, volume, and volume centroid.</param> /// <param name="secondMoments">true to calculate volume second moments.</param> /// <param name="productMoments">true to calculate volume product moments.</param> /// <returns>The VolumeMassProperties for the entire collection or null on failure.</returns> /// <since>6.3</since> public static VolumeMassProperties Compute(IEnumerable <GeometryBase> geometry, bool volume, bool firstMoments, bool secondMoments, bool productMoments) { const double relative_tolerance = 1.0e-6; const double absolute_tolerance = 1.0e-6; Pixel.Rhino.Runtime.InteropWrappers.SimpleArrayGeometryPointer array = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(geometry); IntPtr pConstGeometryArray = array.ConstPointer(); IntPtr rc = UnsafeNativeMethods.ON_Geometry_VolumeMassProperties(pConstGeometryArray, volume, firstMoments, secondMoments, productMoments, relative_tolerance, absolute_tolerance); GC.KeepAlive(geometry); return(IntPtr.Zero == rc ? null : new VolumeMassProperties(rc, false)); }
/// <summary> /// Modifies the instance definition geometry and replaces all references /// to the current definition with references to the new definition. /// </summary> /// <param name="idefIndex">The index of the instance definition to be modified.</param> /// <param name="newGeometry">The new geometry.</param> /// <param name="newAttributes">The new attributes.</param> /// <returns>true if operation succeeded.</returns> public bool ModifyGeometry(int idefIndex, IEnumerable <GeometryBase> newGeometry, IEnumerable <ObjectAttributes> newAttributes) { using (Rhino.Runtime.InteropWrappers.SimpleArrayGeometryPointer g = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(newGeometry)) { IntPtr pAttributes = UnsafeNativeMethods.ON_SimpleArray_3dmObjectAttributes_New(); if (newAttributes != null) { foreach (ObjectAttributes att in newAttributes) { IntPtr pAtt = att.ConstPointer(); UnsafeNativeMethods.ON_SimpleArray_3dmObjectAttributes_Add(pAttributes, pAtt); } } IntPtr pGeometry = g.ConstPointer(); bool rc = UnsafeNativeMethods.CRhinoInstanceDefinitionTable_ModifyGeometry(m_doc.m_docId, idefIndex, pGeometry, pAttributes); UnsafeNativeMethods.ON_SimpleArray_3dmObjectAttributes_Delete(pAttributes); return(rc); } }
/// <summary> /// Adds an instance definition to the instance definition table. /// </summary> /// <param name="name">The definition name.</param> /// <param name="description">The definition description.</param> /// <param name="basePoint">A base point.</param> /// <param name="geometry">An array, a list or any enumerable set of geometry.</param> /// <param name="attributes">An array, a list or any enumerable set of attributes.</param> /// <returns> /// >=0 index of instance definition in the instance definition table. -1 on failure. /// </returns> /// <example> /// <code source='examples\vbnet\ex_createblock.vb' lang='vbnet'/> /// <code source='examples\cs\ex_createblock.cs' lang='cs'/> /// <code source='examples\py\ex_createblock.py' lang='py'/> /// </example> public int Add(string name, string description, Point3d basePoint, IEnumerable <GeometryBase> geometry, IEnumerable <DocObjects.ObjectAttributes> attributes) { using (Rhino.Runtime.InteropWrappers.SimpleArrayGeometryPointer g = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(geometry)) { IntPtr pAttributes = UnsafeNativeMethods.ON_SimpleArray_3dmObjectAttributes_New(); if (attributes != null) { foreach (ObjectAttributes att in attributes) { IntPtr pAtt = att.ConstPointer(); UnsafeNativeMethods.ON_SimpleArray_3dmObjectAttributes_Add(pAttributes, pAtt); } } IntPtr pGeometry = g.ConstPointer(); int rc = UnsafeNativeMethods.CRhinoInstanceDefinitionTable_Add(m_doc.m_docId, name, description, basePoint, pGeometry, pAttributes); UnsafeNativeMethods.ON_SimpleArray_3dmObjectAttributes_Delete(pAttributes); return(rc); } }
/// <summary> /// Projects a curve to a set of meshes using a direction and tolerance. /// </summary> /// <param name="curves">A list, an array or any enumerable of curves.</param> /// <param name="meshes">A list, an array or any enumerable of meshes.</param> /// <param name="direction">A direction vector.</param> /// <param name="tolerance">A tolerance value.</param> /// <returns>A curve array.</returns> public static Curve[] ProjectToMesh(IEnumerable<Curve> curves, IEnumerable<Mesh> meshes, Vector3d direction, double tolerance) { foreach (Curve crv in curves) { if (crv == null) throw new ArgumentNullException("curves"); } List<GeometryBase> g = new List<GeometryBase>(); foreach (Mesh msh in meshes) { if (msh == null) throw new ArgumentNullException("meshes"); g.Add(msh); } using (SimpleArrayCurvePointer crv_array = new SimpleArrayCurvePointer(curves)) using (Runtime.InteropWrappers.SimpleArrayGeometryPointer mesh_array = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(g)) using (SimpleArrayCurvePointer curves_out = new SimpleArrayCurvePointer()) { IntPtr pCurvesIn = crv_array.ConstPointer(); IntPtr pMeshes = mesh_array.ConstPointer(); IntPtr pCurvesOut = curves_out.NonConstPointer(); Curve[] rc = new Curve[0]; if (UnsafeNativeMethods.RHC_RhinoProjectCurveToMesh(pMeshes, pCurvesIn, direction, tolerance, pCurvesOut)) rc = curves_out.ToNonConstArray(); return rc; } }
/// <summary> /// Computes the Area properties for a collection of geometric objects. /// At present only Breps, Surfaces, Meshes and Planar Closed Curves are supported. /// </summary> /// <param name="geometry">Objects to include in the area computation.</param> /// <returns>The Area properties for the entire collection or null on failure.</returns> public static AreaMassProperties Compute(IEnumerable<GeometryBase> geometry) { const double relativeTolerance = 1.0e-6; const double absoluteTolerance = 1.0e-6; Rhino.Runtime.InteropWrappers.SimpleArrayGeometryPointer array = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(geometry); IntPtr pConstGeometryArray = array.ConstPointer(); IntPtr rc = UnsafeNativeMethods.ON_Geometry_AreaMassProperties(pConstGeometryArray, relativeTolerance, absoluteTolerance); return IntPtr.Zero == rc ? null : new AreaMassProperties(rc, false); }
/// <summary> /// Executes unrolling operations. /// </summary> /// <param name="unrolledCurves">An array of unrolled curves is assigned during the call in this out parameter.</param> /// <param name="unrolledPoints">An array of unrolled points is assigned during the call in this out parameter.</param> /// <param name="unrolledDots">An array of unrolled text dots is assigned during the call in this out parameter.</param> /// <returns>An array of breps. This array can be empty.</returns> /// <since>5.0</since> public Brep[] PerformUnroll(out Curve[] unrolledCurves, out Point3d[] unrolledPoints, out TextDot[] unrolledDots) { unrolledCurves = new Curve[0]; unrolledPoints = new Point3d[0]; unrolledDots = new TextDot[0]; IntPtr ptr_unroller = IntPtr.Zero; if (m_surface != null) { IntPtr const_ptr_surface = m_surface.ConstPointer(); ptr_unroller = UnsafeNativeMethods.CRhinoUnroll_NewSrf(const_ptr_surface, m_absolute_tolerance, m_relative_tolerance); } else if (m_brep != null) { IntPtr const_ptr_brep = m_brep.ConstPointer(); ptr_unroller = UnsafeNativeMethods.CRhinoUnroll_NewBrp(const_ptr_brep, m_absolute_tolerance, m_relative_tolerance); } if (ptr_unroller == IntPtr.Zero) { throw new Exception("Unable to access input surface or brep"); } var rc = new Brep[0]; if (0 == UnsafeNativeMethods.CRhinoUnroll_PrepareFaces(ptr_unroller)) { if (m_curves.Count > 0) { var crvs = new Runtime.InteropWrappers.SimpleArrayCurvePointer(m_curves); IntPtr const_ptr_curves = crvs.ConstPointer(); UnsafeNativeMethods.CRhinoUnroll_PrepareCurves(ptr_unroller, const_ptr_curves); } if (m_points.Count > 0) { Point3d[] pts = m_points.ToArray(); UnsafeNativeMethods.CRhinoUnroll_PreparePoints(ptr_unroller, pts.Length, pts); } if (m_dots.Count > 0) { using (var dots = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(m_dots)) { IntPtr const_ptr_dots = dots.ConstPointer(); UnsafeNativeMethods.CRhinoUnroll_PrepareDots(ptr_unroller, const_ptr_dots); } } int brep_count = 0; int curve_count = 0; int point_count = 0; int dot_count = 0; double explode_dist = -1; if (m_explode_output) { explode_dist = m_explode_spacing; } IntPtr ptr_results = UnsafeNativeMethods.CRhinoUnroll_CreateFlatBreps(ptr_unroller, explode_dist, ref brep_count, ref curve_count, ref point_count, ref dot_count); if (ptr_results != IntPtr.Zero) { if (brep_count > 0) { rc = new Brep[brep_count]; for (int i = 0; i < brep_count; i++) { IntPtr ptr_brep = UnsafeNativeMethods.CRhinoUnrollResults_GetBrep(ptr_results, i); if (ptr_brep != IntPtr.Zero) { rc[i] = new Brep(ptr_brep, null); } } } if (curve_count > 0) { unrolledCurves = new Curve[curve_count]; for (int i = 0; i < curve_count; i++) { IntPtr ptr_curve = UnsafeNativeMethods.CRhinoUnrollResults_GetCurve(ptr_results, i); if (ptr_curve != IntPtr.Zero) { unrolledCurves[i] = new Curve(ptr_curve, null); } } } if (point_count > 0) { unrolledPoints = new Point3d[point_count]; UnsafeNativeMethods.CRhinoUnrollResults_GetPoints(ptr_results, point_count, unrolledPoints); } if (dot_count > 0) { unrolledDots = new TextDot[dot_count]; for (int i = 0; i < dot_count; i++) { IntPtr ptr_dots = UnsafeNativeMethods.CRhinoUnrollResults_GetDot(ptr_results, i); if (ptr_dots != IntPtr.Zero) { unrolledDots[i] = new TextDot(ptr_dots, null); } } } UnsafeNativeMethods.CRhinoUnrollResults_Delete(ptr_results); } } UnsafeNativeMethods.CRhinoUnroll_Delete(ptr_unroller); return(rc); }
/// <summary> /// Computes point intersections that occur when shooting a ray to a collection of surfaces. /// </summary> /// <param name="ray">A ray used in intersection.</param> /// <param name="geometry">Only Surface and Brep objects are currently supported. Trims are ignored on Breps.</param> /// <param name="maxReflections">The maximum number of reflections. This value should be any value between 1 and 1000, inclusive.</param> /// <returns>An array of points: one for each face that was passed by the faceIds out reference.</returns> /// <exception cref="ArgumentNullException">geometry is null.</exception> /// <exception cref="ArgumentOutOfRangeException">maxReflections is strictly outside the [1-1000] range.</exception> public static Point3d[] RayShoot(Ray3d ray, IEnumerable<GeometryBase> geometry, int maxReflections) { if (geometry == null) throw new ArgumentNullException("geometry"); if (maxReflections < 1 || maxReflections > 1000) throw new ArgumentOutOfRangeException("maxReflections", "maxReflections must be between 1-1000"); // We should handle better the case of a null entry inside the geometry collection. // Currently a NullReferenceException occurs. using (Rhino.Runtime.InteropWrappers.SimpleArrayGeometryPointer geom = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(geometry)) { IntPtr pGeometry = geom.ConstPointer(); Point3d[] rc = null; using (Rhino.Runtime.InteropWrappers.SimpleArrayPoint3d points = new Rhino.Runtime.InteropWrappers.SimpleArrayPoint3d()) { IntPtr pPoints = points.NonConstPointer(); int count = UnsafeNativeMethods.ON_RayShooter_ShootRay(ray.Position, ray.Direction, pGeometry, pPoints, maxReflections); if (count > 0) rc = points.ToArray(); } return rc; } }
/// <summary> /// Executes unrolling operations. /// </summary> /// <param name="unrolledCurves">An array of unrolled curves is assigned during the call in this out parameter.</param> /// <param name="unrolledPoints">An array of unrolled points is assigned during the call in this out parameter.</param> /// <param name="unrolledDots">An array of unrolled text dots is assigned during the call in this out parameter.</param> /// <returns>An array of breps. This array can be empty.</returns> public Brep[] PerformUnroll(out Curve[] unrolledCurves, out Point3d[] unrolledPoints, out TextDot[] unrolledDots) { unrolledCurves = new Curve[0]; unrolledPoints = new Point3d[0]; unrolledDots = new TextDot[0]; IntPtr pUnroller = IntPtr.Zero; if (m_surface != null) { IntPtr pSurface = m_surface.ConstPointer(); pUnroller = UnsafeNativeMethods.CRhinoUnroll_NewSrf(pSurface, m_dAbsoluteTolerance, m_dRelativeTolerance); } else if (m_brep != null) { IntPtr pBrep = m_brep.ConstPointer(); pUnroller = UnsafeNativeMethods.CRhinoUnroll_NewBrp(pBrep, m_dAbsoluteTolerance, m_dRelativeTolerance); } if (pUnroller == IntPtr.Zero) throw new Exception("Unable to access input surface or brep"); Brep[] rc = new Brep[0]; if (0 == UnsafeNativeMethods.CRhinoUnroll_PrepareFaces(pUnroller)) { if (m_curves.Count > 0) { Runtime.InteropWrappers.SimpleArrayCurvePointer crvs = new Rhino.Runtime.InteropWrappers.SimpleArrayCurvePointer(m_curves); IntPtr pCrvs = crvs.ConstPointer(); UnsafeNativeMethods.CRhinoUnroll_PrepareCurves(pUnroller, pCrvs); } if (m_points.Count > 0) { Point3d[] pts = m_points.ToArray(); UnsafeNativeMethods.CRhinoUnroll_PreparePoints(pUnroller, pts.Length, pts); } if (m_dots.Count > 0) { using (Rhino.Runtime.InteropWrappers.SimpleArrayGeometryPointer dots = new Runtime.InteropWrappers.SimpleArrayGeometryPointer(m_dots)) { IntPtr pDots = dots.ConstPointer(); UnsafeNativeMethods.CRhinoUnroll_PrepareDots(pUnroller, pDots); } } int brepCount = 0; int curveCount = 0; int pointCount = 0; int dotCount = 0; double explode_dist = -1; if (m_bExplodeOutput) explode_dist = m_dExplodeSpacing; IntPtr pResults = UnsafeNativeMethods.CRhinoUnroll_CreateFlatBreps(pUnroller, explode_dist, ref brepCount, ref curveCount, ref pointCount, ref dotCount); if (pResults != IntPtr.Zero) { if (brepCount > 0) { rc = new Brep[brepCount]; for (int i = 0; i < brepCount; i++) { IntPtr pBrep = UnsafeNativeMethods.CRhinoUnrollResults_GetBrep(pResults, i); if (pBrep != IntPtr.Zero) rc[i] = new Brep(pBrep, null); } } if (curveCount > 0) { unrolledCurves = new Curve[curveCount]; for (int i = 0; i < curveCount; i++) { IntPtr pCurve = UnsafeNativeMethods.CRhinoUnrollResults_GetCurve(pResults, i); if (pCurve != IntPtr.Zero) unrolledCurves[i] = new Curve(pCurve, null); } } if (pointCount > 0) { unrolledPoints = new Point3d[pointCount]; UnsafeNativeMethods.CRhinoUnrollResults_GetPoints(pResults, pointCount, unrolledPoints); } if (dotCount > 0) { unrolledDots = new TextDot[dotCount]; for (int i = 0; i < dotCount; i++) { IntPtr pDot = UnsafeNativeMethods.CRhinoUnrollResults_GetDot(pResults, i); if (pDot != IntPtr.Zero) unrolledDots[i] = new TextDot(pDot, null); } } UnsafeNativeMethods.CRhinoUnrollResults_Delete(pResults); } } UnsafeNativeMethods.CRhinoUnroll_Delete(pUnroller); return rc; }