static void notesImagePlanar_Executing(object sender, EventArgs e) { Bitmap bitmap = OpenBitmap(); if (bitmap == null) { return; } Part part = CreateImagePart(); Point[] points = new Point[4]; for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { points[0] = Point.Create((i + 0) * stepSize, (j + 0) * stepSize, 0); points[1] = Point.Create((i + 1) * stepSize, (j + 0) * stepSize, 0); points[2] = Point.Create((i + 1) * stepSize, (j + 1) * stepSize, 0); points[3] = Point.Create((i + 0) * stepSize, (j + 1) * stepSize, 0); DesignBody designBody = ShapeHelper.CreatePolygon(points, Plane.PlaneXY, 0, part); designBody.SetColor(null, GetOpaquePixel(bitmap, i, j)); } } }
static void notesImageCylindrical_Executing(object sender, EventArgs e) { Bitmap bitmap = OpenBitmap(); if (bitmap == null) { return; } Part part = CreateImagePart(); int width = bitmap.Width; double radius = 0.5 * stepSize / Math.Sin(Math.PI / width) * Math.PI; Point[] points = new Point[4]; for (int i = 0; i < width; i++) { double angle1 = (double)i / width * 2 * Math.PI; double angle2 = (double)(i + 1) / width * 2 * Math.PI; for (int j = 0; j < bitmap.Height; j++) { double x1 = Math.Sin(angle1) * radius; double y1 = Math.Cos(angle1) * radius; double z1 = j * stepSize; double x2 = Math.Sin(angle2) * radius; double y2 = Math.Cos(angle2) * radius; double z2 = (j + 1) * stepSize; points[0] = Point.Create(x1, y1, z1); points[1] = Point.Create(x1, y1, z2); points[2] = Point.Create(x2, y2, z2); points[3] = Point.Create(x2, y2, z1); DesignBody designBody = ShapeHelper.CreatePolygon(points, null, 0, part); designBody.SetColor(null, GetOpaquePixel(bitmap, i, j)); } } }
protected override void OnExecute(Command command, ExecutionContext context, Rectangle buttonRect) { /* * Provide dialogue box and user inputs what they want to locate * These selections are Tori, Small Holes or Small Volumes * For Small Volumes and Holes the user will need to input a value * Each will be handled seperately and find matching bodies in all part design bodies * All will be then added to a final selection list for the user */ // Instance common functions class InstanceClasses.CommonSpaceClaimFunctions FunctionsClass = new InstanceClasses.CommonSpaceClaimFunctions(); // Variables Window window = Window.ActiveWindow; window.SetTool(new ToolClass()); Document doc = window.Document; Part rootPart = doc.MainPart; InteractionContext interContext = window.ActiveContext; List <IDesignBody> selectionBodies = new List <IDesignBody>(); List <IDesignBody> matchingBodies = new List <IDesignBody>(); List <Face> facesList = new List <Face>(); List <IDesignFace> iFacesList = new List <IDesignFace>(); List <IDesignFace> ifinalFacesList = new List <IDesignFace>(); ICollection <IDesignFace> IcollectionFaces = null; double minradius; double minVolume; double minFace; bool findCone = false; bool findTori = false; bool findHoles = false; bool findVolume = false; bool findFace = false; #pragma warning disable CS0219 // The variable 'faceCounter' is assigned but its value is never used int faceCounter = 0; #pragma warning restore CS0219 // The variable 'faceCounter' is assigned but its value is never used #pragma warning disable CS0219 // The variable 'intMode' is assigned but its value is never used InteractionMode intMode = InteractionMode.Solid; #pragma warning restore CS0219 // The variable 'intMode' is assigned but its value is never used if (window == null) { return; } #region Dialogue // Create dialogue box using (var dialogue = new UI.BodySelectForm()) { if (dialogue.ShowDialog() != DialogResult.OK) { return; } var nameAndRendering = new List <KeyValuePair <string, Graphic> >(); var partToGraphic = new Dictionary <Part, Graphic>(); var style = new GraphicStyle { EnableDepthBuffer = true }; findCone = dialogue.LocateCone; findTori = dialogue.LocateTori; findHoles = dialogue.LocateHoles; findVolume = dialogue.LocateVolume; findFace = dialogue.LocateFace; minradius = dialogue.minRadius; minVolume = dialogue.minVolume; minFace = dialogue.minFace; } #endregion // Gather all design bodies //----------------------------------------------- selectionBodies.AddRange(FunctionsClass.GatherAllVisibleBodies(rootPart, window)); // Check if each body fits wanted criteria //----------------------------------------------- foreach (IDesignBody iDesBod in selectionBodies) { DesignBody master = iDesBod.Master; Body body = master.Shape; var faces = body.Faces; facesList.AddRange(faces); var iFaces = iDesBod.Faces; iFacesList.AddRange(iFaces); Matrix masterTrans = iDesBod.TransformToMaster; Matrix reverseTrans = masterTrans.Inverse; // If body fits volume criteria //---------------------------------------- if (findVolume && body.Volume * 1e6 < minVolume) { matchingBodies.Add(iDesBod); } else { // If body fits tori or hole criteria //---------------------------------------- for (int i = 0; i < iFacesList.Count; i++) { IDesignFace face = iFacesList[i]; if (face.Area * 1e4 < minFace) { ifinalFacesList.Clear(); ifinalFacesList.Add(face); window.ActiveTool.CreateIndicator(ifinalFacesList); } if (face.Shape.GetGeometry <Cone>() != null && findCone) { ifinalFacesList.Clear(); ifinalFacesList.Add(face); window.ActiveTool.CreateIndicator(ifinalFacesList); } if (face.Shape.GetGeometry <Torus>() != null && findTori) { ifinalFacesList.Clear(); ifinalFacesList.Add(face); window.ActiveTool.CreateIndicator(ifinalFacesList); } else if (face.Shape.GetGeometry <Cylinder>() != null && face.Shape.GetGeometry <Cylinder>().Radius * 1e2 < minradius && findHoles) { //FunctionsClass.ColourFace(facesList[i], master); ifinalFacesList.Clear(); ifinalFacesList.Add(face); window.ActiveTool.CreateIndicator(ifinalFacesList); var radius = face.Shape.GetGeometry <Cylinder>().Radius; /* * if (IsHole(face, radius)) * { * ifinalFacesList.Clear(); * ifinalFacesList.Add(face); * window.ActiveTool.CreateIndicator(ifinalFacesList); * } */ } else { // Set all the other face to slightly translucent //FunctionsClass.SetFacetranslucent(face, master); } } } facesList.Clear(); iFacesList.Clear(); } //ICollection<IDocObject> bodyCollection = null; // For each matching body change the colour to red //---------------------------------------------------- foreach (IDesignBody finalBody in matchingBodies) { //bodyCollection.Add(finalBody); DesignBody finalMaster = finalBody.Master; finalMaster.SetColor(null, Color.Red); } var point = Point.Create(1, 1, 1); IcollectionFaces = ifinalFacesList; // Also set them as selected by the user //---------------------------------------------------- //interContext.Selection = bodyCollection; }
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); }
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); }
static void notesImageSpherical_Executing(object sender, EventArgs e) { Bitmap bitmap = OpenBitmap(); if (bitmap == null) { return; } Part part = CreateImagePart(); int width = bitmap.Width; int height = bitmap.Height; double radius = 0.5 * stepSize / Math.Sin(Math.PI / width) * Math.PI; Point startPoint = Point.Create(radius, 0, 0); Line yAxis = Line.Create(Point.Origin, Direction.DirY); Line zAxis = Line.Create(Point.Origin, Direction.DirZ); List <Point> points = new List <Point>(4); for (int i = 0; i < 4; i++) { points.Add(Point.Origin); } for (int i = 0; i < width; i++) { double angle1 = (double)i / width * 2 * Math.PI; double angle2 = (double)(i + 1) / width * 2 * Math.PI; for (int j = 0; j < height; j++) { double azimuth1 = ((double)j) / height * Math.PI - Math.PI / 2; double azimuth2 = ((double)j + 1) / height * Math.PI - Math.PI / 2; points[0] = Matrix.CreateRotation(zAxis, angle1) * Matrix.CreateRotation(yAxis, azimuth1) * startPoint; points[1] = Matrix.CreateRotation(zAxis, angle1) * Matrix.CreateRotation(yAxis, azimuth2) * startPoint; points[2] = Matrix.CreateRotation(zAxis, angle2) * Matrix.CreateRotation(yAxis, azimuth2) * startPoint; points[3] = Matrix.CreateRotation(zAxis, angle2) * Matrix.CreateRotation(yAxis, azimuth1) * startPoint; Point?extraPoint = null; if (Accuracy.Equals(points[3], points[0])) { extraPoint = points[0]; points.Remove(points[0]); } else { for (int k = 0; k < 3; k++) { if (Accuracy.Equals(points[k], points[k + 1])) { extraPoint = points[k]; points.Remove(points[k]); break; } } } DesignBody designBody = ShapeHelper.CreatePolygon(points, null, 0, part); designBody.SetColor(null, GetOpaquePixel(bitmap, i, j)); if (extraPoint != null) { points.Add(extraPoint.Value); } } } }
protected override void OnExecute(Command command, ExecutionContext context, Rectangle buttonRect) { /* * Max Surface Tool * A. Burns 06/11/2017 * Read user inuput max surfaces per body * Hightlight body with greater number of faces * Optionally can chnage the bodies colour * as well as highlighting */ // Instance common functions class InstanceClasses.CommonSpaceClaimFunctions FunctionsClass = new InstanceClasses.CommonSpaceClaimFunctions(); // Essential Variables Window window = Window.ActiveWindow; Document doc = window.Document; Part rootPart = doc.MainPart; InteractionContext interContext = window.ActiveContext; // Other Variables List <IDesignBody> allBodies = new List <IDesignBody>(); List <IDesignFace> ifinalFacesList = new List <IDesignFace>(); window.SetTool(new ToolClass()); int faceCount; int maxFaces; bool changeColours; string colourString; using (var dialogue = new UI.MaxSurfacesForm()) { if (dialogue.ShowDialog() != DialogResult.OK) { return; } var nameAndRendering = new List <KeyValuePair <string, Graphic> >(); var partToGraphic = new Dictionary <Part, Graphic>(); var style = new GraphicStyle { EnableDepthBuffer = true }; maxFaces = (int)dialogue.MaxSurfaces; changeColours = dialogue.ChangeColour; colourString = dialogue.ColourSelection; } if (window == null) { return; } allBodies.AddRange(FunctionsClass.GatherAllVisibleBodies(rootPart, window)); foreach (IDesignBody iDesBody in allBodies) { DesignBody desBody = iDesBody.Master; Body body = desBody.Shape.Copy(); var faces = body.Faces; var iFaces = iDesBody.Faces; // ifinalFacesList.AddRange(iDesBody.Faces); faceCount = faces.Count; if (faceCount > maxFaces) { window.ActiveTool.CreateIndicator(iFaces); if (changeColours) { if (colourString == "Blue") { desBody.SetColor(null, Color.Blue); } else if (colourString == "Red") { desBody.SetColor(null, Color.Red); } else if (colourString == "Green") { desBody.SetColor(null, Color.Green); } else if (colourString == "Orange") { desBody.SetColor(null, Color.Orange); } else if (colourString == "Black") { desBody.SetColor(null, Color.Black); } else if (colourString == "Yellow") { desBody.SetColor(null, Color.Yellow); } else if (colourString == "Salmon Pink") { desBody.SetColor(null, Color.Salmon); } } } } }
public void ColorBody(DesignBody designBody) { designBody.SetColor(null, ColorForBody(designBody)); }