private void CreatePart_Click(object sender, EventArgs e) { if (inventor == null) { MessageBox.Show("No inventor instance detected", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } PartDocument doc = inventor.Documents.Add(DocumentTypeEnum.kPartDocumentObject, null, true) as PartDocument; doc.PropertySets["{F29F85E0-4FF9-1068-AB91-08002B27B3D9}"]["Author"].Value = "Vladyslav Romanchuk"; //User-defined property doc.PropertySets["{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"].Add("Parts R Us", "Supplier"); PartComponentDefinition partDefinition = (PartComponentDefinition)doc.ComponentDefinition; // Create a 2D sketch on the X-Y plane. PlanarSketch sketch1 = (PlanarSketch)partDefinition.Sketches.Add(partDefinition.WorkPlanes[3]); TransientGeometry tg = inventor.TransientGeometry; Point2d[] points = new Point2d[5] { tg.CreatePoint2d(0, 0), tg.CreatePoint2d(0, 20), tg.CreatePoint2d(20, 20), tg.CreatePoint2d(20, -10), tg.CreatePoint2d(10, -10) }; SketchLine[] lines = new SketchLine[5]; lines[0] = sketch1.SketchLines.AddByTwoPoints(points[0], points[1]); for (int i = 1; i < lines.Length - 1; i++) { lines[i] = sketch1.SketchLines.AddByTwoPoints(lines[i - 1].EndSketchPoint, points[i + 1]); } sketch1.SketchArcs.AddByCenterStartEndPoint(tg.CreatePoint2d(10, 0), lines[3].EndSketchPoint, lines[0].StartSketchPoint, false); //Extrude Profile profile = sketch1.Profiles.AddForSolid(); ExtrudeDefinition extrudeDefinition = partDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(profile, PartFeatureOperationEnum.kNewBodyOperation); extrudeDefinition.SetDistanceExtent(6, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection); ExtrudeFeature extrude = (ExtrudeFeature)partDefinition.Features.ExtrudeFeatures.Add(extrudeDefinition); //second scatch Face topCap = extrude.EndFaces[1]; sketch1 = partDefinition.Sketches.Add(topCap, false); Point2d center = sketch1.ModelToSketchSpace(tg.CreatePoint(2.5, 1.5, 1.5)); SketchCircle Circle = sketch1.SketchCircles.AddByCenterRadius(center, 1); profile = sketch1.Profiles.AddForSolid(true, null, null); extrudeDefinition = partDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(profile, PartFeatureOperationEnum.kJoinOperation); extrudeDefinition.SetDistanceExtent(4, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection); extrude = (ExtrudeFeature)partDefinition.Features.ExtrudeFeatures.Add(extrudeDefinition); Edges cylinderEdges = extrude.SideFaces[1].Edges; EdgeCollection filletEdges = inventor.TransientObjects.CreateEdgeCollection(null); //foreach (var el in cylinderEdges) // filletEdges.Add(el); filletEdges.Add(cylinderEdges[2]); //adding fillet partDefinition.Features.FilletFeatures.AddSimple(filletEdges, 0.25, false, false, false, false, false, true); //doc.SaveAs("D:\\SaveTest2.ipt", false); }
/// <summary> /// Method that adjusts the points that make up the rectangle according to the "new" location of /// the upper Right Point2d /// </summary> /// <param name="modelPosition"></param> protected void AdjustRectanglePosition(Point modelPosition) { if (_planarSketch != null) { _upperRightPoint2d = _planarSketch.ModelToSketchSpace(modelPosition); } else if (_drawingSketch != null) { PointToPoint2d(modelPosition, out _upperRightPoint2d); } PositionPoint2dObjects(); WriteToInteractionPointCoords(); _rectangleCoordSet.PutCoordinates(_rectanglePointCoords); _rectangleLineStripGraphics.CoordinateSet = _rectangleCoordSet; _rectangleLineStripGraphics.CoordinateIndexSet = _rectangleIndexSet; _rectangleInteractionGraphics.UpdateOverlayGraphics(_inventorApplication.ActiveView); }
/// <summary> /// When the user clicks on a point in the model, this method runs and creates an interaction graphics rectangle. /// It also subscribes to the Mouse Move event to allow for the size of the rectangle to be driven by the location /// of the mouse on the sketch. Ultimately, this method waits for the next click event, in which case it calls a /// new method to actually create the sketched rectangle. It has separate paths to react to whether it's a part /// sketch or a drawing sketch. /// </summary> /// <param name="button"></param> /// <param name="shiftKeys"></param> /// <param name="modelPosition"></param> /// <param name="viewPosition"></param> /// <param name="view"></param> protected void OnMouseClick_CreateRectangle(MouseButtonEnum button, ShiftStateEnum shiftKeys, Point modelPosition, Point2d viewPosition, Inventor.View view) { try { if (button == MouseButtonEnum.kLeftMouseButton) { _onMouseMove_Delegate = new MouseEventsSink_OnMouseMoveEventHandler(OnMouseMove_DragCornerOfRectangle); _mouseEvents.OnMouseClick -= _onMouseClick_Delegate; _onMouseClick_Delegate = null; _userInputEvents.OnContextMenu -= _userInputEvents_OnContextMenuDelegate; _userInputEvents_OnContextMenuDelegate = null; if (_inventorApplication.ActiveEditObject is PlanarSketch) { _planarSketch = (PlanarSketch)_inventorApplication.ActiveEditObject; if (_mouseEvents.PointInferences.Count > 0) { _inferredOriginPoint = _mouseEvents.PointInferences[1]; switch (_inferredOriginPoint.InferenceType) { case PointInferenceEnum.kPtAtIntersection: _firstSelectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[1]; _firstInferredIntersectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[2]; break; case PointInferenceEnum.kPtOnCurve: _firstSelectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[1]; break; case PointInferenceEnum.kPtOnPt: _firstSelectedSketchEntity = (SketchPoint)_inferredOriginPoint.Entity[1]; _pickedSketchPoint = (SketchPoint)_firstSelectedSketchEntity; _rectangleOriginSketchPoint = _pickedSketchPoint; break; case PointInferenceEnum.kPtAtMidPoint: _firstSelectedSketchEntity = (SketchLine)_inferredOriginPoint.Entity[1]; break; } } _pickedPoint2d = _planarSketch.ModelToSketchSpace(modelPosition); DrawInteractionRectangle(); _mouseEvents.OnMouseMove += _onMouseMove_Delegate; _mouseEvents.MouseMoveEnabled = true; } else if (_inventorApplication.ActiveEditObject is DrawingSketch) { _drawingSketch = (DrawingSketch)_inventorApplication.ActiveEditObject; if (_mouseEvents.PointInferences.Count > 0) { _inferredOriginPoint = _mouseEvents.PointInferences[1]; switch (_inferredOriginPoint.InferenceType) { case PointInferenceEnum.kPtAtIntersection: _firstSelectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[1]; _firstInferredIntersectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[2]; break; case PointInferenceEnum.kPtOnCurve: _firstSelectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[1]; break; case PointInferenceEnum.kPtOnPt: _firstSelectedSketchEntity = (SketchPoint)_inferredOriginPoint.Entity[1]; _pickedSketchPoint = (SketchPoint)_firstSelectedSketchEntity; _rectangleOriginSketchPoint = _pickedSketchPoint; break; case PointInferenceEnum.kPtAtMidPoint: _firstSelectedSketchEntity = (SketchLine)_inferredOriginPoint.Entity[1]; break; } } PointToPoint2d(modelPosition, out _pickedPoint2d); DrawInteractionRectangle(); _mouseEvents.OnMouseMove += _onMouseMove_Delegate; _mouseEvents.MouseMoveEnabled = true; } else { MessageBox.Show("You must be editing a sketch in order to use this tool."); } } } catch (Exception e) { MessageBox.Show(e.ToString()); } }
///////////////////////////////////////////////////////////// // Use: Creates new solid bodies affected by the future // CoilFeature for Tapered Thread. ///////////////////////////////////////////////////////////// private static bool CreateCoilBodyTapered(PartDocument doc, ThreadInfo threadInfo, Face threadedFace, double depth, bool isInteriorFace) { try { PartComponentDefinition compDef = doc.ComponentDefinition; Vector direction = threadInfo.ThreadDirection; Point basePoint = threadInfo.ThreadBasePoints[1] as Point; Point endPoint = _Tg.CreatePoint( basePoint.X + direction.X, basePoint.Y + direction.Y, basePoint.Z + direction.Z); UnitVector yAxis = direction.AsUnitVector(); UnitVector xAxis = Toolkit.GetOrthoVector(yAxis); WorkPoint wpt = compDef.WorkPoints.AddFixed(basePoint, _ConstructionWorkFeature); WorkPlane wpl = compDef.WorkPlanes.AddFixed(basePoint, xAxis, yAxis, _ConstructionWorkFeature); WorkAxis xWa = compDef.WorkAxes.AddFixed(basePoint, xAxis, _ConstructionWorkFeature); WorkAxis yWa = compDef.WorkAxes.AddFixed(basePoint, yAxis, _ConstructionWorkFeature); PlanarSketch sketch = compDef.Sketches.AddWithOrientation(wpl, xWa, true, true, wpt, false); Cone cone = threadedFace.Geometry as Cone; double revDepth = depth / Math.Cos(cone.HalfAngle) * (isInteriorFace ? -1.0 : 1.0); Line l1 = Toolkit.GetFaceSideDirection(threadedFace, xAxis); Line l2 = _Tg.CreateLine(basePoint, xAxis.AsVector()); Line l3 = _Tg.CreateLine(endPoint, xAxis.AsVector()); Point p1 = l1.IntersectWithCurve(l2, 0.0001)[1] as Point; Point p2 = l1.IntersectWithCurve(l3, 0.0001)[1] as Point; Point p3 = _Tg.CreatePoint( p2.X - xAxis.X * revDepth, p2.Y - xAxis.Y * revDepth, p2.Z - xAxis.Z * revDepth); Point p4 = _Tg.CreatePoint( p1.X - xAxis.X * revDepth, p1.Y - xAxis.Y * revDepth, p1.Z - xAxis.Z * revDepth); SketchPoint skp1 = sketch.SketchPoints.Add( sketch.ModelToSketchSpace(p1), false); SketchPoint skp2 = sketch.SketchPoints.Add( sketch.ModelToSketchSpace(p2), false); SketchPoint skp3 = sketch.SketchPoints.Add( sketch.ModelToSketchSpace(p3), false); SketchPoint skp4 = sketch.SketchPoints.Add( sketch.ModelToSketchSpace(p4), false); sketch.SketchLines.AddByTwoPoints(skp1, skp2); sketch.SketchLines.AddByTwoPoints(skp2, skp3); sketch.SketchLines.AddByTwoPoints(skp3, skp4); sketch.SketchLines.AddByTwoPoints(skp4, skp1); Profile profile = sketch.Profiles.AddForSolid(true, null, null); RevolveFeature rev1 = compDef.Features.RevolveFeatures.AddFull( profile, yWa, PartFeatureOperationEnum.kCutOperation); sketch = compDef.Sketches.AddWithOrientation(wpl, xWa, true, true, wpt, false); skp1 = sketch.SketchPoints.Add( sketch.ModelToSketchSpace(p1), false); skp2 = sketch.SketchPoints.Add( sketch.ModelToSketchSpace(p2), false); skp3 = sketch.SketchPoints.Add( sketch.ModelToSketchSpace(p3), false); skp4 = sketch.SketchPoints.Add( sketch.ModelToSketchSpace(p4), false); sketch.SketchLines.AddByTwoPoints(skp1, skp2); sketch.SketchLines.AddByTwoPoints(skp2, skp3); sketch.SketchLines.AddByTwoPoints(skp3, skp4); sketch.SketchLines.AddByTwoPoints(skp4, skp1); profile = sketch.Profiles.AddForSolid(true, null, null); RevolveFeature rev2 = compDef.Features.RevolveFeatures.AddFull( profile, yWa, PartFeatureOperationEnum.kNewBodyOperation); return(true); } catch { return(false); } }
/// <summary> /// When the user clicks on a point in the model, this method runs and creates an interaction graphics rectangle. /// It also subscribes to the Mouse Move event to allow for the size of the rectangle to be driven by the location /// of the mouse on the sketch. Ultimately, this method waits for the next click event, in which case it calls a /// new method to actually create the sketched rectangle. It has separate paths to react to whether it's a part /// sketch or a drawing sketch. /// </summary> /// <param name="button"></param> /// <param name="shiftKeys"></param> /// <param name="modelPosition"></param> /// <param name="viewPosition"></param> /// <param name="view"></param> protected void OnMouseClick_CreateRectangle(MouseButtonEnum button, ShiftStateEnum shiftKeys, Point modelPosition, Point2d viewPosition, Inventor.View view) { try { if (button == MouseButtonEnum.kLeftMouseButton) { _onMouseMove_Delegate = new MouseEventsSink_OnMouseMoveEventHandler(OnMouseMove_DragCornerOfRectangle); _mouseEvents.OnMouseClick -= _onMouseClick_Delegate; _onMouseClick_Delegate = null; _userInputEvents.OnContextMenu -= _userInputEvents_OnContextMenuDelegate; _userInputEvents_OnContextMenuDelegate = null; if (_inventorApplication.ActiveEditObject is PlanarSketch) { _planarSketch = (PlanarSketch)_inventorApplication.ActiveEditObject; if (_mouseEvents.PointInferences.Count > 0) { _inferredOriginPoint = _mouseEvents.PointInferences[1]; switch (_inferredOriginPoint.InferenceType) { case PointInferenceEnum.kPtAtIntersection: _firstSelectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[1]; _firstInferredIntersectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[2]; break; case PointInferenceEnum.kPtOnCurve: _firstSelectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[1]; break; case PointInferenceEnum.kPtOnPt: _firstSelectedSketchEntity = (SketchPoint)_inferredOriginPoint.Entity[1]; _pickedSketchPoint = (SketchPoint)_firstSelectedSketchEntity; _rectangleOriginSketchPoint = _pickedSketchPoint; break; case PointInferenceEnum.kPtAtMidPoint: _firstSelectedSketchEntity = (SketchLine)_inferredOriginPoint.Entity[1]; break; } } _pickedPoint2d = _planarSketch.ModelToSketchSpace(modelPosition); DrawInteractionRectangle(); _mouseEvents.OnMouseMove += _onMouseMove_Delegate; _mouseEvents.MouseMoveEnabled = true; } else if (_inventorApplication.ActiveEditObject is DrawingSketch) { _drawingSketch = (DrawingSketch)_inventorApplication.ActiveEditObject; if (_mouseEvents.PointInferences.Count > 0) { _inferredOriginPoint = _mouseEvents.PointInferences[1]; switch (_inferredOriginPoint.InferenceType) { case PointInferenceEnum.kPtAtIntersection: _firstSelectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[1]; _firstInferredIntersectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[2]; break; case PointInferenceEnum.kPtOnCurve: _firstSelectedSketchEntity = (SketchEntity)_inferredOriginPoint.Entity[1]; break; case PointInferenceEnum.kPtOnPt: _firstSelectedSketchEntity = (SketchPoint)_inferredOriginPoint.Entity[1]; _pickedSketchPoint = (SketchPoint)_firstSelectedSketchEntity; _rectangleOriginSketchPoint = _pickedSketchPoint; break; case PointInferenceEnum.kPtAtMidPoint: _firstSelectedSketchEntity = (SketchLine)_inferredOriginPoint.Entity[1]; break; } } PointToPoint2d(modelPosition, out _pickedPoint2d); DrawInteractionRectangle(); _mouseEvents.OnMouseMove += _onMouseMove_Delegate; _mouseEvents.MouseMoveEnabled = true; } else MessageBox.Show("You must be editing a sketch in order to use this tool."); } } catch (Exception e) { MessageBox.Show(e.ToString()); } }