Example #1
0
        /// <summary>
        /// 偏移
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="osketch"></param>
        /// <param name="source"></param>
        /// <param name="distance"></param>
        /// <param name="Direction"></param>
        /// <returns></returns>
        private T offsetLine <T>(PlanarSketch osketch, T source, double distance, bool Direction)
        {
            ObjectCollection obj = InventorTool.Inventor.TransientObjects.CreateObjectCollection();

            obj.Add(source);
            SketchEntitiesEnumerator Entities = osketch.OffsetSketchEntitiesUsingDistance(obj, distance, Direction, true, true);

            return(InventorTool.GetFirstFromIEnumerator <T>(Entities.GetEnumerator()));
        }
Example #2
0
        public PlanarSketch Separator(double direction)
        {
            PlanarSketch sketch       = partCompDef.Sketches.Add(partCompDef.WorkPlanes[3]);
            SketchPoints points_      = sketch.SketchPoints;
            SketchArcs   arcs_        = sketch.SketchArcs;
            SketchLines  lines_       = sketch.SketchLines;
            Point2d      center_point = transGeom.CreatePoint2d(Dw / 2 * direction, (D / 2 - d / 2) / 2 + d / 2);
            SketchPoint  points       = sketch.SketchPoints.Add(transGeom.CreatePoint2d(Dw * direction, (D / 2 - d / 2) / 2 + d / 2));
            SketchPoint  points2      = sketch.SketchPoints.Add(transGeom.CreatePoint2d(0, (D / 2 - d / 2) / 2 + d / 2));

            center_line = sketch.SketchLines.AddByTwoPoints(points, points2);
            SketchArc      arc      = sketch.SketchArcs.AddByCenterStartEndPoint(center_point, points, points2, true);
            RevolveFeature revolve1 = Create_Revolve(sketch, PartFeatureOperationEnum.kNewBodyOperation);

            Create_Circular_Array(revolve1);
            sketch  = partCompDef.Sketches.Add(partCompDef.WorkPlanes[3]);
            points_ = sketch.SketchPoints;
            lines_  = sketch.SketchLines;
            points_.Add(transGeom.CreatePoint2d((Dw / 2 + 0.25) * direction, (D / 2 - d / 2) / 2 + d / 2 + 0.15));
            points_.Add(transGeom.CreatePoint2d(0, (D / 2 - d / 2) / 2 + d / 2 + 0.3));
            points_.Add(transGeom.CreatePoint2d(0, (D / 2 - d / 2) / 2 + d / 2 - 0.15));
            points_.Add(transGeom.CreatePoint2d(Dw / 2 * direction, (D / 2 - d / 2) / 2 + d / 2 - 0.1));
            for (int i = 1; i < 4; i++)
            {
                lines_.AddByTwoPoints(points_[i], points_[i + 1]);
            }
            ObjectCollection obj_collection = InventorApplication.TransientObjects.CreateObjectCollection();

            obj_collection.Add(sketch.SketchEntities[5]);
            obj_collection.Add(sketch.SketchEntities[6]);
            obj_collection.Add(sketch.SketchEntities[7]);
            sketch.OffsetSketchEntitiesUsingDistance(obj_collection, 0.1 * direction, false);
            lines_.AddByTwoPoints(sketch.SketchLines[1].StartSketchPoint, sketch.SketchLines[4].StartSketchPoint);
            lines_.AddByTwoPoints(sketch.SketchLines[3].EndSketchPoint, sketch.SketchLines[6].EndSketchPoint);
            center_line = sketch.SketchLines.AddByTwoPoints(transGeom.CreatePoint2d(-1, 0), transGeom.CreatePoint2d(1, 0));
            RevolveFeature revolve2 = Create_Revolve(sketch, PartFeatureOperationEnum.kNewBodyOperation);

            obj_collection = InventorApplication.TransientObjects.CreateObjectCollection();
            obj_collection.Add(revolve1.SurfaceBodies[1]);
            CombineFeature combine = partCompDef.Features.CombineFeatures.Add(revolve2.SurfaceBodies[1], obj_collection, PartFeatureOperationEnum.kCutOperation);

            return(sketch);
        }
