/// <summary> /// Draws the perforation. /// </summary> /// <returns></returns> public Result calculatePointMap(List <Circle> circleList) { pointMap = new PointMap(); this.PunchingToolList[0].X = circleList[0].Diameter; // Create the pointMap foreach (Circle circle in circleList) { pointMap.AddPoint(new PunchingPoint(circle.Center)); } SortedDictionary <int, PunchingPoint> xDict0 = null; SortedDictionary <int, PunchingPoint> xDict1 = null; if (pointMap.Count > 0) { xDict0 = pointMap.getXDictionary(0); } if (pointMap.Count > 1) { xDict1 = pointMap.getXDictionary(1); } if (xDict0.Count > 2) { XSpacing = Math.Abs(xDict0.ElementAt(1).Value.Point.X - xDict0.ElementAt(0).Value.Point.X); } if (xDict0.Count > 1 && xDict1.Count > 1) { YSpacing = Math.Abs(xDict1.ElementAt(0).Value.Point.Y - xDict0.ElementAt(0).Value.Point.Y); } return(Result.Success); }
/// <summary> /// Draws the perforation. /// </summary> /// <returns></returns> public override double drawPerforation(Curve boundaryCurve) { List <PointMap> pointMapList = new List <PointMap>(); PointMap pointMapTool1 = new PointMap(); pointMapList.Add(pointMapTool1); // Find the boundary BoundingBox boundingBox = boundaryCurve.GetBoundingBox(Plane.WorldXY); Point3d min = boundingBox.Min; Point3d max = boundingBox.Max; double spanX = max.X - min.X; double spanY = max.Y - min.Y; int punchQtyX = ((int)((spanX - punchingToolList[0].X) / XSpacing)) + 1; double marginX = (spanX - ((punchQtyX - 1) * XSpacing)) / 2; double YSpacing = XSpacing; int punchQtyY = ((int)((spanY - punchingToolList[0].Y) / YSpacing)) + 1; double marginY = (spanY - ((punchQtyY - 1) * YSpacing)) / 2; Point3d point; RhinoDoc doc = RhinoDoc.ActiveDoc; double firstX = min.X + marginX; double firstY = min.Y + marginY; // Set the current layer int currentLayer = doc.Layers.CurrentLayerIndex; doc.Layers.SetCurrentLayerIndex(currentLayer, false); // We don't use cluster tool so just perf it onto Tool Hit layer //if (!MetrixUtilities.IsLayerFound("TemporaryPerfLayer")) //{ // RhinoUtilities.SetActiveLayer(Properties.Settings.Default.ToolHitLayerName, System.Drawing.Color.Black); //} //else //{ // RhinoUtilities.SetActiveLayer("TemporaryToolHit", System.Drawing.Color.Black); //} // Generate the point map first for (int y = 0; y < punchQtyY; y++) { for (int x = 0; x < punchQtyX; x++) { point = new Point3d(firstX + x * XSpacing, firstY + y * YSpacing, 0); if (punchingToolList[0].isInside(boundaryCurve, point) == true) { pointMapTool1.AddPoint(new PunchingPoint(point)); } } } Random random = new Random(); int testResult; int slotNumber = 1; PunchingTools.Obround threeHoleSlot = new PunchingTools.Obround(); threeHoleSlot.X = 2 * this.XSpacing + punchingToolList[0].X; threeHoleSlot.Y = punchingToolList[0].X; PunchingTools.Obround fiveHoleSlot = new PunchingTools.Obround(); fiveHoleSlot.X = 4 * this.XSpacing + punchingToolList[0].X; fiveHoleSlot.Y = punchingToolList[0].X; int chanceForHoles = 60; int chanceForMidSlot = 86; int chanceForLargeSlot = 100; int holeCounter = 0; int midSlotCounter = 0; int largeSlotCounter = 0; double toolArea = 0; // Go through each point in the point Map list to determine whether it is a hole or a slot. for (int i = 0; i < pointMapTool1.YCount; i++) { SortedDictionary <int, PunchingPoint> xDict = pointMapTool1.getXDictionary(i); chanceForHoles = 60; chanceForMidSlot = 86; chanceForLargeSlot = 100; for (int j = 0; j < xDict.Count; j = j + slotNumber) { if (xDict.Count - j > 4) { // Generate whether the current hole testResult = random.Next(1, chanceForLargeSlot); } else if (xDict.Count - j > 2) { chanceForHoles = 30; chanceForMidSlot = 66; chanceForLargeSlot = 100; // Generate whether the current slot testResult = random.Next(1, chanceForMidSlot); } else { chanceForHoles = 30; chanceForMidSlot = 66; chanceForLargeSlot = 100; // Generate whether the current large slot testResult = random.Next(1, chanceForHoles); } if (testResult >= 1 && testResult <= chanceForHoles) { slotNumber = 1; holeCounter++; midSlotCounter = 0; largeSlotCounter = 0; if (holeCounter == 1) { chanceForHoles = 60; chanceForMidSlot = 86; chanceForLargeSlot = 100; } else if (holeCounter == 2) { chanceForHoles = 30; chanceForMidSlot = 66; chanceForLargeSlot = 100; } else if (holeCounter > 2) { chanceForHoles = 10; chanceForMidSlot = 53; chanceForLargeSlot = 100; } // Draw circle punchingToolList[0].drawTool(xDict.ElementAt(j).Value.Point); toolArea += punchingToolList[0].getArea(); } else if (testResult > chanceForHoles && testResult <= chanceForMidSlot) { slotNumber = 3; holeCounter = 0; midSlotCounter++; largeSlotCounter = 0; if (midSlotCounter == 1) { chanceForHoles = 80; chanceForMidSlot = 90; chanceForLargeSlot = 100; } else if (midSlotCounter > 1) { chanceForHoles = 85; chanceForMidSlot = 93; chanceForLargeSlot = 100; } // Draw slot that span over 3 holes threeHoleSlot.drawTool(xDict.ElementAt(j + 1).Value.Point); toolArea += threeHoleSlot.getArea(); } else if (testResult > chanceForMidSlot && testResult <= chanceForLargeSlot) { slotNumber = 5; holeCounter = 0; midSlotCounter = 0; largeSlotCounter++; if (largeSlotCounter >= 1) { chanceForHoles = 80; chanceForMidSlot = 100; chanceForLargeSlot = 100; } // Draw slot that span over 5 holes fiveHoleSlot.drawTool(xDict.ElementAt(j + 2).Value.Point); toolArea += fiveHoleSlot.getArea(); } } } // Display the open area calculation AreaMassProperties area = AreaMassProperties.Compute(boundaryCurve); RhinoApp.WriteLine("Total area: {0} mm^2", area.Area.ToString("#.##")); RhinoApp.WriteLine("Tool area: {0} mm^2", toolArea.ToString("#.##")); openArea = toolArea * 100 / area.Area; RhinoApp.WriteLine("Open area: {0}%", openArea.ToString("#.")); //for (int y = 0; y < punchQtyY; y++) //{ // for (int x = 0; x < punchQtyX; x++) // { // point = new Point3d(firstX + x * XSpacing, firstY + y * YSpacing, 0); // if (punchingToolList[0].isInside(boundaryCurve, point) == true) // { // pointMap.AddPoint(new PunchingPoint(point)); // punchingToolList[0].drawTool(point); // } // if(random.NextDouble() < randomness) // { // if (punchingToolList[1].isInside(boundaryCurve, point) == true) // { // doc.Layers.SetCurrentLayerIndex(brailleLayerIndex, true); // punchingToolList[1].drawTool(point); // doc.Layers.SetCurrentLayerIndex(perforationlayerIndex, true); // } // } // } //} doc.Views.Redraw(); doc.Layers.SetCurrentLayerIndex(currentLayer, true); return(openArea); }
/// <summary> /// Draws the cluster. /// </summary> /// <param name="pointMap">The point map.</param> /// <param name="punchingTool">The punching tool.</param> /// <returns></returns> public override Result drawCluster(PointMap pointMap, PunchingTool punchingTool) { RhinoDoc doc = RhinoDoc.ActiveDoc; List <PunchingPoint> clusterPoints = new List <PunchingPoint>(); List <Point3d> clusterRelativePoints = new List <Point3d>(); int clusterCounter = 0; // Create a new layer string layerName = Properties.Settings.Default.ToolHitLayerName; // Does a layer with the same name already exist? int layerIndex = doc.Layers.Find(layerName, true); // If layer does not exist if (layerIndex == -1) { // Add a new layer to the document layerIndex = doc.Layers.Add(layerName, System.Drawing.Color.Black); } int currentLayer = doc.Layers.CurrentLayerIndex; doc.Layers.SetCurrentLayerIndex(layerIndex, true); // Create the cluster tool that has the relative position for (int y = 0; y < punchingTool.ClusterTool.PinsY; y++) { for (int x = 0; x < punchingTool.ClusterTool.PinsX; x++) { clusterRelativePoints.Add(new Point3d(x * XSpacing * punchingTool.ClusterTool.MultiplierX, y * YSpacing * punchingTool.ClusterTool.MultiplierY, 0)); } } Point2d centre = new Point2d(clusterRelativePoints.Last().X / 2, clusterRelativePoints.Last().Y / 2); // Go through each point in the point Map list to find cluster tool starting from bottom left corner for (int i = 0; i < pointMap.YCount; i++) { SortedDictionary <int, PunchingPoint> xDict = pointMap.getXDictionary(i); for (int j = 0; j < xDict.Count; j++) { // If the point has already punched, ignore it. if (xDict.ElementAt(j).Value.HasPunched == true) { continue; } // Clear the cluster points clusterPoints.Clear(); // Add the first point to it clusterPoints.Add(xDict.ElementAt(j).Value); // Cluster tool X+ Y+ configuration for (int k = 1; k < clusterRelativePoints.Count; k++) { // Look for point in the point list PunchingPoint resultPoint = pointMap.getPoint2(clusterPoints[0].Point.X + clusterRelativePoints[k].X, clusterPoints[0].Point.Y + clusterRelativePoints[k].Y); if (resultPoint == null) { // Cannot find point in the cluster tool, then skip break; } else { clusterPoints.Add(resultPoint); } } // If all the points are added if (clusterPoints.Count == clusterRelativePoints.Count) { double clusterX = centre.X + clusterPoints[0].Point.X; double clusterY = centre.Y + clusterPoints[0].Point.Y; // Draw all the points in the cluster tool clusterCounter++; if (Properties.Settings.Default.DrawClusterOnSeparateLayer == true) { doc.Layers.SetCurrentLayerIndex(CreateLayer("Cluster " + clusterCounter), true); doc.Views.Redraw(); } foreach (PunchingPoint pp in clusterPoints) { if (Properties.Settings.Default.DrawClusterOnSeparateLayer == true) { // Draw the tool punchingTool.drawTool(pp.Point); } // Remove all the points in the clustertool list pp.HasPunched = true; } // Draw the rectangle punchingTool.ClusterTool.drawClusterTool(clusterX, clusterY, 0); } } } // If the Cluster tool allow over punch have to do some more work if (punchingTool.ClusterTool.AllowOverPunch == true) { // Bottom right search // Clear the cluster tool points clusterRelativePoints.Clear(); // Create the cluster tool that has the relative position for (int y = 0; y < punchingTool.ClusterTool.PinsY; y++) { for (int x = 0; x < punchingTool.ClusterTool.PinsX; x++) { clusterRelativePoints.Add(new Point3d(-x * XSpacing * punchingTool.ClusterTool.MultiplierX, y * YSpacing * punchingTool.ClusterTool.MultiplierY, 0)); } } // Go through each point in the point Map list to find cluster tool starting from bottom right corner for (int i = 0; i < pointMap.YCount; i++) { SortedDictionary <int, PunchingPoint> xDict = pointMap.getXDictionary(i); for (int j = xDict.Count - 1; j >= 0; j--) { // If the point is punched, skip to the next point if (xDict.ElementAt(j).Value.HasPunched == true) { continue; } clusterPoints.Clear(); clusterPoints.Add(xDict.ElementAt(j).Value); // Cluster tool X- Y+ configuration for (int k = 1; k < clusterRelativePoints.Count; k++) { // Look for point in the point list PunchingPoint resultPoint = pointMap.getPoint2(clusterPoints[0].Point.X + clusterRelativePoints[k].X, clusterPoints[0].Point.Y + clusterRelativePoints[k].Y); if (resultPoint == null) { // Cannot find point in the cluster tool, then skip break; } else { clusterPoints.Add(resultPoint); } } // If all the points are added if (clusterPoints.Count == clusterRelativePoints.Count) { double clusterX = clusterPoints[0].Point.X - centre.X; double clusterY = clusterPoints[0].Point.Y + centre.Y; // Draw all the points in the cluster tool clusterCounter++; if (Properties.Settings.Default.DrawClusterOnSeparateLayer == true) { doc.Layers.SetCurrentLayerIndex(CreateLayer("Cluster " + clusterCounter), true); } foreach (PunchingPoint pp in clusterPoints) { if (Properties.Settings.Default.DrawClusterOnSeparateLayer == true) { // Draw the tool punchingTool.drawTool(pp.Point); } // Remove all the points in the clustertool list pp.HasPunched = true; } // draw the rectangle punchingTool.ClusterTool.drawClusterTool(clusterX, clusterY, 0); } } } // Top Left corner search // Clear the cluster tool points clusterRelativePoints.Clear(); // Create the cluster tool that has the relative position for (int y = 0; y < punchingTool.ClusterTool.PinsY; y++) { for (int x = 0; x < punchingTool.ClusterTool.PinsX; x++) { clusterRelativePoints.Add(new Point3d(x * XSpacing * punchingTool.ClusterTool.MultiplierX, -y * YSpacing * punchingTool.ClusterTool.MultiplierY, 0)); } } // Go through each point in the point Map list to find cluster tool starting from top left for (int i = pointMap.YCount - 1; i >= 0; i--) { SortedDictionary <int, PunchingPoint> xDict = pointMap.getXDictionary(i); for (int j = 0; j < xDict.Count; j++) { // If the point is punched, skip to the next point if (xDict.ElementAt(j).Value.HasPunched == true) { continue; } clusterPoints.Clear(); clusterPoints.Add(xDict.ElementAt(j).Value); // Cluster tool X- Y+ configuration for (int k = 1; k < clusterRelativePoints.Count; k++) { // Look for point in the point list PunchingPoint resultPoint = pointMap.getPoint2(clusterPoints[0].Point.X + clusterRelativePoints[k].X, clusterPoints[0].Point.Y + clusterRelativePoints[k].Y); if (resultPoint == null) { // Cannot find point in the cluster tool, then skip break; } else { clusterPoints.Add(resultPoint); } } // If all the points are added if (clusterPoints.Count == clusterRelativePoints.Count) { double clusterX = clusterPoints[0].Point.X + centre.X; double clusterY = clusterPoints[0].Point.Y - centre.Y; // Draw all the points in the cluster tool clusterCounter++; if (Properties.Settings.Default.DrawClusterOnSeparateLayer == true) { doc.Layers.SetCurrentLayerIndex(CreateLayer("Cluster " + clusterCounter), true); } foreach (PunchingPoint pp in clusterPoints) { if (Properties.Settings.Default.DrawClusterOnSeparateLayer == true) { // Draw the tool punchingTool.drawTool(pp.Point); } // Remove all the points in the clustertool list pp.HasPunched = true; } // draw the rectangle punchingTool.ClusterTool.drawClusterTool(clusterX, clusterY, 0); } } } // Top right corner search // Clear the cluster tool points clusterRelativePoints.Clear(); // Create the cluster tool that has the relative position for (int y = 0; y < punchingTool.ClusterTool.PinsY; y++) { for (int x = 0; x < punchingTool.ClusterTool.PinsX; x++) { clusterRelativePoints.Add(new Point3d(-x * XSpacing * punchingTool.ClusterTool.MultiplierX, -y * YSpacing * punchingTool.ClusterTool.MultiplierY, 0)); } } centre = new Point2d(-clusterRelativePoints.Last().X / 2, -clusterRelativePoints.Last().Y / 2); // Go through each point in the point Map list to find cluster tool starting from top right for (int i = pointMap.YCount - 1; i >= 0; i--) { SortedDictionary <int, PunchingPoint> xDict = pointMap.getXDictionary(i); for (int j = xDict.Count - 1; j >= 0; j--) { // If the point is punched, skip to the next point if (xDict.ElementAt(j).Value.HasPunched == true) { continue; } clusterPoints.Clear(); clusterPoints.Add(xDict.ElementAt(j).Value); // Cluster tool X- Y+ configuration for (int k = 1; k < clusterRelativePoints.Count; k++) { // Look for point in the point list PunchingPoint resultPoint = pointMap.getPoint2(clusterPoints[0].Point.X + clusterRelativePoints[k].X, clusterPoints[0].Point.Y + clusterRelativePoints[k].Y); if (resultPoint == null) { // Cannot find point in the cluster tool, then skip break; } else { clusterPoints.Add(resultPoint); } } // If all the points are added if (clusterPoints.Count == clusterRelativePoints.Count) { double clusterX = clusterPoints[0].Point.X - centre.X; double clusterY = clusterPoints[0].Point.Y - centre.Y; // Draw all the points in the cluster tool clusterCounter++; if (Properties.Settings.Default.DrawClusterOnSeparateLayer == true) { doc.Layers.SetCurrentLayerIndex(CreateLayer("Cluster " + clusterCounter), true); } foreach (PunchingPoint pp in clusterPoints) { if (Properties.Settings.Default.DrawClusterOnSeparateLayer == true) { // Draw the tool punchingTool.drawTool(pp.Point); } // Remove all the points in the clustertool list pp.HasPunched = true; } // draw the cluster tool punchingTool.ClusterTool.drawClusterTool(clusterX, clusterY, 0); } } } } doc.Layers.SetCurrentLayerIndex(layerIndex, true); // Places where cannot cluster punch for (int i = 0; i < pointMap.YCount; i++) { SortedDictionary <int, PunchingPoint> xDict = pointMap.getXDictionary(i); for (int j = 0; j < xDict.Count; j++) { if (xDict.ElementAt(j).Value.HasPunched == false) { // Draw the tool punchingTool.drawTool(xDict.ElementAt(j).Value.Point); } } } doc.Views.Redraw(); doc.Layers.SetCurrentLayerIndex(currentLayer, true); return(Result.Success); }