ChronoEngine_SwAddin.SWTaskpaneHost.button_setcollshape_Click C# (CSharp) Method

button_setcollshape_Click() private method

private button_setcollshape_Click ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        private void button_setcollshape_Click(object sender, EventArgs e)
        {
            ModelDoc2 swModel;
            swModel = (ModelDoc2)this.mSWApplication.ActiveDoc;
            if (swModel == null)
            {
                System.Windows.Forms.MessageBox.Show("Please open a part and select a solid body!");
                return;
            }

            SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;

            if (swSelMgr.GetSelectedObjectCount2(-1) == 0)
            {
                System.Windows.Forms.MessageBox.Show("Please select one or more solid bodies!");
                return;
            }

            /* TEST
            // stub for code to try to convert whole body:
            ModelDoc2 selpart;
            Component2 selcomp;

            for (int isel = 1; isel <= swSelMgr.GetSelectedObjectCount2(-1); isel++ )
            {
                if ((swSelectType_e)swSelMgr.GetSelectedObjectType3(isel, -1) == swSelectType_e.swSelCOMPONENTS)
                {
                    selcomp = (Component2)swSelMgr.GetSelectedObject6(isel, -1);
                    if (selcomp != null)
                    {
                        selpart = (ModelDoc2)selcomp.GetModelDoc2();
                        String partinfo = "Component2! Part/assembly name:" + selcomp.Name + "\n" + "Config:" + selcomp.ReferencedConfiguration + "\n" + "Path:" + selpart.GetPathName();
                        System.Windows.Forms.MessageBox.Show(partinfo);
                    }
                }
            }
            */

            for (int isel = 1; isel <= swSelMgr.GetSelectedObjectCount2(-1); isel++ )
            {
                if ((swSelectType_e)swSelMgr.GetSelectedObjectType3(isel, -1) != swSelectType_e.swSelSOLIDBODIES)
                {
                    System.Windows.Forms.MessageBox.Show("This function can be applied only to solid bodies! Select one or more bodies before using it.");
                    return;
                }

                bool rbody_converted = false;
                Body2 swBody = (Body2)swSelMgr.GetSelectedObject6(isel, -1);

                // ----- Try to see if this is a sphere

                if (ConvertToCollisionShapes.SWbodyToSphere(swBody))
                {
                    string mname = swBody.Name;
                    mname.Replace("COLL.SPHERE-", "");
                    swBody.Name = "COLL.SPHERE-" + mname;
                    rbody_converted = true;
                }

                // ----- Try to see if this is a box

                if (ConvertToCollisionShapes.SWbodyToBox(swBody))
                {
                    string mname = swBody.Name;
                    mname.Replace("COLL.BOX-", "");
                    swBody.Name = "COLL.BOX-" + mname;
                    rbody_converted = true;
                }

                // ----- Try to see if this is a cylinder

                if (ConvertToCollisionShapes.SWbodyToCylinder(swBody))
                {
                    string mname = swBody.Name;
                    mname.Replace("COLL.CYLINDER-", "");
                    swBody.Name = "COLL.CYLINDER-" + mname;
                    rbody_converted = true;
                }

                // ----- Try to see if this is a convex hull (except if already converted bacause it's a box)

                if (ConvertToCollisionShapes.SWbodyToConvexHull(swBody, 30) && !rbody_converted)
                {
                    string mname = swBody.Name;
                    mname.Replace("COLL.CONV.HULL-", "");
                    swBody.Name = "COLL.CONV.HULL-" + mname;
                    rbody_converted = true;
                }

                // Change color of the body if it can be used as collision shape
                if (rbody_converted)
                {
                    //int materr = swBody.SetMaterialProperty("Default", "Materiali personalizzati.sldmat", "CollisionShapeMaterial");
                    int materr = swBody.SetMaterialProperty("Default", "", "Air");
                    if (materr !=1)
                        materr = swBody.SetMaterialProperty("Default", "", "Aria");
                    if (materr != 1)
                        materr = swBody.SetMaterialProperty("Default", "", "Luft");
                    if (materr != 1)
                        System.Windows.Forms.MessageBox.Show("Warning! could not assign 'air' material to the collision shape: "+ swBody.Name +"\n This can affect mass computations.");

                    double[] mcolor = new double[9] { 1, 0, 0, 1, 1, 1, 0, 0.7, 0 }; //  R, G, B, Ambient, Diffuse, Specular, Shininess, Transparency, Emission
                    swBody.MaterialPropertyValues2 = mcolor;

                    // must force rebuild otherwise one cannot see in the design tree that an 'air' material has been added to collision bodies, etc.
                    //swModel.Rebuild((int)swRebuildOptions_e.swForceRebuildAll);
                    swModel.ForceRebuild3(false);
                }

            } // end loop on selected items
        }