コード例 #1
0
    public void Assembly()
    {
        Inventor.Application InventorApplication = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
        AssemblyDocument     assemblyDoc         = InventorApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, "", true) as AssemblyDocument;

        assDefinition = assemblyDoc.ComponentDefinition;
        Inventor.Matrix matrix = InventorApplication.TransientGeometry.CreateMatrix();
        matrix.SetTranslation(InventorApplication.TransientGeometry.CreateVector(0, 0, 0));
        string[] pathName = new string[10];
        pathName[0] = @"C:\BearingDetails\Внешнее кольцо подшипника.ipt";
        pathName[1] = @"C:\BearingDetails\Внутреннее кольцо подшипника.ipt";
        pathName[2] = @"C:\BearingDetails\Первый ряд шариков.ipt";
        pathName[3] = @"C:\BearingDetails\Второй ряд шариков.ipt";
        pathName[4] = @"C:\BearingDetails\Сепаратор1.ipt";
        pathName[5] = @"C:\BearingDetails\Сепаратор2.ipt";
        for (int i = 0; i < 6; i++)
        {
            assDefinition.Occurrences.Add(pathName[i], matrix);
        }
        Rotate();
        for (int i = 1; i < 4; i++)
        {
            Tangent(i, 2, 3, 7);
        }
        Tangent(1, 5, 3, 1);
        Tangent(3, 5, 3, 5);
        Tangent(5, 5, 3, 9);
        //Tangent(1, 6, 4, 1);
        //Tangent(3, 6, 4, 5);
        //Tangent(5, 6, 4, 9);
        //for (int i = 2; i < 5; i++)
        //    Tangent(i, 2, 4, 1);
        assemblyDoc.SaveAs(@"C:\BearingDetails\Сборка_подшипника.iam", false);
    }
コード例 #2
0
        public void ChangeView(double gyroY, double gyroX, double gyroZ, bool apply)
        {
            Inventor.Vector screenX = null;
            Inventor.Vector screenY = null;
            Inventor.Vector screenZ = null;
            screenZ = oCamera.Target.VectorTo(oCamera.Eye);
            screenZ.Normalize();

            screenY = oCamera.UpVector.AsVector();
            screenX = screenY.CrossProduct(screenZ);

            Inventor.Matrix rotX = oTG.CreateMatrix();
            rotX.SetToRotation(gyroX, screenX, oCamera.Target);

            Inventor.Matrix rotY = oTG.CreateMatrix();
            rotY.SetToRotation(gyroY, screenY, oCamera.Target);

            Inventor.Matrix rotZ = oTG.CreateMatrix();
            rotZ.SetToRotation(gyroZ, screenZ, oCamera.Target);

            Inventor.Matrix rot = oTG.CreateMatrix();
            rot = rotX;
            rot.PostMultiplyBy(rotY);
            rot.PostMultiplyBy(rotZ);
            Inventor.Point newEye = oCamera.Eye;
            newEye.TransformBy(rot);
            Inventor.UnitVector newUp = oCamera.UpVector;
            newUp.TransformBy(rot);
            oCamera.Eye      = newEye;
            oCamera.UpVector = newUp;
            if (apply)
            {
                oCamera.Apply();
            }
            else
            {
                oCamera.ApplyWithoutTransition();
            }
        }
