12 Replies Latest reply on Mar 3, 2016 3:19 AM by Saugat Kc

    Detect collision between Features in part doc.

    Saugat Kc

      I am implementing a check from macro to determine whether two features collide with each other or not. I tried implementing IDragOperator  but it only works with Assembly doc. More view of what i want to do is shown in below picture:

      ques.png

      Is there any way that I can detect above collision on part document and return false if they collide with each other.

        • Re: Detect collision between Features in part doc.
          Brad Phelan

          You could use IBody2::Operation2 and perform an intersect operation and see if there is any result.

          • Re: Detect collision between Features in part doc.
            Artem Taturevych

            Hi,

             

            You can sew the feature faces and find the intersection between two temp bodies to identify the collision. Here is the sample which demonstrates how to do this: #CODE|Check the Collision Between Selected Features

             

            Thanks,
            Artem

              • Re: Detect collision between Features in part doc.
                Saugat Kc

                Thanx for the help. I think it will work but let me confirm it.

                  • Re: Detect collision between Features in part doc.
                    Saugat Kc

                    Hello Artem,

                    I tried to run your code on macro with two hole features selected but it always gets empty bodies from these features and always gives "Selected features do not intersect" even tough they intersect with each other.

                    err.png

                    what can be the possible issue with my model or is there anything that i need to change in the model to make it work.?

                     

                    Thanks.

                      • Re: Detect collision between Features in part doc.
                        Artem Taturevych

                        Can you please attach the model? Did you selected features in the tree or in the graphics area?

                          • Re: Detect collision between Features in part doc.
                            Saugat Kc

                            Yes I selected features from the tree. It gets all the faces from the feature but generates error while creating body from those faces.

                            I have attached my test model.

                                • Re: Detect collision between Features in part doc.
                                  Saugat Kc

                                  It worked in SW macro. I am now implementing it using C#. I will let you know if I get correct result using C#.

                                  • Re: Detect collision between Features in part doc.
                                    Saugat Kc

                                    I got it working in Solidworks macro but when i try to run it from C#, it still throws error from CreateBodyFromFaces2. I tried to debug but i was left blank as all the required parameters and values are passed into that function.

                                    Here's my code (Not a final code though). I have broken the code for easy debug. Can you please look into it.

                                    public bool validateCollison(object[] objCollection)

                                            {

                                                bool valid = false;

                                                Modeler swModeler = swApp.GetModeler();

                                     

                                                for (int i = 0; i < objCollection.Count() - 1; i++ )

                                                {

                                                    try

                                                    {

                                                        Feature featObj1 = (Feature)objCollection[0];

                                                        Feature featObj2 = (Feature)objCollection[1];

                                     

                                                        Body2 featBody1 = default(Body2);

                                                        Body2 featBody2 = default(Body2);

                                     

                                                        featObj2.SetSuppression2((int)swFeatureSuppressionAction_e.swSuppressFeature, (int)swInConfigurationOpts_e.swThisConfiguration, "");

                                                        object[] vFaces1 = featObj1.GetFaces();

                                     

                                                        if (vFaces1.Count() > 0)

                                                        {

                                                            bool local = false;

                                                            featBody1 = swModeler.CreateBodyFromFaces2(vFaces1.Count(), vFaces1, (int)swCreateFacesBodyAction_e.swCreateFacesBodyActionCap, false, out local);

                                                            featBody1.Display3(swModel, 0, 0);

                                                        }

                                                        swModel.EditUndo2(1);

                                     

                                                        featObj1.SetSuppression2((int)swFeatureSuppressionAction_e.swSuppressFeature, (int)swInConfigurationOpts_e.swThisConfiguration, "");

                                                        object[] vFaces2 = featObj2.GetFaces();

                                                        if (vFaces2.Count() > 0)

                                                        {

                                                            bool local = false;

                                                            featBody2 = swModeler.CreateBodyFromFaces2(vFaces2.Count(), vFaces2, (int)swCreateFacesBodyAction_e.swCreateFacesBodyActionCap, false, out local);

                                                            featBody2.Display3(swModel, 0, 0);

                                                        }

                                                        swModel.EditUndo2(1);

                                     

                                     

                                                        swModel.FeatureManager.EditRollback((int)swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, "");

                                                        int errorCode = 0;

                                     

                                                        Body2 swBody = (Body2)featBody1;

                                                        var swIntersectBodies = swBody.Operations2((int)swBodyOperationType_e.SWBODYINTERSECT, featBody2, out errorCode);

                                     

                                                        if (swIntersectBodies != null)

                                                        {

                                                            return true;

                                                        }

                                                    }

                                                    catch { }

                                                }

                                                return valid;

                                            }