public double GetValueFromExpression(string expression) { double value = 0.0; //get the active document Document activeDocument = m_inventorApplication.ActiveDocument; //get the unit of measure object UnitsOfMeasure unitsOfMeasure = activeDocument.UnitsOfMeasure; //get the current length units of the user UnitsTypeEnum lengthUnitsType = unitsOfMeasure.LengthUnits; //convert the expression to the current length units of user try { object vVal; vVal = unitsOfMeasure.GetValueFromExpression(expression, lengthUnitsType); value = System.Convert.ToDouble(vVal); } catch (System.Exception e) { string strErrorMsg = e.Message; value = 0.0; return(value); } return(value); }
private Inventor.ExtrudeFeature CreateFlangeExtrustion(ref Inventor.PartDocument PartDocument, string FlangeDiaOD, string FlangeDiaID, string FlangeThickness) { Inventor.UnitsOfMeasure UnitsOfMeasure; Inventor.PlanarSketch Sketch; Inventor.TransientGeometry TransientGeometry; Inventor.SketchCircle SketchCircle; Inventor.WorkPoint WorkPoint; Inventor.WorkPlane BaseWorkPlane; Inventor.RadiusDimConstraint RadiusDimConstraint = null; Inventor.SketchEntity SketchEntity; Inventor.ObjectCollection SketchObjectCollection; Inventor.Profile Profile; Inventor.ExtrudeDefinition ExtrudeDefinition; Inventor.ExtrudeFeature ExtrudeFeature = null; SketchObjectCollection = InvApp.TransientObjects.CreateObjectCollection(); UnitsOfMeasure = PartDocument.UnitsOfMeasure; double DiaOD = 0, DiaID = 0, Thickness = 0; DiaOD = UnitsOfMeasure.GetValueFromExpression(FlangeDiaOD, Inventor.UnitsTypeEnum.kMillimeterLengthUnits); DiaID = UnitsOfMeasure.GetValueFromExpression(FlangeDiaID, Inventor.UnitsTypeEnum.kMillimeterLengthUnits); Thickness = UnitsOfMeasure.GetValueFromExpression(FlangeThickness, Inventor.UnitsTypeEnum.kMillimeterLengthUnits); TransientGeometry = InvApp.TransientGeometry; WorkPoint = PartDocument.ComponentDefinition.WorkPoints[1]; BaseWorkPlane = GetPartDocumentWorkPlane(ref PartDocument, "XY"); Sketch = PartDocument.ComponentDefinition.Sketches.Add(BaseWorkPlane, false); SketchEntity = Sketch.AddByProjectingEntity(WorkPoint); SketchCircle = Sketch.SketchCircles.AddByCenterRadius(TransientGeometry.CreatePoint2d(0, 0), DiaOD / 2); RadiusDimConstraint = Sketch.DimensionConstraints.AddRadius((Inventor.SketchEntity)SketchCircle, TransientGeometry.CreatePoint2d(0, 0), false); Sketch.GeometricConstraints.AddCoincident(SketchEntity, (Inventor.SketchEntity)SketchCircle.CenterSketchPoint); RadiusDimConstraint = null; SketchCircle = null; SketchCircle = Sketch.SketchCircles.AddByCenterRadius(TransientGeometry.CreatePoint2d(0, 0), DiaID / 2); RadiusDimConstraint = Sketch.DimensionConstraints.AddRadius((Inventor.SketchEntity)SketchCircle, TransientGeometry.CreatePoint2d(0, 0), false); Sketch.GeometricConstraints.AddCoincident(SketchEntity, (Inventor.SketchEntity)SketchCircle.CenterSketchPoint); SketchObjectCollection.Add(SketchCircle); Profile = Sketch.Profiles.AddForSolid(true, SketchObjectCollection); ExtrudeDefinition = PartDocument.ComponentDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(Profile, Inventor.PartFeatureOperationEnum.kNewBodyOperation); ExtrudeDefinition.SetDistanceExtent(Thickness, Inventor.PartFeatureExtentDirectionEnum.kPositiveExtentDirection); ExtrudeFeature = PartDocument.ComponentDefinition.Features.ExtrudeFeatures.Add(ExtrudeDefinition); ExtrudeFeature.Name = "FlangeBase"; return(ExtrudeFeature); }
private void Button2_Click(object sender, EventArgs e) { try { PartDocument oPartDoc = mApp.ActiveDocument as PartDocument; Parameter oParam = oPartDoc.ComponentDefinition.Parameters["Length"]; double value = (double)mUOM.GetValueFromExpression(TextBox2.Text, UnitsTypeEnum.kDefaultDisplayLengthUnits); oParam.Value = value; oPartDoc.Update(); mApp.ActiveView.Fit(true); } catch { System.Windows.Forms.MessageBox.Show("Error: Parameter \"Length\" does not exist in active document..."); } }
private void AddFlangeiMateDefinitions(ref Inventor.PartDocument PartDocument, string Dia, string MateNamePrefix, string Offset) { Inventor.ExtrudeFeature ExtrudeFeature; Inventor.UnitsOfMeasure UnitsOfMeasure; Inventor.Edge Edge = null; Inventor.EdgeLoops EdgeLoops; double FlangeHoleDiaOD = 0; int Counter = 0; ExtrudeFeature = PartDocument.ComponentDefinition.Features.ExtrudeFeatures[1]; UnitsOfMeasure = PartDocument.UnitsOfMeasure; FlangeHoleDiaOD = UnitsOfMeasure.GetValueFromExpression(Dia, Inventor.UnitsTypeEnum.kMillimeterLengthUnits); foreach (Inventor.Face Face in ExtrudeFeature.Faces) { if (Face.SurfaceType == Inventor.SurfaceTypeEnum.kCylinderSurface) { Inventor.Cylinder Cylinder; Cylinder = Face.Geometry; if (Cylinder.Radius == FlangeHoleDiaOD / 2) { EdgeLoops = Face.EdgeLoops; foreach (Inventor.EdgeLoop EdgeLoop in EdgeLoops) { if (EdgeLoop.IsOuterEdgeLoop) { foreach (Inventor.Edge mEdge in EdgeLoop.Edges) { if (mEdge.CurveType == Inventor.CurveTypeEnum.kCircleCurve) { Edge = mEdge; PartDocument.ComponentDefinition.iMateDefinitions.AddInsertiMateDefinition(Edge, false, Offset, null, MateNamePrefix + ":" + Counter.ToString()); Counter++; } } } } } } } }
private Inventor.ObjectCollection CreateHoleSketch(ref Inventor.PartDocument PartDocument, ref Inventor.ExtrudeFeature ExtrudeFeature, string PCD, string PCDAngle) { Inventor.UnitsOfMeasure UnitsOfMeasure; Inventor.PlanarSketch Sketch = null; Inventor.WorkPoint WorkPoint = null; Inventor.SketchEntity CenterPointSketchEntity = null; Inventor.SketchArc pcdSketchArc = null; Inventor.SketchLine AngleSketchLine = null; Inventor.SketchLine CenterSketchLine = null; Inventor.SketchPoint SketchPoint = null; Inventor.TransientGeometry TransientGeometry; Inventor.RadiusDimConstraint RadiusDimConstraint = null; Inventor.TwoLineAngleDimConstraint TwoLineAngleDimConstraint = null; Inventor.ObjectCollection ObjectCollection = null; Inventor.ModelParameter ModelParameter = null; ObjectCollection = InvApp.TransientObjects.CreateObjectCollection(); double pcdDia = 0, pcdAngle = 0.0; UnitsOfMeasure = PartDocument.UnitsOfMeasure; pcdDia = UnitsOfMeasure.GetValueFromExpression(PCD, Inventor.UnitsTypeEnum.kMillimeterLengthUnits); pcdAngle = UnitsOfMeasure.GetValueFromExpression(PCDAngle, Inventor.UnitsTypeEnum.kDegreeAngleUnits); WorkPoint = PartDocument.ComponentDefinition.WorkPoints[1]; TransientGeometry = InvApp.TransientGeometry; foreach (Inventor.Face Face in ExtrudeFeature.EndFaces) { if (Face.SurfaceType == Inventor.SurfaceTypeEnum.kPlaneSurface) { Sketch = PartDocument.ComponentDefinition.Sketches.Add(Face, false); CenterPointSketchEntity = Sketch.AddByProjectingEntity(WorkPoint); CenterSketchLine = Sketch.SketchLines.AddByTwoPoints(TransientGeometry.CreatePoint2d(0, 0), TransientGeometry.CreatePoint2d(0, pcdDia / 2)); CenterSketchLine.Construction = true; AngleSketchLine = Sketch.SketchLines.AddByTwoPoints(CenterSketchLine.StartSketchPoint, TransientGeometry.CreatePoint2d(20, pcdDia / 2)); AngleSketchLine.Construction = true; Sketch.GeometricConstraints.AddCoincident(CenterPointSketchEntity, (Inventor.SketchEntity)CenterSketchLine.StartSketchPoint); Sketch.GeometricConstraints.AddEqualLength(CenterSketchLine, AngleSketchLine); pcdSketchArc = Sketch.SketchArcs.AddByCenterStartEndPoint(CenterSketchLine.StartSketchPoint, CenterSketchLine.EndSketchPoint, AngleSketchLine.EndSketchPoint, false); pcdSketchArc.Construction = true; Sketch.GeometricConstraints.AddVertical((Inventor.SketchEntity)CenterSketchLine, false); Sketch.GeometricConstraints.AddCoincident(CenterPointSketchEntity, (Inventor.SketchEntity)pcdSketchArc.CenterSketchPoint); SketchPoint = Sketch.SketchPoints.Add(TransientGeometry.CreatePoint2d(0, 0), true); Sketch.GeometricConstraints.AddCoincident((Inventor.SketchEntity)SketchPoint, (Inventor.SketchEntity)AngleSketchLine.EndSketchPoint); RadiusDimConstraint = Sketch.DimensionConstraints.AddRadius((Inventor.SketchEntity)pcdSketchArc, pcdSketchArc.Geometry.Center, false); if (RadiusDimConstraint.Parameter.ParameterType == Inventor.ParameterTypeEnum.kModelParameter) { ModelParameter = (Inventor.ModelParameter)RadiusDimConstraint.Parameter; if (ModelParameter.BuiltIn) { ModelParameter.Name = "PCD"; ModelParameter.Value = pcdDia / 2; } } TwoLineAngleDimConstraint = Sketch.DimensionConstraints.AddTwoLineAngle(CenterSketchLine, AngleSketchLine, TransientGeometry.CreatePoint2d(1, 1), false); if (TwoLineAngleDimConstraint.Parameter.ParameterType == Inventor.ParameterTypeEnum.kModelParameter) { ModelParameter = null; ModelParameter = (Inventor.ModelParameter)TwoLineAngleDimConstraint.Parameter; if (ModelParameter.BuiltIn) { ModelParameter.Name = "Angle"; ModelParameter.Value = pcdAngle / 2; } } ObjectCollection.Add(SketchPoint); } PartDocument.Update(); } return(ObjectCollection); }