CRhinoCommand::result CCommandSampleTriangulatePolygon::RunCommand( const CRhinoCommandContext& context ) { CRhinoGetObject go; go.SetCommandPrompt( L"Select closed planar polygon to triangulate" ); go.SetGeometryFilter( CRhinoGetObject::curve_object ); go.SetGeometryFilter( CRhinoGetObject::closed_curve ); go.EnableSubObjectSelect( FALSE ); go.GetObjects( 1, 1 ); if( go.CommandResult() != CRhinoCommand::success ) return go.CommandResult(); const CRhinoObjRef& ref = go.Object(0); ON_3dPointArray vertices; const ON_PolylineCurve* pc = ON_PolylineCurve::Cast( ref.Curve() ); if( pc ) { vertices = pc->m_pline; } else { const ON_NurbsCurve* nc = ON_NurbsCurve::Cast( ref.Curve() ); if( nc ) nc->IsPolyline( &vertices ); } if( vertices.Count() < 5 ) { RhinoApp().Print( L"Curve not polygon with at least four sides.\n" ); return CRhinoCommand::nothing; } int* triangles = (int*)onmalloc( (vertices.Count()-3) * sizeof(int) * 3 ); if( 0 == triangles ) return CRhinoCommand::failure; // out of memory memset( triangles, 0, (vertices.Count()-3) * sizeof(int) * 3 ); int rc = RhinoTriangulate3dPolygon( vertices.Count()-1, 3, (const double*)vertices.Array(), 3, triangles); if( 0 == rc ) { int i; for( i = 0; i < vertices.Count()-3; i++ ) { ON_Polyline pline; pline.Append( vertices[triangles[i * 3]] ); pline.Append( vertices[triangles[i * 3 + 1]] ); pline.Append( vertices[triangles[i * 3 + 2]] ); pline.Append( pline[0] ); context.m_doc.AddCurveObject( pline ); } context.m_doc.Redraw(); } onfree( triangles ); return CRhinoCommand::success; }
CRhinoCommand::result CCommandSampleLineMeshIntersect::RunCommand( const CRhinoCommandContext& context ) { CRhinoGetObject gm; gm.SetCommandPrompt(L"Select mesh to intersect"); gm.SetGeometryFilter(CRhinoGetObject::mesh_object); gm.GetObjects(1, 1); if (gm.CommandResult() != CRhinoCommand::success) return gm.CommandResult(); const ON_Mesh* mesh = gm.Object(0).Mesh(); if (0 == mesh) return CRhinoCommand::failure; CRhinoGetObject gl; gl.SetCommandPrompt(L"Select line to intersect with"); gl.SetGeometryFilter(CRhinoGetObject::curve_object); gl.SetGeometryAttributeFilter(CRhinoGetObject::open_curve); gl.EnablePreSelect(FALSE); gl.EnableDeselectAllBeforePostSelect(FALSE); gl.GetObjects(1, 1); if (gl.CommandResult() != CRhinoCommand::success) return gl.CommandResult(); const ON_Curve* curve = gl.Object(0).Curve(); if (0 == curve) return CRhinoCommand::failure; const ON_LineCurve* line_curve = ON_LineCurve::Cast(curve); if (0 == line_curve) { RhinoApp().Print(L"Not a line curve.\n"); return CRhinoCommand::nothing; } ON_3dPointArray points; points.Append(line_curve->m_line.from); points.Append(line_curve->m_line.to); const ON_MeshTree* mesh_tree = mesh->MeshTree(true); if (mesh_tree) { ON_SimpleArray<ON_CMX_EVENT> cmx; if (mesh_tree->IntersectPolyline(2, points.Array(), cmx)) { for (int i = 0; i < cmx.Count(); i++) { RhinoApp().Print(L"Intesection found at face index = %d.\n", cmx[i].m_M[0].m_face_index); CRhinoPointObject* point_object = context.m_doc.AddPointObject(cmx[i].m_M[0].m_P); if (point_object) point_object->Select(); } context.m_doc.Redraw(); } if (1 == cmx.Count()) RhinoApp().Print(L"1 intesection found.\n"); else RhinoApp().Print(L"%d intesections found.\n", cmx.Count()); } return CRhinoCommand::success; }
void ON_TextLog::Print( const ON_3dPointArray& a, const char* sPreamble ) { const double* p = (a.Array() ? &a.Array()[0].x : NULL ); PrintPointList( 3, false, a.Count(), 3, p, sPreamble ); }