Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
AlexFielder
2151 Views, 16 Replies

Wrap to Surface API Implementation

Hi all,

 

I have recently been looking into the above and it seems that despite there being API implementations for project along Vector and Project To Closest Point, there is nothing available for Wrap to surface.

 

The best information I could find is this post from a while back by @Anonymous: but since he's off doing all-things-Forge these days I have needed to cobble together my own implementation:

 

private static void createGeomForHolePositions()
        {
            try
            {
#if DEBUG
                ClientGraphics debuggingGraphics = null;
                try
                {
                    debuggingGraphics = partCompDef.ClientGraphicsCollection["debuggingGraphics"];
                }
                catch (Exception)
                {
                    debuggingGraphics = partCompDef.ClientGraphicsCollection.Add("debuggingGraphics");
                }
#endif
                new3DSketch = partCompDef.Sketches3D.Add();

                ObjectCollection objCollection = partDoc.AttributeManager.FindObjects("RadialHole", "HolePositionX");
                for (int i = 1; i < objCollection.Count + 1; i++)
                {
                    if(objCollection[i] is SketchPoint)
                    {
                        SketchPoint skpoint = (SketchPoint)objCollection[i];
                        AttributeSet AttSet = skpoint.AttributeSets[1];
                        Inventor.Attribute holeName = AttSet["HoleName"];
                        string ThisHoleName = holeName.Value;
                        Inventor.Attribute xPosAtt = AttSet["HolePositionX"];
                        double xPosition = xPosAtt.Value;
                        Inventor.Attribute yPosAtt = AttSet["HolePositionY"];
                        double yPosition = yPosAtt.Value;
                        Inventor.Point tmpOriginPoint = transGeom.CreatePoint(0, yPosition, 0);
                        Inventor.Point tmpWPPoint = null;
                        UnitVector tmpXAxis = null;
                        UnitVector tmpYAxis = null;
                        UnitVector cylinderNormal = null;

                        selectedWorkPlane.GetPosition(out tmpWPPoint, out tmpXAxis, out tmpYAxis);
                        double radius = selectedFace.Geometry.Radius;
                        double pi = Math.PI;
                        double circumference = ((2 * pi) * radius);
                        double arcLength = xPosition;
                        double arcAngle = (arcLength / ((2 * radius) * pi)) * 360;
                        //default vectors
                        UnitVector xPositive = transGeom.CreateUnitVector(1, 0, 0);
                        UnitVector xNegative = transGeom.CreateUnitVector(-1, 0, 0);
                        UnitVector yPositive = transGeom.CreateUnitVector(0, 1, 0);
                        UnitVector yNegative = transGeom.CreateUnitVector(0, -1, 0);
                        UnitVector zPositive = transGeom.CreateUnitVector(0, 0, 1);
                        UnitVector zNegative = transGeom.CreateUnitVector(0, 0, -1);

                        //this needs more work in case things aren't quite perpendicular to the origin axes...
                        // or for occasions when the part is modelled in a different direction.
                        if (tmpXAxis.IsParallelTo(xPositive))
                        {
                            cylinderNormal = xPositive;
                        }
                        else if (tmpXAxis.IsParallelTo(yPositive))
                        {
                            cylinderNormal = yPositive;
                        }
                        else
                        {
                            cylinderNormal = zPositive;
                        }

                        Arc3d tmpArc = transGeom.CreateArc3d(tmpOriginPoint, cylinderNormal, selectedWorkPlane.Plane.Normal, radius, 0, (arcAngle * (pi / 180)));
#if DEBUG
                        GraphicsNode graphicsNode = debuggingGraphics.AddNode(1);
                        CurveGraphics curveGraphics2 = graphicsNode.AddCurveGraphics(tmpArc);
#endif
                        RadialHoleDefinition holeDef = (from RadialHoleDefinition hole in radialHoles
                                                       where hole.HoleName == ThisHoleName
                                                       select hole).FirstOrDefault();
                        if(holeDef?.HoleName.Length > 0)
                        {
                            Inventor.Point tmpPoint = tmpArc.EndPoint;
                            WorkPoint newWorkPoint = partCompDef.WorkPoints.AddFixed(tmpPoint, false);
                            newWorkPoint.Name = ThisHoleName;
                            newWorkPoint.Visible = false;
                            holeDef.HoleWorkPoint = newWorkPoint;
                            SurfaceBody CylinderSurface = selectedFace.SurfaceBody;
                            Sketch3D tmpSketch3D = partCompDef.Sketches3D.Add();
                            SketchPoint3D tmpSketchPt3d = tmpSketch3D.SketchPoints3D.Add(tmpPoint);
                            WorkAxis newWorkAxis = partCompDef.WorkAxes.AddByNormalToSurface(selectedFace, tmpSketchPt3d);
                            newWorkAxis.Name = "Work Axis: " + ThisHoleName;
                            newWorkAxis.Visible = false;
                            holeDef.HoleWorkAxis = newWorkAxis;
                            tmpSketch3D.Delete();
                        }
                        else
                        {
                            log.Error("One of the HoleDefinitions is incomplete.");
                            reporter.UpdateStatusBar("One of the HoleDefinitions is incomplete, check the spreadsheet and run again!");
                        }
                        RadialHoles.m_InventorApp.ActiveView.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }


            //new3DSketch.curves
        }

 

Is there an API method I have overlooked that simplifies the above?

 

Thanks,

 

Alex.

 

 

PS. Another minor issue is that currently my method above ONLY works on cylindrical surfaces and doesn't create a link between the SketchPoint objects and WorkPoints it creates; because of the lack of a link I think that it could probably be massively simplified forgoing the use of Attributes and AttributeSets (which have their own special ways to trip you up...)