// ! Aggiunge il piano in mezzo alla lamiera
        public static WorkPlane addPlaneInTheMiddleOfBox(PartDocument oDoc)
        {
            SheetMetalComponentDefinition oComp = (SheetMetalComponentDefinition)oDoc.ComponentDefinition;

            try
            {
                return(oComp.WorkPlanes["Manual"]);
            }
            catch { }

            Box oRb = oComp.SurfaceBodies[1].RangeBox;

            TransientBRep oTransientBRep = iApp.TransientBRep;

            SurfaceBody oBody = oTransientBRep.CreateSolidBlock(oRb);

            NonParametricBaseFeature oBaseFeature = oComp.Features.NonParametricBaseFeatures.Add(oBody);

            FaceCollection oFaceColl = iApp.TransientObjects.CreateFaceCollection();

            foreach (Face f in oBaseFeature.SurfaceBodies[1].Faces)
            {
                WorkPlane tmpWp = oComp.WorkPlanes.AddByPlaneAndOffset(f, 0);

                //if (tmpWp.Plane.IsParallelTo[oComp.WorkPlanes[1].Plane])
                if (tmpWp.Plane.IsParallelTo[oComp.WorkPlanes[1].Plane])
                {
                    oFaceColl.Add(f);
                }

                tmpWp.Delete();
            }

            WorkPlane wpWork = null;

            if (oFaceColl.Count >= 2)
            {
                WorkPlane wp1 = oComp.WorkPlanes.AddByPlaneAndOffset(oFaceColl[1], 0);
                WorkPlane wp2 = oComp.WorkPlanes.AddByPlaneAndOffset(oFaceColl[2], 0);

                wpWork          = oComp.WorkPlanes.AddByTwoPlanes(wp1, wp2);
                wpWork.Name     = "wpWorkReference";
                wpWork.Grounded = true;
                wpWork.Visible  = false;

                oBaseFeature.Delete(false, true, true);

                wp1.Delete();
                wp2.Delete();
            }

            return(wpWork);
        }
        // ! Elimina tutti i fillet
        public static List <string> deleteFillet_(PartDocument oDoc)
        {
            SheetMetalComponentDefinition oCompDef = (SheetMetalComponentDefinition)oDoc.ComponentDefinition;

            List <string> faceCollToKeep = new List <string>();

            if (oCompDef.Bends.Count > 0)
            {
                Bend oBend = oCompDef.Bends[1];

                FaceCollection oFaceColl = oBend.FrontFaces[1].TangentiallyConnectedFaces;
                oFaceColl.Add(oBend.FrontFaces[1]);

                foreach (Face oFace in oFaceColl)
                {
                    faceCollToKeep.Add(oFace.InternalName);
                }

                NonParametricBaseFeature oBaseFeature = oCompDef.Features.NonParametricBaseFeatures[1];

                oBaseFeature.Edit();

                SurfaceBody basebody = oBaseFeature.BaseSolidBody;

                ObjectCollection oColl = iApp.TransientObjects.CreateObjectCollection();

                foreach (Face f in basebody.Faces)
                {
                    if (faceCollToKeep.Contains(f.InternalName))
                    {
                        if (f.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                        {
                            oColl.Add(f);
                        }
                    }
                }
                try
                {
                    oBaseFeature.DeleteFaces(oColl);
                    oBaseFeature.ExitEdit();
                }
                catch
                {
                    oBaseFeature.ExitEdit();
                    return(null);
                }
            }

            return(faceCollToKeep);
        }
Exemplo n.º 3
0
        private void CalculateHoleB_Click(object sender, EventArgs e)
        {
            if (inventor == null)
            {
                MessageBox.Show("No inventor instance detected", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AssemblyDocument doc = inventor.ActiveDocument as AssemblyDocument;

            if (doc == null || doc.ComponentDefinition == null)
            {
                return;
            }

            AssemblyComponentDefinition partComponentDefinition = doc.ComponentDefinition;
            var wall   = partComponentDefinition.Occurrences.get_ItemByName("wall:1");
            var weight = partComponentDefinition.Occurrences.get_ItemByName("weight:1");
            PartComponentDefinition weightDefinition = weight.Definition as PartComponentDefinition;
            PartComponentDefinition wallDefinition   = wall.Definition as PartComponentDefinition;
            SurfaceBody             surfaceBody2     = inventor.TransientBRep.Copy(weight.SurfaceBodies[1]);
            Vector rotationVector = inventor.TransientGeometry.CreateVector(0, 1, 0);

            Inventor.Point point = inventor.TransientGeometry.CreatePoint(0, 0, 0);



            var collection = inventor.TransientObjects.CreateObjectCollection();

            for (int i = 1; i <= 120; i++)
            {
                Matrix wallMatrix = wall.Transformation, weigthMatrix = weight.Transformation;
                weigthMatrix.Invert();
                wallMatrix.PreMultiplyBy(weigthMatrix);
                SurfaceBody surfaceBody = inventor.TransientBRep.Copy(weight.SurfaceBodies[1]);
                wallMatrix.SetToRotation(i * Math.PI / 180, rotationVector, point);
                inventor.TransientBRep.Transform(surfaceBody, wallMatrix);
                inventor.TransientBRep.DoBoolean(surfaceBody2, surfaceBody, BooleanTypeEnum.kBooleanTypeUnion);
            }

            NonParametricBaseFeatureDefinition featureDefinition = weightDefinition.Features.NonParametricBaseFeatures.CreateDefinition();

            featureDefinition.OutputType = BaseFeatureOutputTypeEnum.kSolidOutputType;

            collection.Add(surfaceBody2);
            featureDefinition.BRepEntities = collection;
            NonParametricBaseFeature baseFeature = weightDefinition.Features.NonParametricBaseFeatures.AddByDefinition(featureDefinition);

            //CombineFeature combineFeature = doc.Features.CombineFeatures.Add(wall.SurfaceBodies[1], collection, PartFeatureOperationEnum.kCutOperation);
        }
        public void CreateSurface(NameValueMap nv)
        {
            var T0 = DateTime.Now;
            var T1 = DateTime.Now;
            var T2 = DateTime.Now;

            var N  = 100;
            var Nu = N;    // number of u control points
            var Lx = 10.0; // x-extent in cm
            var Nv = N;    // number of v control points
            var Ly = 10.0; // y-extent in cm

            var tg       = mApp.TransientGeometry;
            var partDoc  = (PartDocument)mApp.ActiveDocument;
            var cd       = partDoc.ComponentDefinition;
            var features = cd.Features;

            int dimU     = 3 + Nu;
            int dimV     = 3 + Nv;
            int dimC     = 3 * Nu * Nv;
            var uKnots   = new double[dimU];
            var vKnots   = new double[dimV];
            var controls = new double[dimC];
            // NOTE: The Inventor documentation does not seem to mention this,
            // but providing an empty array of weights apparently causes
            // Inventor to assume all points are equally weighted.
            //var weights = new double[Nu * Nv];
            var weights = new double[0];

            for (int i = 0; i <= 2; ++i)
            {
                uKnots[i] = vKnots[i] = 0.0;
            }

            for (int u = 3; u < Nu; ++u)
            {
                uKnots[u] = (u - 2) * (1.0f / (Nu - 2));
            }
            for (int v = 3; v < Nv; ++v)
            {
                vKnots[v] = (v - 2) * (1.0f / (Nv - 2));
            }

            for (int u = Nu; u < Nu + 3; ++u)
            {
                uKnots[u] = 1.0;
            }
            for (int v = Nv; v < Nv + 3; ++v)
            {
                vKnots[v] = 1.0;
            }

            for (int u = 0; u < Nu; ++u)
            {
                var x = u * Lx / (Nu - 1);
                for (int v = 0; v < Nv; ++v)
                {
                    var y = v * Ly / (Nv - 1);

                    var idx = 3 * (v * Nu + u);
                    controls[idx]     = x;
                    controls[idx + 1] = y;
                    var r = Math.Sqrt(x * x + y * y);
                    controls[idx + 2] = 0.3 * Math.Cos(2 * r);
                }
            }

            var polesu0 = new double[3 * Nv]; // u = 0 edge
            var polesuN = new double[3 * Nv]; // u = Nu-1 edge
            var polesv0 = new double[3 * Nu]; // v = 0 edge
            var polesvN = new double[3 * Nu]; // v = Nv-1 edge

            for (int u = 0; u < Nu; ++u)
            {
                // The v0 edge runs from (0, 0) to (Nu - 1, 0)
                int v0base = 3 * u;
                // The vN edge runs from (Nu - 1, Nv - 1) to (0, Nv - 1)
                int vNbase = 3 * ((Nu - 1 - u) + Nu * (Nv - 1));
                for (int k = 0; k < 3; ++k)
                {
                    polesv0[3 * u + k] = controls[v0base + k];
                    polesvN[3 * u + k] = controls[vNbase + k];
                }
            }
            for (int v = 0; v < Nv; ++v)
            {
                // The u0 edge runs from (0, Nv - 1) to (0, 0)
                int u0base = 3 * Nu * (Nv - 1 - v);
                // The uN edge runs from (Nu - 1, 0) to (Nu - 1, Nv - 1)
                int uNbase = 3 * (Nu - 1 + Nu * v);
                for (int k = 0; k < 3; ++k)
                {
                    polesu0[3 * v + k] = controls[u0base + k];
                    polesuN[3 * v + k] = controls[uNbase + k];
                }
            }

            var order = new int[2] {
                3, 3
            };
            var isPeriodic = new bool[2] {
                false, false
            };

            var transaction = mApp.TransactionManager.StartTransaction(mApp.ActiveDocument, "Create B-Spline Surface");

            try
            {
                var curvev0 = tg.CreateBSplineCurve(3, ref polesv0, ref uKnots, ref weights, isPeriodic[0]);
                var curveuN = tg.CreateBSplineCurve(3, ref polesuN, ref vKnots, ref weights, isPeriodic[1]);

                // TODO: Should the knots be reversed for curvevN and curve u0?
                // We can get away with not reversing them now because they are always symmetric.
                var curvevN = tg.CreateBSplineCurve(3, ref polesvN, ref uKnots, ref weights, isPeriodic[0]);
                var curveu0 = tg.CreateBSplineCurve(3, ref polesu0, ref vKnots, ref weights, isPeriodic[1]);

                var topSurface = tg.CreateBSplineSurface(ref order, ref controls, ref uKnots, ref vKnots, ref weights, ref isPeriodic);

                if (topSurface == null)
                {
                    throw new Exception("TransientGeometry.CreateBSplineSurface returned null");
                }

                var bodyDef = mApp.TransientBRep.CreateSurfaceBodyDefinition();

                var corners = new VertexDefinition[4];
                corners[0] = bodyDef.VertexDefinitions.Add(tg.CreatePoint(controls[0], controls[1], controls[2]));
                int c1 = 3 * (Nu - 1);
                corners[1] = bodyDef.VertexDefinitions.Add(tg.CreatePoint(controls[c1], controls[c1 + 1], controls[c1 + 2]));
                int c2 = c1 + 3 * Nu * (Nv - 1);
                corners[2] = bodyDef.VertexDefinitions.Add(tg.CreatePoint(controls[c2], controls[c2 + 1], controls[c2 + 2]));
                int c3 = 3 * Nu * (Nv - 1);
                corners[3] = bodyDef.VertexDefinitions.Add(tg.CreatePoint(controls[c3], controls[c3 + 1], controls[c3 + 2]));

                var edges = new EdgeDefinition[4];
                edges[0] = bodyDef.EdgeDefinitions.Add(corners[0], corners[1], curvev0);
                edges[1] = bodyDef.EdgeDefinitions.Add(corners[1], corners[2], curveuN);
                edges[2] = bodyDef.EdgeDefinitions.Add(corners[2], corners[3], curvevN);
                edges[3] = bodyDef.EdgeDefinitions.Add(corners[3], corners[0], curveu0);

                var lumpDef  = bodyDef.LumpDefinitions.Add();
                var shellDef = lumpDef.FaceShellDefinitions.Add();

                var topFace = shellDef.FaceDefinitions.Add(topSurface, false);
                // TODO: What is this ID for?
                topFace.AssociativeID = 501;

                var topFaceLoop = topFace.EdgeLoopDefinitions.Add();
                topFaceLoop.EdgeUseDefinitions.Add(edges[3], false);
                topFaceLoop.EdgeUseDefinitions.Add(edges[2], false);
                topFaceLoop.EdgeUseDefinitions.Add(edges[1], true);
                topFaceLoop.EdgeUseDefinitions.Add(edges[0], true);

                T1 = DateTime.Now;
                NameValueMap errors;
                var          newBody = bodyDef.CreateTransientSurfaceBody(out errors);
                T2 = DateTime.Now;

                if (newBody == null)
                {
                    throw new Exception("SurfaceBodyDefinition.CreateTransientSurfaceBody returned null");
                }

                NonParametricBaseFeatureDefinition baseDef = features.NonParametricBaseFeatures.CreateDefinition();
                ObjectCollection objColl = mApp.TransientObjects.CreateObjectCollection();
                objColl.Add(newBody);
                baseDef.BRepEntities = objColl;
                baseDef.OutputType   = BaseFeatureOutputTypeEnum.kSurfaceOutputType;
                NonParametricBaseFeature baseFeature = features.NonParametricBaseFeatures.AddByDefinition(baseDef);

                transaction.End();
            }
            catch (Exception exc)
            {
                transaction.Abort();
            }

            var Tfinal = DateTime.Now;

            var d01   = T1 - T0;
            var msg01 = string.Format("Time before CreateTransientSurfaceBody: {0} sec", d01.TotalSeconds);
            var d12   = T2 - T1;
            var msg12 = string.Format("CreateTransientSurfaceBody time: {0} sec", d12.TotalSeconds);
            var d2f   = Tfinal - T2;
            var msg2f = string.Format("Time after CreateTransientSurfaceBody: {0} sec", d2f.TotalSeconds);

            MessageBox.Show(msg01 + "\n" + msg12 + "\n" + msg2f);
        }
Exemplo n.º 5
0
        public static void deleteLavorazione(PartDocument oDoc)
        {
            SheetMetalComponentDefinition oCompDef = (SheetMetalComponentDefinition)oDoc.ComponentDefinition;
            int numeroFacceTan = oCompDef.Bends.Count * 2;

            NonParametricBaseFeature oBaseFeature = oCompDef.Features.NonParametricBaseFeatures[1];

            oBaseFeature.Edit();

            SurfaceBody basebody = oBaseFeature.BaseSolidBody;

            foreach (Face f in basebody.Faces)
            {
                if (f.TangentiallyConnectedFaces.Count == numeroFacceTan)
                {
                    string nameFace = f.InternalName;

                    ObjectCollection oColl = iApp.TransientObjects.CreateObjectCollection();

                    List <string> listName = new List <string>();

                    foreach (EdgeLoop oEdgeLoops in f.EdgeLoops)
                    {
                        Edges oEdges = oEdgeLoops.Edges;

                        string lav = IdentificazioneEntita.whois(oEdges);

                        if (!string.IsNullOrEmpty(lav))
                        {
                            Edge oEdge = oEdges[1];

                            Faces oFaceColl = oEdge.Faces;

                            foreach (Face oFaceLav in oFaceColl)
                            {
                                if (oFaceLav.InternalName != nameFace)
                                {
                                    if (!listName.Contains(oFaceLav.InternalName))
                                    {
                                        oColl.Add(oFaceLav);
                                        listName.Add(oFaceLav.InternalName);
                                    }
                                    foreach (Face oFaceLavTang in oFaceLav.TangentiallyConnectedFaces)
                                    {
                                        if (!listName.Contains(oFaceLavTang.InternalName))
                                        {
                                            oColl.Add(oFaceLavTang);
                                            listName.Add(oFaceLavTang.InternalName);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (oColl.Count > 0)
                    {
                        try
                        {
                            oBaseFeature.DeleteFaces(oColl);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            oBaseFeature.ExitEdit();
        }
        // ! Elimina le lavorazioni
        public static void deleteLavorazione(PartDocument oDoc)
        {
            SheetMetalComponentDefinition oCompDef = (SheetMetalComponentDefinition)oDoc.ComponentDefinition;

            NonParametricBaseFeature oBaseFeature = oCompDef.Features.NonParametricBaseFeatures[1];

            oBaseFeature.Edit();

            SurfaceBody basebody = oBaseFeature.BaseSolidBody;

            foreach (Face f in basebody.Faces)
            {
                try
                {
                    string nameFaceRif = f.InternalName;

                    ObjectCollection oFaceColl = iApp.TransientObjects.CreateObjectCollection();

                    if (f.EdgeLoops.Count > 1)
                    {
                        foreach (EdgeLoop oEdgeLoop in f.EdgeLoops)
                        {
                            string lavorazione = IdentificazioneEntita.whois(oEdgeLoop.Edges);

                            if (!string.IsNullOrEmpty(lavorazione))
                            {
                                foreach (Face oFaceLav in oEdgeLoop.Edges[1].Faces)
                                {
                                    string nameFaceLav = oFaceLav.InternalName;

                                    if (nameFaceLav != nameFaceRif)
                                    {
                                        oFaceColl.Add(oFaceLav);

                                        foreach (Face oFaceTan in oFaceLav.TangentiallyConnectedFaces)
                                        {
                                            oFaceColl.Add(oFaceTan);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (oFaceColl.Count > 0)
                    {
                        try
                        {
                            oBaseFeature.DeleteFaces(oFaceColl);
                            oBaseFeature.ExitEdit();
                            deleteLavorazione(oDoc);
                        }
                        catch
                        {
                        }
                    }
                }
                catch
                {
                }
            }
            oBaseFeature.ExitEdit();
        }
        // ATTENZIONE NEL CASO CI FOSSERO LAVORAZIONI
        public static List <string> deleteFillet(PartDocument oDoc)
        {
            SheetMetalComponentDefinition oCompDef = (SheetMetalComponentDefinition)oDoc.ComponentDefinition;

            List <string> faceCollToKeep = new List <string>();

            foreach (Face f in oCompDef.SurfaceBodies[1].Faces)
            {
                if (f.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                {
                    faceCollToKeep.Add(f.InternalName);
                }
            }

            //foreach(Face f in oCompDef.SurfaceBodies[1].Faces)
            //{
            //    if (f.SurfaceType == SurfaceTypeEnum.kCylinderSurface) {
            //        foreach (Face ff in f.TangentiallyConnectedFaces)
            //        {
            //            if (ff.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
            //            {
            //                coloroEntita(oDoc, 255, 0, 0, ff);
            //                faceCollToKeep.Add(ff.InternalName);
            //            }
            //        }
            //        coloroEntita(oDoc, 255, 0, 0, f);
            //        faceCollToKeep.Add(f.InternalName);
            //        break;
            //    }
            //}

            NonParametricBaseFeature oBaseFeature = oCompDef.Features.NonParametricBaseFeatures[1];

            oBaseFeature.Edit();

            SurfaceBody basebody = oBaseFeature.BaseSolidBody;

            ObjectCollection oColl = iApp.TransientObjects.CreateObjectCollection();

            foreach (Face f in basebody.Faces)
            {
                if (faceCollToKeep.Contains(f.InternalName))
                {
                    if (f.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                    {
                        oColl.Add(f);
                    }
                }
            }

            try
            {
                oBaseFeature.DeleteFaces(oColl);
                oBaseFeature.ExitEdit();
            }
            catch
            {
                oBaseFeature.ExitEdit();
                return(null);
            }

            return(faceCollToKeep);
        }