private IBody2 CreateBodyFromSketchContour(IModeler modeler, IMathUtility mathUtils, ISketchContour skCont, double height, bool midPlane) { var sketch = skCont.Sketch; if (sketch.Is3D()) { throw new UserErrorException("Only 2D sketches are supported"); } var transform = sketch.ModelToSketchTransform.IInverse(); var boundary = (skCont.GetEdges() as object[]) .Cast <IEdge>() .Select(e => { var curve = e.IGetCurve().ICopy(); //must copy curve otherwise CreateTrimmedSheet4 is failing return(curve); }).ToArray(); var centerPt = mathUtils.CreatePoint(new double[] { 0, 0, 0 }) as IMathPoint; var dirVec = mathUtils.CreateVector(new double[] { 0, 0, 1 }) as IMathVector; var refVec = mathUtils.CreateVector(new double[] { 1, 0, 0 }) as IMathVector; centerPt = centerPt.IMultiplyTransform(transform); dirVec = dirVec.IMultiplyTransform(transform); refVec = refVec.IMultiplyTransform(transform); if (midPlane) { var dirRevVec = new Vector(dirVec.ArrayData as double[]); dirRevVec.Scale(-1); var origPt = new Point(centerPt.ArrayData as double[]); MoveCurves(origPt, dirRevVec, height / 2, boundary, mathUtils); centerPt = mathUtils.CreatePoint(origPt.Move(dirRevVec, height / 2).ToArray()) as IMathPoint; } var surf = modeler.CreatePlanarSurface2(centerPt.ArrayData, dirVec.ArrayData, refVec.ArrayData) as ISurface; var sheetBody = surf.CreateTrimmedSheet4(boundary, true) as Body2; if (sheetBody == null) { throw new NullReferenceException("Failed to create trimmed sheet from surface region"); } var solidBody = modeler.CreateExtrudedBody(sheetBody, dirVec as MathVector, height) as IBody2; var faces = (solidBody.GetFaces() as object[]).Cast <IFace2>().ToArray(); return(modeler.CreateSheetFromFaces(faces)); }
//TODO: calculate height based on bounding box private IBody2 CreateBodyFromSketchContour(IModeler modeler, IMathUtility mathUtils, ISketchContour skCont, double height = 1000) { var sketch = skCont.Sketch; if (sketch.Is3D()) { throw new UserErrorException("Only 2D sketches are supported"); } var transform = sketch.ModelToSketchTransform.IInverse(); var boundary = (skCont.GetEdges() as object[]) .Cast <IEdge>() .Select(e => { var curve = e.IGetCurve().ICopy(); //must copy curve otherwise CreateTrimmedSheet4 is failing return(curve); }).ToArray(); var centerPt = mathUtils.CreatePoint(new double[] { 0, 0, 0 }) as IMathPoint; var dirVec = mathUtils.CreateVector(new double[] { 0, 0, 1 }) as IMathVector; var refVec = mathUtils.CreateVector(new double[] { 1, 0, 0 }) as IMathVector; centerPt = centerPt.IMultiplyTransform(transform); dirVec = dirVec.IMultiplyTransform(transform); refVec = refVec.IMultiplyTransform(transform); var surf = modeler.CreatePlanarSurface2(centerPt.ArrayData, dirVec.ArrayData, refVec.ArrayData) as ISurface; var sheetBody = surf.CreateTrimmedSheet4(boundary, true) as Body2; if (sheetBody == null) { throw new NullReferenceException("Failed to create trimmed sheet from surface region"); } var firstBody = modeler.CreateExtrudedBody(sheetBody, dirVec as MathVector, height * 0.9 / 2) as IBody2; var secondBody = modeler.CreateExtrudedBody(sheetBody, dirVec.IScale(-1), height * 0.9 / 2) as IBody2; int err; var res = firstBody.Operations2((int)swBodyOperationType_e.SWBODYADD, secondBody, out err) as object[]; return(res.First() as IBody2); }