instruction
stringclasses
3 values
output
stringclasses
9 values
Write a Autoodesk Inventor API Code for this Rule. Rule: Check standard scales are use as 1:1, 1:2, 1:5, 1:10, 1:20, 1:50, 2:1. 5:1, 10:1 in title block
```csharp DrawingDocument oDoc = (DrawingDocument)mApplication.ActiveDocument; Sheet s = oDoc.ActiveSheet; TitleBlock t = s.TitleBlock; TitleBlockDefinition tDef = t.Definition; HashSet<string> allowedValues = new HashSet<string> { "1:1", "1:2", "1:5", "1:10", "1:20", "1:50", "2:1", "5:1", "10:1" }; foreach (TextBox txtBox in tDef.Sketch.TextBoxes) { if (txtBox.Text == "<Initial View Scale>") { if (allowedValues.Contains(t.GetResultText(txtBox))) { Console.WriteLine("Standard scale is used"); } else { Console.WriteLine("Standard scale is not used"); } } }```
Write a Autoodesk Inventor API Code for this Rule. Rule: Check standard scales are use as 1:1, 1:2, 1:5, 1:10, 1:20, 1:50, 2:1. 5:1, 10:1 in title block
```csharp DrawingDocument oDoc = (DrawingDocument)mApplication.ActiveDocument; Sheet s = oDoc.ActiveSheet; TitleBlock t = s.TitleBlock; TitleBlockDefinition tDef = t.Definition; HashSet<string> allowedValues = new HashSet<string> { "1:1", "1:2", "1:5", "1:10", "1:20", "1:50", "2:1", "5:1", "10:1" }; foreach (TextBox txtBox in tDef.Sketch.TextBoxes) { if (txtBox.Text == "<Initial View Scale>") { if (allowedValues.Contains(t.GetResultText(txtBox))) { Console.WriteLine(" The geometry is according to the Standard scale"); } else { Console.WriteLine("Standard scale is not used"); } } }```
Write a Autoodesk Inventor API Code for this Rule. Rule: Check standard scales are use as 1:1, 1:2, 1:5, 1:10, 1:20, 1:50, 2:1. 5:1, 10:1 in title block
```csharp DrawingDocument oDoc = (DrawingDocument)mApplication.ActiveDocument; Sheet s = oDoc.ActiveSheet; TitleBlock t = s.TitleBlock; TitleBlockDefinition tDef = t.Definition; HashSet<string> allowedValues = new HashSet<string> { "1:1", "1:2", "1:5", "1:10", "1:20", "1:50", "2:1", "5:1", "10:1" }; foreach (TextBox txtBox in tDef.Sketch.TextBoxes) { if (txtBox.Text == "<Initial View Scale>") { if (allowedValues.Contains(t.GetResultText(txtBox))) { Console.WriteLine("Active Geometry is in Standard scale"); } else { Console.WriteLine("Standard scale is not used"); } } }```
Write a Autoodesk Inventor API Code for this Rule. Rule: Check if sheet is Aluminium, thickness should be more than 1.6 cm
```csharp public static void ThicknessCheck(Inventor.Application application) { Document activeDoc = application.ActiveDocument; if (activeDoc != null && activeDoc.DocumentType == DocumentTypeEnum.kPartDocumentObject) { PartDocument partDoc = activeDoc as PartDocument; // Make sure the document is a sheet metal document if (partDoc.ComponentDefinition.Type == ObjectTypeEnum.kSheetMetalComponentDefinitionObject) { SheetMetalComponentDefinition smComponent = partDoc.ComponentDefinition as SheetMetalComponentDefinition; // Get the thickness parameter Parameter thicknessParam = smComponent.Thickness; // Check if the material is Aluminum Material material = smComponent.Material; /* if (material.Name.Equals("Aluminum")) {*/ // Get the thickness value double thicknessValue = (double)thicknessParam.Value; // Check if the thickness is less than 1.6 cm (16 mm) if (thicknessValue < 1.6) { // Mark it as a violation of the rule (you can implement your own logic here) MessageBox.Show("Thickness is less than 1.6 cm (16 mm)"); } else { MessageBox.Show("Thickness is within the acceptable range."); } /* } else { MessageBox.Show("The material is not Aluminum."); }*/ } else { MessageBox.Show("The active document is not a sheet metal part."); } } }```
Write a Autoodesk Inventor API Code for this Rule. Rule: Check if sheet is Aluminium, thickness should be more than 1.6 cm
```csharp public static void ThicknessCheck(Inventor.Application application) { Document activeDoc = application.ActiveDocument; if (activeDoc != null && activeDoc.DocumentType == DocumentTypeEnum.kPartDocumentObject) { PartDocument partDoc = activeDoc as PartDocument; // Make sure the document is a sheet metal document if (partDoc.ComponentDefinition.Type == ObjectTypeEnum.kSheetMetalComponentDefinitionObject) { SheetMetalComponentDefinition smComponent = partDoc.ComponentDefinition as SheetMetalComponentDefinition; // Get the thickness parameter Parameter thicknessParam = smComponent.Thickness; // Check if the material is Aluminum Material material = smComponent.Material; /* if (material.Name.Equals("Aluminum")) {*/ // Get the thickness value double thicknessValue = (double)thicknessParam.Value; // Check if the thickness is less than 1.6 cm (16 mm) if (thicknessValue < 1.6) { // Mark it as a violation of the rule (you can implement your own logic here) MessageBox.Show("The Thickness of the material is less than 1.6 cm (16 mm)"); } else { MessageBox.Show("Thickness is within the acceptable range."); } /* } else { MessageBox.Show("The material is not Aluminum."); }*/ } else { MessageBox.Show("The active document is not a sheet metal part."); } } }```
Write a Autoodesk Inventor API Code for this Rule. Rule: Check if sheet is Aluminium, thickness should be more than 1.6 cm
```csharp public static void ThicknessCheck(Inventor.Application application) { Document activeDoc = application.ActiveDocument; if (activeDoc != null && activeDoc.DocumentType == DocumentTypeEnum.kPartDocumentObject) { PartDocument partDoc = activeDoc as PartDocument; // Make sure the document is a sheet metal document if (partDoc.ComponentDefinition.Type == ObjectTypeEnum.kSheetMetalComponentDefinitionObject) { SheetMetalComponentDefinition smComponent = partDoc.ComponentDefinition as SheetMetalComponentDefinition; // Get the thickness parameter Parameter thicknessParam = smComponent.Thickness; // Check if the material is Aluminum Material material = smComponent.Material; /* if (material.Name.Equals("Aluminum")) {*/ // Get the thickness value double thicknessValue = (double)thicknessParam.Value; // Check if the thickness is less than 1.6 cm (16 mm) if (thicknessValue < 1.6) { // Mark it as a violation of the rule (you can implement your own logic here) MessageBox.Show("Thickness is less than 1.6 cm (16 mm)"); } else { MessageBox.Show("Thickness is within the standards."); } /* } else { MessageBox.Show("The material is not Aluminum."); }*/ } else { MessageBox.Show("The active document is not a sheet metal part."); } } }```
Write a Autoodesk Inventor API Code for this Rule. Rule: Check the distance between two holes should be greater than 10 cm
```csharp private void HoleToHoleDetect() { try { WindowState = WindowState.Minimized; if (_inventorApp == null) { Console.WriteLine("Inventor application instance is not properly initialized."); return; } if (_inventorApp.ActiveDocument == null) { Console.WriteLine("No active document."); return; } Document activeDoc = _inventorApp.ActiveDocument; if (!(activeDoc is PartDocument)) { Console.WriteLine("Please open a part document."); return; } PartDocument partDoc = activeDoc as PartDocument; SurfaceBody mainBody = partDoc.ComponentDefinition.SurfaceBodies[1]; List<Face> requiredHolefaces = DetectHole(partDoc, mainBody); int holeCount = requiredHolefaces.Count; if (holeCount <= 0) { MessageBox.Show("No holes detected.", "Hole Detection Result", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // Calculate the centroid of the detected holes double[] centerX = new double[holeCount]; double[] centerY = new double[holeCount]; double[] centerZ = new double[holeCount]; for (int i = 0; i < holeCount; i++) { foreach (Vertex vertex in requiredHolefaces[i].Vertices) { centerX[i] += Math.Round(vertex.Point.X * 25.4, 2); centerY[i] += Math.Round(vertex.Point.Y * 25.4, 2); centerZ[i] += Math.Round(vertex.Point.Z * 25.4, 2); } centerX[i] /= 4; // 4 vertices per face centerY[i] /= 4; centerZ[i] /= 4; } // Calculate and display distances between centroids StringBuilder resultMessage = new StringBuilder(); resultMessage.AppendLine($"Number of holes: {holeCount}"); resultMessage.AppendLine(); for (int i = 0; i < holeCount; i++) { resultMessage.AppendLine($"Hole {i + 1} Center coordinates: X={centerX[i]}, Y={centerY[i]}, Z={centerZ[i]}"); } // Check distances and store distances below 10 cm string[] distancesBelow10Cm = new string[holeCount * (holeCount - 1) / 2]; List<Face> facesToColorRed = new List<Face>(); int failedRuleIndex = 0; resultMessage.AppendLine(); for (int i = 0; i < holeCount; i++) { for (int j = i + 1; j < holeCount; j++) { double distance = CalculateDistance(centerX[i], centerY[i], centerZ[i], centerX[j], centerY[j], centerZ[j]); resultMessage.AppendLine($"Distance from Hole {i + 1} to Hole {j + 1} : {Math.Round(distance, 2)} mm"); if (distance < 10) { distancesBelow10Cm[failedRuleIndex] = $" Hole { i + 1} to Hole { j + 1} distance is { Math.Round(distance, 2)} mm which is less than 10mm so rule failed"; failedRuleIndex++; } else { resultMessage.AppendLine(); resultMessage.AppendLine(" Rule Passed :"); resultMessage.AppendLine($"Hole {i + 1} to Hole {j + 1} Distance is {Math.Round(distance, 2)}mm which is more than 10mm so rule passed"); } } resultMessage.AppendLine(); } if (failedRuleIndex > 0) { resultMessage.AppendLine(" Rule Failed :"); for (int i = 0; i < distancesBelow10Cm.Length; i++) { resultMessage.AppendLine($"{distancesBelow10Cm[i]}"); } } else { resultMessage.AppendLine(); } MessageBox.Show(resultMessage.ToString(), "Hole Distance Result", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Ensure the UI is re-enabled when the operation is finished, even if there's an exception WindowState = WindowState.Maximized; } } private double CalculateDistance(double x1, double y1, double z1, double x2, double y2, double z2) { return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2) + Math.Pow(z2 - z1, 2)); } private List<Face> DetectHole(PartDocument iPartDoc, SurfaceBody iMainBody) { List<Face> requiredHolefaces = new List<Face>(); try { //for selection and highlight SelectSet selectSet = iPartDoc.SelectSet; if (selectSet == null) throw new Exception("Failed to get Selection Set"); //checking all faces Faces allFaces = iMainBody.Faces; if (allFaces.Count <= 0) throw new Exception("Failed to get faces from body"); //finding hole faces List<Face> requiredThreadedHolefaces = new List<Face>(); int iShouldParaReversed = -1; bool bShouldParaReversed = false; foreach (Face currentface in allFaces) { selectSet.Select(currentface); if (currentface.SurfaceType == SurfaceTypeEnum.kCylinderSurface) { if (iShouldParaReversed == -1) { bool isFaceInterior = isCylindricalFaceInterior(iPartDoc.ComponentDefinition, currentface); bShouldParaReversed = (isFaceInterior) ? currentface.IsParamReversed : !currentface.IsParamReversed; iShouldParaReversed = 0; } bool isParaReversed = currentface.IsParamReversed; if (bShouldParaReversed == currentface.IsParamReversed) { int circularCurveCount = 0; int linearCurveCount = 0; int splineCurveCount = 0; foreach (Edge currentEdge in currentface.Edges) { if (currentEdge.CurveType == CurveTypeEnum.kBSplineCurve) splineCurveCount++; else if (currentEdge.CurveType == CurveTypeEnum.kCircleCurve) circularCurveCount++; else if (currentEdge.CurveType == CurveTypeEnum.kLineCurve) linearCurveCount++; } /* if ((linearCurveCount == 0 && splineCurveCount >= 2 && circularCurveCount == 2 && currentface.EdgeLoops.Count == 1))//condition for through threaded hole requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 1 && splineCurveCount >= 2 && circularCurveCount == 1))//condition for depth threaded hole requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 2 && splineCurveCount >= 2 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for depth threaded holes from both sides requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole requiredHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 1 && splineCurveCount >= 1 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole intersecting with another hole requiredHolefaces.Add(currentface);*/ if ((linearCurveCount == 0 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole requiredHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 1 && splineCurveCount >= 1 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole intersecting with another hole requiredHolefaces.Add(currentface); } } selectSet.Clear(); } holeCount = requiredHolefaces.Count; threadedHoleCount = requiredThreadedHolefaces.Count; if (requiredHolefaces.Count > 0) { HighlightSet highlightSet = iPartDoc.CreateHighlightSet(); if (highlightSet == null) throw new Exception("Failed to get Highlight Set "); highlightSet.Color.SetColor(255, 0, 0); foreach (Face currentHoleFace in requiredHolefaces) { highlightSet.Clear(); selectSet.Select(currentHoleFace); highlightSet.AddItem(currentHoleFace); } } if (requiredThreadedHolefaces.Count > 0) { HighlightSet highlightSet = iPartDoc.CreateHighlightSet(); if (highlightSet == null) throw new Exception("Failed to get Highlight Set "); highlightSet.Color.SetColor(0, 255, 0); foreach (Face currentHoleFace in requiredThreadedHolefaces) { highlightSet.Clear(); selectSet.Select(currentHoleFace); highlightSet.AddItem(currentHoleFace); } } return requiredHolefaces; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("---(WARNING)Failed - Hole Detection"); Console.WriteLine(); } Console.WriteLine("No. of holes = " + holeCount); Console.WriteLine("No. of threaded holes = " + threadedHoleCount); Console.WriteLine("---Please see detected holes highlighted in the model"); Console.WriteLine("---END - Hole Detection"); Console.WriteLine(); return requiredHolefaces; } private bool isCylindricalFaceInterior(PartComponentDefinition iPartDefin, Face iFace) { bool isInterior = false; try { Cylinder oCylinder = iFace.Geometry as Cylinder; if (oCylinder == null) throw new Exception("Failed to get cylinder from face"); Application iApplication = iPartDefin.Application as Application; if (iApplication == null) throw new Exception("Failed to get application from part defination."); double[] para = new double[2]; para[0] = 0.5; para[1] = 0.5; //getting point on surface at 0.5,0.5 parameters. double[] points = new double[3]; iFace.Evaluator.GetPointAtParam(ref para, ref points); //creating point Point oPoint = iApplication.TransientGeometry.CreatePoint(points[0], points[1], points[2]); if (oPoint == null) throw new Exception("Failed to create point."); //getting normal at this location. double[] normals = new double[3]; iFace.Evaluator.GetNormal(ref para, ref normals); //creating normal vector Inventor.Vector oNormal = iApplication.TransientGeometry.CreateVector(normals[0], normals[1], normals[2]); if (oNormal == null) throw new Exception("Failed to create vector."); //scaling vector by radius of cylinder oNormal.ScaleBy(oCylinder.Radius); //translating sample point in direction of normal i.e tip of normal vector Inventor.Point oSamplePoint = oPoint; oSamplePoint.TranslateBy(oNormal); //creating line starting from basepoint of cylinder in direction of cylindrical axis Inventor.Line oAxisLine = iApplication.TransientGeometry.CreateLine(oCylinder.BasePoint, oCylinder.AxisVector.AsVector()); if (oAxisLine == null) throw new Exception("Failed to create line."); //creating line starting from created sample point in direction of cylindrical axis Inventor.Line oSampleLine = iApplication.TransientGeometry.CreateLine(oSamplePoint, oCylinder.AxisVector.AsVector()); if (oSampleLine == null) throw new Exception("Failed to create line."); //if those lines are colinear then face is inner face else outer face if (oSampleLine.IsColinearTo[oAxisLine]) isInterior = true; else isInterior = false; } catch (Exception ex) { Console.WriteLine(ex.Message); return isInterior; } return isInterior; }```
Write a Autoodesk Inventor API Code for this Rule. Rule: Check the distance between two holes should be greater than 10 cm
```csharp private void HoleToHoleDetect() { try { WindowState = WindowState.Minimized; if (_inventorApp == null) { Console.WriteLine("Inventor application instance is not properly initialized."); return; } if (_inventorApp.ActiveDocument == null) { Console.WriteLine("No active document."); return; } Document activeDoc = _inventorApp.ActiveDocument; if (!(activeDoc is PartDocument)) { Console.WriteLine("Please open a part document."); return; } PartDocument partDoc = activeDoc as PartDocument; SurfaceBody mainBody = partDoc.ComponentDefinition.SurfaceBodies[1]; List<Face> requiredHolefaces = DetectHole(partDoc, mainBody); int holeCount = requiredHolefaces.Count; if (holeCount <= 0) { MessageBox.Show("No holes detected.", "Hole Detection Result", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // Calculate the centroid of the detected holes double[] centerX = new double[holeCount]; double[] centerY = new double[holeCount]; double[] centerZ = new double[holeCount]; for (int i = 0; i < holeCount; i++) { foreach (Vertex vertex in requiredHolefaces[i].Vertices) { centerX[i] += Math.Round(vertex.Point.X * 25.4, 2); centerY[i] += Math.Round(vertex.Point.Y * 25.4, 2); centerZ[i] += Math.Round(vertex.Point.Z * 25.4, 2); } centerX[i] /= 4; // 4 vertices per face centerY[i] /= 4; centerZ[i] /= 4; } // Calculate and display distances between centroids StringBuilder resultMessage = new StringBuilder(); resultMessage.AppendLine($"Number of holes: {holeCount}"); resultMessage.AppendLine(); for (int i = 0; i < holeCount; i++) { resultMessage.AppendLine($"Hole {i + 1} Center coordinates: X={centerX[i]}, Y={centerY[i]}, Z={centerZ[i]}"); } // Check distances and store distances below 10 cm string[] distancesBelow10Cm = new string[holeCount * (holeCount - 1) / 2]; List<Face> facesToColorRed = new List<Face>(); int failedRuleIndex = 0; resultMessage.AppendLine(); for (int i = 0; i < holeCount; i++) { for (int j = i + 1; j < holeCount; j++) { double distance = CalculateDistance(centerX[i], centerY[i], centerZ[i], centerX[j], centerY[j], centerZ[j]); resultMessage.AppendLine($"Distance from Hole {i + 1} to Hole {j + 1} : {Math.Round(distance, 2)} mm"); if (distance < 10) { distancesBelow10Cm[failedRuleIndex] = $" Hole { i + 1} to Hole { j + 1} distance is { Math.Round(distance, 2)} mm which is less than 10mm so rule failed"; failedRuleIndex++; } else { resultMessage.AppendLine(); resultMessage.AppendLine(" Rule Passed :"); resultMessage.AppendLine($"Hole {i + 1} to Hole {j + 1} Distance is {Math.Round(distance, 2)}mm which is more than 10mm so rule passed"); } } resultMessage.AppendLine(); } if (failedRuleIndex > 0) { resultMessage.AppendLine(" Rule Failed :"); for (int i = 0; i < distancesBelow10Cm.Length; i++) { resultMessage.AppendLine($"{distancesBelow10Cm[i]}"); } } else { resultMessage.AppendLine(); } MessageBox.Show(resultMessage.ToString(), "Result of Hole Distance ", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error - Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Ensure the UI is re-enabled when the operation is finished, even if there's an exception WindowState = WindowState.Maximized; } } private double CalculateDistance(double x1, double y1, double z1, double x2, double y2, double z2) { return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2) + Math.Pow(z2 - z1, 2)); } private List<Face> DetectHole(PartDocument iPartDoc, SurfaceBody iMainBody) { List<Face> requiredHolefaces = new List<Face>(); try { //for selection and highlight SelectSet selectSet = iPartDoc.SelectSet; if (selectSet == null) throw new Exception("Failed to get Selection Set"); //checking all faces Faces allFaces = iMainBody.Faces; if (allFaces.Count <= 0) throw new Exception("Failed to get faces from body"); //finding hole faces List<Face> requiredThreadedHolefaces = new List<Face>(); int iShouldParaReversed = -1; bool bShouldParaReversed = false; foreach (Face currentface in allFaces) { selectSet.Select(currentface); if (currentface.SurfaceType == SurfaceTypeEnum.kCylinderSurface) { if (iShouldParaReversed == -1) { bool isFaceInterior = isCylindricalFaceInterior(iPartDoc.ComponentDefinition, currentface); bShouldParaReversed = (isFaceInterior) ? currentface.IsParamReversed : !currentface.IsParamReversed; iShouldParaReversed = 0; } bool isParaReversed = currentface.IsParamReversed; if (bShouldParaReversed == currentface.IsParamReversed) { int circularCurveCount = 0; int linearCurveCount = 0; int splineCurveCount = 0; foreach (Edge currentEdge in currentface.Edges) { if (currentEdge.CurveType == CurveTypeEnum.kBSplineCurve) splineCurveCount++; else if (currentEdge.CurveType == CurveTypeEnum.kCircleCurve) circularCurveCount++; else if (currentEdge.CurveType == CurveTypeEnum.kLineCurve) linearCurveCount++; } /* if ((linearCurveCount == 0 && splineCurveCount >= 2 && circularCurveCount == 2 && currentface.EdgeLoops.Count == 1))//condition for through threaded hole requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 1 && splineCurveCount >= 2 && circularCurveCount == 1))//condition for depth threaded hole requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 2 && splineCurveCount >= 2 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for depth threaded holes from both sides requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole requiredHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 1 && splineCurveCount >= 1 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole intersecting with another hole requiredHolefaces.Add(currentface);*/ if ((linearCurveCount == 0 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole requiredHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 1 && splineCurveCount >= 1 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole intersecting with another hole requiredHolefaces.Add(currentface); } } selectSet.Clear(); } holeCount = requiredHolefaces.Count; threadedHoleCount = requiredThreadedHolefaces.Count; if (requiredHolefaces.Count > 0) { HighlightSet highlightSet = iPartDoc.CreateHighlightSet(); if (highlightSet == null) throw new Exception("Failed to get Highlight Set "); highlightSet.Color.SetColor(255, 0, 0); foreach (Face currentHoleFace in requiredHolefaces) { highlightSet.Clear(); selectSet.Select(currentHoleFace); highlightSet.AddItem(currentHoleFace); } } if (requiredThreadedHolefaces.Count > 0) { HighlightSet highlightSet = iPartDoc.CreateHighlightSet(); if (highlightSet == null) throw new Exception("Failed to get Highlight Set "); highlightSet.Color.SetColor(0, 255, 0); foreach (Face currentHoleFace in requiredThreadedHolefaces) { highlightSet.Clear(); selectSet.Select(currentHoleFace); highlightSet.AddItem(currentHoleFace); } } return requiredHolefaces; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("---(WARNING)Failed - Hole Detection"); Console.WriteLine(); } Console.WriteLine("No. of holes = " + holeCount); Console.WriteLine("No. of threaded holes = " + threadedHoleCount); Console.WriteLine("---Please see detected holes highlighted in the model"); Console.WriteLine("---END - Hole Detection"); Console.WriteLine(); return requiredHolefaces; } private bool isCylindricalFaceInterior(PartComponentDefinition iPartDefin, Face iFace) { bool isInterior = false; try { Cylinder oCylinder = iFace.Geometry as Cylinder; if (oCylinder == null) throw new Exception("Failed to get cylinder from face"); Application iApplication = iPartDefin.Application as Application; if (iApplication == null) throw new Exception("Failed to get application from part defination."); double[] para = new double[2]; para[0] = 0.5; para[1] = 0.5; //getting point on surface at 0.5,0.5 parameters. double[] points = new double[3]; iFace.Evaluator.GetPointAtParam(ref para, ref points); //creating point Point oPoint = iApplication.TransientGeometry.CreatePoint(points[0], points[1], points[2]); if (oPoint == null) throw new Exception("Failed to create point."); //getting normal at this location. double[] normals = new double[3]; iFace.Evaluator.GetNormal(ref para, ref normals); //creating normal vector Inventor.Vector oNormal = iApplication.TransientGeometry.CreateVector(normals[0], normals[1], normals[2]); if (oNormal == null) throw new Exception("Failed to create vector."); //scaling vector by radius of cylinder oNormal.ScaleBy(oCylinder.Radius); //translating sample point in direction of normal i.e tip of normal vector Inventor.Point oSamplePoint = oPoint; oSamplePoint.TranslateBy(oNormal); //creating line starting from basepoint of cylinder in direction of cylindrical axis Inventor.Line oAxisLine = iApplication.TransientGeometry.CreateLine(oCylinder.BasePoint, oCylinder.AxisVector.AsVector()); if (oAxisLine == null) throw new Exception("Failed to create line."); //creating line starting from created sample point in direction of cylindrical axis Inventor.Line oSampleLine = iApplication.TransientGeometry.CreateLine(oSamplePoint, oCylinder.AxisVector.AsVector()); if (oSampleLine == null) throw new Exception("Failed to create line."); //if those lines are colinear then face is inner face else outer face if (oSampleLine.IsColinearTo[oAxisLine]) isInterior = true; else isInterior = false; } catch (Exception ex) { Console.WriteLine(ex.Message); return isInterior; } return isInterior; }```
Write a Autoodesk Inventor API Code for this Rule. Rule: Check the distance between two holes should be greater than 10 cm
```csharp private void HoleToHoleDetect() { try { WindowState = WindowState.Minimized; if (_inventorApp == null) { Console.WriteLine("Inventor application instance is not properly initialized."); return; } if (_inventorApp.ActiveDocument == null) { Console.WriteLine("No active document."); return; } Document activeDoc = _inventorApp.ActiveDocument; if (!(activeDoc is PartDocument)) { Console.WriteLine("Please open a part document."); return; } PartDocument partDoc = activeDoc as PartDocument; SurfaceBody mainBody = partDoc.ComponentDefinition.SurfaceBodies[1]; List<Face> requiredHolefaces = DetectHole(partDoc, mainBody); int holeCount = requiredHolefaces.Count; if (holeCount <= 0) { MessageBox.Show("No holes detected.", "Result of Hole Detection ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // Calculate the centroid of the detected holes double[] centerX = new double[holeCount]; double[] centerY = new double[holeCount]; double[] centerZ = new double[holeCount]; for (int i = 0; i < holeCount; i++) { foreach (Vertex vertex in requiredHolefaces[i].Vertices) { centerX[i] += Math.Round(vertex.Point.X * 25.4, 2); centerY[i] += Math.Round(vertex.Point.Y * 25.4, 2); centerZ[i] += Math.Round(vertex.Point.Z * 25.4, 2); } centerX[i] /= 4; // 4 vertices per face centerY[i] /= 4; centerZ[i] /= 4; } // Calculate and display distances between centroids StringBuilder resultMessage = new StringBuilder(); resultMessage.AppendLine($"Number of holes: {holeCount}"); resultMessage.AppendLine(); for (int i = 0; i < holeCount; i++) { resultMessage.AppendLine($"Hole {i + 1} Center coordinates: X={centerX[i]}, Y={centerY[i]}, Z={centerZ[i]}"); } // Check distances and store distances below 10 cm string[] distancesBelow10Cm = new string[holeCount * (holeCount - 1) / 2]; List<Face> facesToColorRed = new List<Face>(); int failedRuleIndex = 0; resultMessage.AppendLine(); for (int i = 0; i < holeCount; i++) { for (int j = i + 1; j < holeCount; j++) { double distance = CalculateDistance(centerX[i], centerY[i], centerZ[i], centerX[j], centerY[j], centerZ[j]); resultMessage.AppendLine($"Distance from Hole {i + 1} to Hole {j + 1} : {Math.Round(distance, 2)} mm"); if (distance < 10) { distancesBelow10Cm[failedRuleIndex] = $" Hole { i + 1} to Hole { j + 1} distance is { Math.Round(distance, 2)} mm which is less than 10mm, Hence the rule is failed"; failedRuleIndex++; } else { resultMessage.AppendLine(); resultMessage.AppendLine(" Rule Passed :"); resultMessage.AppendLine($"Hole {i + 1} to Hole {j + 1} Distance is {Math.Round(distance, 2)}mm which is more than 10mm so rule passed"); } } resultMessage.AppendLine(); } if (failedRuleIndex > 0) { resultMessage.AppendLine(" Rule Failed :"); for (int i = 0; i < distancesBelow10Cm.Length; i++) { resultMessage.AppendLine($"{distancesBelow10Cm[i]}"); } } else { resultMessage.AppendLine(); } MessageBox.Show(resultMessage.ToString(), "Result of Hole Distance ", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error - Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Ensure the UI is re-enabled when the operation is finished, even if there's an exception WindowState = WindowState.Maximized; } } private double CalculateDistance(double x1, double y1, double z1, double x2, double y2, double z2) { return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2) + Math.Pow(z2 - z1, 2)); } private List<Face> DetectHole(PartDocument iPartDoc, SurfaceBody iMainBody) { List<Face> requiredHolefaces = new List<Face>(); try { //for selection and highlight SelectSet selectSet = iPartDoc.SelectSet; if (selectSet == null) throw new Exception("Failed to get Selection Set"); //checking all faces Faces allFaces = iMainBody.Faces; if (allFaces.Count <= 0) throw new Exception("Failed to get faces from body"); //finding hole faces List<Face> requiredThreadedHolefaces = new List<Face>(); int iShouldParaReversed = -1; bool bShouldParaReversed = false; foreach (Face currentface in allFaces) { selectSet.Select(currentface); if (currentface.SurfaceType == SurfaceTypeEnum.kCylinderSurface) { if (iShouldParaReversed == -1) { bool isFaceInterior = isCylindricalFaceInterior(iPartDoc.ComponentDefinition, currentface); bShouldParaReversed = (isFaceInterior) ? currentface.IsParamReversed : !currentface.IsParamReversed; iShouldParaReversed = 0; } bool isParaReversed = currentface.IsParamReversed; if (bShouldParaReversed == currentface.IsParamReversed) { int circularCurveCount = 0; int linearCurveCount = 0; int splineCurveCount = 0; foreach (Edge currentEdge in currentface.Edges) { if (currentEdge.CurveType == CurveTypeEnum.kBSplineCurve) splineCurveCount++; else if (currentEdge.CurveType == CurveTypeEnum.kCircleCurve) circularCurveCount++; else if (currentEdge.CurveType == CurveTypeEnum.kLineCurve) linearCurveCount++; } /* if ((linearCurveCount == 0 && splineCurveCount >= 2 && circularCurveCount == 2 && currentface.EdgeLoops.Count == 1))//condition for through threaded hole requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 1 && splineCurveCount >= 2 && circularCurveCount == 1))//condition for depth threaded hole requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 2 && splineCurveCount >= 2 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for depth threaded holes from both sides requiredThreadedHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole requiredHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 1 && splineCurveCount >= 1 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole intersecting with another hole requiredHolefaces.Add(currentface);*/ if ((linearCurveCount == 0 && circularCurveCount == 2 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole requiredHolefaces.Add(currentface); else if ((linearCurveCount == 0 && circularCurveCount == 1 && splineCurveCount >= 1 && currentface.EdgeLoops.Count >= 2))//condition for non threaded hole intersecting with another hole requiredHolefaces.Add(currentface); } } selectSet.Clear(); } holeCount = requiredHolefaces.Count; threadedHoleCount = requiredThreadedHolefaces.Count; if (requiredHolefaces.Count > 0) { HighlightSet highlightSet = iPartDoc.CreateHighlightSet(); if (highlightSet == null) throw new Exception("Failed to get Highlight Set "); highlightSet.Color.SetColor(255, 0, 0); foreach (Face currentHoleFace in requiredHolefaces) { highlightSet.Clear(); selectSet.Select(currentHoleFace); highlightSet.AddItem(currentHoleFace); } } if (requiredThreadedHolefaces.Count > 0) { HighlightSet highlightSet = iPartDoc.CreateHighlightSet(); if (highlightSet == null) throw new Exception("Failed to get Highlight Set "); highlightSet.Color.SetColor(0, 255, 0); foreach (Face currentHoleFace in requiredThreadedHolefaces) { highlightSet.Clear(); selectSet.Select(currentHoleFace); highlightSet.AddItem(currentHoleFace); } } return requiredHolefaces; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("---(WARNING)Failed - Hole Detection"); Console.WriteLine(); } Console.WriteLine("No. of holes = " + holeCount); Console.WriteLine("No. of threaded holes = " + threadedHoleCount); Console.WriteLine("---Please see detected holes highlighted in the model"); Console.WriteLine("---END - Hole Detection"); Console.WriteLine(); return requiredHolefaces; } private bool isCylindricalFaceInterior(PartComponentDefinition iPartDefin, Face iFace) { bool isInterior = false; try { Cylinder oCylinder = iFace.Geometry as Cylinder; if (oCylinder == null) throw new Exception("Failed to get cylinder from face"); Application iApplication = iPartDefin.Application as Application; if (iApplication == null) throw new Exception("Failed to get application from part defination."); double[] para = new double[2]; para[0] = 0.5; para[1] = 0.5; //getting point on surface at 0.5,0.5 parameters. double[] points = new double[3]; iFace.Evaluator.GetPointAtParam(ref para, ref points); //creating point Point oPoint = iApplication.TransientGeometry.CreatePoint(points[0], points[1], points[2]); if (oPoint == null) throw new Exception("Failed to create point."); //getting normal at this location. double[] normals = new double[3]; iFace.Evaluator.GetNormal(ref para, ref normals); //creating normal vector Inventor.Vector oNormal = iApplication.TransientGeometry.CreateVector(normals[0], normals[1], normals[2]); if (oNormal == null) throw new Exception("Failed to create vector."); //scaling vector by radius of cylinder oNormal.ScaleBy(oCylinder.Radius); //translating sample point in direction of normal i.e tip of normal vector Inventor.Point oSamplePoint = oPoint; oSamplePoint.TranslateBy(oNormal); //creating line starting from basepoint of cylinder in direction of cylindrical axis Inventor.Line oAxisLine = iApplication.TransientGeometry.CreateLine(oCylinder.BasePoint, oCylinder.AxisVector.AsVector()); if (oAxisLine == null) throw new Exception("Failed to create line."); //creating line starting from created sample point in direction of cylindrical axis Inventor.Line oSampleLine = iApplication.TransientGeometry.CreateLine(oSamplePoint, oCylinder.AxisVector.AsVector()); if (oSampleLine == null) throw new Exception("Failed to create line."); //if those lines are colinear then face is inner face else outer face if (oSampleLine.IsColinearTo[oAxisLine]) isInterior = true; else isInterior = false; } catch (Exception ex) { Console.WriteLine(ex.Message); return isInterior; } return isInterior; }```

No dataset card yet

Downloads last month
6