public static Brep GetTrimmedBrep(pps.Surface surface, SurfaceConverter surfConv, CurveConverter curveConv) { var rhSurf = surfConv.FromPipe <Surface, pps.Surface>(surface); var brep = Brep.CreateFromSurface(rhSurf); //if (!brep.IsValid) { brep.Repair(Rhino.RhinoMath.ZeroTolerance); } if (typeof(pps.NurbsSurface).IsAssignableFrom(surface.GetType()) && ((pps.NurbsSurface)surface).TrimCurves.Count > 0) { List <ppc.Curve> trims = ((pps.NurbsSurface)surface).TrimCurves; List <ppc.Curve> loops = brep.Faces.First().Loops.Select((l) => curveConv.ToPipe <Curve, ppc.Curve>(l.To3dCurve())).ToList(); if (!PipeDataUtil.EqualIgnoreOrder(loops, trims)) { var rhTrims = trims.Select((c) => curveConv.FromPipe <Curve, ppc.Curve>(c)).ToList(); var faceToSplit = brep.Faces.First(); var brep2 = faceToSplit.Split(rhTrims, Rhino.RhinoMath.ZeroTolerance); //if (brep2 != null && !brep2.IsValid) { brep2.Repair(Rhino.RhinoMath.ZeroTolerance); } if (brep2 != null && brep2.IsValid) { brep = GetEnclosedFacesAsBrep(brep2, rhTrims) ?? brep2; } } } return(brep); }
public GeometryConverter() { var ptConv = AddConverter(new PointConverter(_pt3dConv)); var curveConv = new CurveConverter(_pt3dConv, _arcConv, _lineConv); AddConverter(curveConv); var meshConv = AddConverter(new MeshConverter(_pt3fConv)); var surfaceConverter = new SurfaceConverter(curveConv, _vec3DConv, _pt3dConv); AddConverter(surfaceConverter); var brepConv = new BrepConverter(surfaceConverter, curveConv, _pt3dConv); AddConverter(brepConv); }
public BrepConverter(SurfaceConverter surfConv, CurveConverter curveConv, Point3dConverter ptConv) : base( (rb) => { List <pps.Surface> faces = new List <pps.Surface>(); List <List <int> > adjacency = new List <List <int> >(); for (int i = 0; i < rb.Faces.Count; i++) { var surf = (pps.NurbsSurface)surfConv.ToPipe <rh.Surface, pps.Surface>(rb.Faces[i].ToNurbsSurface()); surf.OuterTrims.Clear(); surf.OuterTrims.AddRange(rb.Faces[i].Loops.Where((l) => l.LoopType == rh.BrepLoopType.Outer).Select((l) => curveConv.ToPipe <rh.Curve, ppc.Curve>(l.To3dCurve()))); surf.InnerTrims.Clear(); surf.InnerTrims.AddRange(rb.Faces[i].Loops.Where((l) => l.LoopType == rh.BrepLoopType.Inner).Select((l) => curveConv.ToPipe <rh.Curve, ppc.Curve>(l.To3dCurve()))); faces.Add(surf); adjacency.Add(rb.Faces[i].AdjacentFaces().ToList()); } var polySurf = new pps.PolySurface(faces, adjacency); polySurf.IsSolid = rb.IsSolid; return(polySurf); }, (pb) => { if (pb.Surfaces.Count <= 0) { return(null); } rh.Brep brep = null; int attempts = 0; while (attempts < 15) { if (pb.Surfaces.Count == 1) { var surf = pb.Surfaces.FirstOrDefault(); brep = Util.GetTrimmedBrep(surf, surfConv, curveConv); } else { for (int i = 0; i < pb.Surfaces.Count; i++) { pps.Surface s = pb.Surfaces[i]; var subrep = Util.GetTrimmedBrep(s, surfConv, curveConv); //if (!subrep.IsValid) { subrep.Repair(Rhino.RhinoMath.ZeroTolerance); } if (i == 0) { brep = subrep; } else { //brep = Brep.MergeSurfaces(brep, subrep, Rhino.RhinoMath.ZeroTolerance, // Rhino.RhinoMath.ZeroTolerance, Point2d.Unset, Point2d.Unset, 0.0, true) ?? // Brep.MergeSurfaces(brep, subrep, Rhino.RhinoMath.ZeroTolerance, Rhino.RhinoMath.ZeroTolerance) ?? // Brep.JoinBreps(new List<Brep>() { brep, subrep }, Rhino.RhinoMath.ZeroTolerance).First(); brep = rh.Brep.MergeBreps(new List <rh.Brep>() { brep, subrep }, Rhino.RhinoMath.ZeroTolerance); } } } attempts += 1; //not doing anymore attempts if this time was successful if (brep.IsValid) { break; } } string msg; if (!brep.IsValidWithLog(out msg)) { System.Diagnostics.Debug.WriteLine(msg); attempts = 0; while (!brep.IsValid && attempts < 15) { brep = rh.Brep.MergeBreps(pb.Surfaces.Select((s) => rh.Brep.CreateFromSurface(surfConv.FromPipe <rh.Surface, pps.Surface>(s))), Rhino.RhinoMath.ZeroTolerance); attempts += 1; } if (brep.IsValid) { return(brep); } throw new InvalidOperationException("Failed to create a valid brep from " + "received data because: \n" + msg); } return(brep); } ) { }