// Try to make up a 1st derivative if one is zero length static void CookDerivativesHelper( ON_3dVector& du, ON_3dVector& dv, ON_3dVector& duu, ON_3dVector& duv, ON_3dVector& dvv) { bool du_ok = du.LengthSquared() > ON_SQRT_EPSILON; bool dv_ok = dv.LengthSquared() > ON_SQRT_EPSILON; if( !du_ok || !dv_ok) { ON_3dVector normal; bool normal_ok = ON_EvNormal( 0, du, dv, duu, duv, dvv, normal ) ? true : false; if( normal_ok) normal_ok = normal.LengthSquared() > ON_SQRT_EPSILON; if( normal_ok) { if(( !du_ok) && ( dv_ok && normal_ok)) { du = ON_CrossProduct( dv, normal); du_ok = du.Unitize(); du *= (0.00390625*dv.Length()); } if( du_ok && ( !dv_ok) && normal_ok) { dv = ON_CrossProduct( normal, du); dv_ok = dv.Unitize(); dv *= (0.00390625*du.Length()); } } } }
ON_BOOL32 ON_Light::Transform( const ON_Xform& xform ) { ON_3dVector v; double vlen; TransformUserData(xform); m_location = xform*m_location; v = xform*m_direction; vlen = v.Length(); if ( vlen > 0.0 ) { m_direction = v; } v = xform*m_length; vlen = v.Length(); if ( vlen > 0.0 ) { m_length = v; } v = xform*m_width; vlen = v.Length(); if ( vlen > 0.0 ) { m_width = v; } return true; }
bool ON_Plane::CreateFromFrame( const ON_3dPoint& P, // point on the plane const ON_3dVector& X, // non-zero vector in plane const ON_3dVector& Y // another non-zero vector in the plane ) { origin = P; xaxis = X; xaxis.Unitize(); yaxis = Y - ON_DotProduct( Y, xaxis)*xaxis; yaxis.Unitize(); zaxis = ON_CrossProduct( xaxis, yaxis ); bool b = zaxis.Unitize(); UpdateEquation(); if ( b ) { // 11 February 2004 Dale Lear // Add more validation checks. b = IsValid(); if ( b ) { // make sure zaxis is perp to Y if ( fabs(Y*zaxis) > ON_SQRT_EPSILON*Y.Length() ) b = false; } } return b; }
// ON_BoundingBox::MaximumDistance has a copy/paste bug in it in V4. Using local // version of this function with the fix so things continue to work under V4 grasshopper static double RhCmnMaxDistance_Helper(const ON_BoundingBox& bbox, const ON_3dPoint& P) { ON_3dVector V; V.x = ( (P.x < 0.5*(bbox.m_min.x+bbox.m_max.x)) ? bbox.m_max.x : bbox.m_min.x) - P.x; V.y = ( (P.y < 0.5*(bbox.m_min.y+bbox.m_max.y)) ? bbox.m_max.y : bbox.m_min.y) - P.y; V.z = ( (P.z < 0.5*(bbox.m_min.z+bbox.m_max.z)) ? bbox.m_max.z : bbox.m_min.z) - P.z; return V.Length(); }
int ON_PlaneSurface::GetNurbForm( // returns 0: unable to create NURBS representation // with desired accuracy. // 1: success - returned NURBS parameterization // matches the surface's to wthe desired accuracy // 2: success - returned NURBS point locus matches // the surfaces's to the desired accuracy but, on // the interior of the surface's domain, the // surface's parameterization and the NURBS // parameterization may not match to the // desired accuracy. ON_NurbsSurface& nurbs, double tolerance ) const { ON_BOOL32 rc = IsValid(); if( !rc ) { if ( m_plane.origin.x != ON_UNSET_VALUE && m_plane.xaxis.x != ON_UNSET_VALUE && m_plane.yaxis.x != ON_UNSET_VALUE && m_domain[0].IsIncreasing() && m_domain[1].IsIncreasing() && m_extents[0].Length() > 0.0 && m_extents[1].Length() > 0.0 ) { ON_3dVector N = ON_CrossProduct(m_plane.xaxis,m_plane.yaxis); if ( N.Length() <= 1.0e-4 ) { ON_WARNING("ON_PlaneSurface::GetNurbForm - using invalid surface."); rc = true; } } } if ( rc ) { nurbs.m_dim = 3; nurbs.m_is_rat = 0; nurbs.m_order[0] = nurbs.m_order[1] = 2; nurbs.m_cv_count[0] = nurbs.m_cv_count[1] = 2; nurbs.m_cv_stride[1] = nurbs.m_dim; nurbs.m_cv_stride[0] = nurbs.m_cv_stride[1]*nurbs.m_cv_count[1]; nurbs.ReserveCVCapacity(12); nurbs.ReserveKnotCapacity(0,2); nurbs.ReserveKnotCapacity(1,2); nurbs.m_knot[0][0] = m_domain[0][0]; nurbs.m_knot[0][1] = m_domain[0][1]; nurbs.m_knot[1][0] = m_domain[1][0]; nurbs.m_knot[1][1] = m_domain[1][1]; nurbs.SetCV( 0, 0, PointAt( m_domain[0][0], m_domain[1][0] )); nurbs.SetCV( 0, 1, PointAt( m_domain[0][0], m_domain[1][1] )); nurbs.SetCV( 1, 0, PointAt( m_domain[0][1], m_domain[1][0] )); nurbs.SetCV( 1, 1, PointAt( m_domain[0][1], m_domain[1][1] )); } return rc; }
void ON_Quaternion::SetRotation(double angle, const ON_3dVector& axis) { double s = axis.Length(); s = (s > 0.0) ? sin(0.5*angle)/s : 0.0; a = cos(0.5*angle); b = s*axis.x; c = s*axis.y; d = s*axis.z; }
bool ON_Localizer::CreatePlaneLocalizer( ON_3dPoint P, ON_3dVector N, double h0, double h1 ) { Destroy(); if ( P.IsValid() && N.IsValid() && N.Length() > 0.0 && ON_IsValid(h0) && ON_IsValid(h1) && h0 != h1 ) { m_V = N; m_V.Unitize(); m_P.Set( -(m_V.x*P.x + m_V.y*P.y + m_V.z*P.z), 0.0, 0.0 ); m_d.Set(h0,h1); m_type = plane_type; } return (plane_type == m_type); }
ON_Surface* ON_PlaneSurface::Offset( double offset_distance, double tolerance, double* max_deviation ) const { if ( max_deviation ) *max_deviation = 0.0; ON_PlaneSurface* offset_srf = new ON_PlaneSurface(*this); ON_3dVector delta = offset_srf->m_plane.zaxis; double d = delta.Length(); if ( fabs(1.0-d) <= ON_SQRT_EPSILON ) d = 1.0; d = offset_distance/d; offset_srf->m_plane.origin = offset_srf->m_plane.origin + (d*delta); offset_srf->m_plane.UpdateEquation(); return offset_srf; }
bool ON_Localizer::CreateCylinderLocalizer( ON_3dPoint P, ON_3dVector V, double r0, double r1 ) { Destroy(); if ( P.IsValid() && V.IsValid() && V.Length() > 0.0 && ON_IsValid(r0) && ON_IsValid(r1) && r0 > 0.0 && r1 > 0.0 && r0 != r1 ) { m_P = P; m_V = V; m_V.Unitize(); m_d.Set(r0,r1); m_type = cylinder_type; } return (cylinder_type == m_type); }
int ON_Intersect( const ON_Sphere& sphere0, const ON_Sphere& sphere1, ON_Circle& circle ) { double r0 = sphere0.Radius(); double r1 = sphere1.Radius(); ON_3dPoint C0 = sphere0.Center(); ON_3dPoint C1 = sphere1.Center(); ON_3dVector D = C1-C0; double d = D.Length(); if (!D.Unitize()){ if (fabs(r1-r0) > ON_ZERO_TOLERANCE) return 0;//Same center, different radii return 3;//Same sphere. } //Spheres are appart. if (d > r0 + r1) return 0; //Spheres tangent and appart if (d == r0+r1){ ON_3dPoint P = C0 + r0*D; circle.Create(P, 0.0); return 1; } //Spheres tangent, one inside the other if (d == fabs(r0-r1)){ ON_3dPoint P = (r0 > r1) ? C0 + r0*D : C0 - r0*D; circle.Create(P, 0.0); return 1; } //Spheres don't intersect, one inside the other. if (d < fabs(r0-r1)) return 0; //Intersection is a circle double x = 0.5*(d*d + r0*r0 - r1*r1)/d; if (x >= r0){//Shouldn't happen ON_3dPoint P = C0 + r0*D; circle.Create(P, 0.0); return 1; } if (x <= -r0){//Shouldn't happen ON_3dPoint P = C0 - r0*D; circle.Create(P, 0.0); return 1; } double y = r0*r0 - x*x; if (y < 0.0)//Shouldn't happen return 0; y = sqrt(y); ON_3dPoint P = C0 + x*D; ON_Plane plane(P, D); circle.Create(plane, y); return 2; }
ON_Quaternion ON_Quaternion::Rotation(double angle, const ON_3dVector& axis) { double s = axis.Length(); s = (s > 0.0) ? sin(0.5*angle)/s : 0.0; return ON_Quaternion(cos(0.5*angle),s*axis.x,s*axis.y,s*axis.z); }
ON_BOOL32 ON_Surface::EvNormal( // returns false if unable to evaluate double s, double t, // evaluation parameters (s,t) ON_3dPoint& point, // returns value of surface ON_3dVector& ds, // first partial derivatives (Ds) ON_3dVector& dt, // (Dt) ON_3dVector& normal, // unit normal int side, // optional - determines which side to evaluate from // 0 = default // 1 from NE quadrant // 2 from NW quadrant // 3 from SW quadrant // 4 from SE quadrant int* hint // optional - evaluation hint (int[2]) used to speed // repeated evaluations ) const { // simple cross product normal - override to support singular surfaces ON_BOOL32 rc = Ev1Der( s, t, point, ds, dt, side, hint ); if ( rc ) { const double len_ds = ds.Length(); const double len_dt = dt.Length(); // do not reduce the tolerance used here - there is a retry in the code // below. if ( len_ds > ON_SQRT_EPSILON*len_dt && len_dt > ON_SQRT_EPSILON*len_ds ) { ON_3dVector a = ds/len_ds; ON_3dVector b = dt/len_dt; normal = ON_CrossProduct( a, b ); rc = normal.Unitize(); } else { // see if we have a singular point double v[6][3]; int normal_side = side; ON_BOOL32 bOnSide = false; ON_Interval sdom = Domain(0); ON_Interval tdom = Domain(1); if (s == sdom.Min()) { normal_side = (normal_side >= 3) ? 4 : 1; bOnSide = true; } else if (s == sdom.Max()) { normal_side = (normal_side >= 3) ? 3 : 2; bOnSide = true; } if (t == tdom.Min()) { normal_side = (normal_side == 2 || normal_side == 3) ? 2 : 1; bOnSide = true; } else if (t == tdom.Max()) { normal_side = (normal_side == 2 || normal_side == 3) ? 3 : 4; bOnSide = true; } if ( !bOnSide ) { // 2004 November 11 Dale Lear // Added a retry again with a more generous tolerance if ( len_ds > ON_EPSILON*len_dt && len_dt > ON_EPSILON*len_ds ) { ON_3dVector a = ds/len_ds; ON_3dVector b = dt/len_dt; normal = ON_CrossProduct( a, b ); rc = normal.Unitize(); } else { rc = false; } } else { rc = Evaluate( s, t, 2, 3, &v[0][0], normal_side, hint ); if ( rc ) { rc = ON_EvNormal( normal_side, v[1], v[2], v[3], v[4], v[5], normal); } } } } if ( !rc ) { normal.Zero(); } return rc; }
// Copied from opennurbs_intersect.cpp but with a bug fix. // We can remove it once the bug is fixed in OpenNurbs and once // Grasshopper has dropped Rhino4 support. int PS_Intersect( const ON_Plane& plane, const ON_Sphere& sphere, ON_Circle& circle ) { // 16 April 2011 Dale Lear // Prior to this date, this function did not return the correct answer. int rc = 0; const double sphere_radius = fabs(sphere.radius); double tol = sphere_radius*ON_SQRT_EPSILON; if ( !(tol >= ON_ZERO_TOLERANCE) ) tol = ON_ZERO_TOLERANCE; const ON_3dPoint sphere_center = sphere.Center(); ON_3dPoint circle_center = plane.ClosestPointTo(sphere_center); double d = circle_center.DistanceTo(sphere_center); circle.radius = 0.0; if ( ON_IsValid(sphere_radius) && ON_IsValid(d) && d <= sphere_radius + tol ) { if ( sphere_radius > 0.0 ) { d /= sphere_radius; d = 1.0 - d*d; // The d > 4.0*ON_EPSILON was picked by testing spheres with // radius = 1 and center = (0,0,0). Do not make 4.0*ON_EPSILON // any smaller and please discuss changes with Dale Lear. circle.radius = (d > 4.0*ON_EPSILON) ? sphere_radius*sqrt(d) : 0.0; } else circle.radius = 0.0; if ( circle.radius <= ON_ZERO_TOLERANCE ) { // return a single point rc = 1; circle.radius = 0.0; // When tolerance is in play, put the point on the sphere. // If the caller prefers the plane, then they can adjust the // returned answer to get the plane. ON_3dVector R = circle_center - sphere_center; double r0 = R.Length(); if ( r0 > 0.0 ) { R.Unitize(); ON_3dPoint C1 = sphere_center + sphere_radius*R; double r1 = C1.DistanceTo(sphere_center); if ( fabs(sphere.radius-r1) < fabs(sphere.radius-r0) ) circle_center = C1; } } else { // return a circle rc = 2; } } // Update circle's plane here in case the input plane // is the circle's plane member. circle.plane = plane; circle.plane.origin = circle_center; circle.plane.UpdateEquation(); return rc; }
CRhinoCommand::result CGenCylinder::RunCommand( const CRhinoCommandContext& context ) { Cscript1PlugIn& plugin = script1PlugIn(); if( !plugin.IsDlgVisible() ) { return CRhinoCommand::nothing; } /*****************************************/ /*CHECKING IF THERE IS ALREADY A CYLINDER*/ /*****************************************/ const CRhinoLayer& layer = context.m_doc.m_layer_table.CurrentLayer(); ON_SimpleArray<CRhinoObject*> objects; int object_count = context.m_doc.LookupObject( layer, objects ); const CRhinoBrepObject* brep_obj; const CRhinoCurveObject* curve_obj; const CRhinoSurfaceObject* surface_obj; int surf_count=0; if( object_count > 0 ) { int brep_obj_count = 0; int polycurve_count = 0; const CRhinoObject* object = 0; for(int i = 0; i < object_count; i++ ) { object = objects[ i ]; /************************************/ /*TRY CASTING AS A RHINO BREP OBJECT*/ /************************************/ brep_obj = CRhinoBrepObject::Cast( object ); if( brep_obj && object->IsSolid()) { brep_obj_count++; } /*******************************/ /*TRY CASTING AS A CURVE OBJECT*/ /*******************************/ curve_obj = CRhinoCurveObject::Cast( object ); if( curve_obj ) { polycurve_count++; } //surface_obj = CRhinoSurfaceObject::Cast( object ); // if( surface_obj ) // { //surf_count++; // } } if( brep_obj_count == 0) { ON_3dPoint center_point( 0.0, 0.0, 0.0 ); double radius = 63.5; int __count = plugin.m_dialog->m_comboAltTacco.GetCount(); int nIndex = plugin.m_dialog->m_comboAltTacco.GetCurSel(); CString strCBText; plugin.m_dialog->m_comboAltTacco.GetLBText( nIndex, strCBText); int height = _wtoi(strCBText); ON_3dPoint height_point( 0.0, 0.0, height); ON_3dVector zaxis = height_point - center_point; ON_Plane planeCir( center_point, zaxis ); /*ADD CIRCLE FOR CYLINDER'S BASE*/ ON_Circle circle( planeCir, radius ); /*ADD CYLINDER*/ ON_Cylinder cylinder( circle, zaxis.Length() ); ON_Brep* brep = ON_BrepCylinder( cylinder, TRUE, TRUE ); unsigned int first_SN; unsigned int next_SN; if( brep ) { first_SN = CRhinoObject::NextRuntimeObjectSerialNumber(); /********************/ /*TRANSLATE CYLINDER*/ /********************/ int nIndex1 = plugin.m_dialog->AltezzaFondelloControllo.GetCurSel(); CString strCBText1; plugin.m_dialog->AltezzaFondelloControllo.GetLBText( nIndex1, strCBText1); int altfondello = _wtoi(strCBText1); ON_wString obj_nameCyl = L"CILINDRO"; brep->Translate(ON_3dVector( 0.0, 0.0, -altfondello)); CRhinoBrepObject* cylinder_object = new CRhinoBrepObject(); cylinder_object->SetBrep( brep ); if( context.m_doc.AddObject(cylinder_object) ) { context.m_doc.Redraw(); next_SN = CRhinoObject::NextRuntimeObjectSerialNumber(); if( first_SN == next_SN ) { return CRhinoCommand::nothing; } else { SetNametoObject(context.m_doc,first_SN,obj_nameCyl,true); } } else { delete cylinder_object; } /*********************************************/ /* DA SISTEMARE */ /*********************************************/ CRhinoSnapContext snap; bool dec1 = snap.SnapToPoint(ON_3dPoint(63.5, 0.0, (height - altfondello))); bool dec2 = snap.SnapToPoint(ON_3dPoint(63.5, 0.0, -altfondello)); bool dec3 = snap.SnapToPoint(ON_3dPoint(63.5, 0.0, 0.0)); if(dec1 && dec2) { CRhinoLinearDimension* dim_obj = new CRhinoLinearDimension(); ON_Plane plane( ON_zx_plane ); plane.SetOrigin( ON_3dPoint(63.5, 0.0, 0.0) ); dim_obj->SetPlane( plane ); ON_3dPoint pt_1(63.5, 0.0, (height - altfondello)); ON_3dPoint pt_2(63.5, 0.0, -altfondello); double u, v; plane.ClosestPointTo( pt_1, &u, &v ); dim_obj->SetPoint( 0, ON_2dPoint(u, v) ); dim_obj->SetPoint( 1, ON_2dPoint(u, (v + height/2)) ); plane.ClosestPointTo( pt_2, &u, &v ); dim_obj->SetPoint( 2, ON_2dPoint(u, v) ); dim_obj->SetPoint( 3, ON_2dPoint(u, (v + height/2)) ); dim_obj->UpdateText(); if( context.m_doc.AddObject(dim_obj) ) { context.m_doc.Redraw(); } else { delete dim_obj; } } /*********************************************/ /* DA SISTEMARE */ /*********************************************/ if(dec2 && dec3) { CRhinoLinearDimension* dim_obj = new CRhinoLinearDimension(); ON_Plane plane( ON_zx_plane ); plane.SetOrigin( ON_3dPoint(63.5, 0.0, 0.0) ); dim_obj->SetPlane( plane ); ON_3dPoint pt_1(63.5, 0.0, 0.0); ON_3dPoint pt_2(63.5, 0.0, -altfondello); double u, v; plane.ClosestPointTo( pt_1, &u, &v ); dim_obj->SetPoint( 0, ON_2dPoint(u, v) ); dim_obj->SetPoint( 1, ON_2dPoint(u, (v + height/2)) ); plane.ClosestPointTo( pt_2, &u, &v ); dim_obj->SetPoint( 2, ON_2dPoint(u, v) ); dim_obj->SetPoint( 3, ON_2dPoint(u, (v + height/2)) ); dim_obj->UpdateText(); if( context.m_doc.AddObject(dim_obj) ) { context.m_doc.Redraw(); } else { delete dim_obj; } } /*********************************************/ /* DA SISTEMARE By Nello */ /*********************************************/ ON_3dPoint provapunto2 = AltezzaTacco; ON_3dPoint provapunto(59.5,0,provapunto2.z); provapunto.z-=0.7; ON_3dPoint pt_1(59.5, 0.0, (height - altfondello)); CRhinoLinearDimension* dim_obj = new CRhinoLinearDimension(); ON_Plane plane( ON_zx_plane ); plane.SetOrigin(pt_1); dim_obj->SetPlane( plane ); //ON_3dPoint pt_1(63.5, 0.0, 0.0); ON_3dPoint pt_2 = provapunto; double u, v; plane.ClosestPointTo( pt_1, &u, &v ); dim_obj->SetPoint( 0, ON_2dPoint(u, v) ); dim_obj->SetPoint( 1, ON_2dPoint(u, (v + height/2)) ); plane.ClosestPointTo( pt_2, &u, &v ); dim_obj->SetPoint( 2, ON_2dPoint(u, v) ); dim_obj->SetPoint( 3, ON_2dPoint(u, (v + height/2)) ); dim_obj->UpdateText(); if( context.m_doc.AddObject(dim_obj) ) { context.m_doc.Redraw(); } else { delete dim_obj; } /*INIZIO FUNZIONE CONTROLLO*/ if ( (height - altfondello)>=provapunto.z+10){ ::RhinoApp().Print( L"Funzione controllo altezza OK"); } else{ ::RhinoApp().Print( L"Funzione controllo altezza NOK: CONTROLLARE!! Il valore della testa e' minore del valore minimo di 10 mm. Occorre diminuire l'altezza del fondello o aumentare l'altezza dello stampo."); } /*********************************************/ /* CREATE FONDELLO PLANE */ /*********************************************/ ON_3dPoint point0((63.5 + 20.0),0.0, 0.0); ON_3dPoint point1(-(63.5 + 20.0),0.0, 0.0); ON_LineCurve curve0( point0, point1 ); context.m_doc.AddCurveObject(curve0); context.m_doc.Redraw(); /******************************************************/ /*****************************/ /*USER GIVES NAME TO SURFACES*/ /*****************************/ unsigned int first_sn; unsigned int next_sn; ON_wString obj_name; object_count = context.m_doc.LookupObject( layer, objects ); for(int i = 0; i < object_count; i++ ) { object = objects[ i ]; first_sn = CRhinoObject::NextRuntimeObjectSerialNumber(); /*******************************/ /*TRY CASTING AS A CURVE OBJECT*/ /*******************************/ curve_obj = CRhinoCurveObject::Cast( object ); if( curve_obj ) { const ON_Geometry* geo = curve_obj->Geometry(); const ON_Curve* curve00 = ON_Curve::Cast(geo); ON_3dPoint point = curve00->PointAt(0.0); ON_3dPoint point_ = curve00->PointAt(1.0); if((point.z + point_.z)/2 > 0.0) { obj_name = L"SURFPV"; } else { obj_name = L"SURFFO"; } ON_3dPoint point0(point.x, (point.y + 70.0), point.z); ON_3dPoint point1(point.x, (point.y - 70.0), point.z); ON_LineCurve* curve = new ON_LineCurve(); curve->SetStartPoint(point0); curve->SetEndPoint(point1); ON_SumSurface sumSurf0; sumSurf0.Create(*curve, *curve00); if( context.m_doc.AddSurfaceObject(sumSurf0) ) { context.m_doc.Redraw(); next_sn = CRhinoObject::NextRuntimeObjectSerialNumber(); if( first_sn == next_sn ) { return CRhinoCommand::nothing; } else { SetNametoObject(context.m_doc,first_sn,obj_name,true); } } } }/*CLOSED FOR*/ //int R = 0; //object_count = context.m_doc.LookupObject( layer, objects ); //for(int i = 0; i < object_count; i++) //{ // object = objects[ i ]; // const CRhinoCurveObject* surface_obj = CRhinoCurveObject::Cast(object); // if(surface_obj) // { // R++; // } //} // // Get the string entered by the user // ON_wString obj_name = gs.String(); // obj_name.TrimLeftAndRight(); // // // Is name the same? // if( obj_name.Compare(obj_attribs.m_name) == 0 ) //return CRhinoCommand::nothing; // // // Modify the attributes of the object // obj_attribs.m_name = obj_name; // context.m_doc.ModifyObjectAttributes( objref, obj_attribs ); // if(selectobjectbyuuid_s(context.m_doc,obj_Surf[j],false)) // { //int R = 0; // } ON_wString obj_Surf[2]; ON_wString name; obj_Surf[0] = L"SURFPV"; obj_Surf[1] = L"SURFFO"; object_count = context.m_doc.LookupObject( layer, objects ); int R = 0; /************************/ /*TRY SPLITTING THE BREP*/ /************************/ for(int j = 0; j < 2; j++) { for(int i = 0; i < object_count; i++) { object = objects[ i ]; /*MAKE COPY OF OBJECT ATTRIBUTES. THIS OBJECTS*/ /*HOLDS AN OBJECT'S USER-DEFINED NAME.*/ ON_3dmObjectAttributes obj_attribs = object->Attributes(); name = object->Attributes().m_name; surface_obj = CRhinoSurfaceObject::Cast( object ); if( surface_obj && !surface_obj->IsSolid()) { ON_wString obj_SurfLoc = obj_Surf[j]; /*IS THE CUTTING SURFACE?*/ if( obj_SurfLoc.Compare(name) == 0 ) { R++; } }/*CHIUSURA IF SUPERFICIE*/ } // /************************/ // /*PICK THE BREP TO SPLIT*/ // /************************/ // CRhinoGetObject go; // go.SetCommandPrompt( L"SELECT SOLID TO SPLIT" ); // go.SetGeometryFilter( CRhinoGetObject::polysrf_object );//CRhinoGetObject::surface_object | CRhinoGetObject::polysrf_object // go.GetObjects( 1, 1 ); // if( go.CommandResult() != success ) // { //return go.CommandResult(); // } // else // { // RhinoMessageBox(L"CYLINDER IS SELECTED", PlugIn()->PlugInName(), MB_OK | MB_ICONEXCLAMATION ); // } // // const CRhinoObjRef& split_ref = go.Object(0); // // const CRhinoObject* split_object = split_ref.Object(); // if( !split_object ) // { //return failure; // } // // const ON_Brep* split = split_ref.Brep(); // if( !split ) // { //return failure; // } // ON_SimpleArray<ON_Brep*> pieces; // double tol = context.m_doc.AbsoluteTolerance(); // /***********************/ // /*PICK THE CUTTING BREP*/ // /***********************/ // go.SetCommandPrompt( L"SELECT CUTTING SURFACE OR POLYSUFACE" ); // go.SetGeometryFilter( CRhinoGetObject::surface_object | CRhinoGetObject::polysrf_object ); // go.EnablePreSelect( FALSE ); // go.EnableDeselectAllBeforePostSelect( FALSE ); // go.GetObjects( 1, 2 ); // if( go.CommandResult() != success ) // { //return go.CommandResult(); // } // const ON_Brep* cutter = go.Object(0).Brep(); // if( !cutter ) // { //return failure; // } // // if( !RhinoBrepSplit(*split, *cutter, tol, pieces) ) // { //RhinoApp().Print( L"UNABLE TO SPLIT BREP.\n" ); // } // // int i, count = pieces.Count(); // if( count == 0 | count == 1 ) // { //if( count == 1 ) //{ // delete pieces[0]; //} //return nothing; // } // // CRhinoObjectAttributes attrib = split_object->Attributes(); // attrib.m_uuid = ON_nil_uuid; // // const CRhinoObjectVisualAnalysisMode* vam_list = split_object->m_analysis_mode_list; // // for( i = 0; i < count; i++ ) // { //CRhinoBrepObject* brep_object = new CRhinoBrepObject( attrib ); //if( brep_object ) //{ // brep_object->SetBrep( pieces[i] ); // if( context.m_doc.AddObject(brep_object) ) // { // RhinoCopyAnalysisModes( vam_list, brep_object ); // } // else // { // delete brep_object; // } //} // } // // context.m_doc.DeleteObject( split_ref ); // context.m_doc.Redraw(); } /*CREATE A NEW LAYER*/ ON_Layer layer; int layer_index = 0; ON_Color color = ON_Color(0, 0, 0); ON_wString layer_name_FONDELLO = L"FONDELLO"; layer.SetLayerName( layer_name_FONDELLO ); layer.SetPlotColor(color.Green()); /*ADD THE LAYER TO THE LAYER TABLE*/ layer_index = context.m_doc.m_layer_table.AddLayer( layer ); ON_wString layer_name_MATRICE = L"MATRICE"; layer.SetLayerName( layer_name_MATRICE ); layer.SetColor(color.Red()); /*ADD THE LAYER TO THE LAYER TABLE*/ layer_index = context.m_doc.m_layer_table.AddLayer( layer ); ON_wString layer_name_FISSO = L"FISSO"; layer.SetLayerName( layer_name_FISSO ); layer.SetColor(color.Blue()); /*ADD THE LAYER TO THE LAYER TABLE*/ layer_index = context.m_doc.m_layer_table.AddLayer( layer ); context.m_doc.Redraw(); /*********************************************************/ } }/*CLOSED IF OVER CHECKING BREP COUNT OBJECT*/ else { RhinoMessageBox(L"THERE IS ALREADY A CYLINDER OBJECT", PlugIn()->PlugInName(), MB_OK | MB_ICONEXCLAMATION ); } } /**********************************************************************/ return CRhinoCommand::success; }
bool ON_BrepExtrude( ON_Brep& brep, const ON_Curve& path_curve, bool bCap ) { ON_Workspace ws; const int vcount0 = brep.m_V.Count(); const int tcount0 = brep.m_T.Count(); const int lcount0 = brep.m_L.Count(); const int ecount0 = brep.m_E.Count(); const int fcount0 = brep.m_F.Count(); const ON_3dPoint PathStart = path_curve.PointAtStart(); ON_3dPoint P = path_curve.PointAtEnd(); if ( !PathStart.IsValid() || !P.IsValid() ) return false; const ON_3dVector height = P - PathStart; if ( !height.IsValid() || height.Length() <= ON_ZERO_TOLERANCE ) return false; ON_Xform tr; tr.Translation(height); // count number of new sides int side_count = 0; int i, vi, ei, fi; bool* bSideEdge = (bool*)ws.GetIntMemory(ecount0*sizeof(bSideEdge[0])); for ( ei = 0; ei < ecount0; ei++ ) { const ON_BrepEdge& e = brep.m_E[ei]; if ( 1 == e.m_ti.Count() ) { side_count++; bSideEdge[ei] = true; } else { bSideEdge[ei] = false; } } brep.m_V.Reserve( 2*vcount0 ); i = 4*side_count + (bCap?tcount0:0); brep.m_T.Reserve( tcount0 + i ); brep.m_C2.Reserve( brep.m_C2.Count() + i ); brep.m_L.Reserve( lcount0 + side_count + (bCap?lcount0:0) ); i = side_count + (bCap?ecount0:side_count); brep.m_E.Reserve( ecount0 + i ); brep.m_C3.Reserve( brep.m_C3.Count() + i ); i = side_count + (bCap?fcount0:0); brep.m_F.Reserve( fcount0 + i ); brep.m_S.Reserve( brep.m_S.Count() + i ); bool bOK = true; // build top vertices int* topvimap = ws.GetIntMemory(vcount0); memset(topvimap,0,vcount0*sizeof(topvimap[0])); if ( bCap ) { for ( vi = 0; vi < vcount0; vi++ ) { const ON_BrepVertex& bottomv = brep.m_V[vi]; ON_BrepVertex& topv = brep.NewVertex(bottomv.point+height,bottomv.m_tolerance); topvimap[vi] = topv.m_vertex_index; } } else { for ( ei = 0; ei < ecount0; ei++ ) { if ( bSideEdge[ei] ) { const ON_BrepEdge& bottome = brep.m_E[ei]; int bottomvi0 = bottome.m_vi[0]; if ( bottomvi0 < 0 || bottomvi0 >= vcount0 ) { bOK = false; break; } int bottomvi1 = bottome.m_vi[1]; if ( bottomvi1 < 0 || bottomvi1 >= vcount0 ) { bOK = false; break; } if ( !topvimap[bottomvi0] ) { const ON_BrepVertex& bottomv = brep.m_V[bottomvi0]; ON_BrepVertex& topv = brep.NewVertex(bottomv.point+height,bottomv.m_tolerance); topvimap[bottomvi0] = topv.m_vertex_index; } if ( !topvimap[bottomvi1] ) { const ON_BrepVertex& bottomv = brep.m_V[bottomvi1]; ON_BrepVertex& topv = brep.NewVertex(bottomv.point+height,bottomv.m_tolerance); topvimap[bottomvi1] = topv.m_vertex_index; } } } } // build top edges int* topeimap = ws.GetIntMemory(ecount0); memset(topeimap,0,ecount0*sizeof(topeimap[0])); if ( bOK ) for ( ei = 0; ei < ecount0; ei++ ) { if ( bCap || bSideEdge[ei] ) { const ON_BrepEdge& bottome = brep.m_E[ei]; ON_BrepVertex& topv0 = brep.m_V[topvimap[bottome.m_vi[0]]]; ON_BrepVertex& topv1 = brep.m_V[topvimap[bottome.m_vi[1]]]; ON_Curve* c3 = bottome.DuplicateCurve(); if ( !c3 ) { bOK = false; break; } c3->Transform(tr); int c3i = brep.AddEdgeCurve(c3); ON_BrepEdge& tope = brep.NewEdge(topv0,topv1,c3i,0,bottome.m_tolerance); topeimap[ei] = tope.m_edge_index; } } // build side edges int* sideveimap = ws.GetIntMemory(vcount0); memset(sideveimap,0,vcount0*sizeof(sideveimap[0])); if ( bOK ) for ( vi = 0; vi < vcount0; vi++ ) { ON_BrepVertex& bottomv = brep.m_V[vi]; for ( int vei = 0; vei < bottomv.m_ei.Count(); vei++ ) { if ( bSideEdge[bottomv.m_ei[vei]] && topvimap[vi] ) { ON_BrepVertex& topv = brep.m_V[topvimap[vi]]; ON_Curve* c3 = path_curve.DuplicateCurve(); if ( !c3 ) { bOK = false; } else { ON_3dVector D = bottomv.point - PathStart; c3->Translate(D); int c3i = brep.AddEdgeCurve(c3); const ON_BrepEdge& e = brep.NewEdge(bottomv,topv,c3i,0,0.0); sideveimap[vi] = e.m_edge_index; } break; } } } if ( bOK && bCap ) { // build top faces for (fi = 0; fi < fcount0; fi++ ) { const ON_BrepFace& bottomf = brep.m_F[fi]; ON_Surface* srf = bottomf.DuplicateSurface(); if ( !srf ) { bOK = false; break; } srf->Transform(tr); int si = brep.AddSurface(srf); ON_BrepFace& topf = brep.NewFace(si); topf.m_bRev = !bottomf.m_bRev; const int loop_count = bottomf.m_li.Count(); topf.m_li.Reserve(loop_count); for ( int fli = 0; fli < loop_count; fli++ ) { const ON_BrepLoop& bottoml = brep.m_L[bottomf.m_li[fli]]; ON_BrepLoop& topl = brep.NewLoop(bottoml.m_type,topf); const int loop_trim_count = bottoml.m_ti.Count(); topl.m_ti.Reserve(loop_trim_count); for ( int lti = 0; lti < loop_trim_count; lti++ ) { const ON_BrepTrim& bottomt = brep.m_T[bottoml.m_ti[lti]]; ON_NurbsCurve* c2 = ON_NurbsCurve::New(); if ( !bottomt.GetNurbForm(*c2) ) { delete c2; bOK = false; break; } int c2i = brep.AddTrimCurve(c2); ON_BrepTrim* topt = 0; if ( bottomt.m_ei >= 0 ) { ON_BrepEdge& tope = brep.m_E[topeimap[bottomt.m_ei]]; topt = &brep.NewTrim(tope,bottomt.m_bRev3d,topl,c2i); } else { // singular trim ON_BrepVertex& topv = brep.m_V[topvimap[bottomt.m_vi[0]]]; topt = &brep.NewSingularTrim(topv,topl,bottomt.m_iso,c2i); } topt->m_tolerance[0] = bottomt.m_tolerance[0]; topt->m_tolerance[1] = bottomt.m_tolerance[1]; topt->m_pbox = bottomt.m_pbox; topt->m_type = bottomt.m_type; topt->m_iso = bottomt.m_iso; } topl.m_pbox = bottoml.m_pbox; } } } // build sides int bRev3d[4] = {0,0,1,1}; int vid[4], eid[4]; if( bOK ) for ( ei = 0; ei < ecount0; ei++ ) { if ( bSideEdge[ei] && topeimap[ei] ) { ON_BrepEdge& bottome = brep.m_E[ei]; ON_BrepEdge& tope = brep.m_E[topeimap[ei]]; vid[0] = bottome.m_vi[0]; vid[1] = bottome.m_vi[1]; vid[2] = topvimap[vid[1]]; vid[3] = topvimap[vid[0]]; if ( sideveimap[vid[0]] && sideveimap[vid[1]] ) { ON_BrepEdge& leftedge = brep.m_E[sideveimap[vid[0]]]; ON_BrepEdge& rightedge = brep.m_E[sideveimap[vid[1]]]; ON_Curve* cx = bottome.DuplicateCurve(); if ( !cx ) { bOK = false; break; } ON_Curve* cy = leftedge.DuplicateCurve(); if ( !cy ) { delete cx; bOK = false; break; } ON_SumSurface* srf = new ON_SumSurface(); srf->m_curve[0] = cx; srf->m_curve[1] = cy; srf->m_basepoint = srf->m_curve[1]->PointAtStart(); srf->m_basepoint.x = -srf->m_basepoint.x; srf->m_basepoint.y = -srf->m_basepoint.y; srf->m_basepoint.z = -srf->m_basepoint.z; eid[0] = bottome.m_edge_index; eid[1] = rightedge.m_edge_index; eid[2] = tope.m_edge_index; eid[3] = leftedge.m_edge_index; ON_BrepFace* face = brep.NewFace(srf,vid,eid,bRev3d); if ( !face ) { bOK = false; break; } else if ( bottome.m_ti.Count() == 2 ) { const ON_BrepTrim& trim0 = brep.m_T[bottome.m_ti[0]]; const ON_BrepTrim& trim1 = brep.m_T[bottome.m_ti[1]]; const ON_BrepLoop& loop0 = brep.m_L[trim0.m_li]; const ON_BrepLoop& loop1 = brep.m_L[trim1.m_li]; bool bBottomFaceRev = brep.m_F[(loop0.m_fi != face->m_face_index) ? loop0.m_fi : loop1.m_fi].m_bRev; bool bSideFaceRev = ( trim0.m_bRev3d != trim1.m_bRev3d ) ? bBottomFaceRev : !bBottomFaceRev; face->m_bRev = bSideFaceRev; } } } } if ( !bOK ) { for ( vi = brep.m_V.Count(); vi >= vcount0; vi-- ) { brep.DeleteVertex(brep.m_V[vi]); } } return bOK; }