private void mark(BrowserNodesEnumerator browserNodes)
        {
            foreach (BrowserNode node in browserNodes)
            {
                if (node.NativeObject is BrowserFolder)
                {
                    mark(node.BrowserNodes);
                }

                if (node.NativeObject is ComponentOccurrence)
                {
                    ComponentOccurrence occ = (ComponentOccurrence)node.NativeObject;
                    int TranslationDegreesCount;
                    ObjectsEnumerator TranslationDegreesVectors;
                    int RotationDegreesCount;
                    ObjectsEnumerator RotationDegreesVectors;
                    Point             DOFCenter;

                    occ.GetDegreesOfFreedom(out TranslationDegreesCount, out TranslationDegreesVectors,
                                            out RotationDegreesCount, out RotationDegreesVectors, out DOFCenter);

                    BrowserNodeDisplayStateEnum displaystate = node.BrowserNodeDefinition.DisplayState;
                    if (displaystate == BrowserNodeDisplayStateEnum.kDefaultDisplayState ||
                        displaystate == BrowserNodeDisplayStateEnum.kGreenCheckDisplayState ||
                        displaystate == BrowserNodeDisplayStateEnum.kCyclicDisplayState)
                    {
                        if ((TranslationDegreesCount == 0) && (RotationDegreesCount == 0))
                        {
                            node.BrowserNodeDefinition.DisplayState = BrowserNodeDisplayStateEnum.kGreenCheckDisplayState;
                        }
                        //else if ((TranslationDegreesCount == 0) && (RotationDegreesCount == 1))
                        //{
                        //    node.BrowserNodeDefinition.DisplayState = BrowserNodeDisplayStateEnum.kCyclicDisplayState;
                        //}
                        else
                        {
                            node.BrowserNodeDefinition.DisplayState = BrowserNodeDisplayStateEnum.kDefaultDisplayState;
                        }
                    }
                }
            }
        }
Пример #2
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
        }