AnsweredAssumed Answered

How to return a wire body from a macro feature

Question asked by Johannes Kronberger on Feb 22, 2016
Latest reply on Sep 27, 2016 by Brad Phelan

As a test I wish to build a wire body from a selected sketch in my macro feature.

 

I have the below code in my macro feature rebuild method. I select a sketch with only a single open contour in it and then build the wire body. However Solidworks always errors out and says that I need to check my geometry and/or tracking ids. Is there anything obviously wrong below?

 

            var cookies = SwApp.RegisterTrackingDefinition("Tracking_API");

            if (cookies == -1)

                return null; // No cookies found.

            var a = SwFeatureData.GetSelectedObjects(

                (type, mark) =>

                    (type == swSelectType_e.swSelSKETCHES && mark == (int) SelectionMarksEnum.ProjectionCurve))

                .FirstOrDefault();

            var projectionCurveSketch = GetSketch(a);

 

            var b = SwFeatureData.GetSelectedObjects(

                (type, mark) =>

                    (type == swSelectType_e.swSelSKETCHES && mark == (int) SelectionMarksEnum.DepthCurve))

                .FirstOrDefault();

            var depthCurveSketch = GetSketch(b);

 

 

            Debug.Assert(depthCurveSketch != null, "depthCurveSketch != null");

 

 

            var xform = depthCurveSketch.ModelToSketchXform;

            var edgesO = (object[]) depthCurveSketch.GetContourEdges(xform);

            var edges = edgesO.Select(o => (IEdge) o).ToArray();

            var curves= edges.Select(edge => (ICurve) edge.GetCurve())

                .Select(c=>(ICurve)c.Copy())

                .ToArray();

            var wireBody = modeler.CreateWireBody(curves , (int)swCreateWireBodyOptions_e.swCreateWireBodyByDefault);

            wireBody.SetTrackingID(cookies, 12);

            return wireBody;

 

with helper methods

 

        private static ISketch GetSketch(object a)

        {

            return (ISketch)((IFeature) a)?.GetSpecificFeature2();

        }

 

and

 

        public static IEnumerable<object> GetSelectedObjects(this IMacroFeatureData selMgr, Func<swSelectType_e, int, bool> filter)

        {

            {

                object objects;

                object objectTypes;

                object marks;

                object drViews;

                object componentXForms;

                selMgr.GetSelections3(out objects, out objectTypes, out marks, out drViews, out componentXForms);

 

                var objectsArray = (object[])objects;

                var typesArray = (swSelectType_e[])objectTypes;

                var marksArray = (int[])marks;

                var i = 0;

                foreach (var item in objectsArray)

                {

                    if(filter(typesArray[i], marksArray[i]))

                    {

                        yield return item;

                    }

                    i++;

                }

            }

        }

Outcomes