示例#1
0
        static public void addCutDef(SheetMetalComponentDefinition smcd, Double a, Double b, string name = "Паз")
        {
            try
            {
                SketchBlockDefinition d = smcd.SketchBlockDefinitions[name];
                return;
            }
            catch (Exception)
            {
            }
            SketchBlockDefinition def = smcd.SketchBlockDefinitions.Add(name);
            TransientGeometry     tg  = Macros.StandardAddInServer.m_inventorApplication.TransientGeometry;
            Point2d     pt            = tg.CreatePoint2d();
            SketchPoint origin        = def.SketchPoints.Add(pt, false);

            def.GeometricConstraints.AddGround((SketchEntity)origin);
            SketchLine sl1, sl2, sl3;
            SketchEntitiesEnumerator en = def.SketchLines.AddAsTwoPointRectangle(tg.CreatePoint2d(-b / 2, -a / 2), tg.CreatePoint2d(b / 2, a / 2));

            sl1 = (SketchLine)en[2]; sl2 = (SketchLine)en[4];
            sl3 = midleLine(sl1, sl2);

            //foreach (DimensionConstraint item in def.DimensionConstraints)
            //{
            //    item.Delete();
            //}
        }
示例#2
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()));
        }
示例#3
0
        static public void rect(SketchLine sl, SketchPoint mp, double a, double b)
        {
            TransientGeometry             tg  = Macros.StandardAddInServer.m_inventorApplication.TransientGeometry;
            PlanarSketch                  ps  = (PlanarSketch)sl.Parent;
            Point2d                       pt  = tg.CreatePoint2d(mp.Geometry.X, mp.Geometry.Y);
            SketchEntitiesEnumerator      en  = ps.SketchLines.AddAsThreePointRectangle(pt, tg.CreatePoint2d(pt.X + a, pt.Y), tg.CreatePoint2d(pt.X, pt.Y + b));
            SketchLine                    sl1 = (SketchLine)en[1];
            SketchLine                    sl2 = (SketchLine)en[2];
            SketchLine                    sl3;
            TwoPointDistanceDimConstraint tpdc = ps.DimensionConstraints.AddTwoPointDistance(sl1.StartSketchPoint, sl1.EndSketchPoint, DimensionOrientationEnum.kAlignedDim, sl1.Geometry.MidPoint);

            tpdc = ps.DimensionConstraints.AddTwoPointDistance(sl2.StartSketchPoint, sl2.EndSketchPoint, DimensionOrientationEnum.kAlignedDim, sl2.Geometry.MidPoint);
            if ((int)(sl1.Length * 1000) <= (int)(a * 2000))
            {
                sl3 = midleLine((SketchLine)en[1], (SketchLine)en[3]);
            }
            else
            {
                sl3 = midleLine((SketchLine)en[2], (SketchLine)en[4]);
            }
            ps.GeometricConstraints.AddMidpoint(mp, sl3);
            ps.GeometricConstraints.AddParallel((SketchEntity)sl, (SketchEntity)sl3);
        }
示例#4
0
        /// <summary>
        /// Add iMate definitions using AddMateiMateDefinition and AddInsertiMateDefinition.
        /// </summary>
        /// <remarks></remarks>

        public void CreateiMateDefinition()
        {
            // Create a new part document, using the default part template.
            PartDocument oPartDoc = (PartDocument)_InvApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, _InvApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject));

            // Set a reference to the component definition.
            PartComponentDefinition oCompDef = default(PartComponentDefinition);

            oCompDef = oPartDoc.ComponentDefinition;

            // Create a new sketch on the X-Y work plane.
            PlanarSketch oSketch = default(PlanarSketch);

            oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes[3]);

            // Set a reference to the transient geometry object.
            TransientGeometry oTransGeom = default(TransientGeometry);

            oTransGeom = _InvApplication.TransientGeometry;

            // Draw a 4cm x 3cm rectangle with the corner at (0,0)
            SketchEntitiesEnumerator oRectangleLines = default(SketchEntitiesEnumerator);

            oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle(oTransGeom.CreatePoint2d(0, 0), oTransGeom.CreatePoint2d(4, 3));

            // Create a profile.
            Profile oProfile = default(Profile);

            oProfile = oSketch.Profiles.AddForSolid();

            // Create a base extrusion 1cm thick.
            ExtrudeDefinition oExtrudeDef = default(ExtrudeDefinition);

            oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kNewBodyOperation);
            oExtrudeDef.SetDistanceExtent(1, PartFeatureExtentDirectionEnum.kNegativeExtentDirection);
            ExtrudeFeature oExtrude1 = default(ExtrudeFeature);

            oExtrude1 = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef);

            // Get the top face of the extrusion to use for creating the new sketch.
            Face oFrontFace = default(Face);

            oFrontFace = oExtrude1.StartFaces[1];

            // Create a new sketch on this face, but use the method that allows you to
            // control the orientation and orgin of the new sketch.
            oSketch = oCompDef.Sketches.AddWithOrientation(oFrontFace, oCompDef.WorkAxes[1], true, true, oCompDef.WorkPoints[1]);

            // Create a sketch circle with the center at (2, 1.5).
            SketchCircle oCircle = default(SketchCircle);

            oCircle = oSketch.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(2, 1.5), 0.5);

            // Create a profile.
            oProfile = oSketch.Profiles.AddForSolid();

            // Create the second extrude (a hole).
            ExtrudeFeature oExtrude2 = default(ExtrudeFeature);

            oExtrude2 = oCompDef.Features.ExtrudeFeatures.AddByThroughAllExtent(oProfile, PartFeatureExtentDirectionEnum.kNegativeExtentDirection, PartFeatureOperationEnum.kCutOperation);

            // Create a mate iMateDefinition on a side face of the first extrude.
            MateiMateDefinition oMateiMateDefinition = default(MateiMateDefinition);

            oMateiMateDefinition = oCompDef.iMateDefinitions.AddMateiMateDefinition(oExtrude1.SideFaces[1], 0, InferredTypeEnum.kNoInference, null, "MateA");

            // Create a match list of names to use for the next iMateDefinition.
            string[] strMatchList = new string[3];
            strMatchList[0] = "InsertA";
            strMatchList[1] = "InsertB";
            strMatchList[2] = "InsertC";

            // Create an insert iMateDefinition on the cylindrical face of the second extrude.
            InsertiMateDefinition oInsertiMateDefinition = default(InsertiMateDefinition);

            oInsertiMateDefinition = oCompDef.iMateDefinitions.AddInsertiMateDefinition(oExtrude2.SideFaces[1], false, 0, null, "InsertA", strMatchList);
        }
示例#5
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]);
                }
            }
        }
示例#6
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);
            }
        }