private void Button1_Click(object sender, EventArgs e)
        {
            PartDocument oDoc = mApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject,
                                                   mApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject,
                                                                                    SystemOfMeasureEnum.kDefaultSystemOfMeasure,
                                                                                    DraftingStandardEnum.kDefault_DraftingStandard, null),
                                                   true) as PartDocument;

            // Get the XZ Plane
            WorkPlane oWorkPlane = oDoc.ComponentDefinition.WorkPlanes[2];

            PlanarSketch oSketch = oDoc.ComponentDefinition.Sketches.Add(oWorkPlane, false);

            TransientGeometry oTG = mApp.TransientGeometry;

            //Create some transient points used for defining the lines (see BRep Module)
            Point2d[] oPoints = new Point2d[5];

            oPoints[0] = oTG.CreatePoint2d(0, 0);
            oPoints[1] = oTG.CreatePoint2d(-10, 0);
            oPoints[2] = oTG.CreatePoint2d(-10, -10);
            oPoints[3] = oTG.CreatePoint2d(5, -10);
            oPoints[4] = oTG.CreatePoint2d(5, -5);

            //Add the sketchlines, coincident constraints will be created automatically
            //since the "Line.EndSketchPoint" are provided each time we create a new line
            SketchLine[] oLines = new SketchLine[5];

            oLines[0] = oSketch.SketchLines.AddByTwoPoints(oPoints[0], oPoints[1]);
            oLines[1] = oSketch.SketchLines.AddByTwoPoints(oLines[0].EndSketchPoint, oPoints[2]);
            oLines[2] = oSketch.SketchLines.AddByTwoPoints(oLines[1].EndSketchPoint, oPoints[3]);
            oLines[3] = oSketch.SketchLines.AddByTwoPoints(oLines[2].EndSketchPoint, oPoints[4]);

            oSketch.SketchArcs.AddByCenterStartEndPoint(oTG.CreatePoint2d(0, -5), oLines[3].EndSketchPoint, oLines[0].StartSketchPoint, true);

            //Create a profile for the extrusion, here no need to worry since there is only
            //a single profile that is possible
            Profile oProfile = oSketch.Profiles.AddForSolid(true, null, null);

            //this is the old way, i.e. create the extrude feature directly. it is still supported for backward compatibility
            //ExtrudeFeature oExtrude = oDoc.ComponentDefinition.Features.ExtrudeFeatures.AddByDistanceExtent(oProfile,
            //    5.0, PartFeatureExtentDirectionEnum.kPositiveExtentDirection, PartFeatureOperationEnum.kNewBodyOperation, 0.0);


            // Definition Way:
            PartComponentDefinition oPartDocDef = oDoc.ComponentDefinition;

            // get ExtrudeFeatures collection
            ExtrudeFeatures extrudes = oPartDocDef.Features.ExtrudeFeatures;

            // Create an extrude definition in the new surface body
              ExtrudeDefinition extrudeDef = extrudes.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kNewBodyOperation);

            // Modify the extent and taper angles.
            extrudeDef.SetDistanceExtent(8, PartFeatureExtentDirectionEnum.kPositiveExtentDirection);
            extrudeDef.SetDistanceExtentTwo(20);
            extrudeDef.TaperAngle    = "-2 deg";
            extrudeDef.TaperAngleTwo = "-10 deg";
             
            // Create the extrusion.
            ExtrudeFeature extrude = extrudes.Add(extrudeDef);

            //Fit the view programmatically
            Camera oCamera = mApp.ActiveView.Camera;

            oCamera.ViewOrientationType = ViewOrientationTypeEnum.kIsoTopRightViewOrientation;
            oCamera.Apply();

            mApp.ActiveView.Fit(true);
        }
Esempio n. 2
0
        private void circleSticker(ref PartDocument pDoc, double heigth, double width, double R, string txt = "", double thickness = 0.1)
        {
            PartComponentDefinition compDef = pDoc.ComponentDefinition;

            heigth /= 10; width /= 10; thickness /= 10;
            PlanarSketch ps = compDef.Sketches.Add(compDef.WorkPlanes[2]);
            //SketchEntity origin = ps.AddByProjectingEntity(compDef.WorkPoints[1]);
            double  alfa    = alfaFromL(heigth, R);
            Point2d startPt = m_TG.CreatePoint2d();

            SketchArc arc = ps.SketchArcs.AddByCenterStartSweepAngle(startPt, R, 3.1415926 / 2 - alfa / 2, alfa);
            //ObjectCollection objs = invApp.TransientObjects.CreateObjectCollection();
            //objs.Add(arc);
            //ps.RotateSketchObjects(objs, arc.CenterSketchPoint.Geometry, 1,RemoveConstraints:true);
            //arc = (SketchArc)en[1];
            //ps.GeometricConstraints.AddVerticalAlign((SketchPoint)origin, arc.CenterSketchPoint);
            ObjectCollection objCol = invApp.TransientObjects.CreateObjectCollection();

            objCol.Add(arc);
            SketchEntitiesEnumerator sArc = ps.OffsetSketchEntitiesUsingDistance(objCol, thickness, true);
            SketchArc arc2 = (SketchArc)sArc[1];

            ps.SketchLines.AddByTwoPoints(arc2.StartSketchPoint, arc.StartSketchPoint);
            ps.SketchLines.AddByTwoPoints(arc2.EndSketchPoint, arc.EndSketchPoint);
            Profile           pr     = ps.Profiles.AddForSolid();
            ExtrudeDefinition extDef = compDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(pr, PartFeatureOperationEnum.kNewBodyOperation);

            extDef.SetDistanceExtent(width / 2, PartFeatureExtentDirectionEnum.kNegativeExtentDirection);
            extDef.SetDistanceExtentTwo(width / 2);
            ExtrudeFeature extr = compDef.Features.ExtrudeFeatures.Add(extDef);

            extr.Appearance = InvDoc.u.createColor(pDoc, "white_", "белый_", 255, 255, 255);
            if (txt != "")
            {
                ps = compDef.Sketches.Add(compDef.WorkPlanes[3]);
                Inventor.TextBox tb = ps.TextBoxes.AddByRectangle(pts(-heigth, -width), pts(heigth, width), txt);
                tb.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter;
                tb.VerticalJustification   = VerticalTextAlignmentEnum.kAlignTextMiddle;
                tb.SingleLineText          = false;
                tb.Rotation = Math.PI;
                if (mirror)
                {
                    tb.Rotation = Math.PI;
                }
                if (this.radioButton1.Checked == false)
                {
                    //ObjectCollection col = invApp.TransientObjects.CreateObjectCollection();
                    //col.Add(tb);
                    //ps.RotateSketchObjects(col, pt, Math.PI / 4);
                    tb.Rotation -= Math.PI / 2;
                }
                Face face2 = null;
                foreach (Face f in compDef.SurfaceBodies[1].Faces)
                {
                    if (f.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                    {
                        double val = Math.Round(((Cylinder)f.Geometry).Radius, 3) * 100;
                        val = Math.Round(val, 0);

                        if /*((int)val == (int)((R + thickness)*100))*/ (InvDoc.u.eq(val, (R + thickness) * 100))
                        {
                            face2 = f; break;
                        }
                    }
                }
                EmbossFeature emb = compDef.Features.EmbossFeatures.AddEmbossFromFace(ps.Profiles.AddForSolid(false, ps.TextBoxes), 0.0005, PartFeatureExtentDirectionEnum.kNegativeExtentDirection, WrapFace: face2);
                emb.Appearance = InvDoc.u.createColor(pDoc, "black_", "черный_", 0, 0, 0);
            }
        }