示例#1
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);
        }