Example #3
0
        public static void addProjectCut(PartDocument oDoc, WorkPlane oWpReference, bool manual = false)
        {
            SheetMetalComponentDefinition oCompDef = (SheetMetalComponentDefinition)oDoc.ComponentDefinition;

            WorkPlane oWpWork = oCompDef.WorkPlanes.AddByPlaneAndOffset(oWpReference, 0);

            oWpWork.Name    = "wpWork";
            oWpWork.Visible = false;

            PlanarSketch oSketch     = oCompDef.Sketches.Add(oWpWork);
            ProjectedCut oProjectCut = oSketch.ProjectedCuts.Add();

            if (!manual)
            {
                int tmpSegmThk = countThicknessSegment(oProjectCut.SketchEntities);

                int    loop   = 0;
                double offset = 1;
                while (tmpSegmThk != 2)
                {
                    // Devo spostare il piano se ci sono cose nel mezzo
                    oWpWork.SetByPlaneAndOffset(oWpReference, offset);

                    tmpSegmThk = countThicknessSegment(oProjectCut.SketchEntities);
                    loop++;

                    offset += offset;

                    if (loop == 20)
                    {
                        throw new Exception("Numero massimo offset piano.");
                    }
                }

                oProjectCut.Delete();

                oProjectCut = oSketch.ProjectedCuts.Add();
            }

            List <ObjectCollection> dataLine = splittoLinea(oProjectCut.SketchEntities);

            ObjectCollection linea = lengthPerimetro();

            TransientGeometry oTransGeom     = iApp.TransientGeometry;
            UnitVector        oNormalVector  = oSketch.PlanarEntityGeometry.Normal;
            UnitVector2d      oLineDir       = linea[1].Geometry.Direction;
            UnitVector        oLineVector    = (UnitVector)oTransGeom.CreateUnitVector(oLineDir.X, oLineDir.Y, 0);
            UnitVector        oOffsetVector  = (UnitVector)oLineVector.CrossProduct(oNormalVector);
            UnitVector        oDesiredVector = (UnitVector)oTransGeom.CreateUnitVector(0, 1, 0);

            bool bNaturalOffsetDir;

            if (oOffsetVector.IsEqualTo(oDesiredVector))
            {
                bNaturalOffsetDir = true;
            }
            else
            {
                bNaturalOffsetDir = false;
            }

            SketchEntitiesEnumerator oSSketchEntitiesEnum = oSketch.OffsetSketchEntitiesUsingDistance(linea, 0.5, bNaturalOffsetDir, false);

            oProjectCut.Delete();

            styleSketch(oSketch.SketchEntities);

            int countThicknessSegment(SketchEntitiesEnumerator oSketchEntities)
            {
                int result = 0;

                foreach (SketchEntity e in oSketchEntities)
                {
                    if (e.Type == ObjectTypeEnum.kSketchLineObject)
                    {
                        SketchLine oSketchLine = (SketchLine)e;

                        double length = Math.Round(oSketchLine.Length * 100) / 100;

                        if (length == Math.Round(oCompDef.Thickness.Value * 100) / 100)
                        {
                            result++;
                        }
                    }
                }

                return(result);
            }

            List <ObjectCollection> splittoLinea(SketchEntitiesEnumerator oSketchEntities)
            {
                List <ObjectCollection> tmp = new List <ObjectCollection>();

                tmp.Add(iApp.TransientObjects.CreateObjectCollection());
                tmp.Add(iApp.TransientObjects.CreateObjectCollection());
                tmp.Add(iApp.TransientObjects.CreateObjectCollection());

                int indice = 0;

                foreach (SketchEntity oSketchEntity in oSketchEntities)
                {
                    if (oSketchEntity.Type != ObjectTypeEnum.kSketchPointObject)
                    {
                        if (oSketchEntity.Type == ObjectTypeEnum.kSketchLineObject)
                        {
                            SketchLine oSketchLine = (SketchLine)oSketchEntity;

                            if (Math.Round(oSketchLine.Length * 100) / 100 == Math.Round(oCompDef.Thickness.Value * 100) / 100)
                            {
                                if (indice == 0)
                                {
                                    indice = 1;
                                }
                                else
                                {
                                    indice = 2;
                                }
                            }
                            else
                            {
                                tmp[indice].Add(oSketchEntity);
                            }
                        }

                        if (oSketchEntity.Type == ObjectTypeEnum.kSketchArcObject)
                        {
                            tmp[indice].Add(oSketchEntity);
                        }
                    }
                }

                foreach (SketchEntity oSketchEntity in tmp[0])
                {
                    tmp[2].Add(oSketchEntity);
                }

                List <ObjectCollection> result = new List <ObjectCollection>();

                result.Add(tmp[1]);
                result.Add(tmp[2]);

                return(result);
            }

            void styleSketch(SketchEntitiesEnumerator oSketchEntities)
            {
                foreach (SketchEntity oSe in oSketchEntities)
                {
                    if (oSe.Type == ObjectTypeEnum.kSketchLineObject)
                    {
                        SketchLine se = (SketchLine)oSe;

                        se.OverrideColor = iApp.TransientObjects.CreateColor(0, 0, 255);
                        se.LineType      = LineTypeEnum.kDashDottedLineType;
                    }
                    else if (oSe.Type == ObjectTypeEnum.kSketchArcObject)
                    {
                        SketchArc sa = (SketchArc)oSe;

                        sa.OverrideColor = iApp.TransientObjects.CreateColor(0, 0, 255);
                        sa.LineType      = LineTypeEnum.kDashDottedLineType;
                    }
                }
            }

            ObjectCollection lengthPerimetro()
            {
                double l0 = 0;
                double l1 = 0;

                foreach (SketchEntity oSE in dataLine[0])
                {
                    if (oSE.Type == ObjectTypeEnum.kSketchLineObject)
                    {
                        SketchLine se = (SketchLine)oSE;

                        l0 = l0 + se.Length;
                    }
                    else if (oSE.Type == ObjectTypeEnum.kSketchArcObject)
                    {
                        SketchArc se = (SketchArc)oSE;

                        l0 = l0 + se.Length;
                    }
                }
                foreach (SketchEntity oSE in dataLine[1])
                {
                    if (oSE.Type == ObjectTypeEnum.kSketchLineObject)
                    {
                        SketchLine se = (SketchLine)oSE;

                        l1 = l1 + se.Length;
                    }
                    else if (oSE.Type == ObjectTypeEnum.kSketchArcObject)
                    {
                        SketchArc se = (SketchArc)oSE;

                        l1 = l1 + se.Length;
                    }
                }

                if (l0 > l1)
                {
                    return(dataLine[0]);
                }
                else
                {
                    return(dataLine[1]);
                }
            }
        }
Example #4
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);
            }
        }