ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
GSGuru Subramani19/06/2013

Hi,

I'm trying to create an addin which can extract mass properties from selected components in assemblies. I'm having trouble when there are more than one subassemblies in the document. When there is just one subassembly it macro works, when there are two different subassemblies in the assembly document I just get zero for all the mass properties. I know that all the bodies of all the components are being passed into the AddBodies() method. I think it maybe because I'm using the active document to create the IMassproperty object. I don't know how to work around this either. This seems quite bizarre and I've been stuck with this issue for quite some time. A few code snippets of what I'm trying to do is bellow:

The program is all in c#

NOTE:

static public DispatchWrapper[] GetallBodies(Component2 comp)

        {

            if (comp.IGetChildrenCount() > 0) // assembly subcomponent

            {

                Object[] subcomponents = ((Object[])comp.GetChildren());

                List<DispatchWrapper> DWList = new List<DispatchWrapper>();

                foreach (Object subcomponent in subcomponents)

                {

                    if (((Component2)subcomponent).Visible == (int)swComponentVisibilityState_e.swComponentVisible)

                    {

                        DispatchWrapper[] DWarr = GetallBodies((Component2)subcomponent);

                        if (DWarr != null)

                        {

                            foreach (DispatchWrapper DW in DWarr)

                            {

                                DWList.Add(DW);

                            }

                        }

                    }

                }

                if (DWList.Count != 0) return DWList.ToArray();

                else return null;

            }

            else // part subcomponent

            {

                Object scrap;

                Object[] bodies = comp.GetBodies3((int)swBodyType_e.swSolidBody, out scrap);

                bodies = comp.GetBodies((int)swBodyType_e.swSolidBody);

                if (bodies != null)

                {

                    DispatchWrapper[] DWarr = (DispatchWrapper[])ObjectArrayToDispatchWrapperArray(bodies);

                    return DWarr;

                }

                else

                {

                    return null;

                }

            }

        }

static public DispatchWrapper[] ObjectArrayToDispatchWrapperArray(object[] SwObjects)

        {

            int arraySize = 0;

            arraySize = SwObjects.GetUpperBound(0);

            DispatchWrapper[] dispwrap = new DispatchWrapper[arraySize + 1];

            int arrayIndex = 0;

            for (arrayIndex = 0; arrayIndex <= arraySize; arrayIndex++)

            {

                dispwrap[arrayIndex] = new DispatchWrapper(SwObjects[arrayIndex]);

            }

            return dispwrap;

        }

//// IN THE MAIN CODE THAT CREATES THE MASSPROPERTY OBJECT///

{

//doc is the active document

MassProperty MP = doc.Extension.CreateMassProperty();

//theseones are the components that are selected, a lot of gibberish for selecting and kind of irrelevant so I ommited it all

for (int i = 0; i < theseones.Length; i++)

            {

                Component2 thisone = (Component2)theseones[i];

                bodylist = GetallBodies(thisone);

            }

foreach (DispatchWrapper body in bodylist) { bdlist.Add(body); } // to create a single array //bdlist just holds the array elements all together to use later

bodylist = bdlist.ToArray();

bool check = MP.AddBodies(bodylist);

MP.Mass.ToString(); // returns "0" when component is a subassembly

}

Thanks a lot for going through my code!! I absolutely cannot procede without a solution to this.