コード例 #3
0
        //Generate SDF file.
        private void GenerateSDF()
        {
            UnitsOfMeasure              oUOM        = _invApp.ActiveDocument.UnitsOfMeasure;
            AssemblyDocument            oAsmDoc     = (AssemblyDocument)_invApp.ActiveDocument;
            AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition;
            List <ComponentOccurrence>  compOccurs  = new List <ComponentOccurrence>();
            List <AssemblyConstraint>   constraints = new List <AssemblyConstraint>();

            //Recursively make a list of all component parts.
            List <AssemblyComponentDefinition> loopList = new List <AssemblyComponentDefinition>();

            //TODO: Store OccuranceName vs Pose for assemblies.
            loopList.Add((AssemblyComponentDefinition)oAsmCompDef);
            //Recursively make a list of all component parts.
            while (loopList.Count > 0)
            {
                AssemblyComponentDefinition loopAsmCompDef = loopList[0];
                loopList.RemoveAt(0);
                foreach (ComponentOccurrence occ in loopAsmCompDef.Occurrences)
                {
                    if (occ.DefinitionDocumentType == DocumentTypeEnum.kPartDocumentObject || occ.Definition is WeldmentComponentDefinition)
                    {
                        //Store parts and weldments for link creation
                        compOccurs.Add(occ);
                        //occ.

                        WriteLine("Processing part '" + occ.Name + "' with " + occ.Constraints.Count + " constraints.");
                    }
                    else if (occ.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                    {
                        //Get parts from assemblies.
                        loopList.Add((AssemblyComponentDefinition)occ.Definition);

                        WriteLine("Processing assembly '" + occ.Name + "' with " + occ.Constraints.Count + " constraints.");
                    }
                }

                //Get Assembly Constraints
                foreach (AssemblyConstraint cons in loopAsmCompDef.Constraints)
                {
                    constraints.Add(cons);
                }
            }

            WriteLine(compOccurs.Count.ToString() + " parts to convert.");
            WriteLine(constraints.Count.ToString() + " constraints to convert.");

            //Get all the available links
            foreach (ComponentOccurrence oCompOccur in compOccurs)
            {
                //Define a link
                Link link = new Link(RemoveColon(oCompOccur.Name));

                //Get global position and COM
                double Mass = oCompOccur.MassProperties.Mass;

                //Calculate rotation.
                Inventor.Matrix R1 = oCompOccur.Transformation;

                // Set position and rotation
                //TODO: Check globalised scale
                //TODO: Set pose relative to parent occurance.
                link.Pose = new Pose(R1, precision, scale);

                // Get Moments of Inertia.
                double[] iXYZ = new double[6];
                //Pos of C.O.M., Rot of Link.
                // Ixx, Iyy, Izz, Ixy, Iyz, Ixz
                // Ixx, Ixy, Ixz, Iyy, Iyz, Izz
                oCompOccur.MassProperties.XYZMomentsOfInertia(out iXYZ[0], out iXYZ[3], out iXYZ[5], out iXYZ[1], out iXYZ[4], out iXYZ[2]);
                Inventor.Point COMp = oCompOccur.MassProperties.CenterOfMass;
                double[]       COM  = new double[3] {
                    COMp.X, COMp.Y, COMp.Z
                };

                // Set Moments of Inertia
                //Takes precision from link.Pose.
                link.Inertial = new Inertial(Mass, iXYZ, COM, link.Pose, scale);

                // Set the URI for the link's model.
                String URI = "model://<MODELNAME>/meshes/" + link.Name + ".stl";
                link.Visual    = new Visual(new Mesh(URI, scale));
                link.Collision = new Collision(new Mesh(URI, scale));

                // Print out link information.
                WriteLine("New Link:          --------------------------------------");
                WriteLine("                  Name: " + link.Name);
                WriteLine("                  Pose: " + link.Pose.ToString());
                //link.Pose = new Pose(link.Pose);
                WriteLine("                  Pose: " + link.Pose.ToString());
                WriteLine("           InertiaPose: " + link.Inertial.Pose.ToString());
                Matrix <double> M2 = DenseMatrix.OfArray(link.Inertial.InertiaMatrix);
                WriteLine(M2.ToString());
                WriteLine("                  Mass: " + Mass);
                WriteLine("       Rotation Matrix: " + System.Environment.NewLine + link.Pose.PrintMatrix());

                // Add link to robot
                robo.Links.Add(link);
            }

            WriteLine(constraints.Count.ToString() + " constraints to convert.");

            //Get all the available joints
            foreach (AssemblyConstraint constraint in constraints)
            {
                //Some checks
                if (constraint.Suppressed)
                {
                    //Skip suppressed constraints.
                    WriteLine("Skipped a suppressed constraint.");
                    continue;
                }



                //Set some starting variables.
                String         name = constraint.Name;
                Inventor.Point center;
                JointType      type = JointType.Revolute;

                //Calculate which should be child.
                ComponentOccurrence childP  = constraint.OccurrenceOne;
                ComponentOccurrence parentP = constraint.OccurrenceTwo;
                if (childP == null || parentP == null)
                {
                    //Skip incomplete constraints
                    WriteLine("Skipped a constraint without an Occurance.");
                    continue;
                }

                //Get degrees of freedom information.
                int transDOFCount, rotDOFCount;
                Inventor.ObjectsEnumerator transDOF, rotDOF;
                Inventor.Point             DOFCenter;
                childP.GetDegreesOfFreedom(out transDOFCount, out transDOF, out rotDOFCount, out rotDOF, out DOFCenter);
                if (rotDOFCount + transDOFCount == 0)
                {
                    parentP.GetDegreesOfFreedom(out transDOFCount, out transDOF, out rotDOFCount, out rotDOF, out DOFCenter);
                    if (rotDOFCount + transDOFCount == 0)
                    {
                        WriteLine("Skipped a constraint with 0 DOF.");
                        continue;
                    }
                    //Parent has DOF but child doesn't. Switch Parent and Child.
                    ComponentOccurrence temp = childP;
                    childP  = parentP;
                    parentP = temp;
                }

                //Get Model Links
                Link child  = GetLinkByName(RemoveColon(childP.Name));
                Link parent = GetLinkByName(RemoveColon(parentP.Name));
                if (child == null || parent == null)
                {
                    //Skip incomplete constraints
                    WriteLine("Skipped a constraint without a Link.");
                    continue;
                }

                //Get constraint information
                Pose           Pose;
                Axis           Axis;
                Inventor.Point Geomcenter;
                if (constraint is InsertConstraint && constraint.GeometryOne is Circle)
                {
                    //Handle Insert Constraints (Revolute)
                    Circle circ = (Circle)constraint.GeometryOne;
                    Geomcenter = circ.Center;
                    Axis       = new Axis(circ.Normal.X, circ.Normal.Y, circ.Normal.Z);
                    type       = JointType.Revolute;
                }
                else if (constraint is MateConstraint && constraint.GeometryOne is Line)
                {
                    //Handle Mate Constraints (Prismatic)
                    Inventor.Line line = (Line)constraint.GeometryOne;
                    Geomcenter = line.RootPoint;
                    //TODO: Find out if axis is global, or local to assembly.
                    Axis = new Axis(line.Direction.X, line.Direction.Y, line.Direction.Z);
                    type = JointType.Prismatic;
                }
                else
                {
                    //Skip other constraints
                    WriteLine("Skipped an uknown constraint");
                    continue;
                }
                Pose = new Pose(Geomcenter.X, Geomcenter.Y, Geomcenter.Z, precision, scale);

                WriteLine("New joint:          --------------------------------------");
                WriteLine("                Name: " + name);
                WriteLine("              Parent: " + parent.Name);
                WriteLine("               Child: " + child.Name);
                WriteLine("                Pose: " + Pose.ToString());
                Pose.GetPosRelativeTo(child.Pose);
                WriteLine("           Child Loc: " + child.Pose.ToString());
                WriteLine("       Relative Pose: " + Pose.ToString());
                WriteLine("                Axis: " + Axis.ToString());
                WriteLine("              rotDOF: " + rotDOFCount + "    transDOF: " + transDOFCount);

                //Add the joint to the robot
                Joint joint = new Joint(name, type);
                joint.Axis   = Axis;
                joint.Parent = parent;
                joint.Child  = child;
                joint.Pose   = Pose;
                robo.Joints.Add(joint);
            }

            //Saving turned on?
            if (this.checkBox2.Checked)
            {
                // Save the SDF
                FolderBrowserDialog folderDialog1 = new FolderBrowserDialog();
                if (folderDialog1.ShowDialog() == DialogResult.OK)
                {
                    robo.WriteSDFToFile(folderDialog1.SelectedPath, precision);
                }

                //Save STLs?
                foreach (ComponentOccurrence oCompOccur in compOccurs)
                {
                    if (oCompOccur.DefinitionDocumentType == DocumentTypeEnum.kPartDocumentObject && this.checkBox1.Checked)
                    {
                        PartDocument partDoc = (PartDocument)oCompOccur.Definition.Document;
                        partDoc.SaveAs(folderDialog1.SelectedPath + "\\meshes\\" + RemoveColon(oCompOccur.Name) + ".stl", true);
                        WriteLine("Finished saving: " + folderDialog1.SelectedPath + "\\meshes\\" + RemoveColon(oCompOccur.Name) + ".stl");
                    }
                }
            }

            #region oldcode
            //foreach (ComponentOccurrence oCompOccur in oAsmCompDef.Occurrences)
            //{
            //    // Generate links from available subassemblies in main assembly.
            //    //Debugger.Break();

            //    //New Link
            //    robo.Links.Add(new Link(oCompOccur.Name));
            //    int c = robo.Links.Count - 1;

            //    WriteLine("Added Link: "+ robo.Links[c].Name +", link count: " + robo.Links.Count.ToString());

            //    //Find and set parent link
            //    for (int i = 0; i < robo.Links.Count; i++)
            //    {
            //        if (String.Equals(robo.Links[i].Name, ReturnParentName(oCompOccur)))
            //        {
            //            robo.Links[c].Parent = robo.Links[i];
            //            WriteLine("Link's parent: " + robo.Links[i].Name);
            //        }
            //    }



            //    //If link has a parent
            //    if (robo.Links[c].Parent != null)
            //    {
            //        //Define a joint
            //        robo.Joints.Add(new Joint(FormatJointName(robo.Links[c].Name), JointType.Revolute, robo.Links[c].Parent, robo.Links[c]));
            //        int j = robo.Joints.Count - 1;

            //        //Parse joint axis
            //        switch (robo.Joints[j].Name[robo.Joints[j].Name.Length - 1])
            //        {
            //            case 'R':
            //                robo.Joints[j].Axis = new double[] { 1, 0, 0 };
            //                break;
            //            case 'P':
            //                robo.Joints[j].Axis = new double[] { 0, 1, 0 };
            //                break;
            //            case 'Y':
            //                robo.Joints[j].Axis = new double[] { 0, 0, 1 };
            //                break;
            //            default:
            //                break;
            //        }
            //    }

            //    // Get mass properties for each link.
            //    double[] iXYZ = new double[6];
            //    oCompOccur.MassProperties.XYZMomentsOfInertia(out iXYZ[0], out iXYZ[3], out iXYZ[5], out iXYZ[1], out iXYZ[4], out iXYZ[2]); // Ixx, Iyy, Izz, Ixy, Iyz, Ixz -> Ixx, Ixy, Ixz, Iyy, Iyz, Izz
            //    robo.Links[c].Inertial = new Inertial(oCompOccur.MassProperties.Mass, iXYZ);
            //    robo.Links[c].Inertial.XYZ = FindCenterOfMassOffset(oCompOccur);

            //    // Set shape properties for each link.
            //    robo.Links[c].Visual = new Visual(new Mesh("package://" + robo.Name + "/" + robo.Links[c].Name + ".stl"));
            //}
            #endregion
        }