public ICollection<DesignBody> CreateSolid(Part mainPart) { var bands = new List<ICollection<Body>>(); var cutters = new List<Body[]>(); double newScale = 0.094; double cutterHeight = 0.01 / newScale; double cutterWidth = 0.0005 / newScale; bool swap = false; for (int i = 0; i < iSteps; i++) { var band = new List<Body>(); //if (i == 4) { // DesignCurve.Create(Window.ActiveWindow.Scene as Part, CurveSegment.Create(points[i][0], points[i][1])); // DesignCurve.Create(Window.ActiveWindow.Scene as Part, CurveSegment.Create(points[i + iSteps / 2][jSteps / 2], points[i + iSteps / 2][jSteps / 2 + 1])); //} for (int j = 0; j < jSteps; j++) { // Main ring Point p00 = points[i][j]; Point p01 = points[i][j + 1]; Point p10 = points[i + 1][j]; Point p11 = points[i + 1][j + 1]; Body b0, b1; if ((p00 - p11).Magnitude < (p10 - p01).Magnitude) { b0 = ShapeHelper.CreatePolygon(new Point[] { p00, p01, p11 }, 0); b1 = ShapeHelper.CreatePolygon(new Point[] { p00, p11, p10 }, 0); } else { b0 = ShapeHelper.CreatePolygon(new Point[] { p01, p10, p00 }, 0); b1 = ShapeHelper.CreatePolygon(new Point[] { p01, p11, p10 }, 0); } // Tabs /* Male Female Male * ---p00last-------p00--------p01-------p10next--- v+ * | | | | * | pn0 | | pn1 | * | | | | * ---p10last-------p10--------p11-------p11next--- * */ Point pn0 = (new Point[] { points[i - 1][j], points[i - 1][j + 1] }).Average(); Point pn1 = (new Point[] { points[i + 2][j], points[i + 2][j + 1] }).Average(); Direction normal0 = Vector.Cross(p01 - pn0, p00 - pn0).Direction; Direction normal1 = Vector.Cross(p10 - pn1, p11 - pn1).Direction; Body tab0 = Tabs.CreateCircularTab(p01, p00, -normal0, tabAngles[i][j], swap); Body tab1 = Tabs.CreateCircularTab(p10, p11, -normal1, tabAngles[i + 1][j], !swap); //DesignBody annotateMe = DesignBody.Create(mainPart, "annotatme", (swap ? tab0 : tab1).Copy()); //NoteHelper.AnnotateFace(mainPart, annotateMe.Faces.First(), string.Format("{0},{1}", i, j), 0.02, null); //annotateMe.Delete(); //DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", b0.Copy()); //DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", b1.Copy()); //DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", tab0.Copy()); //DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", tab1.Copy()); try { b0.Unite(new Body[] { b1, tab0, tab1 }); } catch { DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", b0.Copy()); DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", b1.Copy()); DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", tab0.Copy()); DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", tab1.Copy()); return null; } // Debug.Assert(b0.Shells.Count == 1); band.Add(b0); swap = !swap; } bands.Add(band.TryUnionBodies()); // Cutters Point p0ThisSide0 = points[i][0]; Point p0ThisSide1 = points[i][1]; Point p0OtherSide0 = points[i + iSteps / 2][jSteps / 2]; Point p0OtherSide1 = points[i + iSteps / 2][1 + jSteps / 2]; Point p1ThisSide0 = points[i + 1][0]; Point p1ThisSide1 = points[i + 1][1]; Point p1OtherSide0 = points[i + 1 + iSteps / 2][jSteps / 2]; Point p1OtherSide1 = points[i + 1 + iSteps / 2][1 + jSteps / 2]; Point p0 = CurveSegment.Create(p0ThisSide0, p0ThisSide1).GetInBetweenPoint( CurveSegment.Create(p0OtherSide0, p0OtherSide1 )); Point p1 = CurveSegment.Create(p1ThisSide0, p1ThisSide1).GetInBetweenPoint( CurveSegment.Create(p1OtherSide0, p1OtherSide1 )); //Point p0 = CurveSegment.Create(p0ThisSide0, p0ThisSide1).IntersectCurve( // CurveSegment.Create(p0OtherSide0, p0OtherSide1 //)).First().Point; //Point p1 = CurveSegment.Create(p1ThisSide0, p1ThisSide1).IntersectCurve( // CurveSegment.Create(p1OtherSide0, p1OtherSide1 //)).First().Point; Direction n0 = (p0OtherSide1 - p0OtherSide0).Direction; Direction n1 = (p1OtherSide1 - p1OtherSide0).Direction; Direction d0 = (p0ThisSide1 - p0ThisSide0).Direction; Direction d1 = (p1ThisSide1 - p1ThisSide0).Direction; var profiles = new List<ICollection<ITrimmedCurve>>(); profiles.Add(p0.GetRectanglePointsAround(d0 * cutterHeight, n0 * cutterWidth).GetProfile()); profiles.Add(p1.GetRectanglePointsAround(d1 * cutterHeight, n1 * cutterWidth).GetProfile()); Body cutterA = Body.LoftProfiles(profiles, false, true); profiles = new List<ICollection<ITrimmedCurve>>(); profiles.Add(p0.GetRectanglePointsAround(n0 * cutterHeight, d0 * cutterWidth).GetProfile()); profiles.Add(p1.GetRectanglePointsAround(n1 * cutterHeight, d1 * cutterWidth).GetProfile()); Body cutterB = Body.LoftProfiles(profiles, false, true); cutters.Add(new Body[] { cutterA, cutterB }); } var designBands = new List<DesignBody>(); Layer cutterLayer = NoteHelper.CreateOrGetLayer(mainPart.Document, "Cutters", System.Drawing.Color.DarkViolet); Matrix scaleMatrix = Matrix.CreateScale(scale, Point.Origin); for (int i = 0; i < bands.Count; i++) { int whichCutter = i % 2; Part part = Part.Create(mainPart.Document, string.Format("Band {0:00}", i)); Component.Create(mainPart, part); int ii = i; if (whichCutter == 0) ii = i + iSteps / 2; List<Body> mergedCutters = new Body[] { cutters[(ii + iSteps - 1) % iSteps][whichCutter].Copy(), cutters[ii % iSteps][whichCutter].Copy(), cutters[(ii + 1) % iSteps][whichCutter].Copy() }.TryUnionBodies().ToList(); Debug.Assert(mergedCutters.Count == 1, "Couldn't merge cutters"); double nominalRadius = 0.02; double innerRadius = (nominalRadius - cutterWidth / 2) / newScale; double outerRadius = (nominalRadius + cutterWidth / 2) / newScale; var edgeRounds = new List<KeyValuePair<Edge, EdgeRound>>(); foreach (Edge edge in mergedCutters[0].Edges) { if (edge.Length > cutterHeight * 1.1 || edge.Length < cutterHeight * 0.9) continue; double angle = edge.GetAngle(); if (Math.Abs(angle) > Math.PI / 4 || angle == 0) continue; edgeRounds.Add(new KeyValuePair<Edge, EdgeRound>(edge, new FixedRadiusRound(angle > 0 ? outerRadius : innerRadius))); } mergedCutters[0].RoundEdges(edgeRounds); mergedCutters.Add(cutters[(ii - 1 + iSteps / 2) % iSteps][1 - whichCutter].Copy()); mergedCutters.Add(cutters[(ii + 1 + iSteps / 2) % iSteps][1 - whichCutter].Copy()); HSBColor hsbColor = new HSBColor(0, 100, 200); hsbColor.H = (float) ((double) i / bands.Count * 360); var cutBand = new List<Body>(); foreach (Body body in bands[i]) { foreach (Body cutterBody in mergedCutters) { body.Imprint(cutterBody); foreach (Face face in body.Faces) { if (!IsSpanningBody(face, cutterBody)) continue; body.DeleteFaces(new Face[] { face }, RepairAction.None); // DesignBody designBody = DesignBody.Create(part, "Cutter", cutterBody.Copy()); // designBody.SetColor(null, hsbColor.Color); } } cutBand.AddRange(body.SeparatePieces()); } cutBand = cutBand.TryUnionBodies().ToList(); //foreach (Body body in bands[i]) { foreach (Body body in cutBand) { body.Transform(scaleMatrix); DesignBody designBody = DesignBody.Create(part, "Band", body); designBody.SetColor(null, hsbColor.Color); designBands.Add(designBody); } //foreach (Body body in mergedCutters) { // DesignBody designBody = DesignBody.Create(part, "Cutter", body); // designBody.Layer = cutterLayer; // hsbColor.H += 180 * whichCutter; // designBody.SetColor(null, hsbColor.Color); //// designBands[i].Shape.Imprint(designBody.Shape); //} } Trace.WriteLine("vParameters"); for (int j = 0; j < jSteps; j++) { for (int i = 0; i < iSteps; i++) Trace.Write(vParameters[i][j] + " "); Trace.WriteLine(""); } Trace.WriteLine("tabAngles"); for (int j = 0; j < jSteps; j++) { for (int i = 0; i < iSteps; i++) Trace.Write(tabAngles[i][j] + " "); Trace.WriteLine(""); } return designBands; }
public Graphic GetGraphic() { var graphics = new List<Graphic>(); int iSteps = points.Count / 2; int jSteps = points[0].Count; // Body body = Tabs.CreateCircularTab(p0, p1, normal, angle, isMale); bool swap = false; for (int i = 0; i < iSteps; i++) { var facetVertices = new List<FacetVertex>(); var facets = new List<Facet>(); var tabMeshes = new List<MeshPrimitive>(); for (int j = 0; j < jSteps; j++) { int facetOffset = facetVertices.Count; // Main ring Point p00 = points[i][j]; Point p01 = points[i][j + 1]; Point p10 = points[i + 1][j]; Point p11 = points[i + 1][j + 1]; //Vector n0 = Vector.Cross(p00 - p01, p00 - p10); //Vector n1 = Vector.Cross(p11 - p10, p11 - p01); //Direction n = (n0 + n1).Direction; //facetVertices.Add(new FacetVertex(p00, n)); //facetVertices.Add(new FacetVertex(p01, n)); //facetVertices.Add(new FacetVertex(p10, n)); //facetVertices.Add(new FacetVertex(p11, n)); //if ((p00 - p11).Magnitude < (p10 - p01).Magnitude) { // facets.Add(new Facet(0 + facetOffset, 1 + facetOffset, 3 + facetOffset)); // facets.Add(new Facet(0 + facetOffset, 3 + facetOffset, 2 + facetOffset)); //} //else { // facets.Add(new Facet(0 + facetOffset, 2 + facetOffset, 1 + facetOffset)); // facets.Add(new Facet(1 + facetOffset, 3 + facetOffset, 2 + facetOffset)); //} Point pn0 = (new Point[] { points[i - 1][j], points[i - 1][j + 1] }).Average(); Point pn1 = (new Point[] { points[i + 2][j], points[i + 2][j + 1] }).Average(); Direction normal0 = Vector.Cross(p01 - pn0, p00 - pn0).Direction; Direction normal1 = Vector.Cross(p10 - pn1, p11 - pn1).Direction; tabMeshes.Add(Tabs.GetCircularTabMesh(p01, p00, -normal0, tabAngles[i][j], swap)); tabMeshes.Add(Tabs.GetCircularTabMesh(p10, p11, -normal1, tabAngles[i + 1][j], !swap)); swap = !swap; } HSBColor hsbFill = new HSBColor(255, (float) i / iSteps * 360, 122, 88); HSBColor hsbLine = new HSBColor(System.Drawing.Color.MidnightBlue); hsbLine.H = (float) i / iSteps * 360; var style = new GraphicStyle { EnableDepthBuffer = true, LineColor = hsbLine.Color, LineWidth = 1, FillColor = hsbFill.Color, EnableCulling = true }; // graphics.Add(Graphic.Create(style, MeshPrimitive.Create(facetVertices, facets))); foreach (MeshPrimitive mesh in tabMeshes) graphics.Add(Graphic.Create(style, mesh)); } return Graphic.Create(null, null, graphics); }
public ICollection<DesignBody> CreateCircularTabsOnly(Part mainPart) { var bands = new List<ICollection<Body>>(); var cutters = new List<Body[]>(); double newScale = 0.094; double cutterHeight = 0.01 / newScale; double cutterWidth = 0.0005 / newScale; bool swap = false; for (int i = 0; i < iSteps; i++) { var band = new List<Body>(); for (int j = 0; j < jSteps; j++) { // Main ring Point p00 = points[i][j]; Point p01 = points[i][j + 1]; Point p10 = points[i + 1][j]; Point p11 = points[i + 1][j + 1]; // Tabs /* Male Female Male * ---p00last-------p00--------p01-------p01next--- v+ * | | | | * | pn0 | | pn1 | * | | | | * ---p10last-------p10--------p11-------p11next--- * */ Point pn0 = (new Point[] { points[i - 1][j], points[i - 1][j + 1] }).Average(); Point pn1 = (new Point[] { points[i + 2][j], points[i + 2][j + 1] }).Average(); Direction normal0 = Vector.Cross(p01 - pn0, p00 - pn0).Direction; Direction normal1 = Vector.Cross(p10 - pn1, p11 - pn1).Direction; Body tab0 = Tabs.CreateCircularTab(p01, p00, -normal0, tabAngles[i][j], swap); Body tab1 = Tabs.CreateCircularTab(p10, p11, -normal1, tabAngles[i + 1][j], !swap); // Debug.Assert(b0.Shells.Count == 1); band.Add(tab0); band.Add(tab1); swap = !swap; } bands.Add(band); } var designBands = new List<DesignBody>(); Matrix scaleMatrix = Matrix.CreateScale(scale, Point.Origin); for (int i = 0; i < bands.Count; i++) { Part part = Part.Create(mainPart.Document, string.Format("Band {0:00}", i)); Component.Create(mainPart, part); HSBColor hsbColor = new HSBColor(0, 100, 200); hsbColor.H = (float) ((double) i / bands.Count * 360); foreach (Body body in bands[i]) { body.Transform(scaleMatrix); DesignBody designBody = DesignBody.Create(part, "Band", body); designBody.SetColor(null, hsbColor.Color); designBands.Add(designBody); } } return designBands; }
//private Point GetFivePointNurbsAverage(int[,] indices) { // var knots = new Knot[] { // new Knot(0, 4), // new Knot(1, 4) // }; // var controlPoints = new ControlPoint[] { // new ControlPoint(Point.Create(tabAngles[indices[0,0]][indices[0,1]], GetSpanAt(indices[0,0], indices[0,1]),0), 1), // new ControlPoint(Point.Create(tabAngles[indices[1,0]][indices[1,1]], GetSpanAt(indices[1,0], indices[1,1]),0), 1), // new ControlPoint(Point.Create(tabAngles[indices[2,0]][indices[2,1]], GetSpanAt(indices[2,0], indices[2,1]),0), 1), // new ControlPoint(Point.Create(tabAngles[indices[3,0]][indices[3,1]], GetSpanAt(indices[3,0], indices[3,1]),0), 1), // new ControlPoint(Point.Create(tabAngles[indices[4,0]][indices[4,1]], GetSpanAt(indices[4,0], indices[4,1]),0), 1) // }; // NurbsData data = new NurbsData(5, false, false, knots); // NurbsCurve curve = NurbsCurve.CreateFromControlPoints(data, controlPoints); // Point midpoint = curve.Evaluate(0.5).Point; // return midpoint; //} //private Circle GetTabFromPoints(bool swap, int i, int j, int iOther) { // Point p0 = points[i,j]; // Point p1 = points[i,j + 1]; // Point pn = (new Point[] { points[iOther,j], points[iOther,j + 1] }).Average(); // Direction normal = Vector.Cross(p0 - pn, p1 - pn).Direction; // return Tabs.GetCircularTabCircle(p0, p1, normal, tabAngles[i,j], swap); //} //private double GetSpanAt(int i, int j) { // return (vParameters[i][j + 1] - vParameters[i][j] + Math.PI * 2) % (Math.PI * 2); //} public Graphic GetGraphic() { var graphics = new List<Graphic>(); int iSteps = points.GetLength(0); int jSteps = points.GetLength(1); bool swap = false; for (int i = 0; i < iSteps - 1; i++) { var facetVertices = new List<FacetVertex>(); var facets = new List<Facet>(); var tabMeshes = new List<MeshPrimitive>(); var porcupineCurves = new List<CurvePrimitive>(); for (int j = 0; j < jSteps - 1; j++) { int facetOffset = facetVertices.Count; // Main ring Point p00 = points[i, j]; Point p01 = points[i, j + 1]; Point p10 = points[i + 1, j]; Point p11 = points[i + 1, j + 1]; Vector n0 = Vector.Cross(p00 - p01, p00 - p10); Vector n1 = Vector.Cross(p11 - p10, p11 - p01); Direction n = (n0 + n1).Direction; facetVertices.Add(new FacetVertex(p00, n)); facetVertices.Add(new FacetVertex(p01, n)); facetVertices.Add(new FacetVertex(p10, n)); facetVertices.Add(new FacetVertex(p11, n)); if ((p00 - p11).Magnitude < (p10 - p01).Magnitude) { facets.Add(new Facet(0 + facetOffset, 1 + facetOffset, 3 + facetOffset)); facets.Add(new Facet(0 + facetOffset, 3 + facetOffset, 2 + facetOffset)); } else { facets.Add(new Facet(0 + facetOffset, 2 + facetOffset, 1 + facetOffset)); facets.Add(new Facet(1 + facetOffset, 3 + facetOffset, 2 + facetOffset)); } tabMeshes.Add(MeshPrimitive.Create(facetVertices, facets)); Point basePoint = points[i, j]; Point endPoint = basePoint + Gradient(basePoint) / 10; if (basePoint != endPoint) porcupineCurves.Add(CurvePrimitive.Create(CurveSegment.Create(basePoint, endPoint))); swap = !swap; } HSBColor hsbFill = new HSBColor(255, (float) i / iSteps * 360, 122, 88); HSBColor hsbLine = new HSBColor(System.Drawing.Color.MidnightBlue); hsbLine.H = (float) i / iSteps * 360; var style = new GraphicStyle { EnableDepthBuffer = true, LineColor = hsbLine.Color, LineWidth = 1, FillColor = hsbFill.Color, EnableCulling = false }; // graphics.Add(Graphic.Create(style, MeshPrimitive.Create(facetVertices, facets))); foreach (MeshPrimitive mesh in tabMeshes) graphics.Add(Graphic.Create(style, mesh)); foreach (CurvePrimitive curve in porcupineCurves) graphics.Add(Graphic.Create(style, curve)); } return Graphic.Create(null, null, graphics); }
public static HSBColor FromColor(Color color) { HSBColor ret = new HSBColor(0f, 0f, 0f); ret.a = color.A; float r = color.R; float g = color.G; float b = color.B; float max = Math.Max(r, Math.Max(g, b)); if (max <= 0) { return ret; } float min = Math.Min(r, Math.Min(g, b)); float dif = max - min; if (max > min) { if (g == max) { ret.h = (b - r) / dif * 60f + 120f; } else if (b == max) { ret.h = (r - g) / dif * 60f + 240f; } else if (b > g) { ret.h = (g - b) / dif * 60f + 360f; } else { ret.h = (g - b) / dif * 60f; } if (ret.h < 0) { ret.h = ret.h + 360f; } } else { ret.h = 0; } ret.s = (dif / max) * 255f; ret.b = max; return ret; }
public static Color FromHSB(HSBColor hsbColor) { float r = hsbColor.b; float g = hsbColor.b; float b = hsbColor.b; if (hsbColor.s != 0) { float max = hsbColor.b; float dif = hsbColor.b * hsbColor.s / 255f; float min = hsbColor.b - dif; float h = hsbColor.h; if (h < 60f) { r = max; g = h * dif / 60f + min; b = min; } else if (h < 120f) { r = -(h - 120f) * dif / 60f + min; g = max; b = min; } else if (h < 180f) { r = min; g = max; b = (h - 120f) * dif / 60f + min; } else if (h < 240f) { r = min; g = -(h - 240f) * dif / 60f + min; b = max; } else if (h < 300f) { r = (h - 240f) * dif / 60f + min; g = min; b = max; } else if (h <= 360f) { r = max; g = min; b = -(h - 360f) * dif / 60 + min; } else { r = 0; g = 0; b = 0; } } return Color.FromArgb ( hsbColor.a, (int) Math.Round(Math.Min(Math.Max(r, 0), 255)), (int) Math.Round(Math.Min(Math.Max(g, 0), 255)), (int) Math.Round(Math.Min(Math.Max(b, 0), 255)) ); }
public static Color FromHSB(HSBColor hsbColor) { float r = hsbColor.b; float g = hsbColor.b; float b = hsbColor.b; if (hsbColor.s != 0) { float max = hsbColor.b; float dif = hsbColor.b * hsbColor.s / 255f; float min = hsbColor.b - dif; float h = hsbColor.h; if (h < 60f) { r = max; g = h * dif / 60f + min; b = min; } else if (h < 120f) { r = -(h - 120f) * dif / 60f + min; g = max; b = min; } else if (h < 180f) { r = min; g = max; b = (h - 120f) * dif / 60f + min; } else if (h < 240f) { r = min; g = -(h - 240f) * dif / 60f + min; b = max; } else if (h < 300f) { r = (h - 240f) * dif / 60f + min; g = min; b = max; } else if (h <= 360f) { r = max; g = min; b = -(h - 360f) * dif / 60 + min; } else { r = 0; g = 0; b = 0; } } return(Color.FromArgb ( hsbColor.a, (int)Math.Round(Math.Min(Math.Max(r, 0), 255)), (int)Math.Round(Math.Min(Math.Max(g, 0), 255)), (int)Math.Round(Math.Min(Math.Max(b, 0), 255)) )); }